openCV template matching using CV_TM_CCORR_NORMED

不问归期 提交于 2019-12-24 03:31:29

问题


I have this code

cvMatchTemplate(image2, templat2, result, CV_TM_CCORR_NORMED);

How do I make the program execute the following lines if there is a match:

double min_val, max_val;
CvPoint min_loc, max_loc;
cvMinMaxLoc(result, &min_val, &max_val, &min_loc, &max_loc);

cvRectangle(image3, max_loc, cvPoint(max_loc.x+templat->width, 
max_loc.y+templat->height), cvScalar(0,1,1), 1);

Thank you.


回答1:


You need to execute both cvMatchTemplate and cvMinMaxLoc together:

cvMatchTemplate(image2, templat2, result, CV_TM_CCORR_NORMED);

double min_val, max_val;
CvPoint min_loc, max_loc;
cvMinMaxLoc(result, &min_val, &max_val, &min_loc, &max_loc);

Then you can determine whether you have a match or not by checking max_val.

if max_val is 1, you have an exact match, pixel-for-pixel, at the position max_loc in your search picture. The lower max_val is, the more errors are in the best match.

Try it out for some test cases to determine what your threshold should be.

Be aware that if you use CV_TM_SQDIFF_NORMED instead of CV_TM_CCORR_NORMED, a perfect match corresponds to a value of zero instead of one, so you will have to check for min_val instead of max_val



来源:https://stackoverflow.com/questions/16130778/opencv-template-matching-using-cv-tm-ccorr-normed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!