Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

It seems you are using the documentation sample normalize in this samples makes the matching score always equal to one so comment this line and calculate the confidence here it's the code

int match_method = TM_CCOEFF_NORMED;
bool use_mask = false;
Mat mask;
//mask = imread("c:\\temp\tempmask.png", IMREAD_COLOR);
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;
}