# Revision history [back]

### Template Matching is wrong with specific Reference image

Until now I have always gotten reasonable results withc matching score, Im using "Imgproc.TM_CCOEFF_NORMED" method for template matching below are the results.

Reference image:

Outcome 1:

MinMaxLocResult mmr = Core.minMaxLoc(result);
maxMatchScore = mmr.maxVal;


Value of maxMatchScore is always 1.0

Outcome 2:

MinMaxLocResult mmr = Core.minMaxLoc(result);
maxMatchScore = mmr.maxVal;


Value of maxMatchScore is always 1.0

1.0 is the best match value, which means the image is %100 identical, but it isn't and on other reference images I get other values, so the program is working but only with this specific reference image I'm getting 1.0 also no matter what in file(source image) I give, it always finds the match on the first search upper left corner. A little bit annoying, I would be glad for your suggestions for a solution!

### Template Matching is wrong with specific Reference image

Until now I have always gotten reasonable results withc matching score, Im using "Imgproc.TM_CCOEFF_NORMED" method for template matching below are the results.

Reference image:

Outcome 1:

MinMaxLocResult mmr = Core.minMaxLoc(result);
maxMatchScore = mmr.maxVal;


Value of maxMatchScore is always 1.0

Outcome 2:

MinMaxLocResult mmr = Core.minMaxLoc(result);
maxMatchScore = mmr.maxVal;


Value of maxMatchScore is always 1.0

1.0 is the best match value, which means the image is %100 identical, but it isn't and on other reference images I get other values, so the program is working but only with this specific reference image I'm getting 1.0 also no matter what in file(source image) I give, it always finds the match on the first search upper left corner. A little bit annoying, I would be glad for your suggestions for a solution!

Here is some more code:

    Mat img = Highgui.imread(inFile);

// / Create the result matrix
int result_cols = img.cols() - templ.cols() + 1;
int result_rows = img.rows() - templ.rows() + 1;
Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);

// / Do the Matching
Imgproc.matchTemplate(img, templ, result, match_method);
// / Localizing the best match with minMaxLoc
MinMaxLocResult mmr = Core.minMaxLoc(result);


### Template Matching is wrong with specific Reference image

Until now I have always gotten reasonable results withc matching score, Im using "Imgproc.TM_CCOEFF_NORMED" method for template matching below are the results.

Reference image:

Outcome 1:

MinMaxLocResult mmr = Core.minMaxLoc(result);
maxMatchScore = mmr.maxVal;


Value of maxMatchScore is always 1.0

Outcome 2:

MinMaxLocResult mmr = Core.minMaxLoc(result);
maxMatchScore = mmr.maxVal;


Value of maxMatchScore is always 1.0

1.0 is the best match value, which means the image is %100 identical, but it isn't and on other reference images I get other values, so the program is working but only with this specific reference image I'm getting 1.0 also no matter what in file(source image) I give, it always finds the match on the first search upper left corner. A little bit annoying, I would be glad for your suggestions and comments for improvement because TM_CCOEFF_NORMED gives always the first searched square/rectangle as best match with score 1.0, this cant be correct, on the other hand I also tried TM_CCORR_NORMED and TM_SQDIFF_NORMED they gave different match scores, this is promising but still TM_CCORR_NORMED gave good matching score which is still unexpected from my side. I would be glad if someone can explain me the difference between the matching methods or give a solution!link to already an existing page where these methods are being discussed, on opencv docs/tutorials there are only the formulas available but no detailed explanation. In the end I would like to know when to use which match method for what kind of image to get the best results.

Here is some more code:

    Mat img = Highgui.imread(inFile);

// / Create the result matrix
int result_cols = img.cols() - templ.cols() + 1;
int result_rows = img.rows() - templ.rows() + 1;
Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);

// / Do the Matching
Imgproc.matchTemplate(img, templ, result, match_method);
// / Localizing the best match with minMaxLoc
MinMaxLocResult mmr = Core.minMaxLoc(result);


### Template Matching is wrong with specific Reference image

Until now I have always gotten reasonable results withc matching score, Im using "Imgproc.TM_CCOEFF_NORMED" method for template matching below are the results.

Reference image:

Outcome 1:

MinMaxLocResult mmr = Core.minMaxLoc(result);
maxMatchScore = mmr.maxVal;


Value of maxMatchScore is always 1.0

Outcome 2:

MinMaxLocResult mmr = Core.minMaxLoc(result);
maxMatchScore = mmr.maxVal;


Value of maxMatchScore is always 1.0

1.0 is the best match value, which means the image is %100 identical, but it isn't and on other reference images I get other values, so the program is working but only with this specific reference image I'm getting 1.0 also no matter what in file(source image) I give, it always finds the match on the first search upper left corner. A little bit annoying, I would be glad for your suggestions and comments for improvement because TM_CCOEFF_NORMED gives always the first searched square/rectangle as best match with score 1.0, this cant be correct, on the other hand I also tried TM_CCORR_NORMED and TM_SQDIFF_NORMED they gave different match scores, this is promising but still TM_CCORR_NORMED gave good matching score which is still unexpected from my side. I would be glad if someone can explain me the difference between the matching methods or give a link to already an existing page where these methods are being discussed, on opencv docs/tutorials there are only the formulas available but no detailed explanation. In the end I would like to know when to use which match method for what kind of image to get the best results.

Here is some more code:

    Mat img = Highgui.imread(inFile);

// / Create the result matrix
int result_cols = img.cols() - templ.cols() + 1;
int result_rows = img.rows() - templ.rows() + 1;
Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);

// / Do the Matching
Imgproc.matchTemplate(img, templ, result, match_method);
// / Localizing the best match with minMaxLoc
MinMaxLocResult mmr = Core.minMaxLoc(result);


### Template Matching is wrong with specific Reference image

Until now I have always gotten reasonable results withc matching score, Im using "Imgproc.TM_CCOEFF_NORMED" method for template matching below are the results.

Reference image:

Outcome 1:

MinMaxLocResult mmr = Core.minMaxLoc(result);
maxMatchScore = mmr.maxVal;


Value of maxMatchScore is always 1.0

Outcome 2:

MinMaxLocResult mmr = Core.minMaxLoc(result);
maxMatchScore = mmr.maxVal;


Value of maxMatchScore is always 1.0

1.0 is the best match value, which means the image is %100 identical, but it isn't and on other reference images I get other values, so the program is working but only with this specific reference image I'm getting 1.0 also no matter what in file(source image) I give, it always finds the match on the first search upper left corner. A little bit annoying, I would be glad for your suggestions and comments for improvement because TM_CCOEFF_NORMED gives always the first searched square/rectangle as best match with score 1.0, this cant be correct, on the other hand I also tried TM_CCORR_NORMED and TM_SQDIFF_NORMED they gave different match scores, this is promising but still TM_CCORR_NORMED gave good matching score which is still unexpected from my side. I would be glad if someone can explain me the difference between the matching methods or give a link to already an existing page where these methods are being discussed, on opencv docs/tutorials there are only the formulas available but no detailed explanation. In the end I would like to know when to use which match method for what kind of image to get the best results.

Here is some more code:

    Mat img = Highgui.imread(inFile);

// /  Create the result matrix
int result_cols = img.cols() - templ.cols() + 1;
int result_rows = img.rows() - templ.rows() + 1;
Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);

// /  Do the Matching
Imgproc.matchTemplate(img, templ, result, match_method);
// /  Localizing the best match with minMaxLoc
MinMaxLocResult mmr = Core.minMaxLoc(result);


If I add normalization after the matching with

     Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());


Then I become matchscore 0.0, which satisfies me and what I would expect, but this way gives only 1 or 0 nothing else in between, I would accept this if it was always giving correct results but sometimes it gives 0.0 for very good matches as well, i.e. when I cut a part from an image and try to find the cut part in the original image, then it gives 0.0, which should be the case... Also having values like 0.8734543, 0.234356, 0.450985948, helps me a lot to understand and analyze the comparisons, I have threshold of 0.9, any value above 0.9 I accept as correct, found.

 6 retagged sturkmen 6507 ●3 ●43 ●74 https://github.com/stu...

### Template Matching is wrong with specific Reference image

Until now I have always gotten reasonable results withc matching score, Im using "Imgproc.TM_CCOEFF_NORMED" method for template matching below are the results.

Reference image:

Outcome 1:

MinMaxLocResult mmr = Core.minMaxLoc(result);
maxMatchScore = mmr.maxVal;


Value of maxMatchScore is always 1.0

Outcome 2:

MinMaxLocResult mmr = Core.minMaxLoc(result);
maxMatchScore = mmr.maxVal;


Value of maxMatchScore is always 1.0

1.0 is the best match value, which means the image is %100 identical, but it isn't and on other reference images I get other values, so the program is working but only with this specific reference image I'm getting 1.0 also no matter what in file(source image) I give, it always finds the match on the first search upper left corner. A little bit annoying, I would be glad for your suggestions and comments for improvement because TM_CCOEFF_NORMED gives always the first searched square/rectangle as best match with score 1.0, this cant be correct, on the other hand I also tried TM_CCORR_NORMED and TM_SQDIFF_NORMED they gave different match scores, this is promising but still TM_CCORR_NORMED gave good matching score which is still unexpected from my side. I would be glad if someone can explain me the difference between the matching methods or give a link to already an existing page where these methods are being discussed, on opencv docs/tutorials there are only the formulas available but no detailed explanation. In the end I would like to know when to use which match method for what kind of image to get the best results.

Here is some more code:

    Mat img = Highgui.imread(inFile);

//  Create the result matrix
int result_cols = img.cols() - templ.cols() + 1;
int result_rows = img.rows() - templ.rows() + 1;
Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);

//  Do the Matching
Imgproc.matchTemplate(img, templ, result, match_method);
//  Localizing the best match with minMaxLoc
MinMaxLocResult mmr = Core.minMaxLoc(result);


If I add normalization after the matching with

     Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());


Then I become matchscore 0.0, which satisfies me and what I would expect, but this way gives only 1 or 0 nothing else in between, I would accept this if it was always giving correct results but sometimes it gives 0.0 for very good matches as well, i.e. when I cut a part from an image and try to find the cut part in the original image, then it gives 0.0, which should be the case... Also having values like 0.8734543, 0.234356, 0.450985948, helps me a lot to understand and analyze the comparisons, I have threshold of 0.9, any value above 0.9 I accept as correct, found.