Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

MatchTemplate and get matching score or confidence hint

I read the matchtemplate tutorial in the documenation ,

when i try to find the confidence or matching score , it always return the confidence as 1 , i trace the sample code and found the reason is normalize( result, result, 0, 1, NORM_MINMAX, -1, Mat() ); after commenting the previous line the result is fine . so i think we may need to update the documentation and comment the previouse line of code

and here it's my code to get the score or confidence from matchtemplate

bool method_accepts_mask = (TM_SQDIFF == match_method || match_method == TM_CCORR_NORMED);
if (use_mask && method_accepts_mask)
{
    matchTemplate(img, templ, result, match_method, mask);
}
else
{
    matchTemplate(img, templ, result, match_method);
}
//normalize(result, result, 0, 1, NORM_MINMAX, -1, Mat());//new added check
double minVal; double maxVal; Point minLoc; Point maxLoc;
Point matchLoc;
minMaxLoc(result, &minVal, &maxVal, &minLoc, &maxLoc, Mat());
double dConfidence;
if (match_method == TM_SQDIFF || match_method == TM_SQDIFF_NORMED)
{
    matchLoc = minLoc;
    dConfidence = 1 - minVal;
}
else
{
    matchLoc = maxLoc;
    dConfidence = maxVal;
}