Mahotas 简明教程
Mahotas - Conditional Eroding Image
在上一章中,我们探讨了图像腐蚀的概念,这是一个用于将图像中区域的边界缩小至所有像素的操作。另一方面,条件腐蚀根据一些条件缩小(去除)某些特定区域的像素。
In our previous chapter, we explored the concept of image erosion, an operation used to shrink all the pixels to the boundaries of regions in an image. Conditional erosion on the other hand, shrinks (remove) the pixels of certain specific regions, based on some conditions.
条件可以基于图像像素的强度值或图像中某些特定模式。
The condition can be based on the intensity values of the image pixels or some specific pattern in the image.
例如,我们考虑一张灰度图像。你可以有条件地只腐蚀满足某个强度阈值的那些像素,而不是腐蚀所有前景色素。
For example, let’s consider a grayscale image. Instead of eroding all the foreground pixels, you could conditionally erode only those pixels that meet a certain intensity threshold.
如果一个像素的强度低于某个预定义的阈值,则应用腐蚀;否则,像素保持不变。
If a pixel’s intensity is below a predefined threshold, then erosion is applied; otherwise, the pixel remains unchanged.
Conditional Eroding Image in Mahotas
在 mahotas 中,条件腐蚀是传统腐蚀操作的扩展,它包括基于另一个图像(通常称为“标记图像”)的条件。
In mahotas, conditional erosion is an extension of the traditional erosion operation that comprises a condition based on a second image, often referred to as the "marker image."
它允许你控制腐蚀过程,使得腐蚀仅发生在标记图像具有特定像素值的位置。
It allows you to control the erosion process such that erosion only occurs at locations where the marker image has specific pixel values.
我们可以使用 cerode() 函数对 mahotas 中的图像执行条件腐蚀。此函数将腐蚀过程限制在基于标记图像的像素值特定区域内。
We can perform the conditional erosion on an image in mahotas using the cerode() function. This function restricts the erosion process to specific regions based on the marker image’s pixel values.
The mahotas.cerode() function
Mahotas 中的 cerode() 函数采用两个输入——输入图像和掩码(条件)数组。它基于提供的条件对输入图像执行条件腐蚀,并返回生成的腐蚀图像。
The cerode() function in Mahotas takes two inputs− the input image and a mask (condition) array. It performs conditional erosion on the input image based on the provided condition, and it returns the resulting eroded image.
掩码用于识别图像中的特定区域。它们充当过滤器,突出显示某些区域,同时忽略其他区域。
Masks are used to identify specific regions within an image. They act as a filter that highlights certain areas while disregarding others.
Syntax
以下是 mahotas 中 cerode() 函数的基本语法 −
Following is the basic syntax of the cerode() function in mahotas −
mahotas.cerode(f, g, Bc={3x3 cross}, out={np.empty_as(A)})
其中,
Where,
-
f − It is the input image on which the conditional erosion is to be performed.
-
g − It is the mask to be applied during conditional erosion.
-
Bc={3×3 cross} (optional) − It is a structuring element used for dilation. Default is {3×3 cross}.
-
out={np.empty_as(A)} (optional) − It provides an output array to store the result of the erosion operation
Example
在以下示例中,我们通过降低像素强度来对图像执行条件腐蚀 −
In the following example, we are performing the conditional erosion on an image by scaling down its pixel intensity −
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()
执行上面的代码后,我们得到以下输出: -
After executing the above code, we get the following output −
Using Structuring Element
为了使用 Mahotas 中的结构元素执行条件腐蚀,首先我们需要定义腐蚀应用的条件。例如,你可以基于像素强度指定条件或提供一个二值掩码。
To perform conditional erosion using structuring element in Mahotas, first, we need to define the condition based on which the erosion will be applied. For example, you can specify a condition based on pixel intensity or provide a binary mask.
接下来,选择一个定义腐蚀邻域的结构元素。Mahotas 提供了几个预定义的结构元素,例如圆盘和正方形,你可以基于所需的形状和大小进行选择。
Next, choose a structuring element that defines the neighborhood for erosion. Mahotas provides several pre−defined structuring elements, such as disks and squares, which you can select based on the desired shape and size.
最后,检索结果图像,它将仅在满足条件的地方包含腐蚀效果。
Finally, retrieve the resulting image, which will contain the erosion effects only where the condition is satisfied.
Example
在这里,我们尝试使用结构元素对图像执行条件腐蚀 −
In here, we are trying to perform conditional erosion on an image using structuring elements −
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()
获得的输出如下所示 −
The output obtained is as shown below −
Using a Custom Condition Function
我们还可以使用自定义条件函数对图像执行条件腐蚀。
We can also perform conditional erosion on an image using a custom condition function.
若要实现这一点,我们首先定义一个自定义条件函数来确定哪些像素应该经过腐蚀。
To achieve this, we first define a custom condition function that determines which pixels should undergo erosion.
接下来,选择一个结构元素来定义腐蚀操作的形状和大小。最后,执行条件腐蚀并获取腐蚀图像。
Next, choose a structuring element to define the shape and size of erosion operation. Finally, perform the conditional erosion and retrieve the eroded image.
Example
现在,我们使用自定义条件函数膨胀图像 −
Now, we are dilating an image using a custom condition function −
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()
以下是上面代码的输出: -
Following is the output of the above code −