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.
Let us now get into the program for finding waldo. The objective is to find this man who is 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.
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 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 + width, match + 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.
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.