Mahotas - 邊界畫素



邊界畫素是指位於影像邊界或邊緣上的畫素。邊界畫素至少有一個相鄰畫素屬於不同的區域或具有不同的值,表明感興趣區域與背景之間的過渡。

例如,在二值影像中,物體由白色畫素表示,背景由黑色畫素表示,邊界畫素將是那些與黑色畫素相鄰的白色畫素。

Mahotas中的邊界畫素

在Mahotas中,我們可以使用labeled.border()labeled.borders()函式提取邊界畫素。這些函式透過檢查具有不同標籤的相鄰畫素來檢測邊界,同時還考慮結構元素指定的連通性。

使用mahotas.labeled.border()函式

mahotas.labeled.border()函式接受標記影像作為輸入,並返回一個相同大小的二值影像,顯示邊界畫素。此函式提取標記影像中兩個指定區域之間的邊界畫素。

在結果影像中,邊界畫素標記為True(或1),非邊界畫素標記為False(或0)。

語法

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

mahotas.labeled.border(labeled, i, j, Bc={3x3 cross},
out={np.zeros(labeled.shape, bool)}, always_return=True)

其中,

  • labeled - 輸入陣列。

  • i - 第一個區域的標籤。

  • j - 第二個區域的標籤。

  • Bc (可選) - 用於連通性的結構元素。

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

  • always_return (可選) - 一個標誌,指示是否在沒有邊界畫素時返回輸出(預設為True)。

示例

在下面的示例中,我們使用mh.labeled.border()函式提取標記區域1的邊界畫素。

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.label(image)
# Getting border pixels
border_pixels = mh.labeled.border(labeled, 0, 1)
# 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 border pixels
axes[1].imshow(border_pixels)
axes[1].set_title('Border Pixels')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Show the figure
mtplt.show()
輸出

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

Border Pixels Image

使用mahotas.labeled.borders()函式

mahotas.labeled.borders()函式提取標記影像中的所有邊界畫素。它類似於mahotas.labeled.border()函式,因為它檢查具有不同標籤的相鄰畫素以檢測邊界。

它也生成一個二值影像,其中邊界畫素標記為True(或1),非邊界畫素標記為False(或0)。

語法

以下是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_rgb = mh.imread('nature.jpeg')
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)
# Get border pixels
border_pixels = mh.labeled.borders(labeled)
# 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 border pixels
axes[1].imshow(border_pixels)
axes[1].set_title('Border Pixels')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()
輸出

上述程式碼的輸出如下:

Border Pixels Image1

使用自定義結構元素

我們還可以使用自定義結構元素來更精確地檢測邊界畫素。結構元素是一個奇數維度的二值陣列,由1和0組成。

它定義了在識別邊界畫素時鄰域畫素的連線模式。我們可以使用numpy庫中的array()函式定義自定義結構元素。

例如,讓我們考慮二值陣列:[[0, 1, 0],[0, 1, 0],[0, 1, 0]]作為結構元素。此結構元素暗示垂直連線,這意味著對於每個畫素,僅將其正上方和正下方的畫素(在同一列中)視為其鄰居。

預設情況下,mahotas.labeled.border()和mahotas.labeled.borders()都使用3×3十字形結構元素。此結構元素在確定連線性時會考慮每個畫素的四個直接鄰居(頂部、底部、左側和右側)。

示例

以下示例顯示了使用自定義結構元素提取邊界畫素。

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 to a labeled image
labeled, num_objects = mh.label(image)
# Creating a custom structuring element
binary_closure = np.array([[0, 1, 0],
[0, 1, 0],
[0, 1, 0]])
# Getting border pixels
border_pixels = mh.labeled.borders(labeled, Bc=binary_closure)
# Create 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 border pixels
axes[1].imshow(border_pixels)
axes[1].set_title('Border Pixels')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

輸出

產生的輸出如下所示:

Border Pixels Image2
廣告
© . All rights reserved.