...

Find Waldo with OpenCV Template Matching in Python

wally

Introduction

This tutorial shows how to use OpenCV template matching in Python to find a smaller image inside a larger image.
We will solve a fun Where’s Waldo style problem using a single, simple algorithm.
The code reads two images, converts the scene to grayscale, and runs cv2.matchTemplate to measure visual similarity.
We then extract the best match location and draw a bounding box to highlight Waldo in the scene.
This approach is fast, easy to understand, and requires no training data.
It is perfect for quick image matching tasks and for learning the fundamentals of search by template in computer vision.

check out our video tutorial here : https://www.youtube.com/watch?v=_iGmwb5petU

opencv template matching
opencv template matching

Here is the Wally part we are looking for ,,,,

You can find more similar tutorials in my blog posts page here : https://eranfeit.net/blog/

opencv template matching

You can find the full code here : https://ko-fi.com/s/803ce610ff


Full Code And Walkthrough

This section contains one consolidated code block with a line-by-line explanation.
Each explanation appears immediately above the Python command and starts with ###.
A short summary follows the code.

Short description of this part:
We load the scene and the template, convert the scene to grayscale, compute the template match, pick the best location, draw a rectangle, preview the result, and save the output image.

### Import OpenCV, the core computer vision library used for image IO and processing. import cv2  ### Read the main image that contains Waldo from disk into a NumPy array. image = cv2.imread("Finding-Waldo\\WaldoBeach.jpg")  ### Read the Waldo template image in grayscale mode for matching. waldo = cv2.imread("Finding-Waldo\\waldo.jpg", 0)  # load it as gray scale  ### Convert the main scene from BGR (OpenCV default) to grayscale to prepare for template matching. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)  ### Compute the template matching score map using normalized cross-correlation (TM_CCOEFF). ### The result is a 2D heatmap where higher values indicate better matches. result = cv2.matchTemplate(gray, waldo, cv2.TM_CCOEFF)  ### Extract the min and max values and their locations from the score map to find the best match. min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)  ### Print the top-left coordinate of the best match location in the scene for debugging or logging. print(max_loc)  ### Store the top-left corner of the best match as the starting point of the bounding box. top_left = max_loc  ### Define the bottom-right corner of the bounding box by adding a fixed width and height. ### This assumes an approximate template size; adjust if your template size differs. bottom_right = (top_left[0] + 50, top_left[1] + 50)  ### Draw a visible rectangle around the detected Waldo position on the original color image. cv2.rectangle(image, top_left, bottom_right, (255, 0, 0), 5)  ### Display the result window so you can visually verify the detection output. cv2.imshow("img", image)  ### Wait indefinitely for a key press to close the preview window. cv2.waitKey(0)  ### Save the annotated image with the drawn rectangle to disk for later use or sharing. cv2.imwrite("Finding-Waldo\\WaldoBeach2.jpg", image)  ### Clean up any OpenCV windows created during the display step to free resources. cv2.destroyAllWindows() 

You can find the full code here : https://ko-fi.com/s/803ce610ff


The code uses OpenCV template matching to scan the scene and locate the highest-scoring position for the Waldo patch.
It draws a rectangle to highlight this location, previews the result, and saves the annotated image for reference.
If your template is not exactly 50×50 pixels, replace the fixed width and height with the template dimensions.


For example, compute w, h = waldo.shape[::-1] when the template is grayscale and then use (top_left[0] + w, top_left[1] + h).
This ensures the bounding box precisely fits the matched template.


Connect :

☕ Buy me a coffee — https://ko-fi.com/eranfeit

🖥️ Email : feitgemel@gmail.com

🌐 https://eranfeit.net

🤝 Fiverr : https://www.fiverr.com/s/mB3Pbb

Enjoy,

Eran

error: Content is protected !!
Eran Feit