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() 5 1 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_SQDIFF_NORMED Results:
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.