Mahotas - 影像標記函式



影像標記是一種資料標記過程,涉及識別影像中的特定特徵或物體,並向選定的物體新增有意義的資訊以進行分類。

  • 它通常用於生成機器學習模型的訓練資料,尤其是在計算機視覺領域。
  • 影像標記被廣泛應用於各種應用中,包括物體檢測、影像分類、場景理解、自動駕駛、醫學影像等等。
  • 它允許機器學習演算法從標記資料中學習,並根據提供的註釋做出準確的預測或識別。

影像標記函式

以下是 Mahotas 中用於標記影像的不同函式:

序號 函式及描述
1 label()

此函式對二值影像執行連通分量標記,在一行中為連通區域分配唯一標籤。

2 labeled.label()

此函式為影像的不同區域分配從 1 開始的連續標籤。

3 labeled.filter_labeled()

此函式將過濾器應用於影像的選定區域,同時保持其他區域不變。

現在,讓我們看看其中一些函式的示例。

label() 函式

mahotas.label() 函式用於標記陣列,該陣列被解釋為二值陣列。這也被稱為連通分量標記,其中連通性由結構元素定義。

示例

以下是使用 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

labeled.label() 函式

mahotas.labeled.label() 函式用於更新標籤值以使其按順序排列。生成的順序標籤將是一個新的標記影像,標籤從 1 開始連續分配。

在此示例中,我們從一個由 NumPy 陣列表示的標記影像開始,其中標籤是非順序的。

示例

以下是使用 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

我們在本節的其餘章節中詳細討論了這些函式。

廣告