模版匹配
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效果都还行,都识别到位了 (这里不是特征识别,识别的不是常规人脸,所以只对相似特征识别)
这里有这几种算法的说明
https://blog.csdn.net/coroutines/article/details/78229105
查找多个匹配
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, 并且遍历的原理是把已找到结果的数据图填补上,让结果刷新。貌似没有找到旋转后的图像