import numpy as np
import cv2
import sys
import matplotlib.pyplot as plt
Finding Waldo
full_image = cv2.cvtColor(cv2.imread("/work/images/Wheres_Waldo_Full.png"), cv2.COLOR_BGR2RGB)
waldo_image = cv2.cvtColor(cv2.imread("/work/images/Wheres_Waldo_Waldo.png"), cv2.COLOR_BGR2RGB)
wilma_image = cv2.cvtColor(cv2.imread("/work/images/Wheres_Waldo_Wilma.png"), cv2.COLOR_BGR2RGB)
plt.figure(figsize=(20,10))
plt.imshow(full_image)
fig, ax = plt.subplots(1,2,figsize=(12, 6))
ax[0].imshow(waldo_image)
ax[1].imshow(wilma_image)
image = full_image.mean(-1).astype(np.float32)/255
kernel = waldo_image.mean(-1).astype(np.float32)/255
fig, ax = plt.subplots(1,2,figsize=(12, 6))
ax[0].imshow(image)
ax[1].imshow(kernel)
# waldo_filter = cv2.filter2D(image, -1, kernel, borderType=cv2.BORDER_CONSTANT)
# See here: https://docs.opencv.org/3.4/de/da9/tutorial_template_matching.html
waldo_filter = cv2.matchTemplate(image, kernel, cv2.TM_CCOEFF_NORMED)
# waldo_position = np.linalg.norm(waldo_filter, axis=-1)
waldo_position = np.abs(waldo_filter)
print(waldo_position)
plt.figure(figsize=(20,10))
viz = np.pad(waldo_position / np.max(waldo_position), [[20, 19], [20, 19]])
plt.imshow(0.7*viz + 0.3 * image)
y, x = np.unravel_index(waldo_position.argmax(), waldo_position.shape)
print(x, y)
plt.scatter(x+20, y+20, s=100, color='r', marker='x')