Mahotas - 標籤區域的大小



標籤區域的大小指的是標記影像不同區域中存在的畫素數量。標記影像指的是為影像的不同區域(一組畫素)分配唯一標籤(值)的影像。

通常,影像有兩個主要區域:前景和背景。

每個區域的大小取決於影像中存在的區域總數。如果存在更多區域,則每個區域的大小將更小。

相反,如果存在較少的區域,則每個區域的大小將更大。

Mahotas 中標籤區域的大小

在 Mahotas 中,我們可以使用 **mahotas.labeled.labeled_size()** 函式來計算標記影像中每個區域的大小。該函式的工作方式如下:

  • 它首先計算影像中標記區域的數量。

  • 然後,它遍歷所有標記區域並計算每個區域中存在的畫素總數。

一旦遍歷完所有區域,該函式就會返回每個區域的大小。

mahotas.labeled.labeled_size() 函式

mahotas.labeled.labeled_size() 函式將標記影像作為輸入,並返回一個列表,其中包含每個區域以畫素為單位的大小。

我們可以遍歷該值列表以獲取每個區域的大小。

語法

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

mahotas.labeled.labeled_size(labeled)

其中,

  • **labeled** - 輸入的標記影像。

示例

在以下示例中,我們使用 mh.labeled.labeled_size() 函式查詢影像的標記區域的大小。

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('sea.bmp')
# Labeling the image
labeled, num_objects = mh.label(image)
# Getting the sizes of labeled regions
labeled_size = mh.labeled.labeled_size(labeled)
# Printing the sizes of labeled regions
for i, labeled_size in enumerate(labeled_size, 1):
   print(f"Size of Region {i} is = {labeled_size} pixels")
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 1)
# Displaying the original image
axes.imshow(image)
axes.set_title('Labeled Image')
axes.set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()
輸出

以下是上述程式碼的輸出:

Size of Region 1 is = 4263 pixels
Size of Region 2 is = 2234457 pixels

獲得的影像如下:

Labeled Region

灰度影像中的大小

我們也可以找到灰度影像中標記區域的大小。灰度影像指的是隻有一個顏色通道的影像,其中每個畫素由單個強度值表示。

畫素的強度值決定了灰度的深淺。0 將導致黑色畫素,255 將導致白色畫素,而任何其他值將導致具有中間陰影的畫素。

在 Mahotas 中,要獲取灰度影像的標記區域的大小,我們首先使用 colors.rgb2gray() 函式將輸入的 RGB 影像轉換為灰度影像。

然後,我們標記灰度影像並遍歷每個區域以計算其大小。

示例

在下面提到的示例中,我們正在查詢灰度影像的標記區域的大小。

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
image = mh.colors.rgb2gray(image)
# Labeling the image
labeled, num_objects = mh.label(image)
# Getting the sizes of labeled regions
labeled_size = mh.labeled.labeled_size(labeled)
# Printing the sizes of labeled regions
for i, labeled_size in enumerate(labeled_size, 1):
   print(f"Size of Region {i} is = {labeled_size} pixels")
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 1)
# Displaying the original image
axes.imshow(image, cmap='gray')
axes.set_title('Original Image')
axes.set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

輸出

上述程式碼的輸出如下:

Size of Region 1 is = 8 pixels
Size of Region 2 is = 1079032 pixels

生成的影像如下:

Sizes Grayscale Image

隨機布林影像中的大小

除了灰度影像之外,我們還可以獲取隨機布林影像中標記區域的大小。

隨機布林影像指的是每個畫素的值為 1 或 0 的影像,其中值為“1”的畫素稱為前景,值為“0”的畫素稱為背景。

在 Mahotas 中,我們首先使用 np.zeros() 函式生成特定尺寸的隨機布林影像。

生成的隨機影像最初將其所有畫素值設定為 0(僅包含背景區域)。然後,我們將整數值分配給影像的某些部分以建立不同的區域。

然後,我們標記影像並遍歷每個區域以獲取其以畫素為單位的大小。

示例

在這裡,我們正在獲取隨機生成的布林影像的不同標籤的大小。

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Creating a random image
image = np.zeros((10,10), bool)
# Creating regions
image[:2, :2] = 1
image[4:6, 4:6] = 1
image[8:, 8:] = 1
# Labeling the image
labeled, num_objects = mh.label(image)
# Getting the sizes of labeled regions
labeled_size = mh.labeled.labeled_size(labeled)
# Printing the sizes of labeled regions
for i, labeled_size in enumerate(labeled_size, 1):
   print(f"Size of Region {i} is = {labeled_size} pixels")
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 1)
# Displaying the original image
axes.imshow(image)
axes.set_title('Original Image')
axes.set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

輸出

執行上述程式碼後,我們將獲得以下輸出:

Size of Region 1 is = 88 pixels
Size of Region 2 is = 4 pixels
Size of Region 3 is = 4 pixels
Size of Region 4 is = 4 pixels

獲得的影像如下所示:

Sizes Grayscale Image1
廣告

© . All rights reserved.