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

template matching
Advertisements