.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/features_detection/plot_template.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_features_detection_plot_template.py: ================= Template Matching ================= We use template matching to identify the occurrence of an image patch (in this case, a sub-image centered on a single coin). Here, we return a single match (the exact same coin), so the maximum value in the ``match_template`` result corresponds to the coin location. The other coins look similar, and thus have local maxima; if you expect multiple matches, you should use a proper peak-finding function. The ``match_template`` function uses fast, normalized cross-correlation [1]_ to find instances of the template in the image. Note that the peaks in the output of ``match_template`` correspond to the origin (i.e. top-left corner) of the template. .. [1] J. P. Lewis, "Fast Normalized Cross-Correlation", Industrial Light and Magic. .. GENERATED FROM PYTHON SOURCE LINES 22-62 .. image-sg:: /auto_examples/features_detection/images/sphx_glr_plot_template_001.png :alt: template, image, `match_template` result :srcset: /auto_examples/features_detection/images/sphx_glr_plot_template_001.png :class: sphx-glr-single-img .. code-block:: Python import numpy as np import matplotlib.pyplot as plt from skimage import data from skimage.feature import match_template image = data.coins() coin = image[170:220, 75:130] result = match_template(image, coin) 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) ax3 = plt.subplot(1, 3, 3, sharex=ax2, sharey=ax2) ax1.imshow(coin, cmap=plt.cm.gray) ax1.set_axis_off() ax1.set_title('template') ax2.imshow(image, cmap=plt.cm.gray) 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() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.077 seconds) .. _sphx_glr_download_auto_examples_features_detection_plot_template.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/scikit-image/scikit-image/v0.24.0?filepath=notebooks/auto_examples/features_detection/plot_template.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_template.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_template.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_template.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_