Mahotas 简明教程

Mahotas - Labeled Max Array

标记的最大数组是指存储 labeled image 中每个区域的最大强度值的数组。要找到区域的最大强度值,请检查该区域中的每个像素。然后,选择最亮像素的强度值作为最大强度值。简单来说,标记的最大数组用于找到图像中最亮的区域。

例如,假设我们有一个由三个像素组成的区域。这三个像素的强度值分别为 0.5、0.2 和 0.8。则该区域的最大强度值为 0.8。

Labeled Max Array in Mahotas

在 Mahotas 中,我们可以使用 mahotas.labeled.labeled_max() 函数创建一个标记的最大数组。该函数迭代搜索区域中最亮的像素。然后,它将最亮像素的强度值存储在数组中。

结果数组是一个标记的最大数组,具有图像每个区域的最大强度值。

The mahotas.labeled.labeled_max() function

mahotas.labeled.labeled_max() 函数将图像和标记图像作为输入。它返回一个包含每个标记区域的最大强度值的数组。

以下是 mahotas 中 labeled_max() 函数的基本语法 -

mahotas.labeled.labeled_max(array, labeled, minlength=None)

其中,

  1. array − 这是输入图像。

  2. labeled - 这是标记图像。

  3. minlength (optional) - 它指定要包含在输出数组中的最小区域数(默认为 None)。

在以下示例中,我们使用 labeled_max() 函数在标记图像中查找标记的最大数组。

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

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

Labeled max array: [107 111 129 ... 141 119 109]

获得的图像如下所示:

labeled max array

Labeled Max Arrays of a Random Boolean Image

我们还可以找到随机布尔图像的标记最大数组。随机布尔图像是指每个像素值都为 0 或 1 的图像。前景色素用“1”表示,背景像素用“0”表示。

在 mahotas 中,要找到随机布尔图像的标记最大数组,我们首先需要使用 np.zeros() 函数生成特定大小的随机布尔图像。

此图像最初仅包含背景像素。然后,我们将整数值分配给图像的某些部分以创建不同的区域。

然后,我们使用 labeled_max() 函数查找图像的标记的最大数组。

Example

在下面提到的示例中,我们寻找的是随机布尔图像的标注最大数组。

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

上述代码的输出如下:

Labeled max array
Region 0 : 0.9950607583625318
Region 1 : 0.8626363785944107
Region 2 : 0.6343883551171169
Region 3 : 0.8162320509314726

我们获得以下图像作为输出 −

labeled max array1