The world’s leading publication for data science, AI, and ML professionals.

Matchy matchy! Object detection using Template Matching in Python

We discussed object detection on my previous article so this time around I'll be showing another example of how to identify objects…

Photo by Devin Avery on Unsplash
Photo by Devin Avery on Unsplash

We discussed object detection on my previous article so this time around I’ll be showing another example of how to identify objects: Template Matching.

Let’s take a loot at this reference image

Image by Author
Image by Author

Template Matching means that you find small parts of a bigger image in which you can use as a reference template [1]. This particular approach proves to be useful in image detection, as well as in tracking objects.

For this particular case, we’ll be trying out a simple approach by using template matching in a Grayscale image

from skimage.io import imread, imshow
from skimage.color import rgb2gray
lights = imread('medium/lights.jpg')
lights_gray = rgb2gray(lights)
imshow(lights_gray);
Image by Author
Image by Author

Now let’s select one of the yellow colored lanterns as the template

template = lights_gray[79:132,46:83]
imshow(template);
Image by Author
Image by Author

In order to perform Template Matching, we simply use skimage’s match_template

from skimage.feature import match_template
result = match_template(lights_gray, template)
imshow(result, cmap='viridis');
Image by Author
Image by Author

From the match_template result, we can see that there are multiple areas of the original image that have intensity values that are almost or equal to 1. Assuming that the template can only be found once in the original image, we can determine the pixel with the highest value from the resulting output

import numpy as np
x, y = np.unravel_index(np.argmax(result), result.shape)
print((x, y))

Output: (79,46)

Now let’s use this pixel to map out the identified object based on the template we used earlier

import matplotlib.pyplot as plt
imshow(lights_gray)
template_width, template_height = template.shape
rect = plt.Rectangle((y, x), template_height, template_width, color='y', 
                     fc='none')
plt.gca().add_patch(rect);
Image by Author
Image by Author

In order to identify multiple images at once, we can look for peaks based on a given threshold for correlation with the identified peak value and match those with the template size

from skimage.feature import peak_local_max
imshow(lights_gray)
template_width, template_height = template.shape
#set threshold initially at 0.65
for x, y in peak_local_max(result, threshold_abs=0.65):
    rect = plt.Rectangle((y, x), template_height, template_width, color='y', 
                         fc='none')
    plt.gca().add_patch(rect);
Image by Author
Image by Author

We can see here that by setting the threshold at 0.65, we are able to determine some objects that the algorithm identified as similar to the template that we have set. However, one of the identified objects seem to have two lanterns instead of one. Adjusting some of the parameters may provide useful in being able to identify all of the lanterns in this case. Examples of which could be the following:

  1. Changing the template size
  2. Changing the orientation of the template (flipping in either direction)
  3. Changing image properties such as contrast

And there you have it, Template Matching with a few lines of code

References

[1] R. Brunelli, Template Matching Techniques in Computer Vision: Theory and Practice (2008), ResearchGate


Interested in my work? You can see more stories over at my profile

Nico Aguila – Medium


Related Articles