Active Hackathon

My Fun Project With OpenCV – Finding Waldo Game

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

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. 


Sign up for your weekly dose of what's up in emerging technology.

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

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


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


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.

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)


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)


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. 


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. 

More Great AIM Stories

Bhoomika Madhukar
I am an aspiring data scientist with a passion for teaching. I am a computer science graduate from Dayananda Sagar Institute. I have experience in building models in deep learning and reinforcement learning. My goal is to use AI in the field of education to make learning meaningful for everyone.

Our Upcoming Events

Conference, Virtual
Genpact Analytics Career Day
3rd Sep

Conference, in-person (Bangalore)
Cypher 2022
21-23rd Sep

Conference, in-person (Bangalore)
Machine Learning Developers Summit (MLDS) 2023
19-20th Jan

Conference, in-person (Bangalore)
Data Engineering Summit (DES) 2023
21st Apr, 2023

3 Ways to Join our Community

Discord Server

Stay Connected with a larger ecosystem of data science and ML Professionals

Telegram Channel

Discover special offers, top stories, upcoming events, and more.

Subscribe to our newsletter

Get the latest updates from AIM

The curious case of Google Cloud revenue

Porat had earlier said that Google Cloud was putting in money to make more money, but even with the bucket-loads of money that it was making, profitability was still elusive.

Global Parliaments can do much more with Artificial Intelligence

The world is using AI to enhance the performance of its policymakers. India, too, has launched its own machine learning system NeVA, which at the moment is not fully implemented across the nation. How can we learn and adopt from the advancement in the Parliaments around the world? 

Why IISc wins?

IISc was selected as the world’s top research university, trumping some of the top Ivy League colleges in the QS World University Rankings 2022