Skip to content

Instantly share code, notes, and snippets.

@anuragphadke
Created January 24, 2016 01:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anuragphadke/56a3be21128852f69031 to your computer and use it in GitHub Desktop.
Save anuragphadke/56a3be21128852f69031 to your computer and use it in GitHub Desktop.
Template Match -- fixed
import numpy as np
import matplotlib.pyplot as plt
from skimage import data
from skimage.feature import match_template
from skimage.io import imread
import scipy.misc
import scipy
from scipy import misc
f1 = 'a.jpg'
image1 = misc.imread(f1, flatten=True)
f2 = 'b.jpg'
image2 = misc.imread(f2, flatten=True)
result = match_template(image1, image2)
ij = np.unravel_index(np.argmax(result), result.shape)
x, y = ij[::-1]
fig = plt.figure(figsize=(8, 3))
ax1 = plt.subplot(1, 3, 1)
ax2 = plt.subplot(1, 3, 2, adjustable='box-forced')
ax3 = plt.subplot(1, 3, 3, sharex=ax2, sharey=ax2, adjustable='box-forced')
ax1.imshow(image2)
ax1.set_axis_off()
ax1.set_title('template')
ax2.imshow(image1)
ax2.set_axis_off()
ax2.set_title('image')
# highlight matched region
hcoin, wcoin = image2.shape
rect = plt.Rectangle((x, y), wcoin, hcoin, edgecolor='r', facecolor='none')
ax2.add_patch(rect)
ax3.imshow(result)
ax3.set_axis_off()
ax3.set_title('`match_template`\nresult')
# highlight matched region
ax3.autoscale(False)
ax3.plot(x, y, 'o', markeredgecolor='r', markerfacecolor='none', markersize=10)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment