Mahotas 简明教程
Mahotas - Local Minima in Image
图像中的局部最小值是指像素强度在局部邻域内最低的区域。局部邻域仅包含像素的直接邻居;因此,在识别局部最小值时,它仅使用图像的一部分。
Local minima in an image refers to regions where the pixel intensity is the lowest within a local neighborhood. A local neighborhood consists of only immediate neighbors of a pixel; hence it uses only a portion of an image when identifying local minima.
图像可以包含多个局部最小值,每个局部最小值都有不同的强度。发生这种情况是因为区域的强度可能低于其相邻区域,但它可能不是整个图像中强度最低的区域。
An image can contain multiple local minima, each having different intensities. This happens because a region can have lower intensity than its neighboring regions, but it may not be the region with lowest intensity in the entire image.
Local Minima in Image in Mahotas
在 Mahotas 中,我们可以使用 mahotas.locmin() 函数查找图像的局部最小值。局部最小值区域是使用区域最小值找到的,它指的是强度值比图像中所有相邻像素都低的区域。
In Mahotas, we can find the local minima of an image using the mahotas.locmin() function. The local minima regions are found using regional minima, which refers to regions having lower intensity values than all their neighboring pixels in an image.
The mahotas.locmin() function
“mahotas.locmin()”函数以图像为输入,并找到局部极小值区域。它返回一个二进制图像,其中每个局部极小值区域用 1 表示。该函数以以下方式在图像中找到局部极小值:
The 'mahotas.locmin()' function takes an image as input and finds the local minima regions. It returns a binary image where each local minima region is represented by 1. The function works in the following way to find local minima in an image −
-
It first applies a morphological erosion on the input image to find the regional minima points.
-
Next, it compares the eroded image with the original image. If the pixel intensity is lower in the original image, then the pixel represents a regional minima.
-
Finally, a binary array is generated where 1 corresponds to presence of local minima and 0 elsewhere.
Syntax
以下是 mahotas 中 locmin() 函数的基本语法:
Following is the basic syntax of the locmin() function in mahotas −
mahotas.locmin(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).
Example
在以下示例中,我们使用 mh.locmin() 函数获取图像的局部极小值。
In the following example, we are getting the local minima of an image using mh.locmin() 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 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()
以下是上面代码的输出: -
Following is the output of the above code −
Using Custom Structuring Element
我们可以使用自定义结构元素来获取图像的局部极小值。结构元素是一个由 0 和 1 组成的、尺寸为奇数的二进制数组,定义了邻域像素的连通性模式。
We can use a custom structuring element to get local minima of an image. A structuring element is a binary array of odd dimensions consisting of ones and zeroes that defines the connectivity pattern of the neighborhood pixels.
一指示包含在连接分析中的邻域像素,而零表示排除或忽略的邻域像素。
The ones indicate the neighboring pixels that are included in the connectivity analysis, while the zeros represent the neighbors that are excluded or ignored.
在 mahotas 中,我们可以使用自定义结构元素来在提取局部极小值期间定义图像的邻近像素。它允许我们根据要求找到局部极小值区域。
In mahotas, we can use custom structuring element to define the neighboring pixels of an image during local minima extraction. It allows us to find local minima regions as per our requirement.
要使用结构元素,需要将它传递给 locmin() 函数的 Bc 参数。
To use a structuring element, we need to pass it to the Bc parameter of the locmin() function.
例如,让我们考虑自定义结构元素: [[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]]. 该结构元素表示垂直、水平和对角线连通性。
For example, let’s consider the custom structuring element: [[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]]. This structuring element implies vertical, horizontal, and diagonal connectivity.
这意味着对于图像中的每个像素,在提取局部极小值期间,只考虑其垂直、水平或对角线上方和下方的像素作为它的邻居。
It means that for each pixel in the image, only the pixels vertically, horizontally or diagonally above and below it is considered its neighbor during local minima extraction.
Example
在下面的示例中,我们定义了一个自定义结构元素,以获取图像的局部极小值。
In the example below, we are defining a custom structuring element to get the local minima of an image.
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()
上述代码的输出如下:
Output of the above code is as follows −
Using a Binary Image
我们还可以在二进制图像中找到局部极小值。二进制图像是一种图像,其中每个像素用 1 或 0 表示,分别表示前景或背景。可以使用 numpy 库的 array() 函数创建二进制图像。
We can also find the local minima in a binary image. A binary image is an image where each pixel is represented by a 1 or a 0, indicating either a foreground or background respectively. Binary images can be created using the array() function from the numpy library.
在 mahotas 中,我们可以使用 locmin() 函数来查找二进制图像的局部极小值区域。由于二进制图像仅包含 1 和 0,因此具有值 1 的像素将被视为局部极小值,因为前景像素的强度低于背景像素。
In mahotas, we can use the locmin() function to find local minima regions of a binary image. Since binary images consists of only 1’s and 0’s, the pixels having a value 1 will be considered local minima since the intensity of foreground pixels is less than background pixels.
例如,假设从以下数组创建了二进制图像: [[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 0], [1, 0, 1, 0]]. 那么数组中 1 的数量将决定局部极小值区域的数量。因此,第一个数组将有 1 个局部极小值区域,第二个数组将有 2 个局部极小值区域,依此类推。
For example, let’s assume a binary image is created from the following array: [[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 0], [1, 0, 1, 0]]. Then the number of 1’s in an array will determine the number of local minima regions. Hence, the first array will have 1 local minima region, the second array will have 2 local minima regions and so on.
Example
在此,我们在二进制图像中获取局部极小值。
Here, we are getting the local minima in a binary image.
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()
执行上面的代码后,我们得到以下输出: -
After executing the above code, we get the following output −