
- Scikit Image – Introduction
- Scikit Image - Image Processing
- Scikit Image - Numpy Images
- Scikit Image - Image datatypes
- Scikit Image - Using Plugins
- Scikit Image - Image Handlings
- Scikit Image - Reading Images
- Scikit Image - Writing Images
- Scikit Image - Displaying Images
- Scikit Image - Image Collections
- Scikit Image - Image Stack
- Scikit Image - Multi Image
- Scikit Image - Data Visualization
- Scikit Image - Using Matplotlib
- Scikit Image - Using Ploty
- Scikit Image - Using Mayavi
- Scikit Image - Using Napari
- Scikit Image - Color Manipulation
- Scikit Image - Alpha Channel
- Scikit Image - Conversion b/w Color & Gray Values
- Scikit Image - Conversion b/w RGB & HSV
- Scikit Image - Conversion to CIE-LAB Color Space
- Scikit Image - Conversion from CIE-LAB Color Space
- Scikit Image - Conversion to luv Color Space
- Scikit Image - Conversion from luv Color Space
- Scikit Image - Image Inversion
- Scikit Image - Painting Images with Labels
- Scikit Image - Contrast & Exposure
- Scikit Image - Contrast
- Scikit Image - Contrast enhancement
- Scikit Image - Exposure
- Scikit Image - Histogram Matching
- Scikit Image - Histogram Equalization
- Scikit Image - Local Histogram Equalization
- Scikit Image - Tinting gray-scale images
- Scikit Image - Image Transformation
- Scikit Image - Scaling an image
- Scikit Image - Rotating an Image
- Scikit Image - Warping an Image
- Scikit Image - Affine Transform
- Scikit Image - Piecewise Affine Transform
- Scikit Image - ProjectiveTransform
- Scikit Image - EuclideanTransform
- Scikit Image - Radon Transform
- Scikit Image - Line Hough Transform
- Scikit Image - Probabilistic Hough Transform
- Scikit Image - Circular Hough Transforms
- Scikit Image - Elliptical Hough Transforms
- Scikit Image - Polynomial Transform
- Scikit Image - Image Pyramids
- Scikit Image - Pyramid Gaussian Transform
- Scikit Image - Pyramid Laplacian Transform
- Scikit Image - Swirl Transform
- Scikit Image - Morphological Operations
- Scikit Image - Erosion
- Scikit Image - Dilation
- Scikit Image - Black & White Tophat Morphologies
- Scikit Image - Convex Hull
- Scikit Image - Generating footprints
- Scikit Image - Isotopic Dilation & Erosion
- Scikit Image - Isotopic Closing & Opening of an Image
- Scikit Image - Skelitonizing an Image
- Scikit Image - Morphological Thinning
- Scikit Image - Masking an image
- Scikit Image - Area Closing & Opening of an Image
- Scikit Image - Diameter Closing & Opening of an Image
- Scikit Image - Morphological reconstruction of an Image
- Scikit Image - Finding local Maxima
- Scikit Image - Finding local Minima
- Scikit Image - Removing Small Holes from an Image
- Scikit Image - Removing Small Objects from an Image
- Scikit Image - Filters
- Scikit Image - Image Filters
- Scikit Image - Median Filter
- Scikit Image - Mean Filters
- Scikit Image - Morphological gray-level Filters
- Scikit Image - Gabor Filter
- Scikit Image - Gaussian Filter
- Scikit Image - Butterworth Filter
- Scikit Image - Frangi Filter
- Scikit Image - Hessian Filter
- Scikit Image - Meijering Neuriteness Filter
- Scikit Image - Sato Filter
- Scikit Image - Sobel Filter
- Scikit Image - Farid Filter
- Scikit Image - Scharr Filter
- Scikit Image - Unsharp Mask Filter
- Scikit Image - Roberts Cross Operator
- Scikit Image - Lapalace Operator
- Scikit Image - Window Functions With Images
- Scikit Image - Thresholding
- Scikit Image - Applying Threshold
- Scikit Image - Otsu Thresholding
- Scikit Image - Local thresholding
- Scikit Image - Hysteresis Thresholding
- Scikit Image - Li thresholding
- Scikit Image - Multi-Otsu Thresholding
- Scikit Image - Niblack and Sauvola Thresholding
- Scikit Image - Restoring Images
- Scikit Image - Rolling-ball Algorithm
- Scikit Image - Denoising an Image
- Scikit Image - Wavelet Denoising
- Scikit Image - Non-local means denoising for preserving textures
- Scikit Image - Calibrating Denoisers Using J-Invariance
- Scikit Image - Total Variation Denoising
- Scikit Image - Shift-invariant wavelet denoising
- Scikit Image - Image Deconvolution
- Scikit Image - Richardson-Lucy Deconvolution
- Scikit Image - Recover the original from a wrapped phase image
- Scikit Image - Image Inpainting
- Scikit Image - Registering Images
- Scikit Image - Image Registration
- Scikit Image - Masked Normalized Cross-Correlation
- Scikit Image - Registration using optical flow
- Scikit Image - Assemble images with simple image stitching
- Scikit Image - Registration using Polar and Log-Polar
- Scikit Image - Feature Detection
- Scikit Image - Dense DAISY Feature Description
- Scikit Image - Histogram of Oriented Gradients
- Scikit Image - Template Matching
- Scikit Image - CENSURE Feature Detector
- Scikit Image - BRIEF Binary Descriptor
- Scikit Image - SIFT Feature Detector and Descriptor Extractor
- Scikit Image - GLCM Texture Features
- Scikit Image - Shape Index
- Scikit Image - Sliding Window Histogram
- Scikit Image - Finding Contour
- Scikit Image - Texture Classification Using Local Binary Pattern
- Scikit Image - Texture Classification Using Multi-Block Local Binary Pattern
- Scikit Image - Active Contour Model
- Scikit Image - Canny Edge Detection
- Scikit Image - Marching Cubes
- Scikit Image - Foerstner Corner Detection
- Scikit Image - Harris Corner Detection
- Scikit Image - Extracting FAST Corners
- Scikit Image - Shi-Tomasi Corner Detection
- Scikit Image - Haar Like Feature Detection
- Scikit Image - Haar Feature detection of coordinates
- Scikit Image - Hessian matrix
- Scikit Image - ORB feature Detection
- Scikit Image - Additional Concepts
- Scikit Image - Render text onto an image
- Scikit Image - Face detection using a cascade classifier
- Scikit Image - Face classification using Haar-like feature descriptor
- Scikit Image - Visual image comparison
- Scikit Image - Exploring Region Properties With Pandas
Scikit Image - Template Matching
Template matching is an image processing technique used to identify regions within an image that match the given template image. The goal is to identify regions in the larger image where the template image closely matches or aligns with the content in the larger image. It operates by comparing the template image to all possible subregions or windows of the larger image and calculating a similarity or correlation score for each comparison.
This technique is applicable in various scenarios, including quality control during manufacturing, guiding the navigation of mobile robots, and detecting edges within images.
The scikit-image library offers the match_template() function within its feature module, providing an efficient and normalized cross-correlation approach for locating occurrences of the template within an image.
Using the skimage.feature.match_template() function
The match_template() function is used to match a template to a 2-D or 3-D image using normalized correlation.
Syntax
skimage.feature.match_template(image, template, pad_input=False, mode='constant', constant_values=0)
Parameters
Here are the details of its parameters −
image ((M, N[, D]) array): The input image. It can be a 2-D or 3-D array.
template (m, n[, d]) array: The template to locate within the input image. It must be smaller than or equal to the dimensions of the input image (m <= M, n <= N, and d <= D if it's a 3-D image).
pad_input (bool): If set to True, the input image will be padded so that the output has the same size as the input image, and the output values will correspond to the center of the template. If set to False, the output will have a smaller size (M - m + 1, N - n + 1) for an (M, N) image and an (m, n) template, and matches will correspond to the origin (top-left corner) of the template within the input image.
mode (optional): The padding mode to use if pad_input is True. It specifies how the input image should be padded. You can refer to the numpy.pad documentation for more details on the available padding modes.
constant_values (optional): the constant values used in conjunction with mode='constant'.
The function returns an output array that represents the response image with correlation coefficients. The values in this output array will range between -1.0 and 1.0. Each value at a given position in the output corresponds to the correlation coefficient between the input image and the template.
Example
This example demonstrates the process of matching a template to an input image.
import numpy as np from skimage.feature import match_template # Create the template and image arrays template = np.zeros((3, 3)) template[1, 1] = 1 image = np.zeros((6, 6)) image[1, 1] = 1 image[4, 4] = -1 # Print the template and input image print("Template:") print(template) print("Input Image:") print(image) # Perform template matching without padding result = match_template(image, template) # Round the result to 3 decimal places for clarity rounded_result = np.round(result, 3) # Display the result print("Template Matching without Padding:") print(rounded_result) # Perform template matching with padding result_with_padding = match_template(image, template, pad_input=True) # Round the result to 3 decimal places for clarity rounded_result_with_padding = np.round(result_with_padding, 3) # Display the result with padding print("Template Matching with Padding:") print(rounded_result_with_padding)
Output
Template: [[0. 0. 0.] [0. 1. 0.] [0. 0. 0.]] Input Image: [[ 0. 0. 0. 0. 0. 0.] [ 0. 1. 0. 0. 0. 0.] [ 0. 0. 0. 0. 0. 0.] [ 0. 0. 0. 0. 0. 0.] [ 0. 0. 0. 0. -1. 0.] [ 0. 0. 0. 0. 0. 0.]] Template Matching without Padding: [[ 1. -0.125 0. 0. ] [-0.125 -0.125 0. 0. ] [ 0. 0. 0.125 0.125] [ 0. 0. 0.125 -1. ]] Template Matching with Padding: [[-0.125 -0.125 -0.125 0. 0. 0. ] [-0.125 1. -0.125 0. 0. 0. ] [-0.125 -0.125 -0.125 0. 0. 0. ] [ 0. 0. 0. 0.125 0.125 0.125] [ 0. 0. 0. 0.125 -1. 0.125] [ 0. 0. 0. 0.125 0.125 0.125]]
Example
In this example, we use the template matching to locate the occurrence of a specific image patch, represented by a sub-image centered on a single butterfly in the input image. The objective is to identify the position of this particular butterfly within the larger image.
import numpy as np import matplotlib.pyplot as plt from skimage import io from skimage.feature import match_template # Load the input image image = io.imread('Images/black-doted-butterflies.jpg', as_gray=True) butterfly = image[125:254, 50:196] # Perform template matching between the image and the template result = match_template(image, butterfly) # Find the position with the highest match ij = np.unravel_index(np.argmax(result), result.shape) x, y = ij[::-1] # Create a figure with three subplots for visualization fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(10, 5)) # Display the butterfly template ax1.imshow(butterfly, cmap=plt.cm.gray) ax1.set_axis_off() ax1.set_title('Template') # Display the original image with a rectangle highlighting the matched region ax2.imshow(image, cmap=plt.cm.gray) ax2.set_axis_off() ax2.set_title('Image') rect = plt.Rectangle((x, y), butterfly.shape[1], butterfly.shape[0], edgecolor='r', facecolor='none') ax2.add_patch(rect) # Display the result of template matching with the matched region marked ax3.imshow(result) ax3.set_axis_off() ax3.set_title('Matching Result') ax3.autoscale(False) ax3.plot(x, y, 'o', markeredgecolor='r', markerfacecolor='none', markersize=10) plt.show()
Output
