Mahotas 简明教程

Mahotas - Sizes of Labelled Region

标记区域的大小指标记图像不同区域中存在的像素数。标记图像是指为图像的不同区域(像素组)分配唯一标签(值)的图像。

通常情况下,图像有两个主区域:前景和背景。

每个区域的大小取决于图像中存在的区域总数。如果存在更多区域,那么每个区域的大小将会更小。

相反,如果存在更少的区域,那么每个区域的大小将会更大。

Sizes of Labeled Region in Mahotas

在 Mahotas 中,我们可以使用函数 mahotas.labeled.labeled_size() 来计算标记图像中每个区域的大小。函数的工作方式如下:

  1. 它首先统计图像中标记区域的数量。

  2. 然后,它遍历所有标记区域并计算每个区域中存在的像素总数。

一旦遍历所有区域,函数便会返回每个区域的大小。

The mahotas.labeled.labeled_size() function

mahotas.labeled.labeled_size() 函数将一个标记的图像作为输入,并返回一个包含每个区域大小(以像素为单位)的列表。

我们可以遍历这个值列表以取得各个区域的大小。

以下是 mahotas 中 labeled_size() 函数的基本语法:

mahotas.labeled.labeled_size(labeled)

其中,

  1. 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

Sizes in Grayscale Image

我们还可以查找灰度图像中标记区域的大小。灰度图像表示仅具有单一颜色通道的图像,其中的每个像素由单个强度值表示。

像素的强度值决定了灰度的色调。0 将生成黑色像素,255 将生成白色像素,而任何其他值将生成具有中间色调的像素。

在 mahotas 中,要获得灰度图像中标记区域的大小,我们首先使用 colors.rgb2gray() 函数将输入 RGB 图像转换为灰度。

然后,我们给灰度图像标记,并遍历各个区域计算其大小。

Example

在下面提到的示例中,我们正在查找灰度图像中标记区域的大小。

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

产生的图像如下:

grayscale image sizes

Sizes in a Random Boolean Image

除了灰度图像外,我们还可以获得随机布尔图像中标记区域的大小。

随机布尔图像表示每个像素的值都为 1 或 0 的图像,其中值‘1’的像素被称为前景,而值‘0’的像素被称为背景。

在 mahotas 中,我们首先使用 np.zeros() 函数生成特定维度的一个随机布尔图像。

生成的随机图像最初将其所有像素值都设为 0(仅由背景区域组成)。然后,我们向图像中很少一部分分配整数值以创建不同的区域。

然后,我们给图像标记,并遍历各个区域以取得其像素大小。

Example

在这里,我们正在获得随机生成的布尔图像中不同标记的大小。

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

获得的图像如下所示:

grayscale image sizes1