Mahotas - 標記最大值陣列



標記最大值陣列指的是一個數組,它儲存 標記影像 中每個區域的最大強度值。為了找到一個區域的最大強度值,需要檢查該區域中的每個畫素。然後,選擇最亮畫素的強度值作為最大強度值。簡單來說,標記最大值陣列用於查詢影像中最亮的區域。

例如,假設我們有一個包含三個畫素的區域。這三個畫素的強度值分別為0.5、0.2和0.8。那麼該區域的最大強度值將為0.8。

Mahotas中的標記最大值陣列

在Mahotas中,我們可以使用mahotas.labeled.labeled_max()函式來建立一個標記最大值陣列。該函式迭代地搜尋區域中最亮的畫素。然後,它將最亮畫素的強度值儲存在一個數組中。

生成的陣列是一個標記最大值陣列,包含影像每個區域的最大強度值。

mahotas.labeled.labeled_max()函式

mahotas.labeled.labeled_max()函式接受影像和標記影像作為輸入。它返回一個數組,其中包含每個標記區域的最大強度值。

語法

以下是Mahotas中labeled_max()函式的基本語法:

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

其中,

  • array - 輸入影像。

  • labeled - 標記影像。

  • minlength (可選) - 指定輸出陣列中要包含的最小區域數(預設為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

隨機布林影像的標記最大值陣列

我們還可以找到隨機布林影像的標記最大值陣列。隨機布林影像指的是每個畫素的值為0或1的影像。“1”表示前景畫素,“0”表示背景畫素。

在Mahotas中,要查詢隨機布林影像的標記最大值陣列,我們首先需要使用np.zeros()函式生成特定大小的隨機布林影像。

此影像最初僅包含背景畫素。然後,我們將整數值分配給影像的幾個部分以建立不同的區域。

然後,我們使用labeled_max()函式查詢影像的標記最大值陣列。

示例

在下面提到的示例中,我們正在查詢隨機布林影像的標記最大值陣列。

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
廣告