Mahotas 简明教程

Mahotas - Sizes of Labelled Region

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

Sizes of labeled regions refer to the number of pixels present in different regions of a labeled image. A labeled image refers to an image in which a unique label (value) is assigned to distinct regions (a group of pixels) of an image.

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

Usually, an image has two primary regions − the foreground and the background.

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

The size of each region depends on the total number of regions present in the image. If more number of regions are present, then the size of each region will be smaller.

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

Conversely if less number of regions are present, then the size of each region will be bigger.

Sizes of Labeled Region in Mahotas

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

In Mahotas, we can use the mahotas.labeled.labeled_size() function to calculate the size of each region in a labeled image. The function works in the following way −

  1. It first counts the number of labeled regions in the image.

  2. Then, it traverses through all the labeled regions and calculates the total number of pixels present in each region.

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

Once all the regions have been traversed the size of each region is returned by the function.

The mahotas.labeled.labeled_size() function

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

The mahotas.labeled.labeled_size() function takes a labeled image as an input and returns a list containing the size of each region in pixels.

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

We can traverse through the list of values to get the size of each region.

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

Following is the basic syntax of the labeled_size() function in mahotas −

mahotas.labeled.labeled_size(labeled)

其中,

where,

  1. labeled − It is the input labeled image.

在以下示例中,我们正在使用 mh.labeled.labeled_size() 函数查找图像的标记区域的大小。

In the following example, we are finding the sizes of labeled regions of an image using the mh.labeled.labeled_size() function.

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

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

Following is the output of the above code −

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

以下为获得的图像:

Following is the image obtained −

labeled region

Sizes in Grayscale Image

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

We can also possible to find the sizes of labeled regions in a grayscale image. Grayscale images refer to the image having only a single−color channel, where each pixel is represented by a single intensity value.

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

The intensity value of a pixel determines the shade of gray. 0 will result in black pixels, 255 will result in white pixels, while any other value will result in pixels having an intermediate shade.

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

In mahotas, to get the sizes of labeled regions of a grayscale image, we first convert an input RGB image to grayscale using the colors.rgb2gray() function.

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

Then, we label the grayscale image and traverse over each region to calculate its size.

Example

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

In the example mentioned below, we are finding the size of labeled regions of a grayscale image.

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

上述代码的输出如下:

Output of the above code is as follows −

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

产生的图像如下:

The image produced is as follows −

grayscale image sizes

Sizes in a Random Boolean Image

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

In addition to grayscale images, we can also get the sizes of labeled regions in a random boolean image.

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

A random Boolean image refers to an image where each pixel has a value of either 1 or 0, where pixels with the value '1' are referred to as the foreground and pixels with the value '0' are referred to as the background.

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

In mahotas, we first generate a random Boolean image of a specific dimension using the np.zeros() function.

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

The generated random image initially has all its pixel values set to 0 (consists of only the background region). We then assign integer values to few portions of the image to create distinct regions.

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

Then, we label the image and traverse over each region to get its size in pixels.

Example

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

In here, we are getting the size of different labels of a randomly generated 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)
# 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()

执行上面的代码后,我们得到以下输出: -

After executing the above code, we get the following output −

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

获得的图像如下所示:

The image obtained is as shown below −

grayscale image sizes1