I am using openCV’s cvMatchTemplate method to find a smaller image in a bigger image. The \ method below is matching the existing pattern object with the given ‘current’ image, the found pattern is marked by a rectangle drawn on the current image. When the pattern is visible its finding it reliably, when not its drawing somewhere “random” which makes sense.
I have trouble computing the score of the template matching. Is there an easy to determine that? I was looking on various sites but could not find a solution and also been looking at the values computed by cvMinMaxLoc but could not find a way to determine the quality of the match.
- (void)detect:(IplImage *)current
{
int patchx = pattern->width;
int patchy = pattern->height;
int iwidth = current->width - patchx + 1;
int iheight = current->height - patchy + 1;
IplImage *result=cvCreateImage( cvSize(iwidth,iheight),IPL_DEPTH_32F, 1);
cvMatchTemplate( current, pattern, result, CV_TM_SQDIFF );
CvPoint minloc, maxloc;
double minval, maxval;
cvMinMaxLoc( result, &minval, &maxval, &minloc, &maxloc, 0 );
/* draw red rectangle */
cvRectangle( current,
cvPoint( minloc.x, minloc.y ),
cvPoint( minloc.x + patchx, minloc.y + patchy ),
cvScalar( 0, 255, 0, 0 ), 1, 0, 0 );
}
Use a normed metric like
CV_TM_CCORR_NORMEDorCV_TM_SQDIFF_NORMEDand compare the minimum value to a threshold (between 0 and 1.0).