Mahotas 简明教程

Mahotas - Local Maxima in an Image

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

A local maximum is a pixel or a particular region in an image that has a higher intensity or value than its neighboring pixels or regions. It represents a peak or a high point in the image data.

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

One way to find local maxima is by performing a local neighborhood analysis. For each pixel in the image, its neighborhood is examined, and if the pixel is the maximum within its neighborhood, it is considered a local maximum.

Local Maxima in an Image in Mahotas

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

We can find the local maxima in an image in Mahotas using the locmax() function. It takes an image as input and returns a binary mask where the local maxima are marked as True or 1.

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

The local_maxima() function in Mahotas uses a non-maximum suppression algorithm to locate the local maxima efficiently. By examining each pixel and its neighborhood, the function determines whether the pixel is the maximum within its local region.

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

This analysis allows for the detection of peaks or high points in the image data, which can play an important role for various applications such as feature extraction, object detection, and image segmentation.

Using the locmax() Function

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

The locmax() function in Mahotas is used to identify local maxima in an input image efficiently. It takes a grayscale or single channel image as input and returns a binary mask where the local maxima are marked as True or 1.

Syntax

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

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

mahotas.Locmax(image_name)

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

where, 'image_name' is the input image.

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

Following is the basic example to find the local maxima in an image −

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()

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

Following is the output of the above code −

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]]

显示的图像如下所示:

The image displayed is as shown below −

local maxima image

Using the regmax() Function

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

We can also use the regmax() function in Mahotas for finding regional maxima in an image. A regional maximum is defined as a point in the image that has a higher intensity value than all of its neighboring pixels within a specified region.

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

The regmax() function accepts an image as the input parameter and returns a boolean image of the same size as an input image.

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

Following is the basic syntax of regmax function in mahotas −

regmax(image)

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

where, 'image' is a grayscale or color image on which regional maxima need to be identified.

Example

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

In here, we are trying to find the regional maxima within the connected regions in a grayscale image using the regmax() function −

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)

上述代码的输出如下:

Output of the above code is as follows −

[[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 图像中的连通区域内查找区域最大值。

We can also find the regional maxima within the connected regions in a color image using the regmax() function.

Example

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

Now, we are trying to find the regional maxima within the connected regions in a color image using the regmax() function −

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)

我们得到了如下输出 −

We get the output as follows −

[[[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]]]