OpenCV Tutorial
Image Processing
Feature Detection and Description
Drawing Functions
Video Processing
Applications and Projects
Template matching is a technique in computer vision used to find a sub-picture (template) in an image. It's a way to locate specific objects within the larger image. OpenCV provides a convenient function, cv2.matchTemplate()
, for this purpose.
Here's a tutorial on template matching using OpenCV in Python:
First, make sure you've installed OpenCV:
pip install opencv-python
Then, import the required libraries:
import cv2 import numpy as np
main_image = cv2.imread('main_image.jpg', cv2.IMREAD_GRAYSCALE) template = cv2.imread('template.jpg', cv2.IMREAD_GRAYSCALE)
In this example, both the main image and the template are loaded in grayscale, which simplifies the process. Adjust as needed.
Use the cv2.matchTemplate()
function:
result = cv2.matchTemplate(main_image, template, cv2.TM_CCOEFF_NORMED)
There are various methods available such as cv2.TM_CCOEFF
, cv2.TM_CCOEFF_NORMED
, cv2.TM_CCORR
, etc. The method cv2.TM_CCOEFF_NORMED
is used here as it usually provides good results, but you can experiment with others to see which works best for your specific use case.
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
The cv2.minMaxLoc()
function returns the minimum and maximum pixel values and their locations. For cv2.TM_CCOEFF_NORMED
, the maximum location (max_loc
) corresponds to the top-left corner of where the template best matches the main image.
top_left = max_loc bottom_right = (top_left[0] + template.shape[1], top_left[1] + template.shape[0]) cv2.rectangle(main_image, top_left, bottom_right, 255, 2)
Here, 255
is the color of the rectangle (white in this grayscale image) and 2
is the thickness of the rectangle's lines.
cv2.imshow('Detected', main_image) cv2.waitKey(0) cv2.destroyAllWindows()
Putting it all together:
import cv2 import numpy as np # Read the images main_image = cv2.imread('main_image.jpg', cv2.IMREAD_GRAYSCALE) template = cv2.imread('template.jpg', cv2.IMREAD_GRAYSCALE) # Template matching result = cv2.matchTemplate(main_image, template, cv2.TM_CCOEFF_NORMED) min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result) # Draw a rectangle around the matched area top_left = max_loc bottom_right = (top_left[0] + template.shape[1], top_left[1] + template.shape[0]) cv2.rectangle(main_image, top_left, bottom_right, 255, 2) # Display the result cv2.imshow('Detected', main_image) cv2.waitKey(0) cv2.destroyAllWindows()
Run the script, and you should see the area where the template matches the main image highlighted with a rectangle. Remember that template matching is sensitive to size, rotation, and sometimes lighting changes, so it's most effective when the template is a direct subset of the main image.
Template matching with OpenCV in Python:
Template matching is a technique in computer vision to find a template (small image) within a larger image. OpenCV provides a function called cv2.matchTemplate
for template matching.
import cv2 import numpy as np # Read the main image and the template main_image = cv2.imread('main_image.jpg') template = cv2.imread('template_image.jpg') # Convert both images to grayscale gray_main = cv2.cvtColor(main_image, cv2.COLOR_BGR2GRAY) gray_template = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY) # Perform template matching result = cv2.matchTemplate(gray_main, gray_template, cv2.TM_CCOEFF_NORMED) # Get the coordinates of the best match min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result) # Draw a rectangle around the matched area top_left = max_loc bottom_right = (top_left[0] + template.shape[1], top_left[1] + template.shape[0]) cv2.rectangle(main_image, top_left, bottom_right, (0, 255, 0), 2) # Display the result cv2.imshow('Matched Result', main_image) cv2.waitKey(0) cv2.destroyAllWindows()
Real-time template matching in OpenCV: Template matching can be applied in real-time by continuously capturing frames from a camera.
import cv2 import numpy as np # Open a connection to the webcam (0 represents the default camera) cap = cv2.VideoCapture(0) # Read the template image template = cv2.imread('template_image.jpg') gray_template = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY) while True: # Read a frame from the webcam ret, frame = cap.read() # Convert the frame to grayscale gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Perform template matching result = cv2.matchTemplate(gray_frame, gray_template, cv2.TM_CCOEFF_NORMED) # Get the coordinates of the best match _, _, _, max_loc = cv2.minMaxLoc(result) # Draw a rectangle around the matched area top_left = max_loc bottom_right = (top_left[0] + template.shape[1], top_left[1] + template.shape[0]) cv2.rectangle(frame, top_left, bottom_right, (0, 255, 0), 2) # Display the result cv2.imshow('Real-time Template Matching', frame) # Break the loop if 'q' is pressed if cv2.waitKey(1) & 0xFF == ord('q'): break # Release the VideoCapture object cap.release() cv2.destroyAllWindows()
Handling multiple matches in template matching with OpenCV: Use thresholding and non-maximum suppression to handle multiple matches.
import cv2 import numpy as np main_image = cv2.imread('main_image.jpg') template = cv2.imread('template_image.jpg') gray_main = cv2.cvtColor(main_image, cv2.COLOR_BGR2GRAY) gray_template = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY) # Perform template matching result = cv2.matchTemplate(gray_main, gray_template, cv2.TM_CCOEFF_NORMED) # Set a threshold for matches threshold = 0.8 loc = np.where(result >= threshold) # Draw rectangles around multiple matches for pt in zip(*loc[::-1]): cv2.rectangle(main_image, pt, (pt[0] + template.shape[1], pt[1] + template.shape[0]), (0, 255, 0), 2) # Display the result cv2.imshow('Multiple Matches', main_image) cv2.waitKey(0) cv2.destroyAllWindows()