Where's Waldo?with Python and OpenCV
Anna Schneider@windupanna
SF PyLadies Lightning Talk25 June 2013
OpenCV: computer vision in C++
video processing, face recognition, and more!
http://opencv.org/http://sourceforge.net/projects/opencvlibrary/
>>> import cv2
Let's get started
>>> pyladies = cv2.imread('pyladies.png')
>>> cv2.imshow("pyladies", # title pyladies) # image
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)
How would you solve it?>>> waldo = cv2.imread('waldo_face.png')>>> scene = cv2.imread('waldo_scene.png')
Matchmaker, matchmaker
>>> pyplot.imshow(scores)
>>> scores = cv2.matchTemplate(scene,# big waldo, # small
cv2.TM_CCORR_NORMED) # method
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
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)
There's Waldo!
Thanks, and happy searching!
all the code and more:https://gist.github.com/aschn/5861365
say hi:@windupanna