Ask Your Question
1

Calculating a template matching similarity score?

asked 2013-03-11 02:00:02 -0600

hopelessman gravatar image

updated 2020-11-02 17:20:16 -0600

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 flag offensive close merge delete

Comments

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

Mathieu Barnachon gravatar imageMathieu Barnachon ( 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!

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

3 answers

Sort by ยป oldest newest most voted
3

answered 2013-03-11 02:52:46 -0600

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...

edit flag offensive delete link more

Comments

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!

hopelessman gravatar imagehopelessman ( 2013-03-13 07:26:47 -0600 )edit
2

answered 2013-03-11 02:47:18 -0600

rics gravatar image

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:

image description

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.

edit flag offensive delete link more

Comments

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!

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

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

rics gravatar imagerics ( 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.

Immi gravatar imageImmi ( 2013-04-11 07:05:07 -0600 )edit
1

answered 2019-10-20 06:27:52 -0600

essamzaky gravatar image

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;
}
edit flag offensive delete link more

Comments

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

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

Please upload your code and test images to check

essamzaky gravatar imageessamzaky ( 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.

Suisse gravatar imageSuisse ( 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)

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

Question Tools

Stats

Asked: 2013-03-11 02:00:02 -0600

Seen: 24,700 times

Last updated: Oct 20 '19