Question

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.

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top