Opencv Matchtemplate
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 for object recognition.
This article will provide a detailed introduction on how to use the template matching method in OpenCV (`cv2.matchTemplate()`) for object recognition.
### What is Template Matching?
Template matching is a technique for finding the most similar region to a given template image in an image.
Simply put, template matching is finding the best matching part of a template image (the object we want to recognize) in a larger image. This method is suitable for cases where the object's size, orientation, and shape in the image remain basically unchanged.
* **Template Image:** An image fragment 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, calculate the similarity at each position, and find the position with the highest similarity. OpenCV provides multiple similarity calculation methods, such as Square Difference Matching (`cv2.TM_SQDIFF`), Normalized Square Difference Matching (`cv2.TM_SQDIFF_NORMED`), Correlation Matching (`cv2.TM_CCORR`), Normalized 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 images, such as logos, icons, etc.
* Target Tracking: Used to track target objects in videos.
* Image Registration: Used to align two images.
### Implementation Steps of Template Matching
1. **Load Images:** Read the search image and template image.
2. **Template Matching:** Use `cv2.matchTemplate()` to find the template image in the search image.
3. **Get Matching Result:** Use `cv2.minMaxLoc()` to get the best matching position.
4. **Draw Matching Result:** Draw the matching region in the search image.
5. **Display Result:** Display the matching result.
### Matching Methods
OpenCV provides multiple template matching methods, which can be specified through the third parameter of cv2.matchTemplate():
| **Method** | **Description** |
| --- | --- |
| `cv2.TM_SQDIFF` | Square difference matching, the smaller the value, the higher the matching degree. |
| `cv2.TM_SQDIFF_NORMED` | Normalized square difference matching, the smaller the value, the higher the matching degree. |
| `cv2.TM_CCORR` | Correlation matching, the larger the value, the higher the matching degree. |
| `cv2.TM_CCORR_NORMED` | Normalized correlation matching, the larger the value, the higher the matching degree. |
| `cv2.TM_CCOEFF` | Correlation coefficient matching, the larger the value, the higher the matching degree. |
| `cv2.TM_CCOEFF_NORMED` | Normalized correlation coefficient matching, the larger the value, the higher the matching degree. |
## Object Recognition Using `cv2.matchTemplate()`
### 1. Import Required Libraries
First, we need to import the OpenCV and NumPy libraries.
NumPy is the 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 Images and Template
Next, we need to load the target image and 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 target image and template image
img = cv2.imread('target_image.jpg',0)
template = cv2.imread('template_image.jpg',0)
### 3. Get Template Image Dimensions
In order to slide the template image over the target image, we need to know the width and height of the template image.
## Example
# Get template image dimensions
w, h = template.shape[::-1]
### 4. Perform Template Matching
Use the `cv2.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 Matching Threshold and Find Matching Position
We can set a threshold to determine whether the matching is successful.
Then, use the `cv2.minMaxLoc()` function to find the positions of the maximum and minimum values in the result matrix.
## Example
# Set matching threshold
threshold =0.8
# Find matching position
loc = np.where(res >= threshold)
### 6. Mark Matching Position in Target Image
Finally, we can mark the positions that match the template in the target image.
Usually, we use a rectangle to mark the matching region.
## Example
# Mark matching position in target image
for pt in zip(*loc[::-1]):
cv2.rectangle(img, pt,(pt + w, pt + h),(0,255,0),2)
# Display result image
cv2.imshow('Matched Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
* * *
## Complete Code
The following is a complete code example demonstrating how to use `cv2.matchTemplate()` for object recognition.
## Example
import cv2
import numpy as np
# Load target image and template image
img = cv2.imread('target_image.jpg',0)
template = cv2.imread('template_image.jpg',0)
# Get template image dimensions
w, h = template.shape[::-1]
# Perform template matching
res = cv2.matchTemplate(img, template, cv2.TM_CCOEFF_NORMED)
# Set matching threshold
threshold =0.8
# Find matching position
loc = np.where(res >= threshold)
# Mark matching position in target image
for pt in zip(*loc[::-1]):
cv2.rectangle(img, pt,(pt + w, pt + h),(0,255,0),2)
# Display result image
cv2.imshow('Matched Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
YouTip