-
-
Save anuragphadke/48b8565a6bcaa477daa5 to your computer and use it in GitHub Desktop.
Template Matching
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 numpy as np | |
import matplotlib.pyplot as plt | |
from skimage import data | |
from skimage.feature import match_template | |
from skimage.io import imread | |
f1 = 'a.jpg' | |
image1 = imread(f1) | |
f2 = 'b.jpg' | |
image2 = imread(f2) | |
result = match_template(f1, f2) | |
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(coin) | |
ax1.set_axis_off() | |
ax1.set_title('template') | |
ax2.imshow(image) | |
ax2.set_axis_off() | |
ax2.set_title('image') | |
# highlight matched region | |
hcoin, wcoin = coin.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() | |
###output | |
Traceback (most recent call last): | |
File "templateMatch.py", line 15, in <module> | |
result = match_template(f1, f2) | |
File "/Library/Python/2.7/site-packages/skimage/feature/template.py", line 106, in match_template | |
assert_nD(image, (2, 3)) | |
File "/Library/Python/2.7/site-packages/skimage/_shared/utils.py", line 165, in assert_nD | |
raise ValueError(msg % (arg_name, '-or-'.join([str(n) for n in ndim]))) | |
ValueError: The parameter `image` must be a 2-or-3-dimensional array | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment