How to use the cv2.minMaxLoc() in template matching












-1















Here is the code I used for template matching and what do min_val, max_val, min_loc, max_loc mean? what are they used for?



import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('C:\machineLearning\positive\1.jpg', 0)
img2 = img.copy()
template = cv2.imread('C:\machineLearning\positive\1_.jpg', 0)
w, h = template.shape[::-1]

img = img2.copy()
method = eval('cv2.TM_SQDIFF')

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

top_left = min_loc
bottom_right = (top_left[0] + w, top_left[1] + h)

cv2.rectangle(img,top_left, bottom_right, 255, 2)

plt.subplot(121),plt.imshow(res,cmap = 'gray')
plt.title('Matching Result'), plt.xticks(), plt.yticks()
plt.subplot(122),plt.imshow(img,cmap = 'gray')
plt.title('Detected Point'), plt.xticks(), plt.yticks()
plt.suptitle('cv2.TM_SQDIFF')

plt.show()









share|improve this question

























  • min_val == minimum value, max_val == maximum value, min_loc == location of the minimum value (coordinates from the input image) max_loc == location of the maximum value (coordinates from the input image) You can confirm that by doing help(cv2.minMaxLoc) in your python interpreter. In the example of code you give min_loc is used to set the upper corner of a region of interest bounding box. The other variables seem not been used.

    – John_Sharp1318
    Nov 14 '18 at 4:57











  • min_val = minimum value, I definitely know that as I know English, what I asked was the meaning of the minimum value

    – Cherry
    Nov 14 '18 at 8:09











  • As written in the documentation (docs.opencv.org/master/d2/de8/…) the minimum value is the global minimum value (aka the minimum value among all the value of the matrix).

    – John_Sharp1318
    Nov 14 '18 at 22:25











  • Does that mean something? for example If I use TM_SQDIFF, does minimum value indicate the amount of matching?

    – Cherry
    Nov 15 '18 at 6:48











  • Check the documentation of the matchTemplate function. If (and only if) the variable "res" does contains the results of a square difference (and only that) so yes the minimum value will be the a global minimum square difference. If the variable does contains something that is influence by a square difference but is not the square difference so the interpretation is related to what information is store in res. You can easily find this information in the documentation. Please do check the documentation.

    – John_Sharp1318
    Nov 15 '18 at 16:27
















-1















Here is the code I used for template matching and what do min_val, max_val, min_loc, max_loc mean? what are they used for?



import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('C:\machineLearning\positive\1.jpg', 0)
img2 = img.copy()
template = cv2.imread('C:\machineLearning\positive\1_.jpg', 0)
w, h = template.shape[::-1]

img = img2.copy()
method = eval('cv2.TM_SQDIFF')

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

top_left = min_loc
bottom_right = (top_left[0] + w, top_left[1] + h)

cv2.rectangle(img,top_left, bottom_right, 255, 2)

plt.subplot(121),plt.imshow(res,cmap = 'gray')
plt.title('Matching Result'), plt.xticks(), plt.yticks()
plt.subplot(122),plt.imshow(img,cmap = 'gray')
plt.title('Detected Point'), plt.xticks(), plt.yticks()
plt.suptitle('cv2.TM_SQDIFF')

plt.show()









share|improve this question

























  • min_val == minimum value, max_val == maximum value, min_loc == location of the minimum value (coordinates from the input image) max_loc == location of the maximum value (coordinates from the input image) You can confirm that by doing help(cv2.minMaxLoc) in your python interpreter. In the example of code you give min_loc is used to set the upper corner of a region of interest bounding box. The other variables seem not been used.

    – John_Sharp1318
    Nov 14 '18 at 4:57











  • min_val = minimum value, I definitely know that as I know English, what I asked was the meaning of the minimum value

    – Cherry
    Nov 14 '18 at 8:09











  • As written in the documentation (docs.opencv.org/master/d2/de8/…) the minimum value is the global minimum value (aka the minimum value among all the value of the matrix).

    – John_Sharp1318
    Nov 14 '18 at 22:25











  • Does that mean something? for example If I use TM_SQDIFF, does minimum value indicate the amount of matching?

    – Cherry
    Nov 15 '18 at 6:48











  • Check the documentation of the matchTemplate function. If (and only if) the variable "res" does contains the results of a square difference (and only that) so yes the minimum value will be the a global minimum square difference. If the variable does contains something that is influence by a square difference but is not the square difference so the interpretation is related to what information is store in res. You can easily find this information in the documentation. Please do check the documentation.

    – John_Sharp1318
    Nov 15 '18 at 16:27














-1












-1








-1








Here is the code I used for template matching and what do min_val, max_val, min_loc, max_loc mean? what are they used for?



import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('C:\machineLearning\positive\1.jpg', 0)
img2 = img.copy()
template = cv2.imread('C:\machineLearning\positive\1_.jpg', 0)
w, h = template.shape[::-1]

img = img2.copy()
method = eval('cv2.TM_SQDIFF')

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

top_left = min_loc
bottom_right = (top_left[0] + w, top_left[1] + h)

cv2.rectangle(img,top_left, bottom_right, 255, 2)

plt.subplot(121),plt.imshow(res,cmap = 'gray')
plt.title('Matching Result'), plt.xticks(), plt.yticks()
plt.subplot(122),plt.imshow(img,cmap = 'gray')
plt.title('Detected Point'), plt.xticks(), plt.yticks()
plt.suptitle('cv2.TM_SQDIFF')

plt.show()









share|improve this question
















Here is the code I used for template matching and what do min_val, max_val, min_loc, max_loc mean? what are they used for?



import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('C:\machineLearning\positive\1.jpg', 0)
img2 = img.copy()
template = cv2.imread('C:\machineLearning\positive\1_.jpg', 0)
w, h = template.shape[::-1]

img = img2.copy()
method = eval('cv2.TM_SQDIFF')

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

top_left = min_loc
bottom_right = (top_left[0] + w, top_left[1] + h)

cv2.rectangle(img,top_left, bottom_right, 255, 2)

plt.subplot(121),plt.imshow(res,cmap = 'gray')
plt.title('Matching Result'), plt.xticks(), plt.yticks()
plt.subplot(122),plt.imshow(img,cmap = 'gray')
plt.title('Detected Point'), plt.xticks(), plt.yticks()
plt.suptitle('cv2.TM_SQDIFF')

plt.show()






python opencv image-processing opencv3.0 template-matching






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 2:45







Cherry

















asked Nov 14 '18 at 2:05









CherryCherry

97




97













  • min_val == minimum value, max_val == maximum value, min_loc == location of the minimum value (coordinates from the input image) max_loc == location of the maximum value (coordinates from the input image) You can confirm that by doing help(cv2.minMaxLoc) in your python interpreter. In the example of code you give min_loc is used to set the upper corner of a region of interest bounding box. The other variables seem not been used.

    – John_Sharp1318
    Nov 14 '18 at 4:57











  • min_val = minimum value, I definitely know that as I know English, what I asked was the meaning of the minimum value

    – Cherry
    Nov 14 '18 at 8:09











  • As written in the documentation (docs.opencv.org/master/d2/de8/…) the minimum value is the global minimum value (aka the minimum value among all the value of the matrix).

    – John_Sharp1318
    Nov 14 '18 at 22:25











  • Does that mean something? for example If I use TM_SQDIFF, does minimum value indicate the amount of matching?

    – Cherry
    Nov 15 '18 at 6:48











  • Check the documentation of the matchTemplate function. If (and only if) the variable "res" does contains the results of a square difference (and only that) so yes the minimum value will be the a global minimum square difference. If the variable does contains something that is influence by a square difference but is not the square difference so the interpretation is related to what information is store in res. You can easily find this information in the documentation. Please do check the documentation.

    – John_Sharp1318
    Nov 15 '18 at 16:27



















  • min_val == minimum value, max_val == maximum value, min_loc == location of the minimum value (coordinates from the input image) max_loc == location of the maximum value (coordinates from the input image) You can confirm that by doing help(cv2.minMaxLoc) in your python interpreter. In the example of code you give min_loc is used to set the upper corner of a region of interest bounding box. The other variables seem not been used.

    – John_Sharp1318
    Nov 14 '18 at 4:57











  • min_val = minimum value, I definitely know that as I know English, what I asked was the meaning of the minimum value

    – Cherry
    Nov 14 '18 at 8:09











  • As written in the documentation (docs.opencv.org/master/d2/de8/…) the minimum value is the global minimum value (aka the minimum value among all the value of the matrix).

    – John_Sharp1318
    Nov 14 '18 at 22:25











  • Does that mean something? for example If I use TM_SQDIFF, does minimum value indicate the amount of matching?

    – Cherry
    Nov 15 '18 at 6:48











  • Check the documentation of the matchTemplate function. If (and only if) the variable "res" does contains the results of a square difference (and only that) so yes the minimum value will be the a global minimum square difference. If the variable does contains something that is influence by a square difference but is not the square difference so the interpretation is related to what information is store in res. You can easily find this information in the documentation. Please do check the documentation.

    – John_Sharp1318
    Nov 15 '18 at 16:27

















min_val == minimum value, max_val == maximum value, min_loc == location of the minimum value (coordinates from the input image) max_loc == location of the maximum value (coordinates from the input image) You can confirm that by doing help(cv2.minMaxLoc) in your python interpreter. In the example of code you give min_loc is used to set the upper corner of a region of interest bounding box. The other variables seem not been used.

– John_Sharp1318
Nov 14 '18 at 4:57





min_val == minimum value, max_val == maximum value, min_loc == location of the minimum value (coordinates from the input image) max_loc == location of the maximum value (coordinates from the input image) You can confirm that by doing help(cv2.minMaxLoc) in your python interpreter. In the example of code you give min_loc is used to set the upper corner of a region of interest bounding box. The other variables seem not been used.

– John_Sharp1318
Nov 14 '18 at 4:57













min_val = minimum value, I definitely know that as I know English, what I asked was the meaning of the minimum value

– Cherry
Nov 14 '18 at 8:09





min_val = minimum value, I definitely know that as I know English, what I asked was the meaning of the minimum value

– Cherry
Nov 14 '18 at 8:09













As written in the documentation (docs.opencv.org/master/d2/de8/…) the minimum value is the global minimum value (aka the minimum value among all the value of the matrix).

– John_Sharp1318
Nov 14 '18 at 22:25





As written in the documentation (docs.opencv.org/master/d2/de8/…) the minimum value is the global minimum value (aka the minimum value among all the value of the matrix).

– John_Sharp1318
Nov 14 '18 at 22:25













Does that mean something? for example If I use TM_SQDIFF, does minimum value indicate the amount of matching?

– Cherry
Nov 15 '18 at 6:48





Does that mean something? for example If I use TM_SQDIFF, does minimum value indicate the amount of matching?

– Cherry
Nov 15 '18 at 6:48













Check the documentation of the matchTemplate function. If (and only if) the variable "res" does contains the results of a square difference (and only that) so yes the minimum value will be the a global minimum square difference. If the variable does contains something that is influence by a square difference but is not the square difference so the interpretation is related to what information is store in res. You can easily find this information in the documentation. Please do check the documentation.

– John_Sharp1318
Nov 15 '18 at 16:27





Check the documentation of the matchTemplate function. If (and only if) the variable "res" does contains the results of a square difference (and only that) so yes the minimum value will be the a global minimum square difference. If the variable does contains something that is influence by a square difference but is not the square difference so the interpretation is related to what information is store in res. You can easily find this information in the documentation. Please do check the documentation.

– John_Sharp1318
Nov 15 '18 at 16:27












1 Answer
1






active

oldest

votes


















0














If you go through cv2.matchTemplate() docs, the function returns a fuzzy single channel matrix with the matching score of template and input image segments. For cv2.TM_CCOEFF method the point with the highest score would be the brightest, but in case of cv2.TM_SQDIFF_NORMED method, the point with highest score would be darkest



cv2.TM_CCOEFF Results:



cv2.TM_CCOEFF



cv2.TM_SQDIFF_NORMED Results:



enter image description here



So depending upon the various methods available, you may sometimes need to get the brightest spot or the darkest spot in the output matrix. cv2.minMaxLoc() is just a unification of these two generic operations, when you are using minMaxLoc, you can ignore min attributes for your use case.






share|improve this answer


























  • Thank you, Since Im using TM_SQDIFF method and I will need to consider the min attributes right? what does the darkest point mean? is it the best match? and if that become smaller the detection is more accurate? and if it becomes a larger value the detection is more inaccurate?

    – Cherry
    Nov 14 '18 at 8:13











  • Yes exactly :) (y)

    – ZdaR
    Nov 14 '18 at 9:01











  • I see thats what I wanted to know, thank you

    – Cherry
    Nov 15 '18 at 6:50













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292170%2fhow-to-use-the-cv2-minmaxloc-in-template-matching%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














If you go through cv2.matchTemplate() docs, the function returns a fuzzy single channel matrix with the matching score of template and input image segments. For cv2.TM_CCOEFF method the point with the highest score would be the brightest, but in case of cv2.TM_SQDIFF_NORMED method, the point with highest score would be darkest



cv2.TM_CCOEFF Results:



cv2.TM_CCOEFF



cv2.TM_SQDIFF_NORMED Results:



enter image description here



So depending upon the various methods available, you may sometimes need to get the brightest spot or the darkest spot in the output matrix. cv2.minMaxLoc() is just a unification of these two generic operations, when you are using minMaxLoc, you can ignore min attributes for your use case.






share|improve this answer


























  • Thank you, Since Im using TM_SQDIFF method and I will need to consider the min attributes right? what does the darkest point mean? is it the best match? and if that become smaller the detection is more accurate? and if it becomes a larger value the detection is more inaccurate?

    – Cherry
    Nov 14 '18 at 8:13











  • Yes exactly :) (y)

    – ZdaR
    Nov 14 '18 at 9:01











  • I see thats what I wanted to know, thank you

    – Cherry
    Nov 15 '18 at 6:50


















0














If you go through cv2.matchTemplate() docs, the function returns a fuzzy single channel matrix with the matching score of template and input image segments. For cv2.TM_CCOEFF method the point with the highest score would be the brightest, but in case of cv2.TM_SQDIFF_NORMED method, the point with highest score would be darkest



cv2.TM_CCOEFF Results:



cv2.TM_CCOEFF



cv2.TM_SQDIFF_NORMED Results:



enter image description here



So depending upon the various methods available, you may sometimes need to get the brightest spot or the darkest spot in the output matrix. cv2.minMaxLoc() is just a unification of these two generic operations, when you are using minMaxLoc, you can ignore min attributes for your use case.






share|improve this answer


























  • Thank you, Since Im using TM_SQDIFF method and I will need to consider the min attributes right? what does the darkest point mean? is it the best match? and if that become smaller the detection is more accurate? and if it becomes a larger value the detection is more inaccurate?

    – Cherry
    Nov 14 '18 at 8:13











  • Yes exactly :) (y)

    – ZdaR
    Nov 14 '18 at 9:01











  • I see thats what I wanted to know, thank you

    – Cherry
    Nov 15 '18 at 6:50
















0












0








0







If you go through cv2.matchTemplate() docs, the function returns a fuzzy single channel matrix with the matching score of template and input image segments. For cv2.TM_CCOEFF method the point with the highest score would be the brightest, but in case of cv2.TM_SQDIFF_NORMED method, the point with highest score would be darkest



cv2.TM_CCOEFF Results:



cv2.TM_CCOEFF



cv2.TM_SQDIFF_NORMED Results:



enter image description here



So depending upon the various methods available, you may sometimes need to get the brightest spot or the darkest spot in the output matrix. cv2.minMaxLoc() is just a unification of these two generic operations, when you are using minMaxLoc, you can ignore min attributes for your use case.






share|improve this answer















If you go through cv2.matchTemplate() docs, the function returns a fuzzy single channel matrix with the matching score of template and input image segments. For cv2.TM_CCOEFF method the point with the highest score would be the brightest, but in case of cv2.TM_SQDIFF_NORMED method, the point with highest score would be darkest



cv2.TM_CCOEFF Results:



cv2.TM_CCOEFF



cv2.TM_SQDIFF_NORMED Results:



enter image description here



So depending upon the various methods available, you may sometimes need to get the brightest spot or the darkest spot in the output matrix. cv2.minMaxLoc() is just a unification of these two generic operations, when you are using minMaxLoc, you can ignore min attributes for your use case.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 9:00

























answered Nov 14 '18 at 6:47









ZdaRZdaR

13k33553




13k33553













  • Thank you, Since Im using TM_SQDIFF method and I will need to consider the min attributes right? what does the darkest point mean? is it the best match? and if that become smaller the detection is more accurate? and if it becomes a larger value the detection is more inaccurate?

    – Cherry
    Nov 14 '18 at 8:13











  • Yes exactly :) (y)

    – ZdaR
    Nov 14 '18 at 9:01











  • I see thats what I wanted to know, thank you

    – Cherry
    Nov 15 '18 at 6:50





















  • Thank you, Since Im using TM_SQDIFF method and I will need to consider the min attributes right? what does the darkest point mean? is it the best match? and if that become smaller the detection is more accurate? and if it becomes a larger value the detection is more inaccurate?

    – Cherry
    Nov 14 '18 at 8:13











  • Yes exactly :) (y)

    – ZdaR
    Nov 14 '18 at 9:01











  • I see thats what I wanted to know, thank you

    – Cherry
    Nov 15 '18 at 6:50



















Thank you, Since Im using TM_SQDIFF method and I will need to consider the min attributes right? what does the darkest point mean? is it the best match? and if that become smaller the detection is more accurate? and if it becomes a larger value the detection is more inaccurate?

– Cherry
Nov 14 '18 at 8:13





Thank you, Since Im using TM_SQDIFF method and I will need to consider the min attributes right? what does the darkest point mean? is it the best match? and if that become smaller the detection is more accurate? and if it becomes a larger value the detection is more inaccurate?

– Cherry
Nov 14 '18 at 8:13













Yes exactly :) (y)

– ZdaR
Nov 14 '18 at 9:01





Yes exactly :) (y)

– ZdaR
Nov 14 '18 at 9:01













I see thats what I wanted to know, thank you

– Cherry
Nov 15 '18 at 6:50







I see thats what I wanted to know, thank you

– Cherry
Nov 15 '18 at 6:50




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292170%2fhow-to-use-the-cv2-minmaxloc-in-template-matching%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine