Mahotas 简明教程
Mahotas - Hit & Miss Transform
击中和不命中变换是一个二进制 morphological operation ,用于检测图像中的特定模式 = 或形状。
The Hit & Miss transform is a binary morphological operation that detects specific patterns = or shapes within an image.
该操作将结构元素与输入二进制图像进行比较。结构元素由排列成特定模式的前景 (1) 和背景 (0) 像素组成,该模式代表要检测的所需形状或模式。
The operation compares a structuring element with the input binary image. The structuring element consists of foreground (1) and background (0) pixels arranged in a specific pattern that represents the desired shape or pattern to be detected.
击中或不命中变换对结构元素和图像执行像素级逻辑 AND 运算,然后检查结果是否与预定义的条件匹配。
The Hit−or−Miss Transform performs a pixel−wise logical AND operation between the structuring element and the image, and then checks if the result matches a pre−defined condition.
条件指定了匹配模式中应存在的前景和背景像素的确切排列。如果条件满足,则输出像素设置为 1,表示匹配;否则,设置为 0。
The condition specifies the exact arrangement of foreground and background pixels that should be present in the matched pattern. If the condition is satisfied, the output pixel is set to 1, indicating a match; otherwise, it is set to 0.
Hit & Miss transform in Mahotas
在马霍塔斯中,我们可以使用 mahotas.hitmiss() 函数对图像执行击中和不命中变换。该函数使用结构元素 'Bc' 确定输入图像中是否存在特定模式。
In Mahotas, we can use the mahotas.hitmiss() function to perform Hit & Miss transformation on an image. The function uses a structuring element 'Bc' to determine whether a specific pattern exists in the input image.
Mahotas 中的结构化元素可以取三个值:0、1 或 2。1 的值表示结构化元素的前景,而 0 表示背景。
The structuring element in Mahotas can take on three values: 0, 1, or 2. A value of 1 indicates the foreground of the structuring element, while 0 represents the background.
值 2 用作“无关紧要”值,这意味着不应该对该特定像素执行匹配。
The value 2 is used as a "don’t care" value, meaning that a match should not be performed for that particular pixel.
为了识别匹配,结构化元素的值必须与输入图像中对应的像素值重叠。
To identify a match, the structuring element’s values must overlap with the corresponding pixel values in the input image.
如果重叠满足结构化元素指定条件,则像素被视为匹配。
If the overlap satisfies the conditions specified by the structuring element, the pixel is considered a match.
The mahotas.hitmiss() function
mahotas.hitmiss() 将灰度图像作为输入,并将二进制图像作为输出返回。白色像素表示结构化元素和输入图像匹配的区域,而黑色像素表示不匹配的区域。
The mahotas.hitmiss() takes a grayscale image as an input and returns a binary image as output. The white pixels represent areas where there is a match between the structuring element and the input image, while the black pixels represent areas where there is no match.
以下是 mahotas 中 hitmiss() 函数的基本语法:
Following is the basic syntax of the hitmiss() function in mahotas −
mahotas.hitmiss(input, Bc, out=np.zeros_like(input))
其中,
Where,
-
input − It is the input grayscale image.
-
Bc − It is the pattern that needs to be matched in the input image. It can have a value of 0, 1, or 2.
-
out (optional) − It defines in which array to store the output image (default is of same size as input).
以下示例显示了使用 mh.hitmiss() 函数对图像进行命中和遗失变换。
The following example shows hit & miss transformation on an image using the mh.hitmiss() function.
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()
以下是上面代码的输出: -
Following is the output of the above code −
By Detecting Edges
我们还可以通过应用命中和错过转换来检测图像的边缘。边缘表示图像中不同区域之间的边界。
We can also detect edges of an image by applying the Hit & Miss transformation. The edges represent the boundary between different regions in an image.
这些区域是相邻像素之间强度值差异很大的区域。
These are areas where the difference in intensity value between the neighboring pixels is high.
在 mahotas 中,要使用命中和错过转换检测边缘,我们首先创建一个结构化元素。此结构化元素将模板的边缘与输入图像匹配。
In mahotas, to detect edges using Hit & Miss transform, we first create a structuring element. This structuring element matches the edges of the template with the input image.
然后我们对图像执行 thresholding ,然后将结构化元素作为 Bc 参数传递给 hitmiss() 函数。
We then perform thresholding on the image and then pass the structuring element as the Bc parameter to the hitmiss() function.
例如,以下结构化元素可用于检测输入图像中的边缘:
For example, the following structuring element can be used to detect edges in an input image −
[[1, 2, 1]
[2, 2, 2]
[1, 2, 1]]
在这里,1 位于结构化元素的右上角、左上角、右下角和左下角位置。边缘通常出现在图像中的这些位置。
In here, the 1s are present at the top−rightmost, top−leftmost, bottom−rightmost, and bottom−leftmost positions of the structuring element. The edges are usually present at these locations in an image.
结构化元素中存在的 1 与图像中具有强度值 1 的像素匹配,从而将边缘突出显示为前景。
The 1s present in the structuring element matches with the pixels having intensity value 1 in the image, thus highlighting the edges as the foreground.
Example
在此示例中,我们尝试通过应用命中和遗漏转换来检测图像的边缘:
In this example, we are trying to detect edges of an image by applying the Hit & Miss transformation −
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()
上述代码的输出如下:
Output of the above code is as follows −
By Detecting Diagonals
我们还可以使用命中和遗漏转换来检测图像的对角线。对角线由连接图像相对角的线性图案表示。
We can use the Hit & Miss transformation to detect the diagonals of an image as well. Diagonals are indicated by a linear pattern connecting opposite corners of an image.
这些是像素强度沿对角线路径发生的变化的区域。
These are the region where the pixel intensity changes along a diagonal path.
在 mahotas 中,我们首先在输入图像上执行 thresholding 。然后我们将结构元件作为 Bc 参数传递给 hitmiss() 函数。此结构元件使用模板的对角线与输入图像的对角线进行匹配。
In mahotas, we first perform thresholding on the input image. We then pass a structuring element as the Bc parameter to the hitmiss() function. This structuring element matches the diagonals of the template with the diagonals of the input image.
例如,以下结构元件可用于检测输入图像中的对角线 −
For example, the following structuring element can be used to detect diagonals in an input image −
[[0, 2, 0]
[2, 0, 2]
[0, 2, 0]]
在此,0 沿一条对角线路径从左上角延伸到右下角,并从右上角到左下角延伸。通常在图像中的这些位置会出现对角线。
In here, the 0s run along a diagonal path from the top leftmost to the bottom rightmost position, and from the top rightmost to the bottom leftmost position. The diagonals are usually present at these locations in an image.
结构元件中出现的 0 与图像中强度值为 0 的像素相匹配,从而突出对角线作为背景。
The 0s present in the structuring element matches with the pixels having intensity value 0 in the image, thus highlighting the diagonals as the background.
Example
这里,我们尝试使用命中和未命中变换检测图像的对角线 −
Here, we are trying to detect the diagonals of an image using the Hit & Miss transformation −
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()
执行上面的代码后,我们得到以下输出: -
After executing the above code, we get the following output −