Template matching is a computer vision technique used to locate a specific pattern (template) within a larger image. It’s often employed for object detection, image recognition, and image segmentation tasks. The idea is to compare the template to different regions of the image and find the region that best matches the template. This is achieved by sliding the template over the image and computing a similarity measure at each position.
skimage
We can perform template matching using the scikit-image library in Python. scikit-image is a popular image processing library that provides various tools and algorithms for image analysis and manipulation. Here’s a step-by-step guide on how to perform template matching with scikit-image:
We import the required libraries, including skimage
and matplotlib
for visualization.
import numpy as npimport matplotlib.pyplot as pltfrom skimage import io,colorfrom skimage.feature import match_template
We load both the target image and the template image.
image = io.imread("Brain.jpg")template = io.imread("template.jpg")
We use the match_template
function to perform template matching. This function computes the normalized cross-correlation between the template and the target image.
result = match_template(image_gray, template_gray)
To find the position of the best match, we use np.unravel_index
to get the coordinates of the maximum value in the result matrix.
ij = np.unravel_index(np.argmax(result), result.shape)
We visualize the result of template matching by overlaying a rectangle on the target image to highlight the region where the template best matches.
template_height, template_width = template_gray.shapetemplate_extent = (0, template_width, 0, template_height)ax1.imshow(template_gray, cmap=plt.cm.gray, extent=template_extent)ax1.set_axis_off()ax1.set_title('Template')ax2.imshow(image_gray, cmap=plt.cm.gray)ax2.set_axis_off()ax2.set_title('Image')# highlight matched regionh, w = template_gray.shaperect = plt.Rectangle((y, x), w, h, edgecolor='r', facecolor='none')ax2.add_patch(rect)ax3.imshow(result)ax3.set_axis_off()ax3.set_title('Matched Region')# highlight matched regionax3.autoscale(False)ax3.plot(y, x, 'o', markeredgecolor='r', markerfacecolor='none', markersize=10)plt.show()
Note: In the example below, we’re converting the main image and template image into grayscale before applying template matching algorithm. We can also apply same steps of template matching on the color images as well.
Let’s run the following code to perform template matching:
import numpy as npimport matplotlib.pyplot as pltfrom skimage import io,colorfrom skimage.feature import match_template# Load the main image and the templateimage = io.imread("Brain.jpg")template = io.imread("template.jpg")# Convert images to grayscale for template matchingimage_gray = color.rgb2gray(image)template_gray = color.rgb2gray(template)# Perform template matchingresult = match_template(image_gray, template_gray)# Find the location (row, column) of the best match in the result matrixij = np.unravel_index(np.argmax(result), result.shape)x, y = ij # Assign the result to a single variable, not unpack it# Create the figure and subplotsfig, (axis1, axis2, axis3) = plt.subplots(1, 3, figsize=(8, 3))# Display the template with reduced sizetemplate_height, template_width = template_gray.shapetemplate_extent = (0, template_width, 0, template_height)axis1.imshow(template_gray, cmap=plt.cm.gray, extent=template_extent)axis1.set_axis_off()axis1.set_title('Template')axis2.imshow(image_gray, cmap=plt.cm.gray)axis2.set_axis_off()axis2.set_title('Image')# Highlight matched regionh, w = template_gray.shaperectangle = plt.Rectangle((y, x), w, h, edgecolor='r', facecolor='none')axis2.add_patch(rectangle)axis3.imshow(result)axis3.set_axis_off()axis3.set_title('Matched Region')# highlight matched regionaxis3.autoscale(False)axis3.plot(y, x, 'o', markeredgecolor='r', markerfacecolor='none', markersize=10)plt.show()
Let’s understand the code above with the following explanation:
Lines 1–4: We import necessary Python modules and libraries. NumPy is used for numerical operations, Matplotlib for creating plots, and scikit-image for image processing and template matching.
Lines 7–8: We read the main image "Brain.jpg"
and the template image "template.jpg"
using the io.imread()
function.
Lines 11–12: We convert the main image and template to grayscale using the color.rgb2gray()
function. Grayscale images simplify template matching by reducing color information.
Line 15: We perform template matching using the match_template()
function, which compares the template to the main image and stores the result in the result
variable.
Lines 18–19: We find the location (row, column) of the best match within the result matrix by identifying the maximum value in result
and then converting it to a row and column index, which are stored in the x
and y
variables.
Lines 22–46: We create a Matplotlib figure with three subplots (axis1
, axis2
, axis3
) for visualization. The axis1
subplot displays the template image, the axis2
subplot displays the main image with the matched region highlighted, and the axis3
subplot displays the result of template matching. Various properties, such as titles and axis visibility, are set for each subplot, and the matched region is highlighted with a red rectangle and a marker in the axis2
subplot. Finally, we call the plt.show()
function to display the figure with the three subplots showing the template matching result.
Note: Template matching techniques assume that the size of the template image is similar to the size of the object we’re trying to match in the main image. If the two sizes are significantly different, template matching might not work effectively, and the accuracy of the results could be compromised.
Free Resources