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 中给图像贴标签的不同函数:

S.No

Function & Description

1

*label() *该函数执行二进制图像的连通分量标记,将唯一标签分配给一行的连通区域。

2

labeled.label() 该函数为图像的不同区域分配从 1 开始的连续标签。

3

labeled.filter_labeled() 该函数将过滤器应用于图像的选定区域,同时保持其他区域不变。

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

The label() Function

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

Example

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

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()

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

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

获得的图像如下所示:

labeling images

The labeled.label() Function

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

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

Example

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

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()

获得的输出如下 −

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]]

以下是生成图像:

labeling images1

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