OpenCV matchTemplate function tutorial to find matching template in source image
In this article, I will show you how to find a template image in a bigger source image using OpenCV C++. This is a useful technique for object detection, especially template detection, as well as image processing and analysis.
The main idea is to use the template matching function in OpenCV, which compares a template image with a source image and returns a similarity score for each pixel in the source image. The highest score indicates the best match location of the template image in the source image.
Template matching works by sliding the template image over the input image similarity at each location. The similarity comparison is done using a metric that measures how well the template matches the input image patch. The metric is stored in a result matrix, where each element corresponds to a location of the template over the input image. The location with the highest (or lowest) value in the result matrix is considered the best match.
The two methods that I will explain are cv::TM_CCOEFF_NORMED and cv::TM_SQDIFF_NORMED. These methods are normalized versions of cv::TM_CCOEFF and cv::TM_SQDIFF, respectively. Normalization means that the metric values are scaled to fall into the range [0, 1], where 1 indicates a perfect match and 0 indicates no match. This makes the comparison more robust to variations in illumination and contrast.
The formula for Opencv cv::TM_CCOEFF_NORMED is:
This metric calculates the correlation coefficient between the template and the input image patch, which is a measure of how linearly related they are. A high value of the metric means that the template and the input image patch have similar pixel values and variations.
The formula for Opencv cv::TM_SQDIFF_NORMED is:
This metric calculates the normalized squared difference between the template and the input image patch, which is a measure of how dissimilar they are. A low value means that the template and the input image patch have similar pixel values.
Opencv cv::matchTemplate
To use these methods in OpenCV, we can use the function cv::matchTemplate(), which takes four arguments: the input image, the template image, the result matrix, and the matching method. Optionally, we can also provide a mask image that defines which part of the template should be used for matching. For example:
To use the template matching function, we need to follow these steps:
1. Load the template image and the source image as grayscale images using cv::imread().
2. Create a result image with the same size as the source image, where we will store the similarity scores.
3. Choose a template matching method from the available options in OpenCV, such as cv::TM_CCOEFF_NORMED or cv::TM_SQDIFF_NORMED.
4. Call cv::matchTemplate() with the template image, the source image, the result image, and the chosen method as arguments.
5. Find the maximum or minimum value (depending on the method) and its location in the result image using cv::minMaxLoc().
6. Draw a rectangle around the best match location on the source image using cv::rectangle().
7. Display the source image and the result image using cv::imshow().