Mahotas 简明教程
Mahotas - Dilating an Image
图像处理中对图像进行膨胀是指扩展图像的像素。
Dilating an image in image processing refers to expanding the pixels of an image.
膨胀过程将在图像边缘周围添加像素。这是因为该算法查看图像中的每个像素并检查其相邻像素。
The dilation process will add pixels around the edges of an image. This happens because the algorithm looks at each pixel in the image and checks its neighboring pixels.
如果任何相邻像素是对象的一部分,它便将这些像素添加到对象的边界。
If any of the neighboring pixels are part of an object, it adds those pixels to the object’s boundary.
Dilating an Image in Mahotas
通过在 Mahotas 中对图像进行膨胀,我们是将像素数量添加到图像中区域的边界。此操作通常用于增强或修改图像中的形状和结构。
By dilating an image in Mahotas, we refer to adding the number of pixels to the boundaries of regions in an image. This operation is commonly used to enhance or modify the shapes and structures in an image.
我们可以使用 dilate() 函数在 mahotas 中对图像进行膨胀。它用于使用结构元素 B 扩展元素 A。
We can dilate an image in mahotas using the dilate() function. It is used to expand an element A by using structuring element B.
结构元素是一个可以定义每个像素周围邻域的小矩阵或形状。用于确定在膨胀过程中应该考虑的像素。
A structuring element is a small matrix or shape that defines the neighborhood around each pixel. It is used to determine which pixels should be considered during the dilation process.
The mahotas.dilate() function
mahotas.dilate() 函数将输入图像和结构元素作为参数,并返回一个新的 Numpy 数组。
The mahotas.dilate() function takes the input image and the structuring element as arguments, and returns a new Numpy array.
输出像素的值由邻域中所有像素的最大值决定。如果任何相邻像素值为 1,则输出像素被设为 1。
The value of the output pixel is determined as the maximum value of all the pixels in the neighborhood. If any neighboring pixels have a value of 1, the output pixel is set to 1.
dilate() 函数按像素逐行扫描图像,并检查由结构元素定义的邻域。
The dilate() function scans the image, pixel by pixel, and checks the neighborhood defined by the structuring element.
如果任何相邻像素属于某个对象,则膨胀操作会将那些像素添加到该对象的边界上,使得它的体积更大。
If any neighboring pixels are part of an object, the dilation operation adds those pixels to the object’s boundary, making it larger.
Syntax
以下是 mahotas 中 dialte() 函数的基本语法:
Following is the basic syntax of the dilate() function in mahotas −
mahotas.dilate(A, Bc=None, out=None, output=None)
其中,
where,
-
A − It is the input image on which dilation will be performed. It can be a 2D or 3D NumPy array representing grayscale or binary image data.
-
Bc (optional) − It is the structuring element used for dilation. Default is None.
-
out (deprecated) / output (optional) − It specifies the output array to store the result. If not provided, a new array is created and returned as the output.
Example
以下是使用 dilate() 函数在 mahotas 中膨胀图像的基本示例:
Following is the basic example to dilate an image in mahotas using the dilate() function −
import mahotas as mh
import matplotlib.pyplot as plt
import numpy as np
image = mh.imread('nature.jpeg', as_grey=True).astype(np.uint8)
# Performing dilation with a square kernel of size 3x3
dilated_image = mh.dilate(image, Bc=mh.disk(3))
# 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 dilated image
axes[1].imshow(dilated_image, cmap='gray')
axes[1].set_title('Dilated 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 output as follows −
Dilation with Varying Structuring Element Sizes
我们还可以使用不同尺寸的结构元素来膨胀图像,以增强图像的不同方面。这些元素是使用 Mahotas disk() 函数以不同尺寸创建的。
We can also dilate an image using different structuring element sizes to enhance different aspects of the image. These elements are created with different sizes using the Mahotas disk() function.
通过为结构元素选择不同的半径或尺寸,我们可以实现不同的效果。首先,我们使用最大的结构元素执行膨胀。然后,我们使用较小的结构元素继续进行膨胀。
By choosing different radii or sizes for the structuring elements, we can achieve diverse effects. To begin, we perform the dilation using the largest structuring element. Then, we continue with additional dilations using smaller structuring elements.
这种方法允许我们以多种方式修改图像,增强特定特征并实现理想的视觉效果。
This approach allows us to modify the image in multiple ways, enhancing specific features and achieving the desired visual effects.
Example
我们在此尝试使用不同尺寸的结构元素膨胀图像:
In here, we are trying to dilate an image with varying structuring element sizes −
import mahotas as mh
import numpy as np
from pylab import imshow, show
image = mh.imread('nature.jpeg', as_grey=True).astype(np.uint8)
# Performing dilation with the largest structuring element
largest_se = mh.disk(8)
dilated_image = mh.dilate(image, Bc=largest_se)
# Performing additional dilations with smaller structuring elements
smaller_se_1 = mh.disk(2)
smaller_se_2 = mh.disk(5)
dilated_image = mh.dilate(dilated_image, Bc=smaller_se_1)
dilated_image = mh.dilate(dilated_image, Bc=smaller_se_2)
# Displaying the dilated image
imshow(dilated_image)
show()
获得的输出如下所示 −
The output obtained is as shown below −
Dilation with a Circular-shaped Kernel
若要创建圆形内核,我们可以使用 Mahotas 中的 disk() 函数。通过指定所需的半径,此函数会生成表示圆形内核的 NumPy 数组。
To create a circular−shaped kernel, we can use the disk() function from Mahotas. By specifying the desired radius, this function generates a NumPy array representing the circular kernel.
一旦我们准备好图像和圆形核,我们就可以进行膨胀了。此操作将圆形核应用于图像的每个像素,相应地扩展白色区域。
Once we have the image and circular kernel ready, we can proceed to perform dilation. This operation applies the circular kernel to each pixel of the image, expanding the white regions accordingly.
用更简单的术语来说,它增强了图像中的亮区,使它们更大。
In simpler terms, it enhances the bright areas in the image, making them larger.
Example
现在,我们正在使用圆形内核扩张图像 −
Now, we are dilating an image with a circular−shaped kernel −
import mahotas as mh
import numpy as np
from pylab import imshow, show
# Load image
image = mh.imread('sun.png', as_grey=True).astype(np.uint8)
# Circular kernel with radius 5
radius = 5
kernel = mh.disk(radius)
dilated_image = mh.dilate(image, kernel)
# Display the dilated image
imshow(dilated_image)
show()
以下是上面代码的输出: -
Following is the output of the above code −