Mahotas 简明教程

Mahotas - Hit & Miss Transform

击中和不命中变换是一个二进制 morphological operation ,用于检测图像中的特定模式 = 或形状。

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

击中或不命中变换对结构元素和图像执行像素级逻辑 AND 运算,然后检查结果是否与预定义的条件匹配。

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

Hit & Miss transform in Mahotas

在马霍塔斯中,我们可以使用 mahotas.hitmiss() 函数对图像执行击中和不命中变换。该函数使用结构元素 'Bc' 确定输入图像中是否存在特定模式。

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

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

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

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

The mahotas.hitmiss() function

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

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

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

其中,

  1. input - 它是输入的灰度图像。

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

  3. out (optional) - 它定义将输出图像存储在哪个数组中(默认情况下,与输入图像大小相同)。

以下示例显示了使用 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

By Detecting Edges

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

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

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

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

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

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

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

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

Example

在此示例中,我们尝试通过应用命中和遗漏转换来检测图像的边缘:

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

By Detecting Diagonals

我们还可以使用命中和遗漏转换来检测图像的对角线。对角线由连接图像相对角的线性图案表示。

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

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

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

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

在此,0 沿一条对角线路径从左上角延伸到右下角,并从右上角到左下角延伸。通常在图像中的这些位置会出现对角线。

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

Example

这里,我们尝试使用命中和未命中变换检测图像的对角线 −

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