Introduction to Image Processing — Part 8: Template Matching

Aids
3 min readFeb 3, 2021
Photo by cottonbro from Pexels

In part 4 of this series, we have discussed two objection detection methods: Blob detection and Connected components. These methods detect all the objects in our image based on the evident highlighted blobs or pixels after some initial preprocessing methods like image binarization. In this part, We will discuss another object detection method, which is Template Matching.

Template Matching

This method uses a reference image known as template and slides it across the entire source image one pixel at a time to determine the most similar objects. It will produce another image or matrix where pixel values correspond to how similar our template is to the source image. Thus, when we try to view the output image, the pixel values of the matched objects will be peaking or will be highlighted.

Let’s take, for example, the source image and selected template illustrated in Figure 1.

Figure 1. Source image and template

The source image is in grayscale for simplicity, but the method could also work in colored images. Let’s now try to detect other airplanes in the image that would match our template.

As mentioned previously, the resulting image or matrix will highlight the objects that match our template. This is evident in Figure 2, where the lighter spots or pixels are those that are most similar to our template. To better see the matched objects, let’s put a bounding box around them.

Figure 2. Resulting image of template matching

As mentioned previously, the resulting image or matrix will highlight the objects that match our template. This is evident in Figure 2, where the lighter spots or pixels are those that are most similar to our template. To better see the matched objects, let’s put a bounding box around them.

Figure 3. Matched objects based on thresholds

Because the pixel values of the resulting image correspond to their correlation with our template, the number of matched objects will depend on the threshold that we pick. The lower the threshold, the more objects it will consider a match.

The advantage of using template matching instead of the general object detection methods previously discussed is that we can specifically choose a particular object or template from the source image and let the algorithm find similar items. However, there are three main drawbacks to this method. Template matching using cross-correlation is NOT scale-invariant, NOT rotational-invariant, and NOT intensity-invariant. This means that changes in the shape, size, orientation, and color of the template or target objects will affect its capacity to detect the desired objects. SIFT (Scale-Invariant Feature Transform), a more advanced template matching algorithm, is available in OpenCV, but it is a paid feature.

References

[1] B. Borja. “Lecture 9 — Special Topics in Image Processing Part 2 (Supplementary Notebook)”. IIP 2020.

--

--

Aids

Computer Forensic Noob | Aspiring Data Scientist |