Template matching is a technique used to find a specific pattern in a bigger image by comparing it to a predefined template. It is a simple but efficient image processing technique. It's extensively used in applications such as face recognition, object detection, and surveillance.
This Answer will walk through the concept of template matching and demonstrate its implementation using OpenCV, an impactful open-source toolkit utilized for tasks in computer vision and machine learning.
In the world of computer vision, template matching is like searching for a specific pattern within a bigger picture. It scans the larger image (source image) with the smaller image (template), trying to find the best match.
This technique uses pixel intensities to identify the similarity. The template is slid across the source image, and a similarity score is calculated for each position. The position with the highest score is considered the best match.
OpenCV supports several comparison methods for template matching, including:
Square difference (CV_TM_SQDIFF): This method calculates the squared difference between the pixel intensities of the source image and template. A lower score indicates a better match.
Normalized square difference (CV_TM_SQDIFF_NORMED): This is similar to the Square Difference, but the result is normalized.
Cross-correlation (CV_TM_CCORR): It calculates the cross-correlation between the source image and template. A higher score indicates a better match.
Normalized cross-correlation (CV_TM_CCORR_NORMED): In this method, the result of cross-correlation is normalized.
Coefficient correlation (CV_TM_CCOEFF): This method calculates the correlation coefficient between the source image and template. A higher score indicates a better match.
Normalized coefficient correlation (CV_TM_CCOEFF_NORMED): In this method, the correlation coefficient is normalized.
To use OpenCV, first, ensure Python is installed. The Python package manager, pip
, can be used to install OpenCV.
pip install opencv-python
The next step is implementing template matching with OpenCV. Here is a guide to do this:
Begin by importing the necessary libraries, cv2
for OpenCV, and matplotlib
to display images.
import cv2import matplotlib.pyplot as plt
Next, load the source image and the template image using the cv2.imread()
function.
input_image = cv2.imread('source.png', 0)pattern_template = cv2.imread('template.png', 0)
Perform template matching using one of the template matching methods in OpenCV. This tutorial uses the normalized cross-correlation method.
matching_output = cv2.matchTemplate(input_image, pattern_template, cv2.TM_CCOEFF_NORMED)
Utilize the cv2.minMaxLoc()
function to locate the position of the most suitable match.
least_value, peak_value, least_coord, peak_coord = cv2.minMaxLoc(matching_output)
Lastly, draw a rectangle around the match. The top-left corner and the bottom-right corner of the rectangle can be calculated using the position of the best match and the template size.
highlight_start = peak_coordpattern_w, pattern_h = pattern_template.shape[::-1]highlight_end = (highlight_start[0] + pattern_w, highlight_start[1] + pattern_h)cv2.rectangle(input_image, highlight_start, highlight_end, 255, 2)
Finally, use subplots to display both the template and the result image.
fig_instance, axes_arr = plt.subplots(1, 2, figsize=(10, 5))# Show the pattern templateaxes_arr[0].imshow(pattern_template, cmap='gray')axes_arr[0].set_title('Pattern Template')# Show the input image with the highlighted matchaxes_arr[1].imshow(input_image, cmap='gray')axes_arr[1].set_title('Pattern Highlighted')plt.show()
Here's the complete executable code for template matching in Python:
import cv2 import matplotlib.pyplot as plt # Load the input images input_image = cv2.imread('source.png', 0) pattern_template = cv2.imread('template.png', 0) # Execute the template matching process matching_output = cv2.matchTemplate(input_image, pattern_template, cv2.TM_CCOEFF_NORMED) # Identify the best match location least_value, peak_value, least_coord, peak_coord = cv2.minMaxLoc(matching_output) # Highlight the best match in the input image highlight_start = peak_coord pattern_w, pattern_h = pattern_template.shape[::-1] highlight_end = (highlight_start[0] + pattern_w, highlight_start[1] + pattern_h) cv2.rectangle(input_image, highlight_start, highlight_end, 255, 2) # Visualize the pattern template and resulting image fig_instance, axes_arr = plt.subplots(1, 2, figsize=(10, 5)) # Show the pattern template axes_arr[0].imshow(pattern_template, cmap='gray') axes_arr[0].set_title('Pattern Template') # Show the input image with the highlighted match axes_arr[1].imshow(input_image, cmap='gray') axes_arr[1].set_title('Pattern Highlighted') plt.show()
Lines 1–2: The OpenCV library (imported as cv2
) is used for working with images and the matplotlib
library (imported as plt
) is used for displaying the images.
Line 6: These lines load the images from our local directory. The source image is the image we are searching in, and the template is the image we are searching for. The second parameter ‘0
’ in the imread
function is used to load the image in grayscale mode.
Line 9: The function cv2.matchTemplate
operates by moving the template image across the input image, akin to a 2D convolution process. For every position, it evaluates how closely the input image’s segment aligns with the given template. The outcome of this function is a grayscale image, where each pixel’s intensity indicates the degree of match between the template and the surrounding region of that particular pixel in the input image.
Line 12: cv2.minMaxLoc
is a function in OpenCV which returns the minimum and maximum pixel values and their locations in an image. Here it is used to find the area of the best match (highest value) and its top-left location.
Line 18: This line sketches a rectangle around the optimal match within the source image. The rectangle's top-left corner aligns with the location of the highest match, and its dimensions mirror those of the template in terms of width and height.
Lines 21–29: These lines use matplotlib
to display the template and the source image with the rectangle drawn on it. The subplot
function is used to create multiple plots in the same figure. The imshow
function is used to display an image in each plot.
Line 31: plt.show()
is used to display the figure with the two plots.
With a few lines of Python code and OpenCV, template matching becomes a simple task. It's a powerful tool in image processing and can serve as a stepping stone to more complex techniques in computer vision.
Here are some more OpenCV tutorials:
Free Resources