Mahotas 简明教程

Mahotas - Closing Process on Image

闭运算和开运算是相反的过程。在此过程中,先执行膨胀运算,然后执行腐蚀运算。膨胀检查每个像素及其相邻像素。

The closing process is reverse of the opening process. In this, first a dilation operation is performed, followed by an erosion operation. Dilation examines each pixel and its neighboring pixels.

如果任何相邻像素为白色,中心像素也将变为白色。此步骤有助于扩展或加粗图像,并且可以填补小间隙或孔洞。

If any neighboring pixel is white, the center pixel is also turned white. This step helps expand or thicken the image and can fill in small gaps or holes.

在膨胀之后,腐蚀检查每个像素及其相邻像素。如果任何相邻像素为黑色,中心像素也将变为黑色。此步骤有助于收缩或消除不需要的细节,例如细小突起和内部噪声。

After dilation, erosion examines each pixel and its neighboring pixels. If any neighboring pixel is black, the center pixel is also turned black. This step helps shrink or remove unwanted details, such as thin protrusions and internal noise.

闭运算有效地闭合了二值图像中的小孔或间隙,并平滑或消除了前景区域。

The closing operation is effectively closes small holes or gaps in binary images, and smoothens or eliminates the small foreground regions.

Closing Process on Image in Mahotas

要在 Mahotas 中执行闭运算,我们使用 mahotas.close() 函数。此方法允许顺序应用膨胀和腐蚀运算。

To perform the closing process in Mahotas, we use the mahotas.close() function. This method allows for the sequential application of dilation and erosion operations.

膨胀通过用邻居中的最大值替换每个像素来扩展剩余结构,同时保留其关键特征。

Dilation expands the remaining structures while preserving their key features by replacing each pixel with the maximum value in its neighborhood.

随后,侵蚀通过考虑每个像素的邻域并用最小值替换它来减少噪声并消除小结构。

Subsequently, erosion reduces noise and eliminates small structures by considering each pixel’s neighborhood and replacing it with the minimum value.

The mahotas.close() function

Mahotas 中的 close() 函数采用两个主要参数− 二进制图像和结构元素(内核)。该函数首先对输入二进制图像执行膨胀操作。然后,它对膨胀的图像执行侵蚀操作。

The close() function in Mahotas takes two main arguments− the binary image and the structuring element (kernel). The function first applies a dilation operation to the input binary image. It then applies an erosion operation to the dilated image.

执行膨胀和侵蚀操作后,close() 函数返回结果闭合图像。

The close() function returns the resulting closed image after the dilation and erosion operations have been performed.

Syntax

以下是 mahotas 中 close() 函数的基本语法 −

Following is the basic syntax of the close() function in mahotas −

mahotas.close(f, Bc={3x3 cross}, out={np.empty_like(f)})

其中,

Where,

  1. f − It is the input binary image represented as a NumPy array.

  2. Bc − It is the structuring element used for both the dilation and erosion operations. Default is 3x3 cross-shaped structuring element.

  3. out − It is the output array. The result will be stored in a new array with the same shape and data type as the input image f.

Example

在以下示例中,我们对图像执行闭合过程 −

In the following example, we are performing the closing process on an image −

import mahotas as mh
import numpy as np
import matplotlib.pyplot as plt
image = mh.imread('sun.png')
closed_image = mh.close(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 closed image
axes[1].imshow(closed_image, cmap='gray')
axes[1].set_title('Closed 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 −

closing process image

Using Random Binary Image

我们还可以通过创建一个随机二进制图像对图像执行闭合过程。为实现此目的,我们首先使用 NumPy 创建一个随机二进制图像,其中像素为 0(背景)或 1(前景)。

We can also aperform closing process on an image by creating a random binary image. To achieve this, first we create a random binary image using NumPy, where the pixels are either 0 (background) or 1 (foreground).

一旦有了二进制图像,我们就可以使用结构元素执行形态学闭合。它将定义膨胀和侵蚀操作的邻域的形状和大小。

Once we have our binary image, we can proceed to perform morphological closing by using a structuring element. It will define the shape and size of the neighborhood for the dilation and erosion operations.

此外,我们创建一个形状与输入图像相同的空数组来存储结果。最后,我们得到最终的闭合图像。

Additionally, we create an empty array with the same shape as the input image to store the result. Finally, we get the resultant closed image.

Example

在这里,我们尝试通过创建一个随机二进制图像对图像执行闭合过程 −

In here, we are trying to perform closing process on an image by creating a random binary image −

import mahotas as mh
import numpy as np
from pylab import imshow, show
# Create a random binary image
image = np.random.randint(0, 2, size=(100, 50), dtype=np.bool_)
Bc=np.ones((3,3))
# Perform morphological closing with a 3x3 cross structuring element
result = mh.close(image, Bc, out=np.empty_like(image))
# Show the result
imshow(result)
show()

下面显示了产生的输出:

The output produced is as shown below −

using random binary image

Using a Grayscale Image

我们还可以在 mahotas 中对灰度图像执行闭合过程。为实现此目的,我们只需在读取图像时传递 as_grey=True 参数,确保将图像加载为灰度图像。接下来,我们将在灰度图像上执行形态学闭合。

We can also perform closing process on a grayscale image in mahotas. To achive this, we simply pass as_grey=True parameter while reading the image, ensuring that the image is loaded as a grayscale image. Next, we will perform morphological closing on the grayscale image.

Example

现在,我们在 mahotas 中对灰度图像执行闭合过程 −

Now, we are performing the closing process on a grayscale image in mahotas −

import mahotas as mh
import numpy as np
from pylab import imshow, show
# Create a grayscale image
image = mh.imread('nature.jpeg', as_grey = True).astype(np.uint8)
Bc=np.ones((20,15))
# Perform morphological closing with a 3x3 cross structuring element
result = mh.close(image, Bc, out=np.empty_like(image))
# Show the result
imshow(result)
show()

以下是上面代码的输出: -

Following is the output of the above code −

grayscale image mahotas