Mahotas 简明教程
Mahotas - Eroding Image
在图像处理中侵蚀图像指的是收缩图像的像素。
Eroding an image in image processing refers to shrinking the pixels of an image.
侵蚀过程将移除图像边缘周围的像素。此操作会扫描图像并检查某个像素邻域内的所有像素是否都是前景像素。如果是,则像素被侵蚀或移除。
The erosion process will remove pixels around the edges of an image. This operation scans the image and checks if all the pixels within the neighborhood of a particular pixel are foreground pixels. If they are, the pixel is eroded or removed.
Eroding an Image in Mahotas
通过在 Mahotas 中侵蚀图像,我们指的是移除图像中对象的边界或区域的像素数量。此操作通常用于修改图像中的形状和结构。
By eroding an image in Mahotas, we refer to removing the number of pixels to the boundaries of objects or regions in an image. This operation is commonly used to modify the shapes and structures in an image.
我们可以在 mahotas 中使用 erode() 函数侵蚀图像。它用于通过使用结构元素 B 来收缩元素 A。
We can erode an image in mahotas using the erode() function. It is used to shrink 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 erosion process.
The mahotas.erode() function
mahotas.erode() 函数获取输入图像和结构元素作为参数,并返回一个新的 Numpy 数组。
The mahotas.erode() function takes the input image and the structuring element as arguments, and returns a new Numpy array.
输出像素的值由邻域中所有像素的最小值确定。像素被设置为 0,如果任何邻域像素的值为 0。
The value of the output pixel is determined as the minimum value of all the pixels in the neighborhood. A pixel is set to 0, if any neighbourhood pixels have a value of 0.
erode() 函数按像素扫描图像,并检查由结构元素定义的邻域。
The erode() 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 erosion operation removes those pixels to the object’s boundary, making it smaller.
Syntax
以下是在 mahotas 中 erode() 函数的基本语法 −
Following is the basic syntax of the erode() function in mahotas −
mahotas.erode(A, Bc={3x3 cross}, out={np.empty_as(A)})
其中,
where,
-
A − It is the input image on which erosion will be performed. It should be a NumPy array representing a grayscale image.
-
Bc (optional) − It is the structuring element used for erosion. By default, it is set to a 3x3 cross−shaped structuring element.
-
out (optional) − It specifies the output array to store the result.
Example
以下是用 erode() 函数在 mahotas 中侵蚀图像的基本示例 −
Following is the basic example to erode an image in mahotas using the erode() 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 erosion with a square kernel of size 3x3
eroded_image = mh.erode(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 eroded image
axes[1].imshow(eroded_image, cmap='gray')
axes[1].set_title('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 output as follows −
Erosion with Varying Structuring Element Sizes
我们还可以使用不同结构元素尺寸来侵蚀图像,以修改图像的不同方面。这些元素使用 Mahotas disk() 函数以不同的尺寸创建。
We can also erode an image using different structuring element sizes to modify 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 erosion using the largest structuring element. Then, we continue with additional dilations using smaller structuring elements.
每次侵蚀操作都会通过从其边界中移除像素进一步缩小图像中的对象。
Each erosion operation further shrinks the objects in the image by removing pixels from their boundaries.
Example
在这里,我们尝试使用不同结构元素尺寸侵蚀图像 −
In here, we are trying to erode 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 erosion with the largest structuring element
largest_se = mh.disk(8)
eroded_image = mh.erode(image, Bc=largest_se)
# Performing additional erosions with smaller structuring elements
smaller_se_1 = mh.disk(2)
smaller_se_2 = mh.disk(5)
eroded_image = mh.erode(eroded_image, Bc=smaller_se_1)
eroded_image = mh.erode(eroded_image, Bc=smaller_se_2)
# Displaying the eroded image
imshow(eroded_image)
show()
获得的输出如下所示 −
The output obtained is as shown below −
Erosion 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 erosion. This operation applies the circular kernel to each pixel of the image, shrinking the foreground pixels accordingly.
简单来说,它会根据圆形内核定义的连接性缩小区域。
In simpler terms, it it shrinks the regions based on the connectivity defined by the circular kernel.
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 10
radius = 10
kernel = mh.disk(radius)
eroded_image = mh.erode(image, kernel)
# Display the eroded image
imshow(eroded_image)
show()
以下是上面代码的输出: -
Following is the output of the above code −