Mahotas 简明教程

Mahotas - Local Maxima in an Image

局部最大值是图像中一个像素或特定区域,其强度或值高于其相邻像素或区域。它表示图像数据中的峰值或高点。

查找局部最大值的一种方法是执行局部邻域分析。对于图像中的每个像素,都会检查其邻域,如果该像素在其邻域内为最大值,则将其视为局部最大值。

Local Maxima in an Image in Mahotas

我们可以在 Mahotas 中使用 locmax() 函数找到图像中的局部最大值。此函数将图像作为输入,并返回一个二进制蒙版,其中局部最大值标记为 True 或 1。

Mahotas中的local_maxima()函数使用非最大值抑制算法来高效查找局部最大值。此函数通过检查每个像素及其邻域来确定该像素是否在其局部区域内是最大值。

该分析允许检测图像数据中的峰值或高点,这对于特征提取、对象检测和图像分割等各种应用而言都非常重要。

Using the locmax() Function

Mahotas中的locmax()函数用于高效识别输入图像中的局部最大值。它将灰度或单通道图像作为输入,并返回一个二进制掩码,其中局部最大值被标记为True或1。

Syntax

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

mahotas.Locmax(image_name)

其中, 'image_name' 是输入图像。

以下是一个查找图像中局部最大值的简单示例:

import mahotas as mh
import numpy as np
from pylab import imshow, show
import matplotlib.pyplot as plt
image = mh.imread('nature.jpeg', as_grey=True)
maxima = mh.locmax(image)
print("Maxima:", maxima)
imshow(maxima)
show()

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

Maxima: [[ True True True ... True True True]
[ True True True ... True True True]
[ True True True ... True True True]
...
[False False False ... False False False]
[False False False ... False False True]
[ True False True ... False False True]]

显示的图像如下所示:

local maxima image

Using the regmax() Function

我们还可以使用Mahotas中的regmax()函数在图像中查找区域最大值。区域最大值定义为图像中一个点,其强度值高于该点在指定区域内的所有相邻像素。

regmax()函数接受图像作为输入参数,并返回一个与输入图像大小相同的布尔图像。

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

regmax(image)

其中, 'image' 是要识别区域最大值的灰度或彩色图像。

Example

在此,我们尝试使用regmax()函数在灰度图像中的连通区域内查找区域最大值:

import mahotas as mh
def find_local_maxima(image):
   regional_maxima = mh.regmax(image)
   return regional_maxima
image = mh.imread('sun.png', as_grey=True)
local_maxima_points = find_local_maxima(image)
print(local_maxima_points)

上述代码的输出如下:

[[False False False ... False False False]
[False False False ... False False False]
[False False False ... False False False]
...
[False False False ... False False False]
[False False False ... False False False]
[False False False ... True False False]]

Regional Maxima of a Colored Image

我们还可以使用regmax()函数在 color 图像中的连通区域内查找区域最大值。

Example

现在,我们尝试使用regmax()函数在彩色图像中的连通区域内查找区域最大值:

import mahotas as mh
def find_local_maxima(image):
   regional_maxima = mh.regmax(image)
   return regional_maxima
image = mh.imread('tree.tiff')
local_maxima_points = find_local_maxima(image)
print(local_maxima_points)

我们得到了如下输出 −

[[[False False False]
[ True True True]
[False False False]
...
[False False False]
[False False False]
[False False False]]
.
.
.
[False False False]
[False False False]
[ True False False]]]