Mahotas 简明教程
Mahotas - Highlighting Image Maxima
重点突出图像极大值是指显示图像的最亮区域。图像极大值,也称为区域极大值,是指图像所有其他区域中像素强度值最高的区域。
Highlighting image maxima refers to displaying the brightest areas of an image. Image maxima, also known as regional maxima, is the area having the highest pixel intensity value amongst all other areas of an image.
图像最大值在搜索最亮区域时考虑整个图像。图像可以有多个区域最大值,但它们全部具有相同的亮度水平。这是因为只有最亮的值被视为图像最大值。
The image maxima consider the entire image when searching for the brightest areas. An image can have multiple regional maxima, but all of them will have the same level of brightness. This is because only the brightest value is considered as the image maxima.
Highlighting Image Maxima in Mahotas
在 Mahotas 中,我们可以使用函数 mahotas.regmax() 来突出显示图像中的最大值。图像最大值表示高强度区域;因此它们通过查看图像的强度峰来识别。以下是函数用来突出显示图像最大值的基本方法:
In Mahotas, we can use the mahotas.regmax() function to highlight maxima in an image. Image maxima represent high intensity regions; hence they are identified by looking at an image’s intensity peaks. Following is the basic approach used by the function to highlight the image maxima −
-
First, it compares the intensity value of each local maxima region to its neighbors.
-
If a brighter neighbor is found, the function sets it to be the new image maxima.
此过程一直持续到将所有区域与图像最大值进行比较。
This process continues until all the regions have been compared to the image maxima.
The mahotas.regmax() function
mahotas.regmax() 函数将灰度图像作为输入。它返回一个图像,其中 1 表示图像最大值点,而 0 表示常规点。
The mahotas.regmax() function takes a grayscale image as input. It returns an image where the 1s represent the image maxima points, while the 0s represent normal points.
以下是 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,
-
f − It is the input grayscale image.
-
Bc (optional) − It is the structuring element used for connectivity.
-
out(optional) − It is the output array of Boolean data type (defaults to new array of same size as f).
在以下示例中,我们使用 mh.regmax() 函数突出显示图像最大值。
In the following example, we are highlighting image maxima using the mh.regmax() function.
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()
以下是上面代码的输出: -
Following is the output of the above code −
Highlighting Maxima by using custom Structuring Element
我们也可以使用自定义结构化元素来突出显示图像最大值。结构化元素是一种仅由 1 和 0 构成的阵列。它定义相邻像素的连通模式。
We can also highlight the image maxima by using a custom structuring element. A structuring element is an array consisting of only ones and zeroes. It defines the connectivity pattern of the neighborhood pixels.
值等于 1 的像素包含在连通分析中,而值等于 0 的像素则被排除在外。
Pixels with the value 1 are included in connectivity analysis while the pixels with the value 0 are excluded.
在 mahotas 中,我们使用 mh.disk() 函数创建自定义结构化元素。然后,我们将此自定义结构化元素作为 regmax() 函数中的 Bc 参数来突出显示图像最大值。
In mahotas, we create a custom structuring element using the mh.disk() function. Then, we set this custom structuring element as the Bc parameter in the regmax() function to highlight the image maxima.
Example
在此示例中,我们使用自定义结构化元素突出显示图像最大值。
In this example, we are highlighting the image maxima by using a custom structuring element.
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()
执行上面的代码后,我们得到以下输出: -
After executing the above code, we get the following output −