From 5e49eb4fb6bce9cdeae515590530b78e4dde89d9 Mon Sep 17 00:00:00 2001 From: Tony S Yu Date: Mon, 26 Mar 2012 20:26:44 -0400 Subject: [PATCH] Add alternate example for `match_template`. --- doc/examples/plot_match_face_template.py | 41 ++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 doc/examples/plot_match_face_template.py diff --git a/doc/examples/plot_match_face_template.py b/doc/examples/plot_match_face_template.py new file mode 100644 index 00000000..898870e5 --- /dev/null +++ b/doc/examples/plot_match_face_template.py @@ -0,0 +1,41 @@ +""" +================= +Template Matching +================= + +In this example, we use template matching to identify the occurrence of an +image patch (in this case, a sub-image centered on the camera man's head). +Since there's only a single match, the maximum value in the `match_template` +result` corresponds to the head location. If you expect multiple matches, you +should use a proper peak-finding function. + +""" + +import numpy as np +import matplotlib.pyplot as plt +from skimage import data +from skimage.feature import match_template + +image = data.camera() +head = image[70:170, 180:280] + +result = match_template(image, head) + +fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(8, 4)) + +ax1.imshow(head) +ax1.set_axis_off() +ax1.set_title('template') + +ax2.imshow(image) +ax2.set_axis_off() +ax2.set_title('image') + +# highlight matched region +xy = np.unravel_index(np.argmax(result), image.shape)[::-1] # -1 flips ij to xy +wface, hface = head.shape +rect = plt.Rectangle(xy, wface, hface, edgecolor='r', facecolor='none') +ax2.add_patch(rect) + +plt.show() +