Mahotas 简明教程

Mahotas - Template Matching

模板匹配是一种用来在大图像中找到特定图像(一个模板)的技术。用简单的话说,目标是找到一个较小图像与较大图像匹配的地方。

模板匹配涉及到将模板图像与大图像的不同区域进行比较。在比较过程中,模板图像的不同属性,如大小、形状、颜色和强度值将在比较中与较大的图像进行匹配。

比较一直持续到在模板图像和较大的图像之间找到匹配度最佳的区域。

Template Matching in Mahotas

在 Mahotas 中,我们可以使用 mahotas.template_match() 函数来执行模板匹配。该函数将模板图像与大图像的每个区域(大小与模板图像相同)进行比较。

该函数使用平方差和 (SSD) 方法来执行模板匹配。SSD 方法的工作方式如下 −

  1. 第一步是计算模板图像和较大图像的像素值之间的差异。

  2. 在下一步中,将差异平方。

  3. 最后,对较大的图像中所有像素的平方差求和。

最终的 SSD 值决定了模板图像和较大的图像之间的相似性。值越小,模板图像和较大图像之间的匹配度就越高。

The mahotas.template_match() function

mahotas.template_match() 函数将图像和模板图像作为输入。

它从与输入模板图像最匹配的大图像中返回一个区域。

最佳匹配是具有最低 SSD 值的区域。

以下是 mahotas 中 template_match() 函数的基本语法 −

mahotas.template_match(f, template, mode='reflect', cval=0.0, out=None)

其中,

  1. f − 这是输入图像。

  2. template − 这是将在输入图像中进行匹配的模式。

  3. mode (optional) − 它决定了当模板在其边界附近应用时输入图像如何得到扩展(默认是“reflect”)。

  4. cval (optional) − “constant”(默认为 0.0)模式下用于填充的常数值。

  5. out (optional) − 定义用于存储输出图像的数组(默认为 None)。

在以下示例中,我们使用 mh.template_match() 函数执行模板匹配。

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()

以下是上面代码的输出: -

template matching

Matching by Wrapping Boundaries

在 Mahotas 中执行模板匹配时,我们可以对图像的边界进行环绕。环绕边界是指将图像边界折到图像的另一侧。

因此,位于边界之外的像素在图像的另一侧重复出现。

这有助于我们在模板匹配期间处理位于图像边界之外的像素。

在 mahotas 中,我们可以通过将值“wrap”指定给 template_match() 函数的 mode 参数,从而在执行模板匹配时对图像的边界进行环绕。

Example

在下面提到的示例中,我们通过环绕图像的边界执行模板匹配。

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()

上述代码的输出如下:

template matching1

Matching by Ignoring Boundaries

我们还可以通过忽略图像的边界来执行模板匹配。通过忽略边界,在执行模板匹配时将排除超出图像边界的像素。

在 mahotas 中,我们将值“ignore”指定给 template_match() 函数的 mode 参数,以便在执行模板匹配时忽略图像的边界。

Example

在此处,我们执行模板匹配时忽略图像的边界。

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()

执行上面的代码后,我们得到以下输出: -

matching ignoring boundaries