Last modified: Jan 17, 2025 By Alexander Williams

Python OpenCV cv2.matchTemplate() Guide

Template matching is a technique in computer vision. It is used to find a template image within a larger image. OpenCV provides the cv2.matchTemplate() function for this purpose.

This guide will help you understand how to use cv2.matchTemplate() effectively. We will cover the basics, provide examples, and explain the output.

What is cv2.matchTemplate()?

The cv2.matchTemplate() function is used to search for a template image within a larger image. It slides the template over the input image and compares the template with the overlapped region.

The function returns a result matrix. This matrix contains the comparison results. The best match is the location with the highest value in the result matrix.

How to Use cv2.matchTemplate()

To use cv2.matchTemplate(), you need two images: the source image and the template image. The source image is the larger image where you want to find the template.

Here is a simple example:


import cv2
import numpy as np

# Load the source and template images
source = cv2.imread('source_image.jpg', 0)
template = cv2.imread('template_image.jpg', 0)

# Get the dimensions of the template
w, h = template.shape[::-1]

# Apply template matching
result = cv2.matchTemplate(source, template, cv2.TM_CCOEFF_NORMED)

# Get the best match position
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)

# Draw a rectangle around the matched region
top_left = max_loc
bottom_right = (top_left[0] + w, top_left[1] + h)
cv2.rectangle(source, top_left, bottom_right, 255, 2)

# Display the result
cv2.imshow('Matched Result', source)
cv2.waitKey(0)
cv2.destroyAllWindows()

In this example, we load the source and template images. We then apply cv2.matchTemplate() to find the template in the source image. Finally, we draw a rectangle around the matched region and display the result.

Understanding the Output

The output of cv2.matchTemplate() is a result matrix. This matrix contains the comparison results for each position of the template over the source image.

The best match is the location with the highest value in the result matrix. You can use cv2.minMaxLoc() to find this location.

Here is the output of the previous example:


Matched Result: A window showing the source image with a rectangle around the matched region.

Practical Applications

Template matching is useful in various applications. It is used in object detection, image recognition, and video analysis. For example, you can use it to detect logos in images or find specific objects in video frames.

If you are working with more complex image processing tasks, you might also find Python OpenCV cv2.bitwise_and() Guide and Python OpenCV cv2.morphologyEx() Guide helpful.

Conclusion

Template matching is a powerful technique in computer vision. The cv2.matchTemplate() function in OpenCV makes it easy to implement. This guide covered the basics, provided an example, and explained the output.

With this knowledge, you can start using cv2.matchTemplate() in your projects. For more advanced techniques, consider exploring other OpenCV functions like cv2.addWeighted().