MITB Banner

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.

Share

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

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.

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. 

Share
Picture of Bhoomika Madhukar

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.
Related Posts

CORPORATE TRAINING PROGRAMS ON GENERATIVE AI

Generative AI Skilling for Enterprises

Our customized corporate training program on Generative AI provides a unique opportunity to empower, retain, and advance your talent.

Upcoming Large format Conference

May 30 and 31, 2024 | 📍 Bangalore, India

Download the easiest way to
stay informed

Subscribe to The Belamy: Our Weekly Newsletter

Biggest AI stories, delivered to your inbox every week.

AI Forum for India

Our Discord Community for AI Ecosystem, In collaboration with NVIDIA. 

Flagship Events

Rising 2024 | DE&I in Tech Summit

April 4 and 5, 2024 | 📍 Hilton Convention Center, Manyata Tech Park, Bangalore

MachineCon GCC Summit 2024

June 28 2024 | 📍Bangalore, India

MachineCon USA 2024

26 July 2024 | 583 Park Avenue, New York

Cypher India 2024

September 25-27, 2024 | 📍Bangalore, India

Cypher USA 2024

Nov 21-22 2024 | 📍Santa Clara Convention Center, California, USA

Data Engineering Summit 2024

May 30 and 31, 2024 | 📍 Bangalore, India