Mahotas - 條件膨脹影像



在上一章中,我們探討了影像膨脹的概念,這是一種用於將影像中區域的所有畫素擴充套件到邊界的操作。另一方面,條件膨脹根據某些條件擴充套件特定區域的畫素。

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

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

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

在Mahotas中進行條件膨脹影像

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

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

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

mahotas.cdilate()函式

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

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

語法

以下是cdilate()函式的基本語法:

mahotas.cdilate(f, g, Bc={3x3 cross}, n=1)

其中,

  • f - 它是將執行條件膨脹的輸入影像。

  • g - 它是條件膨脹期間要應用的掩碼。

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

  • n - 它是膨脹操作的迭代次數。預設情況下,它設定為1,表示單次迭代。

示例

在下面的示例中,我們透過放大畫素強度來對影像執行條件膨脹:

import mahotas as mh
import numpy as np
import matplotlib.pyplot as plt
image= mh.imread('nature.jpeg')
g = image * 2
conditional_dilated_image = mh.cdilate(image, g)
# 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 dilated image
axes[1].imshow(conditional_dilated_image, cmap='gray')
axes[1].set_title('Conditional Dilated Image')
axes[1].axis('off')
# Adjust the layout and display the plot
plt.tight_layout()
plt.show()

輸出

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

Dilating Image 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 > 100
# Define a structuring element for dilation
structuring_element = mh.disk(5)
conditional_dilated_image = mh.cdilate(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 dilated image
axes[1].imshow(conditional_dilated_image, cmap='gray')
axes[1].set_title('Conditional Dilated Image')
axes[1].axis('off')
# Adjust the layout and display the plot
plt.tight_layout()
plt.show()

輸出

獲得的輸出如下所示:

Using Structuring Element

使用自定義條件函式

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

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

接下來,選擇一個結構元素來定義膨脹操作的形狀和大小。

最後,執行條件膨脹並檢索膨脹後的影像。

示例

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

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 > 100
# 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 dilation
conditional_dilated_image = mh.cdilate(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 dilated image
axes[1].imshow(conditional_dilated_image, cmap='gray')
axes[1].set_title('Conditional Dilated Image')
axes[1].axis('off')
# Adjust the layout and display the plot
plt.tight_layout()
plt.show()

輸出

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

Custom Condition Function
廣告