模版匹配
vector<TemplateMatchModes> methods = {TM_SQDIFF,TM_SQDIFF_NORMED,TM_CCORR,TM_CCORR_NORMED,TM_CCOEFF,TM_CCOEFF_NORMED};
for (int i=0;i<methods.size();i++) {
TemplateMatchModes method = methods[i];
frame.copyTo(f1);
matchTemplate(f1, head, f2, method);
double minVal,maxVal;
cv::Point minLoc,maxLoc;
minMaxLoc(f2, &minVal, &maxVal, &minLoc, &maxLoc);
cv::Point topLeft,bottomRight;
if (method<=TM_SQDIFF_NORMED) {
topLeft = minLoc;
}else{
topLeft = maxLoc;
}
bottomRight = {topLeft.x+head.cols,topLeft.y+head.rows};
rectangle(frame, topLeft, bottomRight, Scalar(255,255,255,255),5);
}
類型 | 同照片 | 異照片 | 其他 |
---|---|---|---|
TM_SQDIFF | |||
TM_SQDIFF_NORMED | |||
TM_CCORR | |||
TM_CCORR_NORMED | |||
TM_CCOEFF | |||
TM_CCOEFF_NORMED |
TM_SQDIFF , TM_SQDIFF_NORMED, TM_CCORR_NORMED效果都還行醉冤,都識(shí)別到位了 (這里不是特征識(shí)別,識(shí)別的不是常規(guī)人臉,所以只對(duì)相似特征識(shí)別)
這里有這幾種算法的說(shuō)明
https://blog.csdn.net/coroutines/article/details/78229105
查找多個(gè)匹配
TemplateMatchModes method = TM_CCOEFF_NORMED;
float threshold = 0.9;
frame.copyTo(f1);
matchTemplate(f1, head, f2, method);
cv::threshold(f2, f2, threshold-.05, 1, THRESH_TOZERO);
double minVal,maxVal;
cv::Point minLoc,maxLoc;
while (true) {
minMaxLoc(f2, &minVal, &maxVal, &minLoc, &maxLoc);
if (maxVal > threshold){
NSLog(@"Matched %f %f",minLoc.x, minLoc.y);
rectangle(
frame,
maxLoc,
cv::Point(maxLoc.x + head.cols, maxLoc.y + head.rows),
CV_RGB(0,255,0), 10
);
floodFill(f2, maxLoc, cv::Scalar(0), 0, cv::Scalar(.1), cv::Scalar(1.));
}else{
NSLog(@"No More Matches");
break;
}
}
需要使用TM_CCOEFF_NORMED, 并且遍歷的原理是把已找到結(jié)果的數(shù)據(jù)圖填補(bǔ)上,讓結(jié)果刷新懂昂。貌似沒(méi)有找到旋轉(zhuǎn)后的圖像
image.png
8448418_152411419000_2.jpg