Mahotas - 獲取標籤邊界



獲取標籤邊界是指提取標記影像的邊界畫素。邊界可以定義為畫素位於影像邊緣的區域。邊界表示影像不同區域之間的過渡。

獲取標籤邊界涉及識別標記影像中的邊界區域並將它們與背景分離。

由於標記影像僅包含前景畫素和背景畫素,因此邊界很容易識別,因為它們位於背景區域的旁邊。

在 Mahotas 中獲取標籤邊界

在 Mahotas 中,我們可以使用`**mahotas.labeled.borders()**` 函式來獲取標籤的邊界。它分析標記影像的相鄰畫素並考慮連線模式以獲取邊界。

`mahotas.labeled.borders()` 函式

`mahotas.labeled.borders()` 函式以標記影像作為輸入,並返回一個帶有突出顯示邊界的影像。

在結果影像中,邊界畫素的值為 1,並且是前景的一部分。

語法

以下是 Mahotas 中 `borders()` 函式的基本語法:

mahotas.labeled.borders(labeled, Bc={3x3 cross}, out={np.zeros(labeled.shape,
bool)})

其中:

  • `**labeled**` - 輸入的標記影像。

  • `**Bc (可選)**` - 用於連線的結構元素。

  • `**out (可選)**` - 輸出陣列(預設為與 `labeled` 形狀相同的新的陣列)。

示例

在下面的示例中,我們使用 `mh.labeled.borders()` 函式獲取標籤的邊界。

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

以下是上述程式碼的輸出:

Border Labels

使用自定義結構元素獲取邊界

我們還可以使用自定義結構元素來獲取標籤的邊界。結構元素是一個僅包含 1 和 0 的陣列。它用於定義相鄰畫素的連線結構。

包含在連線分析中的畫素值為 1,而被排除的畫素值為 0。

在 Mahotas 中,我們使用 `mh.disk()` 函式建立一個自定義結構元素。然後,我們將此自定義結構元素設定為 `borders()` 函式中的 `Bc` 引數以獲取標籤的邊界。

示例

這裡,我們使用自定義結構元素獲取標籤的邊界。

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

輸出

上述程式碼的輸出如下:

Border Labels Element
廣告