2

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()
Cherry
  • 61
  • 1
  • 1
  • 9
  • 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 04: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 08:09
  • As written in the documentation (https://www.docs.opencv.org/master/d2/de8/group__core__array.html#gab473bf2eb6d14ff97e89b355dac20707) 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 06: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 Answers1

5

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.

ZdaR
  • 22,343
  • 7
  • 66
  • 87
  • 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 08:13
  • Yes exactly :) (y) – ZdaR Nov 14 '18 at 09:01