Mahotas - 區域過濾



區域過濾指的是根據某些條件排除標記影像的特定區域。基於區域大小是常用的區域過濾條件。透過指定大小限制,可以排除過小或過大的區域,從而獲得乾淨的輸出影像。

另一個區域過濾標準是檢查區域是否為邊界。透過應用這些過濾器,我們可以選擇性地移除或保留影像中感興趣的區域。

在 Mahotas 中進行區域過濾

在 Mahotas 中,我們可以使用labeled.filter_labeled() 函式轉換標記影像的過濾器區域。此函式將過濾器應用於影像的選定區域,同時保持其他區域不變。

使用 mahotas.labeled.filter_labeled() 函式

mahotas.labeled.filter_labeled() 函式以標記影像作為輸入,並根據某些屬性移除不需要的區域。它根據影像的標籤識別區域。

結果影像僅包含與過濾器條件匹配的區域。

語法

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

mahotas.labeled.filter_labeled(labeled, remove_bordering=False, min_size=None,
max_size=None)

其中,

  • labeled - 它是一個數組。

  • remove_bordering (可選) - 它定義是否移除接觸邊界的區域。

  • min_size (可選) - 這是需要保留的區域的最小大小(預設情況下沒有最小值)。

  • max_size (可選) - 這是需要保留的區域的最大大小(預設情況下沒有最大值)。

示例

在以下示例中,我們正在過濾標記影像以移除邊界畫素。

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image_rgb = mh.imread('tree.tiff')
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)
# Applying filters
filtered_image, num_objects = mh.labeled.filter_labeled(labeled,
remove_bordering=True)
# 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 filtered image
axes[1].imshow(filtered_image)
axes[1].set_title('Filtered Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()
輸出

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

Filter Regions Image

特定大小區域的過濾

我們還可以過濾影像中特定大小的區域。透過這種方式,我們可以從標記影像中移除不符合特定大小限制的區域(過小或過大的區域)。

在 mahotas 中,我們可以透過在 labeled.filter_label() 函式中為可選引數min_sizemax_size 指定值來實現此目的。

示例

以下示例顯示了過濾標記影像以移除特定大小區域。

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image_rgb = mh.imread('tree.tiff')
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)
# Applying filters
filtered_image, num_objects = mh.labeled.filter_labeled(labeled, min_size=10,
max_size=50000)
# 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 filtered image
axes[1].imshow(filtered_image)
axes[1].set_title('Filtered Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

輸出

上述程式碼的輸出如下:

Filter Regions Image1

邊界區域和特定大小區域的過濾

我們可以過濾影像中的邊界區域以及特定大小的區域。在此,我們移除接觸邊界的區域以及不符合特定大小限制的區域。

在 mahotas 中,我們可以透過為可選引數min_sizemax_size 指定值,並將可選引數remove_bordering 設定為True 來實現此目的,這些引數都在 labeled.filter_label() 函式中。

示例

在此示例中,應用過濾器以移除標記影像的邊界區域和特定大小的區域。

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image_rgb = mh.imread('tree.tiff')
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)
# Applying filters
filtered_image, num_objects = mh.labeled.filter_labeled(labeled,
remove_bordering=True, min_size=1000, max_size=50000)
# 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 filtered image
axes[1].imshow(filtered_image)
axes[1].set_title('Filtered Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

輸出

產生的輸出如下所示:

Filter Regions Image2
廣告