Mahotas 简明教程

Mahotas - Regional Maxima of Image

区域极大值指的是图像中像素强度值最高的一个点。在图像中,形成区域极大值的区域是所有其他区域中最明亮的。区域极大值也被称为全局极大值。

Regional maxima refer to a point where the intensity value of pixels is the highest within an image. In an image, regions which form the regional maxima are the brightest amongst all other regions. Regional maxima are also known as global maxima.

区域极大值考虑了整个图像,而局部极大值仅考虑局部邻域,以找到强度最高的像素。

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

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

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

一个图像可以包含多个区域极大值,但所有区域极大值的强度都将相等。发生这种情况的原因是,仅最高强度值被视为区域极大值。

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

Regional Maxima of Image in Mahotas

在 Mahotas 中,我们可以使用 mahotas.regmax() 函数找到图像中的区域极大值。区域极大值通过图像中的强度峰值来识别,因为它们代表高强度区域。

In Mahotas, we can find the regional maxima in an image using the mahotas.regmax() function. Regional maxima are identified through intensity peaks within an image because they represent high intensity regions.

区域极大值点突出显示为白色,而其他点则以黑色显示。

The regional maxima points are highlighted as white while other points are colored in black.

The mahotas.regmax() function

mahotas.regmax()函数从输入灰度图像中提取区域极大值。

The mahotas.regmax() function extracts regional maxima from an input grayscale image.

它输出的图像中,1 表示存在区域极大值点,而 0 表示正常点。

It outputs an image where the 1’s represent presence of regional maxima points and 0’s represent normal points.

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

The regmax() function uses a morphological reconstruction−based approach to find the regional maxima. In this approach, the intensity value of each local maxima region is compared with its neighbors.

如果发现某个相邻点的强度更高,它将成为新的区域极大值。此过程一直持续到没有更高强度的区域为止,这表示已达到区域极大值。

If a neighbor is found to have a higher intensity, it becomes the new regional maxima. This process continues until there no region of higher intensity is left, indicating that the regional maxima has been reached.

Syntax

以下是 mahotas 中 regmax() 函数的基本语法:

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

mahotas.regmax(f, Bc={3x3 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.regmax() 函数获取了图像的区域极大值。

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

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)
# Getting the regional maxima
regional_maxima = mh.regmax(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 maxima
axes[1].imshow(regional_maxima, cmap='gray')
axes[1].set_title('Regional Maxima')
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 −

regional maxima image mahotas

Using Custom Structuring Element

我们还可以使用自定义结构元素从图像中获取区域极大值。结构元素是一个由 1 和 0 组成的一维数组,它定义了图像标记时邻近像素的连接模式。

We can also use a custom structuring element to get the regional maxima from an image. A structuring element is a binary array of odd dimensions consisting of ones and zeroes that defines the connectivity pattern of the neighborhood pixels during image labeling.

一指示包含在连接分析中的邻域像素,而零表示排除或忽略的邻域像素。

The ones indicate the neighboring pixels that are included in the connectivity analysis, while the zeros represent the neighbors that are excluded or ignored.

在 mahotas 中,在提取区域极大值区域时,我们可以使用自定义结构元素来定义相邻像素的连接。我们首先使用 numpy.array() 函数创建一个一维结构元素。

In mahotas, while extracting regional maxima regions we can use a custom structuring element to define the connectivity of neighboring pixels. We do this by first creating an odd dimension structuring element using the numpy.array() function.

然后,我们将此自定义结构元素输入到 regmax() 函数的 Bc 参数中。

Then, we input this custom structuring element to the Bc parameter in the regmax() function.

例如,考虑自定义结构化元素: [[0, 0, 0, 0, 1], [0, 0, 1, 0, 0], [1, 0, 0, 0, 0], [0, 0, 0, 1, 0], [0, 1, 0, 0, 0]]. 此结构化元素暗示水平连接,即,仅水平向左或向右的像素才被认为是其邻域。

For example, let’s consider the custom structuring element: [[0, 0, 0, 0, 1], [0, 0, 1, 0, 0], [1, 0, 0, 0, 0], [0, 0, 0, 1, 0], [0, 1, 0, 0, 0]]. This structuring element implies horizontal connectivity, i.e., only the pixels horizontally left or right of another pixel are considered its neighbors.

Example

在这个示例中,我们使用自定义结构化元素以获取图像的局部最大值。

In this example, we are using a custom structuring element to get the regional maxima 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)
# Setting custom structuring element
struct_element = np.array([[0, 0, 0, 0, 1],
[0, 0, 1, 0, 0],
[1, 0, 0, 0, 0],
[0, 0, 0, 1, 0],
[0, 1, 0, 0, 0]])
# Getting the regional maxima
regional_maxima = mh.regmax(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 maxima
axes[1].imshow(regional_maxima, cmap='gray')
axes[1].set_title('Regional Maxima')
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 −

custom structuring element

Using a Specific Region of an Image

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

We can also find the regional maxima 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 轴所需的维数来裁剪原始图像。然后,我们使用裁剪后的图像并使用 regmax() 函数获取区域最大值。

In mahotas, we can find the regional maxima within a portion of an image. First, we crop the original image by specifying the required dimensions of the x and y axis. Then we use the cropped image and get the regional maxima using the regmax() function.

例如,假设我们分别将 [:800, 70:] 指定为 x 和 y 轴的维数。那么,裁剪后的图像将处于 x 轴的 0 到 800 像素范围和 y 轴的最大维数范围 70 内。

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

Example

在这个示例中,我们正在获取图像的特定区域内的局部最大值。

In this example, we are getting the regional maxima 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('nature.jpeg')
# Converting it to grayscale
image = mh.colors.rgb2gray(image)
# Using specific region of the image
image = image[:800, 70:]
# Getting the regional maxima
regional_maxima = mh.regmax(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 maxima
axes[1].imshow(regional_maxima, cmap='gray')
axes[1].set_title('Regional Maxima')
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