Mahotas - 击中与错过变换



击中与错过变换是一种二进制形态学运算,用于检测图像中的特定模式或形状。

该运算将结构元素与输入二值图像进行比较。结构元素由以特定模式排列的前景 (1) 和背景 (0) 像素组成,这些像素代表要检测的所需形状或模式。

击中或错过变换在结构元素和图像之间执行逐像素逻辑与运算,然后检查结果是否与预定义条件匹配。

该条件指定匹配模式中应该存在的特定前景和背景像素排列。如果满足该条件,则输出像素设置为 1,表示匹配;否则,设置为 0。

Mahotas 中的击中与错过变换

在 Mahotas 中,我们可以使用mahotas.hitmiss()函数对图像执行击中与错过变换。该函数使用结构元素'Bc'来确定输入图像中是否存在特定模式。

Mahotas 中的结构元素可以取三个值:0、1 或 2。值为 1 表示结构元素的前景,而 0 表示背景。

值 2 用作“不关心”值,这意味着不应对该特定像素执行匹配。

为了识别匹配,结构元素的值必须与输入图像中相应的像素值重叠。

如果重叠满足结构元素指定的条件,则该像素被视为匹配。

mahotas.hitmiss() 函数

mahotas.hitmiss() 以灰度图像作为输入,并返回二值图像作为输出。白色像素表示结构元素与输入图像匹配的区域,而黑色像素表示不匹配的区域。

语法

以下是 mahotas 中 hitmiss() 函数的基本语法:

mahotas.hitmiss(input, Bc, out=np.zeros_like(input))

其中,

  • input - 输入灰度图像。

  • Bc - 需要在输入图像中匹配的模式。它可以是 0、1 或 2。

  • out(可选) - 定义在哪个数组中存储输出图像(默认为与输入相同大小)。

示例

以下示例演示了使用 mh.hitmiss() 函数对图像进行击中与错过变换。

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('tree.tiff')
# Converting it to grayscale
image = mh.colors.rgb2gray(image)
# Applying thresholding
threshold_image = mh.thresholding.bernsen(image, 5, 200)
# Creating hit & miss template
template = np.array([[1, 2, 1, 2, 1],[2, 1, 1, 1, 2],[2, 2, 1, 2, 2]])
# Applying hit & miss transformation
hit_miss = mh.hitmiss(threshold_image, template)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original image
axes[0].imshow(image, cmap='gray')
axes[0].set_title('Original Image')
axes[0].set_axis_off()
# Displaying the hit & miss transformed image
axes[1].imshow(hit_miss, cmap='gray')
axes[1].set_title('Hit & Miss Transformed Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()
输出

以下是上述代码的输出:

Hit Miss Transform

通过检测边缘

我们还可以通过应用击中与错过变换来检测图像的边缘。边缘表示图像中不同区域之间的边界。

这些是相邻像素之间强度值差异较大的区域。

在 mahotas 中,要使用击中与错过变换检测边缘,我们首先创建一个结构元素。此结构元素将模板的边缘与输入图像进行匹配。

然后,我们对图像执行阈值化,然后将结构元素作为Bc参数传递给 hitmiss() 函数。

例如,以下结构元素可用于检测输入图像中的边缘:

[[1, 2, 1]
 [2, 2, 2]
 [1, 2, 1]]

在此,1 位于结构元素的右上角、左上角、右下角和左下角位置。边缘通常位于图像中的这些位置。

结构元素中的 1 与图像中强度值为 1 的像素匹配,从而突出显示边缘作为前景。

示例

在此示例中,我们尝试通过应用击中与错过变换来检测图像的边缘:

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('nature.jpeg')
# Converting it to grayscale
image = mh.colors.rgb2gray(image).astype(np.uint8)
# Applying thresholding
threshold_value = mh.thresholding.rc(image)
threshold_image = image > threshold_value
# Creating hit & miss template
template = np.array([[1, 2, 1],[2, 2, 2],[1, 2, 1]])
# Applying hit & miss transformation
hit_miss = mh.hitmiss(threshold_image, template)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original image
axes[0].imshow(image, cmap='gray')
axes[0].set_title('Original Image')
axes[0].set_axis_off()
# Displaying the hit & miss transformed image
axes[1].imshow(hit_miss, cmap='gray')
axes[1].set_title('Hit & Miss Transformed Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

输出

上述代码的输出如下:

Detecting Edges

通过检测对角线

我们也可以使用击中与错过变换来检测图像的对角线。对角线由连接图像相对角的线性图案指示。

这些是像素强度沿对角线路径变化的区域。

在 mahotas 中,我们首先对输入图像执行阈值化。然后,我们将结构元素作为Bc参数传递给 hitmiss() 函数。此结构元素将模板的对角线与输入图像的对角线进行匹配。

例如,以下结构元素可用于检测输入图像中的对角线:

[[0, 2, 0]
 [2, 0, 2]
 [0, 2, 0]]

在此,0 沿从左上角到右下角的对角线路径运行,以及从右上角到左下角的对角线路径运行。对角线通常位于图像中的这些位置。

结构元素中的 0 与图像中强度值为 0 的像素匹配,从而突出显示对角线作为背景。

示例

在这里,我们尝试使用击中与错过变换来检测图像的对角线:

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('sun.png')
# Converting it to grayscale
image = mh.colors.rgb2gray(image)
# Applying thresholding
threshold_image = mh.thresholding.bernsen(image, 10, 10)
# Creating hit & miss template
template = np.array([[0, 2, 0],[2, 0, 2],[0, 2, 0]])
# Applying hit & miss transformation
hit_miss = mh.hitmiss(threshold_image, template)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original image
axes[0].imshow(image, cmap='gray')
axes[0].set_title('Original Image')
axes[0].set_axis_off()
# Displaying the hit & miss transformed image
axes[1].imshow(hit_miss, cmap='gray')
axes[1].set_title('Hit & Miss Transformed Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

输出

执行上述代码后,我们将获得以下输出:

Detecting Diagonals
广告