where's waldo? with python and opencv
TRANSCRIPT
![Page 1: Where's Waldo? with Python and OpenCV](https://reader030.vdocuments.mx/reader030/viewer/2022020710/55a27fea1a28ab26408b473c/html5/thumbnails/1.jpg)
Where's Waldo?with Python and OpenCV
Anna Schneider@windupanna
SF PyLadies Lightning Talk25 June 2013
![Page 2: Where's Waldo? with Python and OpenCV](https://reader030.vdocuments.mx/reader030/viewer/2022020710/55a27fea1a28ab26408b473c/html5/thumbnails/2.jpg)
OpenCV: computer vision in C++
video processing, face recognition, and more!
http://opencv.org/http://sourceforge.net/projects/opencvlibrary/
>>> import cv2
![Page 3: Where's Waldo? with Python and OpenCV](https://reader030.vdocuments.mx/reader030/viewer/2022020710/55a27fea1a28ab26408b473c/html5/thumbnails/3.jpg)
Let's get started
>>> pyladies = cv2.imread('pyladies.png')
>>> cv2.imshow("pyladies", # title pyladies) # image
![Page 4: Where's Waldo? with Python and OpenCV](https://reader030.vdocuments.mx/reader030/viewer/2022020710/55a27fea1a28ab26408b473c/html5/thumbnails/4.jpg)
Images are NumPy arrays!
>>> pyladies.shape(638, 1499, 3)# (height, width, n_colors)
but with weird conventions
>>> pyladies[300,300]array([ 57, 2, 237], dtype=uint8)# (blue, green, red)
![Page 5: Where's Waldo? with Python and OpenCV](https://reader030.vdocuments.mx/reader030/viewer/2022020710/55a27fea1a28ab26408b473c/html5/thumbnails/5.jpg)
How would you solve it?>>> waldo = cv2.imread('waldo_face.png')>>> scene = cv2.imread('waldo_scene.png')
![Page 6: Where's Waldo? with Python and OpenCV](https://reader030.vdocuments.mx/reader030/viewer/2022020710/55a27fea1a28ab26408b473c/html5/thumbnails/6.jpg)
Matchmaker, matchmaker
>>> pyplot.imshow(scores)
>>> scores = cv2.matchTemplate(scene,# big waldo, # small
cv2.TM_CCORR_NORMED) # method
![Page 7: Where's Waldo? with Python and OpenCV](https://reader030.vdocuments.mx/reader030/viewer/2022020710/55a27fea1a28ab26408b473c/html5/thumbnails/7.jpg)
Let the best score win
>>> minmaxs = cv2.minMaxLoc(scores)>>> min, max, min_pos, max_pos = minmaxs>>> print max, max_pos0.999999880791 (979, 842)# best score upper-left corner
![Page 8: Where's Waldo? with Python and OpenCV](https://reader030.vdocuments.mx/reader030/viewer/2022020710/55a27fea1a28ab26408b473c/html5/thumbnails/8.jpg)
Layer it on
>>> cv2.rectangle(scene, # image corner1, # upper-left
corner2, # lower-right (0,255,0), # (r,g,b) 5) # pixels
>>> corner1 = max_pos>>> corner2 = (corner1[0]+waldo.shape[1], corner1[1]+waldo.shape[0])
>>> cv2.imshow("There's Waldo!", scene)
![Page 9: Where's Waldo? with Python and OpenCV](https://reader030.vdocuments.mx/reader030/viewer/2022020710/55a27fea1a28ab26408b473c/html5/thumbnails/9.jpg)
There's Waldo!
![Page 10: Where's Waldo? with Python and OpenCV](https://reader030.vdocuments.mx/reader030/viewer/2022020710/55a27fea1a28ab26408b473c/html5/thumbnails/10.jpg)
Thanks, and happy searching!
all the code and more:https://gist.github.com/aschn/5861365
say hi:@windupanna