Mahotas 简明教程

Mahotas - Morphological Operators

形态学算子是一组图像处理技术,有助于修改图像中对象的形状、大小和空间关系。它们就像用于对图像内的对象进行重新整形或处理的工具。

Morphological operators are a set of image processing techniques that help modify the shape, size, and spatial relationships of objects in an image. They are like tools used to reshape or manipulate the objects within an image.

想象一下,你有一幅包含各种对象(例如圆形、方形和线段)的图片。形态学算子允许你以不同的方式更改这些对象的形状。

Imagine you have a picture with various objects like circles, squares, and lines. Morphological operators allow you to change the appearance of these objects in different ways.

例如,你可以放大或缩小它们,平滑其边缘,移除细小的细节或填补缝隙。

For example, you can make them bigger or smaller, smooth their edges, remove tiny details, or fill in gaps.

为了实现这些变换,形态学算子使用涉及扫描图像的数学运算,扫描图像时使用一种称为结构元素的特殊图案。此结构元素定义在操作期间要考虑的每个像素周围的邻域形状和大小。

To achieve these transformations, morphological operators use mathematical operations that involve scanning the image with a special pattern called a structuring element. This structuring element defines the shape and size of the neighborhood around each pixel that will be considered during the operation.

Mahotas and Morphological Operators

Mahotas 提供了一套全面的形态学算子,这些算子可以应用于二值、灰度或多通道图像。这些算子使用经过优化的算法实现,使其适用于具有大规模数据集的实际应用程序。

Mahotas provides a comprehensive set of morphological operators that can be applied to binary, grayscale, or multichannel images. These operators are implemented efficiently using optimized algorithms, making them suitable for real−world applications with largescale datasets.

让我们看看可以在 mahotas 中执行的一些形态学操作 −

Let us look at few morphological operations which can be done in mahotas −

Dilation

膨胀是一种形态学操作,可扩展图像中对象边界上的像素。它涉及使用结构元素扫描图像,并用结构元素定义的对应邻域中最大值替换每个像素。

Dilation is a morphological operation that expands the pixels on the boundaries of objects in an image. It involves scanning the image with a structuring element and replacing each pixel with the maximum value within the corresponding neighborhood defined by the structuring element.

让我们看看下方的膨胀图像和原始图像 −

Let us look at the dilated image along with the original image below −

dilation image

Erosion

腐蚀是一种基础形态学算子,可收缩或腐蚀图像中对象边界上的像素。

Erosion is a basic morphological operator that shrinks or erodes the pixels on boundaries of objects in an image.

以下是腐蚀图像及其原始图像 −

Following is the eroded image along with its original image −

erosion image

Opening

开运算是一个腐蚀操作,后跟一个膨胀操作。它移除了小的对象,并填补了前景中的小孔,同时保留较大对象的整体形状和连通性。

Opening is a combination of erosion followed by dilation. It removes small objects and fills in small holes in the foreground while preserving the overall shape and connectivity of larger objects.

让我们看看 mahotas 中的开运算图像 −

Let us look at the opened image in mahotas −

opening image

Closing

闭运算与开运算相反,由膨胀操作后跟一个腐蚀操作组成。它填补了前景中的小间隙和小孔,确保了对象的连通性。

Closing is the reverse of opening, consisting of dilation followed by erosion. It fills small gaps and holes within the foreground, ensuring the connectivity of objects.

现在,让我们看看 mahotas 中的闭运算图像 −

Now, let us look at the closed image in mahotas −

closing image

Conditional Erosion

条件腐蚀是一种形态学操作,根据用户定义的条件保留图像中的结构。它有选择地腐蚀图像的区域,所述区域基于第二个称为标记图像的图像指定的条件。

Conditional erosion is a morphological operation that preserves structures in an image based on a user−defined condition. It selectively erodes regions of an image based on the condition specified by a second image called the marker image.

这项操作仅在标记图像具有非零值的位置侵蚀图像。

The operation erodes the image only where the marker image has non−zero values.

下图显示条件侵蚀 −

Following image shows conditional erosion −

conditional erosion image

Conditional Dilation

条件膨胀类似于条件侵蚀,但操作方式相反。它根据标记图像指定的条件有选择性地扩张图像区域。

Conditional dilation is similar to conditional erosion but operates in the opposite way. It selectively dilates regions of an image based on the condition specified by the marker image.

这项操作仅在标记图像具有非零值的位置膨胀图像。

The operation dilates the image only where the marker image has non−zero values.

下图显示条件膨胀 −

Following image shows conditional dilation −

conditional dilation image

Regional Maxima

区域最大值是指图像中在邻域内具有最高强度的点。它们表示图像中的局部峰值或显着特征。

Regional maxima are points in an image that have the highest intensity within their neighborhood. They represent local peaks or salient features in the image.

Mahotas 中的区域最大值操作符识别这些局部最大值并将其标记为前景像素。

The regional maxima operator in Mahotas identifies these local maxima and marks them as foreground pixels.

下图显示区域最大值 −

Following image represents regional maxima −

regional maxima image

Regional Minima

区域最小值是指图像中在邻域内具有最低强度的点。它们表示图像中的局部盆地或洼地。

Regional minima are points in an image that have the lowest intensity within their neighborhood. They represent local basins or depressions in the image.

Mahotas 中的区域最小值操作符识别这些局部最小值并将其标记为背景像素。

The regional minima operator in Mahotas identifies these local minima and marks them as background pixels.

下图显示区域最小值 −

Following image represents regional minima −

regional minima image

Example

在以下示例中,我们要执行上述所有形态学操作 −

In the following example, we are trying to perform all the above explained morphological operations −

import mahotas as mh
import matplotlib.pyplot as plt
import numpy as np
image = mh.imread('nature.jpeg', as_grey=True).astype(np.uint8)
# Dilation
dilated_image = mh.dilate(image, Bc=mh.disk(8))
plt.title("Dilated Image")
plt.imshow(dilated_image)
plt.show()
# Erosion
plt.title("Eroded Image")
eroded_image = mh.erode(image, Bc=mh.disk(8))
plt.imshow(eroded_image)
plt.show()
# Opening
plt.title("Opened Image")
opened_image = mh.open(image)
plt.imshow(opened_image)
plt.show()
# Closing
plt.title("Closed Image")
closed_image = mh.close(image)
plt.imshow(closed_image)
plt.show()
# Conditional Dilation
plt.title("Conditional Dilation")
g = image * 6
cdilated_image = mh.cdilate(image, g)
plt.imshow(cdilated_image)
plt.show()
# Conditional Erosion
plt.title("Conditional Erosion")
scaled_image = image * 0.5
scaled_image = scaled_image.astype(np.uint8)
ceroded_image = mh.cerode(image, scaled_image)
plt.imshow(ceroded_image)
plt.show()
# Regional Maxima
plt.title("Regional Maxima")
regional_maxima = mh.regmax(image)
plt.imshow(regional_maxima)
plt.show()
# Regional Minima
plt.title("Regional Minima")
regional_minima = mh.regmin(image)
plt.imshow(regional_minima)
plt.show()

获得的输出如下所示 −

The output obtained is as shown below −

Dilation:

Dilation:

dilation image1

Erosion:

Erosion:

erosion image1

Opened Image:

Opened Image:

opened image

Closed Image:

Closed Image:

closed image

Conditional Dilation:

Conditional Dilation:

conditional dilation image1

Conditional Erosion:

Conditional Erosion:

conditional erosion image1

Regional Maxima:

Regional Maxima:

regional maxima image1

Regional Minima:

Regional Minima:

regional minima image1

我们将在此部分的剩余章节中详细讨论所有形态学算子。

We will discuss about all the morphological operators in detail in the remaining chapters of this section.