Ask Your Question
1

Calculating a template matching similarity score?

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

hopelessman gravatar image

updated 2013-03-11 02:00:50 -0500

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 -0500 )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 -0500 )edit

3 answers

Sort by ยป oldest newest most voted
2

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

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 -0500 )edit
1

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

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 -0500 )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 -0500 )edit

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 -0500 )edit
0

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

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
Login/Signup to Answer

Question Tools

Stats

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

Seen: 13,588 times

Last updated: yesterday