Mahotas 简明教程

Mahotas - Opening Process on Image

开是指图像处理中使用的两步过程。首先,执行腐蚀操作,然后执行膨胀操作。

Opening is a two−step process used in image processing. First, an erosion operation is performed, followed by a dilation operation.

腐蚀通过检查每个像素及其相邻像素来收缩或去除不需要的细节。如果任何相邻像素是黑色的,则中心像素也会变成黑色。

Erosion shrinks or removes unwanted details by examining each pixel and its neighboring pixels. If any neighboring pixel is black, the center pixel is also turned black.

这一步有助于消除细小的突起和内部噪声。突起是从表面向外延伸的细长结构。

This step helps eliminate thin protrusions and internal noise. Protrusions are thin or elongated structures that extend outward from a surface.

在腐蚀之后,执行膨胀以扩展或加粗图像。它查看每个像素及其邻居,如果任何相邻像素为白色,则中心像素将变为白色。

After erosion, dilation is performed to expand or thicken the image. It looks at each pixel and its neighbors, and if any neighboring pixel is white, the center pixel is turned white.

开有效地去除了小的细节和噪声,同时保留了图像的主要结构。

Opening effectively removes small details and noise while preserving the main structures of the image.

Opening Process on Image in Mahotas

要在 Mahotas 中执行开过程,我们使用 mahotas.open() 函数。此方法允许顺序应用腐蚀和膨胀操作。

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

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

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

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

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

The mahotas.open() function

Mahotas 中的 open() 函数接受两个主要参数——二值图像和结构元素(内核)。此函数首先对输入二值图像应用腐蚀操作。然后将膨胀操作应用于腐蚀图像。

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

开操作返回在执行腐蚀和膨胀操作后得到的已打开图像。

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

Syntax

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

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

mahotas.open(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 erosion and dilation 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 opening process on an image −

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

opening process image

Using Random Binary Image

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

We can also perform opening 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 opening by using a structuring element. It will define the shape and size of the neighborhood for the erosion and dilation operations.

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

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

Example

在这里,我们尝试通过创建随机二进制图像对图像执行开运算 −

In here, we are trying to perform opening 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 opening with a 3x3 cross structuring element
result = mh.open(image, Bc, out=np.empty_like(image))
# Show the result
imshow(result)
show()

下面显示了产生的输出:

The output produced is as shown below −

random binary image

Using a Grayscale Image

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

We can also perform opening 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 opening on the grayscale image.

Example

现在,我们正在 mahotas 中对灰度图像执行开运算 −

Now, we are performing the opening 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 opening with a 3x3 cross structuring element
result = mh.open(image, Bc, out=np.empty_like(image))
# Show the result
imshow(result)
show()

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

Following is the output of the above code −

using grayscale image