Mahotas 简明教程
Mahotas - Getting Border of Labels
获取标签边界是指提取 labeled image 的边界像素。边界可以定义为一个区域,其像素位于图像的边缘。边界表示图像不同区域之间的过渡。
Getting the border of labels refers to extracting the border pixels of a labeled image. A border can be defined as a region whose pixels are located at the edges of an image. A border represents the transition between different regions of an image.
获取标签边界涉及识别标记图像中的边界区域并将它们与背景分离开来。
Getting borders of labels involves identifying the border regions in the labeled image and separating them from the background.
由于标记图像仅由前景像素和背景像素组成,因此边界可以很容易地被识别出来,因为它们与背景区域相邻。
Since a labeled image consists of only the foreground pixels and the background pixels, the borders can be easily identified as they are present adjacent to the background regions.
Getting Border of labels in Mahotas
在 Mahotas 中,我们可以使用 mahotas.labeled.borders() 函数获取标签边界。它分析标记图像的相邻像素并考虑连接模式以获取边界。
In Mahotas, we can use the mahotas.labeled.borders() function to get the border of labels. It analyzes the neighboring pixels of the labeled image and considers the connectivity patterns to get the borders.
The mahotas.labeled.borders() function
mahotas.labeled.borders() 函数将标记图像作为输入,并返回具有高亮边界的图像。
The mahotas.labeled.borders() function takes a labeled image as input and returns an image with the highlighted borders.
在结果图像中,边界像素的值为 1,并且是前景的一部分。
In the resultant image, the border pixels have a value of 1 and are part of the foreground.
以下是 mahotas 中 borders() 函数的基本语法 −
Following is the basic syntax of the borders() function in mahotas −
mahotas.labeled.borders(labeled, Bc={3x3 cross}, out={np.zeros(labeled.shape,
bool)})
其中,
Where,
-
labeled − It is the input labeled image.
-
Bc (optional) − It is the structuring element used for connectivity.
-
out (optional) − It is the output array (defaults to new array of same shape as labeled).
在以下示例中,我们使用 mh.labeled.borders() 函数获取标签边界。
In the following example, we are getting the borders of labels using the mh.labeled.borders() function.
import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('nature.jpeg', as_grey=True)
# Applying thresholding
image = image > image.mean()
# Converting it to a labeled image
labeled, num_objects = mh.label(image)
# Geting border of labels
borders = mh.labeled.borders(labeled)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the labeled image
axes[0].imshow(labeled)
axes[0].set_title('Labeled Image')
axes[0].set_axis_off()
# Displaying the borders
axes[1].imshow(borders)
axes[1].set_title('Border Labels')
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 −
Getting Borders by using a Custom Structuring Element
我们还可以通过使用自定义结构元素获取标签边界。结构元素是一个仅由 1 和 0 组成的数组。它用于定义相邻像素的连接结构。
We can also get borders of labels by using a custom structuring element. A structuring element is an array which consists of only 1s and 0s. It is used to define the connectivity structure of the neighboring pixels.
包含在连通性分析中的像素具有 1 的值,而被排除的像素具有 0 的值。
Pixels that are included in the connectivity analysis have the value 1, while the pixels that are excluded have the value 0.
在 mahotas 中,我们使用 mh.disk() 函数创建自定义结构元素。然后,我们设置此自定义结构元素作为 borders() 函数中的 Bc 参数,以获取标签边界。
In mahotas, we create a custom structuring element using the mh.disk() function. Then, we set this custom structuring element as the Bc parameter in the borders() function to get the borders of labels.
Example
在此处,我们使用自定义结构元素获取标签边界。
Here, we are getting borders of labels using a custom structuring element.
import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('sea.bmp', as_grey=True)
# Applying thresholding
image = image > image.mean()
# Converting it to a labeled image
labeled, num_objects = mh.label(image)
# Geting border of labels
borders = mh.labeled.borders(labeled, mh.disk(5))
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the labeled image
axes[0].imshow(labeled)
axes[0].set_title('Labeled Image')
axes[0].set_axis_off()
# Displaying the borders
axes[1].imshow(borders)
axes[1].set_title('Border Labels')
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 −