OpenCV Object Recognition

In the field of computer vision, object recognition is a very important task.

OpenCV is a powerful open-source computer vision library that provides multiple methods to implement object recognition.

This article will introduce in detail how to use the template matching method in OpenCV (cv2.matchTemplate()) to perform object recognition.

What is template matching?

Template matching is a technique for finding the region in an image that is most similar to a given template image.

In simple terms, template matching is the process of finding the part in a large image that best matches the template image (i.e., the object we want to recognize). This method is suitable for cases where the size, orientation, and shape of the object in the image remain basically unchanged.

  • Template image:An image patch of the target object.

  • Search image:The image to be detected.

  • Matching result:Represents the similarity distribution of the template image in the search image.

Basic principles of template matching

The basic principle of template matching is to slide the template image over the target image, compute the similarity at each position, and find the position with the highest similarity. OpenCV provides multiple similarity calculation methods, such as squared difference matching (cv2.TM_SQDIFF), normalized squared difference matching (cv2.TM_SQDIFF_NORMED), cross-correlation matching (cv2.TM_CCORR), normalized cross-correlation matching (cv2.TM_CCORR_NORMED), correlation coefficient matching (cv2.TM_CCOEFF) and normalized correlation coefficient matching (cv2.TM_CCOEFF_NORMED)。

Application scenarios

  • Object recognition: used to locate specific objects in an image, such as logos, icons, etc.
  • Object tracking: used to track a target object in video.
  • Image registration: used to align two images.

Implementation steps of template matching

  1. Load the image:Read the search image and the template image.

  2. Template matching:Usecv2.matchTemplate()to find the template image in the search image.

  3. Get the matching result:Usecv2.minMaxLoc()to get the best match location.

  4. Draw the matching result:Draw the matched area in the search image.

  5. Display the result:Display the matching result.

Matching methods

OpenCV provides multiple template matching methods, which can be specified via the third parameter of cv2.matchTemplate():

MethodDescription
cv2.TM_SQDIFFSquared difference matching; the smaller the value, the higher the match.
cv2.TM_SQDIFF_NORMEDNormalized squared difference matching; the smaller the value, the higher the match.
cv2.TM_CCORRCross-correlation matching; the larger the value, the higher the match.
cv2.TM_CCORR_NORMEDNormalized cross-correlation matching; the larger the value, the higher the match.
cv2.TM_CCOEFFCorrelation coefficient matching; the larger the value, the higher the match.
cv2.TM_CCOEFF_NORMEDNormalized correlation coefficient matching; the larger the value, the higher the match.

Usingcv2.matchTemplate()for object recognition

1. Import the necessary libraries

First, we need to import the OpenCV and NumPy libraries.

NumPy is a fundamental library for scientific computing in Python, and OpenCV uses NumPy arrays to store image data.

Example

import cv2
import numpy as np

2. Load the image and template

Next, we need to load the target image and the template image.

The target image is the image in which we want to find the object, and the template image is the object we want to recognize.

Example

# Load the target image and the template image
img = cv2.imread('target_image.jpg', 0)
template = cv2.imread('template_image.jpg', 0)

3. Get the dimensions of the template image

To slide the template image over the target image, we need to know the width and height of the template image.

Example

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

4. Perform template matching

Usecv2.matchTemplate()function to perform template matching.

This function returns a result matrix, where each element represents the similarity between the corresponding position in the target image and the template image.

Example

# Perform template matching
res = cv2.matchTemplate(img, template, cv2.TM_CCOEFF_NORMED)

5. Set the matching threshold and find the match location

We can set a threshold to determine whether the match is successful.

Then, usecv2.minMaxLoc()function to find the positions of the maximum and minimum values in the result matrix.

Example

# Set the matching threshold
threshold = 0.8

# Find the match location
loc = np.where(res >= threshold)

6. Mark the match location in the target image

Finally, we can mark the position in the target image that matches the template.

Usually, we use a rectangle to mark the matching area.

Example

# Mark the match location in the target image
for pt in zip(*loc[::-1]):
    cv2.rectangle(img, pt, (pt[0] + w, pt[1] + h), (0, 255, 0), 2)

# Display the result image
cv2.imshow('Matched Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Complete code

The following is a complete code example showing how to usecv2.matchTemplate()to perform object recognition.

Example

import cv2
import numpy as np

# Load the target image and the template image
img = cv2.imread('target_image.jpg', 0)
template = cv2.imread('template_image.jpg', 0)

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

# Perform template matching
res = cv2.matchTemplate(img, template, cv2.TM_CCOEFF_NORMED)

# Set the matching threshold
threshold = 0.8

# Find the match location
loc = np.where(res >= threshold)

# Mark the match location in the target image
for pt in zip(*loc[::-1]):
    cv2.rectangle(img, pt, (pt[0] + w, pt[1] + h), (0, 255, 0), 2)

# Display the result image
cv2.imshow('Matched Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Other extensions