Tag Archives: template matching comparison methods

Template matching using OpenCV

In the previous blogs, we discussed different segmentation algorithms. Now, let’s explore another important computer vision area known as object detection. This simply means identifying and locating objects, that is, where is this object present in the image. There are various algorithms available for object detection. In this blog, let’s discuss one such algorithm known as Template matching. So, let’s get started.

In the template matching, we have a template image and we need to find where is this in the input image. For this, the sliding window approach is used. In the sliding window approach, we simply slide the template image over the input image (similar to convolution) and compare the overlapping patch. For comparison, you can use any method such as cross-correlation, squared difference, etc. Below is the list of the comparison methods provided by OpenCV.

Here, I(x,y) denotes the input image, T(x,y) template image, R(x,y) result, and (w,h) as width and height of the template image.

This outputs a grayscale image, where each pixel represents how much does the neighborhood of that pixel match with the template (i.e. the comparison score). From this, we can either select the maximum/minimum (depending on the comparison method used) or use thresholding to select the probable region of interest. This is how the template matching works. Now, let’s see how to do this using OpenCV-Python.

OpenCV

OpenCV provides a built-in function cv2.matchTemplate() that implements the template matching algorithm. This takes as input the image, template and the comparison method and outputs the comparison result. The syntax is given below.

Let’s take the below input and template image and see how this works.

  • Read both the input and the template image
  • Apply the template matching using cv2.matchTemplate()
  • If the method is cv2.TM_SQDIFF or cv2.TM_SQDIFF_NORMED, take the minimum, otherwise, take the maximum. This can be done using the cv2.minMaxLoc() function which finds the minimum and maximum element values and their positions.
  • Once the min/max position is found, we can easily draw the rectangle by taking this position as the top-left corner and using (w,h) of the template.

Template Matching with Multiple Objects

But what if we have multiple instances of the object present in an image. Clearly, the above approach will not work as this only finds a single instance of an object because we are finding max/min location. One plausible approach would be to use thresholding. Instead of taking out the max/min, we take out all the values greater/less than a certain threshold limit. This will not always work perfectly but still in some cases this approach can provide reasonable results.

Let’s take the below image and template to understand the multiple objects case.

Below is the resultant image.

Problems!!!

From the above result, you might have guessed the problems with this approach. Clearly, template matching is translation invariant. But as you can see from the above image, the hearts which are rotated and which have smaller size are not detected. Thus, this algorithm is not rotation and scale-invariant. Other potential problems include occlusion, illumination, and background changes, etc.

In the next blog, let’s improve this algorithm further and make it more robust against scale. For this, we will use the concept of image pyramids. See you in the next blog. Hope you enjoy reading.

If you have any doubts/suggestions please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.