Mahotas 简明教程

Mahotas - Regional Minima of Image

图像中的区域极小值是指像素强度值最低的点。在图像中,构成区域极小值的区域在所有其他区域中是最暗的。区域极小值也称为全局极小值。

Regional minima in an image refer to a point where the intensity value of pixels is the lowest. In an image, regions which form the regional minima are the darkest amongst all other regions. Regional minima are also known as global minima.

区域极小值考虑整个图像,而局部极小值仅考虑局部邻域,以查找强度最低的像素。

Regional minima consider the entire image, while local minima only consider a local neighborhood, to find the pixels with lowest intensity.

区域极小值是局部极小值的一个子集,因此所有区域极小值都是局部极小值,但并非所有局部极小值都是区域极小值。

Regional minima are a subset of local minima, so all regional minima are a local minima but not all local minima are regional minima.

图像可以包含多个区域极小值,但所有区域极小值强度均相等。这是因为区域极小值仅考虑最低强度值。

An image can contain multiple regional minima but all regional minima will be of equal intensity. This happens because only the lowest intensity value is considered for regional minima.

Regional Minima of Image in Mahotas

在 Mahotas 中,我们可以使用 mahotas.regmin() 函数找到图像中的区域极大值。区域极小值通过图像中的强度谷识别出来,因为它们表示低强度区域。

In Mahotas, we can use the mahotas.regmin() function to find the regional maxima in an image. Regional minima are identified through intensity valleys within an image because they represent low intensity regions.

区域极小值点在黑色背景上突出显示为白色,代表正常点。

The regional minima points are highlighted as white against black background, which represents normal points.

The mahotas.regmin() function

mahotas.regmax() 函数从输入灰度图像中获取区域极小值。它输出一个图像,其中 1 表示区域极小值点,0 表示正常点。

The mahotas.regmax() function gets regional minima from an input grayscale image. It outputs an image where the 1’s represent presence of regional minima points and 0’s represent normal points.

regmin() 函数使用基于形态学重建的方法来查找区域极小值。在此,每个局部极小值区域的强度值与其相邻的局部极小值区域进行比较。

The regmin() function uses a morphological reconstruction−based approach to find the regional minima. Here, the intensity value of each local minima region is compared with its neighboring local minima regions.

如果发现邻居的强度较低,则它将成为新的区域极小值。此过程将持续到没有强度较低的区域为止,表明已经识别出区域极小值。

If a neighbor is found to have a lower intensity, it becomes the new regional minima. This process continues until no region of lower intensity is left, indicating that the regional minima has been identified.

Syntax

以下是 mahotas 中 regmin() 函数的基本语法 -

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

mahotas.regmin(f, Bc={3×3 cross}, out={np.empty(f.shape, bool)})

其中,

Where,

  1. f − It is the input grayscale image.

  2. Bc (optional) − It is the structuring element used for connectivity.

  3. out(optional) − It is the output array of Boolean data type (defaults to new array of same size as f).

Example

在以下示例中,我们使用 mh.regmin() 函数获取图像的区域极小值。

In the following example, we are getting the regional minima of an image using mh.regmin() function.

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('sea.bmp')
# Converting it to grayscale
image = mh.colors.rgb2gray(image)
# Getting the regional minima
regional_minima = mh.regmin(image)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original image
axes[0].imshow(image, cmap='gray')
axes[0].set_title('Original Image')
axes[0].set_axis_off()
# Displaying the regional minima
axes[1].imshow(regional_minima, cmap='gray')
axes[1].set_title('Regional Minima')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

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

Following is the output of the above code −

minima image

Using Custom Structuring Element

我们还可以使用自定义结构元素从图像中获取区域极小值。

We can also use a custom structuring element to get the regional minima from an image.

在 mahotas 中,当从图像中获取区域极小值时,我们可以使用自定义结构元素来定义相邻像素的连接方式。我们可以使用它根据需要获取结果图像。可以通过将结构元素传递给 regmin() 函数中的 Bc 参数来完成。

In mahotas, while getting regional minima from an image we can use a custom structuring element to define how neighboring pixels are connected. We can use this to get a resultant image as per our need. It can by done by passing the structuring element to the Bc parameter in the regmin() function.

例如,让我们考虑自定义结构元素: [[1, 1, 1, 1, 0], [1, 1, 0, 1,1], [0, 1, 1, 1, 1], [1, 1, 1, 0, 1], [1, 0, 1, 1, 1]]. 。此结构元素表示水平和垂直连通性。这意味着只有水平方向的左右两侧以及垂直方向的上下两侧与另一个像素相邻的像素被认为是该像素的邻居。

For example, let’s consider the custom structuring element: [[1, 1, 1, 1, 0], [1, 1, 0, 1,1], [0, 1, 1, 1, 1], [1, 1, 1, 0, 1], [1, 0, 1, 1, 1]]. This structuring element implies horizontal and vertical connectivity. This means that only the pixels horizontally left or right and vertically above or below another pixel are considered its neighbors.

Example

在这里,我们使用自定义结构元素来获取图像的区域极小值。

Here, we are using a custom structuring element to get the regional minima of an image.

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('tree.tiff')
# Converting it to grayscale
image = mh.colors.rgb2gray(image)
# Setting custom structuring element
struct_element = np.array([[1, 1, 1, 1, 0],[1, 1, 0, 1, 1],
[0, 1, 1, 1, 1],[1, 1, 1, 0, 1],[1, 0, 1, 1, 1]])
# Getting the regional minima
regional_minima = mh.regmin(image, Bc=struct_element)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original image
axes[0].imshow(image, cmap='gray')
axes[0].set_title('Original Image')
axes[0].set_axis_off()
# Displaying the regional minima
axes[1].imshow(regional_minima, cmap='gray')
axes[1].set_title('Regional Minima')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

上述代码的输出如下:

Output of the above code is as follows −

structuring element image mahotas

Using a Specific Region of an Image

我们还可以查找图像的特定区域的局部最小值。图像的特定区域是指较大图像的一小部分。可以通过裁剪原始图像去除不必要的区域来提取特定区域。

We can also find the regional minima of a specific region of an image. A specific region of an image refers to a small part of a larger image. The specific region can be extracted by cropping the original image to remove unnecessary areas.

在 Mahotas 中,我们可以使用图像的一部分并获取其局部最小值。首先,通过提供 x 和 y 轴的尺寸从原始图像获取特定区域。

In mahotas, we can use a portion of an image and get its regional minima. First, we get the specific area from the original image by providing dimensions of the x and y axis.

然后,我们使用裁剪的图像并使用 regmin() 函数获取局部最小值。

Then we use the cropped image and get the regional minima using the regmin() function.

例如,假设我们分别指定 [:900, :800] 为 x 和 y 轴的尺寸。然后,特定区域将处于 x 轴的 0 到 900 像素和 y 轴的 0 到 800 像素的范围内。

For example, let’s say we specify [:900, :800] as the dimensions of x and y axis respectively. Then, the specific region will be in range of 0 to 900 pixels for x−axis and 0 to 800 pixels for y−axis.

Example

在下面提到的示例中,我们正在获取图像特定区域内的局部最小值。

In the example mentioned below, we are getting the regional minima within a specific region of an image.

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('sun.png')
# Converting it to grayscale
image = mh.colors.rgb2gray(image)
# Using specific regions of the image
image = image[:900, :800]
# Getting the regional minima
regional_minima = mh.regmin(image)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original image
axes[0].imshow(image, cmap='gray')
axes[0].set_title('Original Image')
axes[0].set_axis_off()
# Displaying the regional minima
axes[1].imshow(regional_minima, cmap='gray')
axes[1].set_title('Regional Minima')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

执行上面的代码后,我们得到以下输出: -

After executing the above code, we get the following output −

specific region image mohatas