Mahotas 简明教程
Mahotas - Labeling Images
图像标记是指将类别(标签)分配给图像的不同区域。标签通常表示为整数值,其中每个值对应于特定类别或区域。
Labeling images refers to assigning categories (labels) to different regions of an image. The labels are generally represented as integer values, where each value corresponds to a specific category or region.
例如,让我们考虑一幅包含各种对象或区域的图像。每个区域都会分配一个唯一值(整数)以将其与其他区域区分开来。背景区域标记为值 0。
For example, let’s consider an image with various objects or regions. Each region is assigned a unique value (integer) to differentiate it from other regions. The background region is labeled with a value of 0.
Labeling Images in Mahotas
在 Mahotas 中,我们可以使用 label() 或 labeled.label() 函数标记图像。
In Mahotas, we can label images using label() or labeled.label() functions.
这些函数将图像分割为不同的区域,方法是为图像中的不同连通分量分配唯一的标签或标识符。每个连通分量都是一组相邻像素,它们共享一个公共属性,例如强度或颜色。
These functions segment an image into distinct regions by assigning unique labels or identifiers to different connected components within an image. Each connected component is a group of adjacent pixels that share a common property, such as intensity or color.
标记过程创建一个图像,其中属于同一区域的像素被分配相同的标签值。
The labeling process creates an image where pixels belonging to the same region are assigned the same label value.
Using the mahotas.label() Function
mahotas.label() 函数将图像作为输入,其中关注区域由前景(非零)值表示,背景由零表示。
The mahotas.label() function takes an image as input, where regions of interest are represented by foreground (non−zero) values and the background is represented by zero.
该函数返回标记数组,其中每个连通分量或区域都分配了一个唯一的整数标签。
The function returns the labeled array, where each connected component or region is assigned a unique integer label.
label() 函数使用 8 连通性执行标记,它指的是图像中像素之间的关系,其中每个像素与其周围的八个邻居像素相连,包括对角线。
The label() function performs labeling using 8−connectivity, which refers to the relationship between pixels in an image, where each pixel is connected to its eight surrounding neighbors, including the diagonals.
以下是 mahotas 中标签功能的基本语法——
Following is the basic syntax of the label() function in mahotas −
mahotas.label(array, Bc={3x3 cross}, output={new array})
其中,
where,
-
array − It is the input array.
-
Bc (optional) − It is the structuring element used for connectivity.
-
output (optional) − It is the output array (defaults to new array of same shape as array).
在以下示例中,则使用 mh.label() 功能来对图像进行标记。
In the following example, we are labeling an image using the mh.label() function.
import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image_rgb = mh.imread('sun.png')
image = image_rgb[:,:,0]
# Applying gaussian filtering
image = mh.gaussian_filter(image, 4)
image = (image > image.mean())
# Converting it to a labeled image
labeled, num_objects = mh.label(image)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original RGB image
axes[0].imshow(image_rgb)
axes[0].set_title('RGB Image')
axes[0].set_axis_off()
# Displaying the labeled image
axes[1].imshow(labeled)
axes[1].set_title('Labeled 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 −

Using the mahotas.labeled.label() Function
mahotas.labeled.label() 函数会从图像的不同区域开始,对标签进行 1 至多个连续分配。其工作方式与 mahotas.label() 功能类似,可将图像分割成不同的区域。
The mahotas.labeled.label() function assigns consecutive labels starting from 1 to different regions of an image. It works similar to mahotas.label() function to segment an image into distinct regions.
如果使用非连续标签值标记图像,labeled.label() 功能会更新标签值以保持顺序。
If you have a labeled image with non−sequential label values, the labeled.label() function updates the label values to be in sequential order.
例如,假设使用标签标记图像,其中四个区域具有标签 2, 4, 7, and 9 。labeled.label() 功能会将图像转换为新标记图像,其中包含连续标签 1, 2, 3, and 4 。
For example, let’s say we have a labeled image with four regions having labels 2, 4, 7, and 9. The labeled.label() function will transform the image into a new labeled image with consecutive labels 1, 2, 3, and 4 respectively.
以下是 mahotas 中 labeled.label() 功能的基本语法——
Following is the basic syntax of the labeled.label() function in mahotas −
mahotas.labeled.label(array, Bc={3x3 cross}, output={new array})
其中,
where,
-
array − It is the input array.
-
Bc (optional) − It is the structuring element used for connectivity.
-
output (optional) − It is the output array (defaults to new array of same shape as array).
以下示例演示如何使用 mh.labeled.label() 功能将图像转换为标记图像。
The following example shows conversion of an image to a labeled image using mh.labeled.label() function.
import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image_rgb = mh.imread('sea.bmp')
image = image_rgb[:,:,0]
# Applying gaussian filtering
image = mh.gaussian_filter(image, 4)
image = (image > image.mean())
# Converting it to a labeled image
labeled, num_objects = mh.labeled.label(image)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original RGB image
axes[0].imshow(image_rgb)
axes[0].set_title('RGB Image')
axes[0].set_axis_off()
# Displaying the labeled image
axes[1].imshow(labeled)
axes[1].set_title('Labeled 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 −

Using Custom Structuring Element
我们可以使用具有标记功能的自定义结构元素,根据要求对图像进行分割。结构元素是二进制数组,具有奇数维度,由一和零组成,用于定义图像标记过程中邻域像素的连接模式。
We can use a custom structuring element with the label functions to segment an image as per the requirement. A structuring element is a binary array of odd dimensions consisting of ones and zeroes that defines the connectivity pattern of the neighborhood pixels during image labeling.
一指示包含在连接分析中的邻域像素,而零表示排除或忽略的邻域像素。
The ones indicate the neighboring pixels that are included in the connectivity analysis, while the zeros represent the neighbors that are excluded or ignored.
例如,让我们考虑自定义结构元素: [[1, 0, 0], [0, 1, 0], [0, 0,1]]. 这个结构元素暗示了对角连接。这意味着对于图像中的每个像素,在标记或分割过程中,仅将该像素的正上方和正下方像素视为其邻域像素。
For example, let’s consider the custom structuring element: [[1, 0, 0], [0, 1, 0], [0, 0,1]]. This structuring element implies diagonal connectivity. It means that for each pixel in the image, only the pixels diagonally above and below it is considered its neighbors during the labeling or segmentation process.
Example
在此处,我们已经定义了一个自定义结构元素来对图像进行标记。
Here, we have defined a custom structuring element to label an image.
import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image_rgb = mh.imread('sea.bmp')
image = image_rgb[:,:,0]
# Applying gaussian filtering
image = mh.gaussian_filter(image, 4)
image = (image > image.mean())
# Creating a custom structuring element
binary_closure = np.array([[0, 1, 0],
[0, 1, 0],
[0, 1, 0]])
# Converting it to a labeled image
labeled, num_objects = mh.labeled.label(image, Bc=binary_closure)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original RGB image
axes[0].imshow(image_rgb)
axes[0].set_title('RGB Image')
axes[0].set_axis_off()
# Displaying the labeled image
axes[1].imshow(labeled)
axes[1].set_title('Labeled Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figure
mtplt.show()
执行上面的代码后,我们得到以下输出: -
After executing the above code, we get the following output −
