Mahotas 简明教程

Mahotas - Highlighting Image Maxima

重点突出图像极大值是指显示图像的最亮区域。图像极大值,也称为区域极大值,是指图像所有其他区域中像素强度值最高的区域。

图像最大值在搜索最亮区域时考虑整个图像。图像可以有多个区域最大值,但它们全部具有相同的亮度水平。这是因为只有最亮的值被视为图像最大值。

Highlighting Image Maxima in Mahotas

在 Mahotas 中,我们可以使用函数 mahotas.regmax() 来突出显示图像中的最大值。图像最大值表示高强度区域;因此它们通过查看图像的强度峰来识别。以下是函数用来突出显示图像最大值的基本方法:

  1. 首先,它将每个局部最大值区域的强度值与其邻域进行比较。

  2. 如果找到更亮的邻域,函数将其设置为新的图像最大值。

此过程一直持续到将所有区域与图像最大值进行比较。

The mahotas.regmax() function

mahotas.regmax() 函数将灰度图像作为输入。它返回一个图像,其中 1 表示图像最大值点,而 0 表示常规点。

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

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

其中,

  1. f − 它是输入的灰度图像。

  2. Bc (optional) − 这是用于连接性的结构元素。

  3. out(optional) - 它是布尔数据类型的输出数组(默认为与 f 大小相同的数组)。

在以下示例中,我们使用 mh.regmax() 函数突出显示图像最大值。

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)
# 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 highlighted image 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()

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

image maxima

Highlighting Maxima by using custom Structuring Element

我们也可以使用自定义结构化元素来突出显示图像最大值。结构化元素是一种仅由 1 和 0 构成的阵列。它定义相邻像素的连通模式。

值等于 1 的像素包含在连通分析中,而值等于 0 的像素则被排除在外。

在 mahotas 中,我们使用 mh.disk() 函数创建自定义结构化元素。然后,我们将此自定义结构化元素作为 regmax() 函数中的 Bc 参数来突出显示图像最大值。

Example

在此示例中,我们使用自定义结构化元素突出显示图像最大值。

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)
# Creating a custom structuring element
se = np.array([[0, 1, 0],[1, 1, 1],[0, 1, 0]])
# Getting the regional maxima
regional_maxima = mh.regmax(image, Bc=se)
# 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 highlighted image 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()

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

image maxima1