Mahotas 简明教程
Mahotas - Labeled Max Array
标记的最大数组是指存储 labeled image 中每个区域的最大强度值的数组。要找到区域的最大强度值,请检查该区域中的每个像素。然后,选择最亮像素的强度值作为最大强度值。简单来说,标记的最大数组用于找到图像中最亮的区域。
A labeled max array refers to an array that stores the maximum intensity value of each region in a labeled image. To find the maximum intensity value of a region, every pixel in that region is examined. Then, the intensity value of the brightest pixel is selected as the maximum intensity value. In simple terms, labeled max arrays are used to find the brightest regions of an image.
例如,假设我们有一个由三个像素组成的区域。这三个像素的强度值分别为 0.5、0.2 和 0.8。则该区域的最大强度值为 0.8。
For example, let’s assume we have a region which consists of three pixels. The intensity value of the three pixels is 0.5, 0.2, and 0.8 respectively. Then the maximum intensity value of the region will be 0.8.
Labeled Max Array in Mahotas
在 Mahotas 中,我们可以使用 mahotas.labeled.labeled_max() 函数创建一个标记的最大数组。该函数迭代搜索区域中最亮的像素。然后,它将最亮像素的强度值存储在数组中。
In Mahotas, we can use the mahotas.labeled.labeled_max() function to create a labeled max array. The function iteratively searches for the brightest pixel in a region. Then it stores the intensity value of the brightest pixel in an array.
结果数组是一个标记的最大数组,具有图像每个区域的最大强度值。
The resultant array is a labeled max array, having the maximum intensity value of each region of the image.
The mahotas.labeled.labeled_max() function
mahotas.labeled.labeled_max() 函数将图像和标记图像作为输入。它返回一个包含每个标记区域的最大强度值的数组。
The mahotas.labeled.labeled_max() function takes an image and a labeled image as inputs. It returns an array that contains the maximum intensity value of each labeled region.
以下是 mahotas 中 labeled_max() 函数的基本语法 -
Following is the basic syntax of the labeled_max() function in mahotas −
mahotas.labeled.labeled_max(array, labeled, minlength=None)
其中,
Where,
-
array − It is the input image.
-
labeled − It is the labeled image.
-
minlength (optional) − It specifies the minimum number of regions to include in the output array (default is None).
在以下示例中,我们使用 labeled_max() 函数在标记图像中查找标记的最大数组。
In the following example, we are finding the labeled max arrays in a labeled image using the labeled_max() function.
import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the images
image = mh.imread('sea.bmp')
# Converting it to grayscale
image = mh.colors.rgb2gray(image).astype(np.uint8)
# Applying thresholding
threshold = mh.thresholding.rc(image)
threshold_image = image > threshold
# Labeling the image
label, num_objects = mh.label(threshold_image)
# Getting the labeled max array
labeled_max = mh.labeled.labeled_max(image, label)
# Printing the labeled max array
print('Labeled max array:', labeled_max)
# 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 labeled image
axes[1].imshow(label, cmap='gray')
axes[1].set_title('Labeled Image')
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 −
Labeled max array: [107 111 129 ... 141 119 109]
获得的图像如下所示:
The image obtained is as follows −

Labeled Max Arrays of a Random Boolean Image
我们还可以找到随机布尔图像的标记最大数组。随机布尔图像是指每个像素值都为 0 或 1 的图像。前景色素用“1”表示,背景像素用“0”表示。
We can also find the labeled max arrays of a random Boolean image. A random boolean image refers to an image where each pixel has a value of either 0 or 1. The foreground pixels are represented by '1', and the background pixels are represented by '0'.
在 mahotas 中,要找到随机布尔图像的标记最大数组,我们首先需要使用 np.zeros() 函数生成特定大小的随机布尔图像。
In mahotas, to find the labeled max arrays of a random Boolean image, we first need to generate a random boolean image of a specific size using the np.zeros() function.
此图像最初仅包含背景像素。然后,我们将整数值分配给图像的某些部分以创建不同的区域。
This image initially consists of only background pixels. We then assign integer values to a few portions of the image to create distinct regions.
然后,我们使用 labeled_max() 函数查找图像的标记的最大数组。
Then, we find the labeled max arrays of the image using the labeled_max() function.
Example
在下面提到的示例中,我们寻找的是随机布尔图像的标注最大数组。
In the example mentioned below, we are finding the labeled max arrays of a random boolean image.
import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Creating a random image
image = np.zeros((10, 10), bool)
# Assigning values to the regions
image[:2, :2] = 1
image[4:6, 4:6] = 1
image[8:, 8:] = 1
# Labeling the image
label, num_objects = mh.label(image)
# Random sampling
random_sample = np.random.random_sample(image.shape)
# Getting the labeled max array
labeled_max = mh.labeled.labeled_max(random_sample, label)
# Printing the labeled max array
print('Labeled max array')
for i, intensity in enumerate(labeled_max):
print('Region', i, ':', intensity)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original image
axes[0].imshow(image)
axes[0].set_title('Original Image')
axes[0].set_axis_off()
# Displaying the labeled image
axes[1].imshow(label)
axes[1].set_title('Labeled Image')
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 −
Labeled max array
Region 0 : 0.9950607583625318
Region 1 : 0.8626363785944107
Region 2 : 0.6343883551171169
Region 3 : 0.8162320509314726
我们获得以下图像作为输出 −
We get the following image as output −
