Created
November 9, 2015 21:23
-
-
Save Swarchal/f1e5c77b4754b4f27170 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
from skimage.io import imread | |
from skimage.feature import match_template | |
import matplotlib.pyplot as plt | |
import numpy as np | |
image = imread("image.jpg") | |
wally = imread("wally.png") | |
# select just the red channel | |
image_red = image[:,:, 0] | |
wally_red = wally[:,:, 0] | |
# perform template matching | |
result = match_template(image = image_red, template = wally_red) | |
ij = np.unravel_index(np.argmax(result), result.shape) | |
x, y = ij[::-1] | |
# plot the results | |
fig = plt.figure(figsize=(8, 3)) | |
ax1 = plt.subplot(1, 2, 1) | |
ax2 = plt.subplot(1, 2, 2, sharex = ax1, sharey = ax1) | |
ax1.imshow(result) | |
ax1.set_axis_off() | |
ax1.set_title('`Match_template`\nresult') | |
# highlight matched region | |
ax1.autoscale(False) | |
ax1.plot(x, y, 'o', | |
markeredgecolor = 'r', | |
markerfacecolor = 'none', | |
markersize = 25) | |
ax2.imshow(image) | |
ax2.set_axis_off() | |
ax2.set_title('Original image') | |
# highlight matched region | |
hwally, wwally = wally_red.shape | |
rect = plt.Rectangle( | |
(x, y), | |
wwally, | |
hwally, | |
edgecolor='r', | |
facecolor='none', | |
linewidth = 1.5) | |
ax2.add_patch(rect) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment