Mahotas 简明教程

Mahotas - Local Minima in Image

图像中的局部最小值是指像素强度在局部邻域内最低的区域。局部邻域仅包含像素的直接邻居;因此,在识别局部最小值时,它仅使用图像的一部分。

图像可以包含多个局部最小值,每个局部最小值都有不同的强度。发生这种情况是因为区域的强度可能低于其相邻区域,但它可能不是整个图像中强度最低的区域。

Local Minima in Image in Mahotas

在 Mahotas 中,我们可以使用 mahotas.locmin() 函数查找图像的局部最小值。局部最小值区域是使用区域最小值找到的,它指的是强度值比图像中所有相邻像素都低的区域。

The mahotas.locmin() function

“mahotas.locmin()”函数以图像为输入,并找到局部极小值区域。它返回一个二进制图像,其中每个局部极小值区域用 1 表示。该函数以以下方式在图像中找到局部极小值:

  1. 它首先对输入图像应用形态侵蚀,来寻找区域极小值点。

  2. 接下来,它将腐蚀后的图像与原始图像进行比较。如果原始图像中的像素强度较低,则该像素表示区域极小值。

  3. 最后,生成一个二进制数组,其中 1 表示局部极小值存在,其他位置表示不存在。

Syntax

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

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

其中,

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

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

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

Example

在以下示例中,我们使用 mh.locmin() 函数获取图像的局部极小值。

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 local minima
local_minima = mh.locmin(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 local minima
axes[1].imshow(local_minima, cmap='gray')
axes[1].set_title('Local Minima')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

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

local minima image

Using Custom Structuring Element

我们可以使用自定义结构元素来获取图像的局部极小值。结构元素是一个由 0 和 1 组成的、尺寸为奇数的二进制数组,定义了邻域像素的连通性模式。

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

在 mahotas 中,我们可以使用自定义结构元素来在提取局部极小值期间定义图像的邻近像素。它允许我们根据要求找到局部极小值区域。

要使用结构元素,需要将它传递给 locmin() 函数的 Bc 参数。

例如,让我们考虑自定义结构元素: [[1, 0, 0, 0, 1], [0, 1, 0, 1,0], [0, 0, 1, 0, 0], [0, 1, 0, 1, 1], [1, 0, 0, 1, 0]]. 该结构元素表示垂直、水平和对角线连通性。

这意味着对于图像中的每个像素,在提取局部极小值期间,只考虑其垂直、水平或对角线上方和下方的像素作为它的邻居。

Example

在下面的示例中,我们定义了一个自定义结构元素,以获取图像的局部极小值。

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)
# Setting custom structuring element
struct_element = np.array([[1, 0, 0, 0, 1],
[0, 1, 0, 1, 0],
[0, 0, 1, 0, 0],
[0, 1, 0, 1, 1],
[1, 0, 0, 1, 0]])
# Getting the local minima
local_minima = mh.locmin(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 local minima
axes[1].imshow(local_minima, cmap='gray')
axes[1].set_title('Local Minima')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

上述代码的输出如下:

local minima image1

Using a Binary Image

我们还可以在二进制图像中找到局部极小值。二进制图像是一种图像,其中每个像素用 1 或 0 表示,分别表示前景或背景。可以使用 numpy 库的 array() 函数创建二进制图像。

在 mahotas 中,我们可以使用 locmin() 函数来查找二进制图像的局部极小值区域。由于二进制图像仅包含 1 和 0,因此具有值 1 的像素将被视为局部极小值,因为前景像素的强度低于背景像素。

例如,假设从以下数组创建了二进制图像: [[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 0], [1, 0, 1, 0]]. 那么数组中 1 的数量将决定局部极小值区域的数量。因此,第一个数组将有 1 个局部极小值区域,第二个数组将有 2 个局部极小值区域,依此类推。

Example

在此,我们在二进制图像中获取局部极小值。

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Creating a binary image
binary_image = np.array([[0, 1, 0, 0],
[1, 0, 1, 0],
[0, 1, 0, 0],
[1, 0, 1, 0]], dtype=np.uint8)
# Getting the local minima
local_minima = mh.locmin(binary_image)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the binary image
axes[0].imshow(binary_image, cmap='gray')
axes[0].set_title('Binary Image')
axes[0].set_axis_off()
# Displaying the local minima
axes[1].imshow(local_minima, cmap='gray')
axes[1].set_title('Local Minima')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

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

binary image mahotas