检测思路
- 计算积分图,使用积分图(Integral Image)对Haar-like进行特征求值
- 挑选最优分类器;
- 使用AdaBoost算法把这些分类器训练成一个强分类器,用于区分鼠标和非鼠标
- 级联,也就是强分类器的强强联手。把强分类器级联到一起,提高准确率
检测方法
利用haar特征,结合级联分类器进行鼠标检测: Haar分类器 = Haar-like特征 + 积分图方法 + AdaBoost +级联
检测目标选择
选择特征明显的物体(颜色和轮廓)
样本构建方法
样本分为正样本(有目标)、负样本(无目标)
- 正样本
正样本构建:
拍20张,通过画图软件编辑图片大小50*50(图片尺寸可以减少训练的时间),通过python程序分别调节每张目标图片的亮度各5张、对比度各5张,正样本最终筛选数量为191张,并生成描述文件。 - 调节亮度和对比度的python代码
from PIL import Image
from PIL import ImageEnhance
import os
root = "C:/Users/空雨衣/Desktop/test/"
fileList = os.listdir(root)
contrasparam = 0.3
brightparam = 0.3
sharpparam = 0.2
contrcounter = 1
brightcounter = 1
sharpcounter = 1
counter = 0
for filename in fileList:
path = root + filename
img = Image.open(path)
enh_con = ImageEnhance.Contrast(img)
enh_bri = ImageEnhance.Brightness(img)
enh_sha = ImageEnhance.Sharpness(img)
#调节对比度
for i in range(1,6):
image_contrasted = enh_con.enhance(contrasparam * i * counter*0.06)
newname1 = root + str(contrcounter) + "1contrast1" + str(i)+".jpg"
image_contrasted.save(newname1)
contrcounter += 1
#调节亮度
for i in range(1, 6):
image_bright = enh_bri.enhance(brightparam * i * counter*0.06)
newname2 = root + str(contrcounter) + "1brightness1" + str(i)+".jpg"
image_bright.save(newname2)
brightcounter += 1
counter += 1
- 负样本:
爬取百度图片870张并生成描述文件。
import json
import itertools
import urllib
import requests
import os
import re
import sys
word=input("请输入关键字:")
path='C:/Users/空雨衣/Desktop/negativePicture/ii'
if not os.path.exists(path):
os.mkdir(path)
word=urllib.parse.quote(word)
url = r"http://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&fp=result&queryWord={word}&cl=2&lm=-1&ie=utf-8&oe=utf-8&st=-1&ic=0&word={word}&face=0&istype=2nc=1&pn={pn}&rn=60"
urls=(url.format(word=word,pn=x)for x in itertools.count(start=0,step=60))
index=0
str_table = {
'_z2C$q': ':',
'_z&e3B': '.',
'AzdH3F': '/'
}
char_table = {
'w': 'a',
'k': 'b',
'v': 'c',
'1': 'd',
'j': 'e',
'u': 'f',
'2': 'g',
'i': 'h',
't': 'i',
'3': 'j',
'h': 'k',
's': 'l',
'4': 'm',
'g': 'n',
'5': 'o',
'r': 'p',
'q': 'q',
'6': 'r',
'f': 's',
'p': 't',
'7': 'u',
'e': 'v',
'o': 'w',
'8': '1',
'd': '2',
'n': '3',
'9': '4',
'c': '5',
'm': '6',
'0': '7',
'b': '8',
'l': '9',
'a': '0'
}
i=1
char_table = {ord(key): ord(value) for key, value in char_table.items()}
for url in urls:
html=requests.get(url,timeout=10).text
a=re.compile(r'"objURL":"(.*?)"')
downURL=re.findall(a,html)
for t in downURL:
for key, value in str_table.items():
t = t.replace(key, value)
t=t.translate(char_table)
try:
html_1=requests.get(t)
if str(html_1.status_code)[0]=="4":
print('失败1')
continue
except Exception as e:
print('失败2')
continue
with open(path+"/"+str(i)+".jpg",'wb') as f:
f.write(html_1.content)
i=i+1
训练模型参数
模型检验
#include "opencv2/objdetect.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
#include <io.h>
using namespace std;
using namespace cv;
void detectAndDisplay( Mat frame );
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
int _count = 0;
int main()
{
//Load the cascades
if( !face_cascade.load("cascade.xml") )
{
cout << "--(!)Error loading face cascade\n";
return -1;
};
//read file
char *filename = "C:\\cascade\\*.jpg";
struct _finddata_t fileinfo;
long handle;
handle = _findfirst(filename,&fileinfo);
if(handle == -1) cout<<"fail..."<<endl;
else
cout<<fileinfo.name<<endl;
while(!_findnext(handle,&fileinfo))
{
cout<<fileinfo.name<<endl;
Mat image=imread(fileinfo.name);
detectAndDisplay(image);
}
cout<<_count<<endl;
_findclose(handle);
system("pause");
waitKey(0);
return 0;
}
void detectAndDisplay( Mat frame )
{
Mat frame_gray;
cvtColor( frame, frame_gray, COLOR_BGR2GRAY );
equalizeHist( frame_gray, frame_gray );
//-- Detect mouse
std::vector<Rect> faces;
face_cascade.detectMultiScale( frame_gray, faces );
for ( size_t i = 0; i < faces.size(); i++ )
{
Point center( faces[i].x + faces[i].width/2, faces[i].y + faces[i].height/2 );
ellipse( frame, center, Size( faces[i].width/2, faces[i].height/2 ), 0, 0, 360, Scalar( 255, 0, 255 ), 4 );
Mat faceROI = frame_gray( faces[i] );
}
if(!faces.empty()){
_count += 1;
}
//-- Show what you got
imshow( "Capture - Face detection", frame );
}