Now Reading
My Fun Project With OpenCV – Finding Waldo Game

My Fun Project With OpenCV – Finding Waldo Game

Bhoomika Madhukar

Download our Mobile App


Finding waldo or where’s wally is a popular game where the goal is to find a named Waldo among hundreds of people. As kids, I am sure you have spent quite some time trying to find waldo by carefully going through the entire picture. Wouldn’t it be interesting and fun to build a project that could identify where waldo is within seconds? We can do this using a simple template matching concept of OpenCV. 

In this article, we will learn what is template matching and write a program to automatically find waldo. 



What is template matching?

Template matching is a process where we take the input image and try to slide the target image over the input. This input image is then matched pixel-wise with the target and the closest match will be highlighted. OpenCV provides a function called matchTemplate() for doing this. This method helps in finding the matching region by providing a threshold. After setting the threshold, the target image is slid over the input and the comparison begins. The result that is obtained is compared to the threshold value. If the result is greater than the threshold the area is marked as detected, else the comparison continues again. 

The parameters that can be passed to the matchTemplate() function are plenty but for this article, we will be using the TM_CCOEFF_NORMED. This parameter finds the mean value of the pixels in both the template and the input image. A score of 1 indicates a good match and a -1 indicates a bad match and 0 is a neutral match. 

Finding waldo

Let us now get into the program for finding waldo. The objective is to find this man who is Waldo. 

finding waldo

It is not very easy to do this since the images would look something like this. 

This is the picture that will be used for the implementation in this article. You can download this image with this link here


Stay Connected

Get the latest updates and relevant offers by sharing your email.
finding waldo

Now that we have the source image we need the template image for the matching to happen which is the image of waldo as shown above. You can download this image here

Importing the libraries

Since this program was done in colab I will mount the drive and import the basic libraries needed. 

from google.colab import drive

drive.mount('/content/gdrive')

import cv2

import numpy as np

Loading the images

source_image = cv2.imread('/content/gdrive/My Drive/waldo.jpg')

grey_scale = cv2.cvtColor(source_image, cv2.COLOR_BGR2GRAY)

template_image = cv2.imread('/content/gdrive/My Drive/wally.png’)

Matching the template

The next step is to set a threshold and use the function to match the templates. 

threshold_val = 0.8

matching=cv2.matchTemplate(grey_scale,template_image,cv2.TM_CCOEFF_NORMED)

Generally, a value of 0.8 is chosen as the threshold value but another common one if the image is very clustered is 0.6.

See Also

Now, we need to take the dimensions of our target image so that the location can be matched with the source and compare the threshold values. 

width, height = template_image.shape[::-1]

finding = np.where( matching >= threshold_val)

for match in zip(*finding[::-1]):

    cv2.rectangle(source_image, match, (match[0] + width, match[1] + height), (0,255,0), 2)

Results

Finally, we will write the output image to the drive and we will find a green anchor box around waldo’s image which makes it easy to find waldo. 

cv2.imwrite('/content/gdrive/My Drive/find.png',source_image)

opencv

As shown above, even among a bunch of people waldo is found based on the matching pixels. You can try to do this with any puzzle for waldo and find him easily. 

Conclusion

With very basic functions we automated the game of finding waldo with openCV in this article. We also saw the importance of the matchTemplate() function which can be used even for object detection. matchTemplate() can also be used dynamically in videos as well. 

What Do You Think?

If you loved this story, do join our Telegram Community.


Also, you can write for us and be one of the 500+ experts who have contributed stories at AIM. Share your nominations here.
What's Your Reaction?
Excited
0
Happy
0
In Love
0
Not Sure
0
Silly
0

Copyright Analytics India Magazine Pvt Ltd

Scroll To Top