Mahotas 简明教程
Mahotas - Template Matching
模板匹配是一种用来在大图像中找到特定图像(一个模板)的技术。用简单的话说,目标是找到一个较小图像与较大图像匹配的地方。
Template matching is a technique that is used to locate a specific image (a template) within a larger image. In simple terms, the goal is to find a place where the smaller image matches the larger image.
模板匹配涉及到将模板图像与大图像的不同区域进行比较。在比较过程中,模板图像的不同属性,如大小、形状、颜色和强度值将在比较中与较大的图像进行匹配。
Template matching involves comparing the template image with different regions of the bigger image. The different properties of the template image such as size, shape, color, and intensity value are matched against the bigger image during comparison.
比较一直持续到在模板图像和较大的图像之间找到匹配度最佳的区域。
The comparison occurs until a region with the best match is found between the template image and the bigger image.
Template Matching in Mahotas
在 Mahotas 中,我们可以使用 mahotas.template_match() 函数来执行模板匹配。该函数将模板图像与大图像的每个区域(大小与模板图像相同)进行比较。
In Mahotas, we can use the mahotas.template_match() function to perform template matching. The function compares the template image to every region of the bigger image having the same size as the template image.
该函数使用平方差和 (SSD) 方法来执行模板匹配。SSD 方法的工作方式如下 −
The function uses the sum of squared differences (SSD) method to perform template matching. The SSD method works in the following way −
-
The first step is to calculate the difference between the pixel values of the template image and the larger image.
-
In the next step, the differences are squared.
-
Finally, the squared differences are summed for all pixels in the larger image.
最终的 SSD 值决定了模板图像和较大的图像之间的相似性。值越小,模板图像和较大图像之间的匹配度就越高。
The final SSD values determine the similarity between the template image and the larger image. The smaller the value, the greater is the match between the template image and the larger image.
The mahotas.template_match() function
mahotas.template_match() 函数将图像和模板图像作为输入。
The mahotas.template_match() function takes an image and a template image as input.
它从与输入模板图像最匹配的大图像中返回一个区域。
It returns a region from the larger image that best matches the input template image.
最佳匹配是具有最低 SSD 值的区域。
The best match is the region which has the lowest SSD value.
以下是 mahotas 中 template_match() 函数的基本语法 −
Following is the basic syntax of the template_match() function in mahotas −
mahotas.template_match(f, template, mode='reflect', cval=0.0, out=None)
其中,
Where,
-
f − It is the input image.
-
template − It is the pattern that will be matched against the input image.
-
mode (optional) − It determines how input image is extended when the template is applied near its boundaries (default is 'reflect').
-
cval (optional) − It is the constant value used in padding when mode is 'constant' (default is 0.0).
-
out (optional) − It defines the array in which the output image is stored (default is None).
在以下示例中,我们使用 mh.template_match() 函数执行模板匹配。
In the following example, we are performing template matching using the mh.template_match() function.
import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the images
image = mh.imread('tree.tiff', as_grey=True)
template = mh.imread('cropped tree.tiff', as_grey=True)
# Applying template matching algorithm
template_matching = mh.template_match(image, template)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 3)
# Displaying the original image
axes[0].imshow(image, cmap='gray')
axes[0].set_title('Original Image')
axes[0].set_axis_off()
# Displaying the template image
axes[1].imshow(template, cmap='gray')
axes[1].set_title('Template Image')
axes[1].set_axis_off()
# Displaying the matched image
axes[2].imshow(template_matching, cmap='gray')
axes[2].set_title('Matched Image')
axes[2].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()
以下是上面代码的输出: -
Following is the output of the above code −

Matching by Wrapping Boundaries
在 Mahotas 中执行模板匹配时,我们可以对图像的边界进行环绕。环绕边界是指将图像边界折到图像的另一侧。
We can wrap the boundaries of an image when performing template matching in Mahotas. Wrapping boundaries refers to folding the image boundaries to the opposite side of the image.
因此,位于边界之外的像素在图像的另一侧重复出现。
Thus, the pixels that are outside the boundary are repeated on the other side of the image.
这有助于我们在模板匹配期间处理位于图像边界之外的像素。
This helps us in handling the pixels that are outside of the image boundaries during template matching.
在 mahotas 中,我们可以通过将值“wrap”指定给 template_match() 函数的 mode 参数,从而在执行模板匹配时对图像的边界进行环绕。
In mahotas, we can wrap the boundaries of an image when performing template matching by specifying the value 'wrap' to the mode parameter of the template_match() function.
Example
在下面提到的示例中,我们通过环绕图像的边界执行模板匹配。
In the example mentioned below, we are performing template matching by wrapping the boundaries of an image.
import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the images
image = mh.imread('sun.png', as_grey=True)
template = mh.imread('cropped sun.png', as_grey=True)
# Applying template matching algorithm
template_matching = mh.template_match(image, template, mode='wrap')
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 3)
# Displaying the original image
axes[0].imshow(image, cmap='gray')
axes[0].set_title('Original Image')
axes[0].set_axis_off()
# Displaying the template image
axes[1].imshow(template, cmap='gray')
axes[1].set_title('Template Image')
axes[1].set_axis_off()
# Displaying the matched image
axes[2].imshow(template_matching, cmap='gray')
axes[2].set_title('Matched Image')
axes[2].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()
上述代码的输出如下:
Output of the above code is as follows −

Matching by Ignoring Boundaries
我们还可以通过忽略图像的边界来执行模板匹配。通过忽略边界,在执行模板匹配时将排除超出图像边界的像素。
We can also perform template matching by ignoring the boundaries of an image. The pixels that are beyond the boundaries of an image are excluded when performing template matching by ignoring boundaries.
在 mahotas 中,我们将值“ignore”指定给 template_match() 函数的 mode 参数,以便在执行模板匹配时忽略图像的边界。
In mahotas, we specify the value 'ignore' to the mode parameter of template_match() function to ignore the boundaries of an image, when performing template matching.
Example
在此处,我们执行模板匹配时忽略图像的边界。
In here, we are ignoring the boundaries of an image when performing template matching.
import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the images
image = mh.imread('nature.jpeg', as_grey=True)
template = mh.imread('cropped nature.jpeg', as_grey=True)
# Applying template matching algorithm
template_matching = mh.template_match(image, template, mode='ignore')
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 3)
# Displaying the original image
axes[0].imshow(image, cmap='gray')
axes[0].set_title('Original Image')
axes[0].set_axis_off()
# Displaying the template image
axes[1].imshow(template, cmap='gray')
axes[1].set_title('Template Image')
axes[1].set_axis_off()
# Displaying the matched image
axes[2].imshow(template_matching, cmap='gray')
axes[2].set_title('Matched Image')
axes[2].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()
执行上面的代码后,我们得到以下输出: -
After executing the above code, we get the following output −
