Mahotas 简明教程

Mahotas - Labeled Image Functions

图像标记是一个数据标记过程,涉及识别图像中的特定特征或对象,添加有意义的标记信息来选择和分类这些对象。

Image labeling is a data labeling process that involves identifying specific features or objects in an image, adding meaningful information to select and classify those objects.

  1. It is commonly used to generate training data for machine learning models, particularly in the field of computer vision.

  2. Image labeling is used in a wide range of applications, including object detection, image classification, scene understanding, autonomous driving, medical imaging, and more.

  3. It allows machine learning algorithms to learn from labeled data and make accurate predictions or identifications based on the provided annotations.

Functions for Labeling Images

以下是用于在 mahotas 中给图像贴标签的不同函数:

Following are the different functions used to label images in mahotas −

S.No

Function & Description

1

*label()*This function performs connected component labeling on a binary image, assigning unique labels to connected regions in one line.

2

*labeled.label()*This function assigns consecutive labels starting from 1 to different regions of an image.

3

*labeled.filter_labeled()*This function applies filters to the selected regions of an image while leaving other regions unchanged.

现在,让我们看看其中一些函数的示例。

Now, lets us see examples of some of these functions.

The label() Function

mahotas.label() 函数用于标记数组,该数组被解释为二进制数组。这也称为连通分量标记,连通性由结构化元素定义。

The mahotas.label() function is used to label the array, which is interpreted as a binary array. This is also known as connected component labeled, where the connectivity is defined by the structuring element.

Example

以下是使用 label() 函数为图像贴标签的基本示例:

Following is the basic example to label an image using the label() function −

import mahotas as mh
import numpy as np
from pylab import imshow, show
# Create a binary image
image = np.array([[0, 0, 1, 1, 0],
[0, 1, 1, 0, 0],
[0, 0, 0, 1, 1],
[0, 0, 0, 0, 1],
[0, 1, 1, 1, 1]], dtype=np.uint8)
# Perform connected component labeling
labeled_image, num_labels = mh.label(image)
# Print the labeled image and number of labels
print("Labeled Image:")
print(labeled_image)
print("Number of labels:", num_labels)
imshow(labeled_image)
show()

执行上面的代码后,我们得到以下输出: -

After executing the above code, we get the following output −

Labeled Image:
[[0 0 1 1 0]
[0 1 1 0 0]
[0 0 0 2 2]
[0 0 0 0 2]
[0 2 2 2 2]]
Number of labels: 2

获得的图像如下所示:

The image obtained is as shown below −

labeling images

The labeled.label() Function

mahotas.labeled.label() 函数用于将标签值更新为顺序顺序。产生的顺序标签将是一个新标记的图像,其标签从 1 开始连续分配。

The mahotas.labeled.label() function is used to update the label values to be in sequential order. The resulting sequential labels will be a new labeled image with labels assigned consecutively starting from 1.

在此示例中,我们从一个由 NumPy 数组表示的标记图像开始,其中标签是非顺序的。

In this example, we start with a labeled image represented by a NumPy array where the labels are non−sequential.

Example

以下是使用 labeled.label() 函数标记图像的基本示例:

Following is the basic example to label an image using the labeled.label() function −

import mahotas as mh
import numpy as np
from pylab import imshow, show
# Create a labeled image with non-sequential labels
labeled_image = np.array([[0, 0, 1, 1, 0],
[0, 2, 2, 0, 0],
[0, 0, 0, 3, 3],
[0, 0, 0, 0, 4],
[0, 5, 5, 5, 5]], dtype=np.uint8)
# Update label values to be sequential
sequential_labels, num_labels = mh.labeled.label(labeled_image)
# Print the updated labeled image
print("Sequential Labels:")
print(sequential_labels)
imshow(sequential_labels)
show()

获得的输出如下 −

The output obtained is as follows −

Sequential Labels:
[[0 0 1 1 0]
[0 1 1 0 0]
[0 0 0 2 2]
[0 0 0 0 2]
[0 2 2 2 2]]

以下是生成图像:

Following is the image produced −

labeling images1

我们已经在本节的其余章节详细讨论了这些函数。

We have discussed these functions in detail in the remaining chapters of this section.