Understand OpenCV Template Matching Algorithm: A Completed Guide – OpenCV Tutorial

By | October 14, 2020

In python opencv, we can use cv2.matchTemplate() to match images. Here are six template matching algorithms we can use. In this tutorial, we will discuss these algorithms.

OpenCV Template Matching Algorithms

If (x, y) in image1 and (x’, y’) in image2. In order to find the similarity between them, we can use six methods:

Algorithm Equation
cv2.TM_CCOEFF the equation of cv2.TM_CCOEFF The bigger the better
cv2.TM_CCOEFF_NORMED the equation of cv2.TM_CCOEFF_NORMED -1<=R<=1

R=1 is the best

cv2.TM_CCORR the equation of cv2.TM_CCORR R=0 is the worst

The bigger the better

cv2.TM_CCORR_NORMED the equation of cv2.TM_CCORR_NORMED 0<=R<=1

R=0 is the worst

cv2.TM_SQDIFF the equation of cv2.TM_SQDIFF R=0 is the best
cv2.TM_SQDIFF_NORMED the equation of cv2.TM_SQDIFF_NORMED 0<=R<=1

R=0 is the best

How to use these algorithm?

Here is an example:

res = cv2.matchTemplate(img1, img2, cv2.TM_SQDIFF_NORMED)

where res is a numpy.ndarray, the value of it is computed using cv2.TM_SQDIFF_NORMED.

Leave a Reply