Mahotas - 條件腐蝕影像



在我們上一章中,我們探討了影像腐蝕的概念,這是一種用於將所有畫素縮小到影像中區域邊界的操作。另一方面,條件腐蝕基於某些條件,縮小(移除)某些特定區域的畫素。

條件可以基於影像畫素的強度值或影像中的一些特定模式。

例如,讓我們考慮一個灰度影像。您可以有條件地僅腐蝕滿足特定強度閾值的畫素,而不是腐蝕所有前景畫素。

如果畫素的強度低於預定義的閾值,則應用腐蝕;否則,畫素保持不變。

在 Mahotas 中條件腐蝕影像

在 mahotas 中,條件腐蝕是傳統腐蝕操作的擴充套件,它包含基於第二張影像(通常稱為“標記影像”)的條件。

它允許您控制腐蝕過程,以便腐蝕僅在標記影像具有特定畫素值的位置發生。

我們可以使用 **cerode()** 函式在 mahotas 中對影像執行條件腐蝕。此函式根據標記影像的畫素值將腐蝕過程限制在特定區域。

mahotas.cerode() 函式

Mahotas 中的 cerode() 函式接受兩個輸入 - 輸入影像和掩碼(條件)陣列。它根據提供的條件對輸入影像執行條件腐蝕,並返回生成的腐蝕影像。

掩碼用於識別影像內的特定區域。它們充當過濾器,突出顯示某些區域,同時忽略其他區域。

語法

以下是 mahotas 中 cerode() 函式的基本語法:

mahotas.cerode(f, g, Bc={3x3 cross}, out={np.empty_as(A)})

其中,

  • **f** - 它是要執行條件腐蝕的輸入影像。

  • **g** - 它是條件腐蝕期間要應用的掩碼。

  • **Bc={3×3 十字} (可選)** - 它是用於膨脹的結構元素。預設為 {3×3 十字}。

  • **out={np.empty_as(A)} (可選)** - 它提供一個輸出陣列來儲存腐蝕操作的結果

示例

在以下示例中,我們透過縮減其畫素強度來對影像執行條件腐蝕:

import mahotas as mh
import numpy as np
import matplotlib.pyplot as plt
image= mh.imread('nature.jpeg')
# Define the scaling factor
scale_factor = 0.5
# Scale down the intensity by multiplying with the scale factor
scaled_image = image * scale_factor
# Convert the scaled image to the appropriate data type
scaled_image = scaled_image.astype(np.uint8)
conditional_eroded_image = mh.cerode(image, scaled_image)
# Create a figure with subplots
fig, axes = plt.subplots(1, 2, figsize=(7,5 ))
# Display the original image
axes[0].imshow(image)
axes[0].set_title('Original Image')
axes[0].axis('off')
# Display the conditional eroded image
axes[1].imshow(conditional_eroded_image, cmap='gray')
axes[1].set_title('Conditional Eroded Image')
axes[1].axis('off')
# Adjust the layout and display the plot
plt.tight_layout()
plt.show()

輸出

執行上述程式碼後,我們將獲得以下輸出:

Conditional Eroding Mahotas

使用結構元素

要在 Mahotas 中使用結構元素執行條件腐蝕,首先,我們需要定義基於其應用腐蝕的條件。例如,您可以根據畫素強度指定條件或提供二值掩碼。

接下來,選擇一個結構元素來定義腐蝕的鄰域。Mahotas 提供了幾個預定義的結構元素,例如圓盤和正方形,您可以根據所需的形狀和大小進行選擇。

最後,檢索生成的影像,該影像將僅包含滿足條件的位置的腐蝕效果。

示例

在這裡,我們嘗試使用結構元素對影像執行條件腐蝕:

import mahotas as mh
import numpy as np
import matplotlib.pyplot as plt
image= mh.imread('nature.jpeg', as_grey = True).astype(np.uint8)
# Define the condition based on pixel intensity
condition = image > 0.5
# Define a structuring element for erosion
structuring_element = mh.disk(5)
conditional_eroded_image = mh.cerode(image, condition, structuring_element)
# Create a figure with subplots
fig, axes = plt.subplots(1, 2, figsize=(7,5 ))
# Display the original image
axes[0].imshow(image)
axes[0].set_title('Original Image')
axes[0].axis('off')
# Display the conditional eroded image
axes[1].imshow(conditional_eroded_image, cmap='gray')
axes[1].set_title('Conditional Eroded Image')
axes[1].axis('off')
# Adjust the layout and display the plot
plt.tight_layout()
plt.show()

輸出

獲得的輸出如下所示:

Structuring Element Mahotas

使用自定義條件函式

我們還可以使用自定義條件函式對影像執行條件腐蝕。

為此,我們首先定義一個自定義條件函式,該函式確定哪些畫素應該進行腐蝕。

接下來,選擇一個結構元素來定義腐蝕操作的形狀和大小。最後,執行條件腐蝕並檢索腐蝕影像。

示例

現在,我們使用自定義條件函式膨脹影像:

import mahotas as mh
import numpy as np
import matplotlib.pyplot as plt
# Load image
image = mh.imread('sea.bmp', as_grey=True).astype(np.uint8)
# Define a custom condition function
def custom_condition(pixel_value):
   return pixel_value > 0.5
# Define a structuring element
structuring_element = mh.disk(5)
# Create a binary mask based on the custom condition function
condition = custom_condition(image)
# Perform conditional erosion
conditional_eroded_image = mh.cerode(image, condition, structuring_element)
# Create a figure with subplots
fig, axes = plt.subplots(1, 2, figsize=(7,5 ))
# Display the original image
axes[0].imshow(image)
axes[0].set_title('Original Image')
axes[0].axis('off')
# Display the conditional eroded image
axes[1].imshow(conditional_eroded_image, cmap='gray')
axes[1].set_title('Conditional Eroded Image')
axes[1].axis('off')
# Adjust the layout and display the plot
plt.tight_layout()
plt.show()

輸出

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

Custom Condition Function Mahotas
廣告