# Calculating a template matching similarity score?

Hello everyone,

I am trying the simple template matching function matchTemplate.

However I'm still having a hard time understanding how to extract the "overall" matching coefficient score for the instance. I know that depending on the method used, the coefficient varies 0-1 or -1 to 1 and each pixel is having a similarity index in result matric.

But then how do we get an overall similarity score for that matching? I'd really really appreciate your help with this. As I'm stuck in this process and have no one else to ask for help.

Thanks a ton!

edit retag close merge delete

Could you precise the ''overal similarity score'' you're expected? The sum of score on the image? the sum for your best patch?

( 2013-03-11 02:28:32 -0600 )edit

For example whenever a match is found, I'd like to know the confidence score for that match. I mean how similar the algorithm believes our template is to the original image. I believe minMaxLoc does that by analyzing the similarities. But how can I check what's the minimum or maximum score? Di I have to go through all values in the matrix? J'espere c'est clair. It's night here now. Will check your response tomorrow morning. Merci d'avance mec!

( 2013-03-11 02:37:21 -0600 )edit

Sort by ยป oldest newest most voted

minMaxLoc is doing everything for you. It returns the min value, the max value, their position. If you call this function on the result of matchTemplate, you will have the best position of the patch (in maxLoc), and the similarity score (in maxValue). If you used 'CV_TM_SQDIFF', you have to get the min instead of max, but it's done with the same function.

I hope it's clear...

more

Thank you so much Mathieu. Which method do you think returns the best results? I'm matching eyes of the same person. Basically I'm matching open eye template with closed eye and open eye. I'd like to get a higher score for open eye and lower score for closed eye. Thank you again!

( 2013-03-13 07:26:47 -0600 )edit

As far as I understand you would like to get one similarity value for a whole template matching instead of the array of individual pixel matches. However the main idea of template matching is to slide a small (relative to the image) template through the image and find the highest matching area. See the template matching tutorial here. The resulting image gives a detailed map on how well the template matched in each location.

The following set of images show how well the template matches on the image with different matching methods:

Back to your original question if you are not interested in the exact location of the match just the highest/lowest match value - because you want to find the images on which the template is certainly there - then you can use the minMaxLoc function and compare the maximal/minimal value with a predefined threshold.

more

Thank you so much. Which method do you think returns the best results? I'm matching eyes of the same person. Basically I'm matching open eye template with closed eye and open eye. I'd like to get a higher score for open eye and lower score for closed eye. Thank you again!

( 2013-03-13 07:26:29 -0600 )edit

I think that you should experiment with them and select the one that matches your problem.

( 2013-03-15 06:26:06 -0600 )edit
1

I've used minMaxLoc in my sample and I'm getting always 0..1 (depends on method). Also when the template is not in picture I get with minMaxLoc best value.

( 2013-04-11 07:05:07 -0600 )edit

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 method_accepts_mask = (TM_SQDIFF == match_method || match_method == TM_CCORR_NORMED);
{
}
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;
}

more

Hi @essamsky. I was trying to test you code, but my minVal -3971198.75 and maxVal 18563520 I using TemplateMatchingType.Ccoeff method for matching. It found template fine, but i have no idea atm how to calulate score of matching and didn't apply result of template with some threshold. As i understand confidence can't be greater than 1 I will appriciate you help in this question

( 2020-05-26 08:31:56 -0600 )edit

( 2020-06-09 09:46:24 -0600 )edit

@essamzaky I got the same question as @dilvish I am using the TM_CCOEFF method and as the dConfidence/maxVal I get numbers like 77766264.0 - how do I get a confidence between 0 and 1?

method = cv2.TM_CCOEFF
res = cv2.matchTemplate(img,template,method)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
print(max_val) #=> 77766264.0


edit: I noticed you need to normalize the result befor using the min_val, max_val:

    cv2.normalize(res, res, 0, 1, cv2.NORM_MINMAX) # take the res and map it to res and normalize it over 0 and 1


and now I always get: something between 0.9999999881524584 and 1.0 - even if the source image is not at all in the template.

( 2020-08-13 08:49:06 -0600 )edit

Try to use cv2.TM_CCOEFF_NORMED

method = cv2.TM_CCOEFF_NORMED res = cv2.matchTemplate(img,template,method) min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) print(max_val)

( 2020-08-19 09:58:57 -0600 )edit

Official site

GitHub

Wiki

Documentation