forked from tioguerra/find_wally
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_wally.py
67 lines (55 loc) · 1.67 KB
/
find_wally.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#
# Where is Wally?
# ===============
# (a practical tutorial on crosscorrelation)
#
# by Rodrigo Guerra
# ------------------------------------------
#
# Import the needed libraries
from PIL import Image, ImageDraw
import numpy as np
import matplotlib as mpl
from matplotlib import pyplot as plt
# Adjust image resolution
mpl.rc("savefig", dpi=160)
# Read the images
wally_img = Image.open("wally.jpg")
the_crowd_img = Image.open("the_crowd.jpg")
# Transform the images into NumPy arrays (matrices)
wally = np.array(wally_img).astype(float)
the_crowd = np.array(the_crowd_img).astype(float)
# Normalize inputs
wally = (wally / 255.0) * 2 - 1
the_crowd = (the_crowd / 255.0) * 2 - 1
# Get the dimensions of the images
w, h, channels = wally.shape
W, H, _ = the_crowd.shape
n = float(w * h * channels)
# Calculates the crosscorrelation
cross_corr = np.zeros((W - w, H - h))
for x in range(W - w):
for y in range(H - h):
m = np.multiply(the_crowd[x:x+w,y:y+h,:], wally)
s = np.sum(m)
cross_corr[x,y] = float(s) / n
# Find the maximum crosscorrelation coordinates
wally_pos = np.unravel_index(np.argmax(cross_corr),cross_corr.shape)
# Normalize results
smallest = np.min(cross_corr)
largest = np.max(cross_corr)
cross_corr = (cross_corr - smallest) / (largest - smallest)
# Displays the crosscorrelation image
plt.imshow(cross_corr)
plt.show()
# Shows the resulting matched position
draw = ImageDraw.Draw(the_crowd_img)
x,y = wally_pos
x = x + w/2 # Adjusting coordinates to point
y = y + h/2 # to the center instead of corner
r = int(np.sqrt(w**2 + h**2))
for i in range(15):
r = r + 0.5
draw.ellipse((y-r, x-r, y+r, x+r))
plt.imshow(the_crowd_img)
plt.show()