Mahotas 简明教程

Mahotas - Conditional Dilating Image

在上一章中,我们探讨了图像膨胀的概念,该操作用于扩展图像中区域的边界的所有像素。另一方面,条件膨胀基于某些条件扩展特定区域的像素。

条件可以基于图像像素的强度值或图像中某些特定模式。

例如,让我们考虑一个灰度图像。您可以有条件地仅扩展满足特定强度阈值的像素,而不是扩展所有前景像素。

如果像素的强度高于预定义阈值,则应用膨胀;否则,像素保持不变。

Conditional Dilating Image in Mahotas

在 mahotas 中,条件膨胀是传统膨胀操作的扩展,包含基于第二个图像的条件,通常称为“标记图像”。

它允许您控制膨胀过程,以便仅在标记图像具有特定像素值的位置发生膨胀。

我们可以在 mahotas 中使用 cdilate() 函数对图像执行条件膨胀。该函数基于标记图像的像素值将膨胀过程限制在特定区域。

The mahotas.cdilate() function

Mahotas 中的 cdilate() 函数有两个输入−输入图像和掩码(条件)数组。它根据提供的条件对输入图像执行条件膨胀,并返回生成的膨胀图像。

掩码用于识别图像中的特定区域。它们充当过滤器,突出显示某些区域,同时忽略其他区域。

Syntax

以下是 cdilate() 函数的基本语法 −

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

其中,

  1. f − 它是将要进行条件膨胀的输入图像。

  2. g − 它是将在条件膨胀期间应用的掩模。

  3. Bc = {3×3 cross} (optional) − 它用于膨胀的结构元素。默认值为 {3×3 十字形}。

  4. n − 它是膨胀操作的迭代次数。默认情况下,它设置为 1,表示单个迭代。

Example

在以下示例中,我们通过增加像素强度在图像上执行条件膨胀 −

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

Using Structuring Element

要使用 Mahotas 中的结构元素执行条件膨胀,首先,我们需要定义膨胀的应用条件。例如,您可以基于像素强度指定一个条件或提供一个二进制掩模。

接下来,选择一个定义膨胀邻域的结构元素。Mahotas 提供了几个预定义的结构元素,如圆盘和正方形,您可以根据所需形状和大小进行选择。

最后,检索结果图像,它仅包含满足条件的膨胀效果。

Example

在此,我们尝试使用结构元素对图像执行条件膨胀 −

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

Using a Custom Condition Function

我们还可以使用自定义条件函数对图像执行条件膨胀。

为此,我们首先定义一个自定义条件函数来确定哪些像素应该进行膨胀。

接下来,选择一个结构元素来定义膨胀操作的形状和大小。

最后,执行条件膨胀并检索膨胀图像。

Example

现在,我们使用自定义条件函数膨胀图像 −

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