Game Hacks: Finding Objects using Template Matching

Kris Tabong
3 min readFeb 1, 2021

Can you use image processing techniques to find objects?

Image from Big Home Hidden Objects

Are your eyes tired of looking for hidden objects in games? Do you want an easy way to do it? Friends and family, may I introduce my algorithm to ease your worries. We will use template matching in finding these objects.

What is template matching?

Template matching is an image processing technique of finding the template image (the image that we want to find) from a source image (the bigger image where we need to search for the image). The intuition behind this is to compare the pixel values of the source and template images. This is done by sliding the template image to the source image, calculating the similarity between the two images per slide. The resulting output is a matrix corresponding to how these images are similar per slide.

Find the chestnut in the image

We are given the list of items below. Luckily, we are given images of the items, a perfect example for template matching. For simplicity, we will use the grayscale channel of the image. We are going to crop the chestnut in our image as our template image. You can use image indexing for this.

Grayscale Image
Template Image of Chestnut

The next procedure is to use the match_template to perform template matching.

from skimage.feature import match_template
template = photo_gray[515:560,140:180] # template
result = match_template(photo_gray, template)
imshow(result, cmap=’viridis’);
Match_Template Output

The result of the match_template function is a matrix (in a form of an image) showing the distribution of similarity of the source image and template. We can find the objects by getting the high values of the matrix. Adjusting the threshold value will be based on user’s preference . And now, let's search for the chestnut.

from skimage.feature import peak_local_max
imshow(photo)
template_width, template_height = template.shape
for x, y in peak_local_max(result, threshold_abs=0.82):
rect = plt.Rectangle((y, x), template_height, template_width, color='r',
fc='none', linewidth=2)
plt.gca().add_patch(rect)
Chestnut Detected

Hooray! We spotted the chestnut. Note that if you want to find other objects, you must change your template image. Let's try for other objects.

Finding the other objects

While doing this post, I learned how to some tricks on using matching templates. These are some of the tips

  1. Focus on the most important part of the feature for the template image. For example, you can focus on the hand parts of the gloves for template matching. For the snowflake, you may focus only on the center.
  2. Template matching is weak against occlusion. As shown in the yellow bounded box, this can’t be detected using a matching template.

Thanks for reading my post. :)

--

--

Kris Tabong

Learning more about Image Processing using Python