Mahotas 简明教程

Mahotas - Labeling Images

图像标记是指将类别(标签)分配给图像的不同区域。标签通常表示为整数值,其中每个值对应于特定类别或区域。

例如,让我们考虑一幅包含各种对象或区域的图像。每个区域都会分配一个唯一值(整数)以将其与其他区域区分开来。背景区域标记为值 0。

Labeling Images in Mahotas

在 Mahotas 中,我们可以使用 label()labeled.label() 函数标记图像。

这些函数将图像分割为不同的区域,方法是为图像中的不同连通分量分配唯一的标签或标识符。每个连通分量都是一组相邻像素,它们共享一个公共属性,例如强度或颜色。

标记过程创建一个图像,其中属于同一区域的像素被分配相同的标签值。

Using the mahotas.label() Function

mahotas.label() 函数将图像作为输入,其中关注区域由前景(非零)值表示,背景由零表示。

该函数返回标记数组,其中每个连通分量或区域都分配了一个唯一的整数标签。

label() 函数使用 8 连通性执行标记,它指的是图像中像素之间的关系,其中每个像素与其周围的八个邻居像素相连,包括对角线。

以下是 mahotas 中标签功能的基本语法——

mahotas.label(array, Bc={3x3 cross}, output={new array})

其中,

  1. array − 这是输入数组。

  2. Bc (optional) − 这是用于连接性的结构元素。

  3. output (optional) − 这是输出数组(默认情况下为与数组相同形状的新数组)。

在以下示例中,则使用 mh.label() 功能来对图像进行标记。

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image_rgb = mh.imread('sun.png')
image = image_rgb[:,:,0]
# Applying gaussian filtering
image = mh.gaussian_filter(image, 4)
image = (image > image.mean())
# Converting it to a labeled image
labeled, num_objects = mh.label(image)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original RGB image
axes[0].imshow(image_rgb)
axes[0].set_title('RGB Image')
axes[0].set_axis_off()
# Displaying the labeled image
axes[1].imshow(labeled)
axes[1].set_title('Labeled Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

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

rgb label image

Using the mahotas.labeled.label() Function

mahotas.labeled.label() 函数会从图像的不同区域开始,对标签进行 1 至多个连续分配。其工作方式与 mahotas.label() 功能类似,可将图像分割成不同的区域。

如果使用非连续标签值标记图像,labeled.label() 功能会更新标签值以保持顺序。

例如,假设使用标签标记图像,其中四个区域具有标签 2, 4, 7, and 9 。labeled.label() 功能会将图像转换为新标记图像,其中包含连续标签 1, 2, 3, and 4

以下是 mahotas 中 labeled.label() 功能的基本语法——

mahotas.labeled.label(array, Bc={3x3 cross}, output={new array})

其中,

  1. array − 这是输入数组。

  2. Bc (optional) − 这是用于连接性的结构元素。

  3. output (optional) − 这是输出数组(默认情况下为与数组相同形状的新数组)。

以下示例演示如何使用 mh.labeled.label() 功能将图像转换为标记图像。

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image_rgb = mh.imread('sea.bmp')
image = image_rgb[:,:,0]
# Applying gaussian filtering
image = mh.gaussian_filter(image, 4)
image = (image > image.mean())
# Converting it to a labeled image
labeled, num_objects = mh.labeled.label(image)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original RGB image
axes[0].imshow(image_rgb)
axes[0].set_title('RGB Image')
axes[0].set_axis_off()
# Displaying the labeled image
axes[1].imshow(labeled)
axes[1].set_title('Labeled Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

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

rgb label image1

Using Custom Structuring Element

我们可以使用具有标记功能的自定义结构元素,根据要求对图像进行分割。结构元素是二进制数组,具有奇数维度,由一和零组成,用于定义图像标记过程中邻域像素的连接模式。

一指示包含在连接分析中的邻域像素,而零表示排除或忽略的邻域像素。

例如,让我们考虑自定义结构元素: [[1, 0, 0], [0, 1, 0], [0, 0,1]]. 这个结构元素暗示了对角连接。这意味着对于图像中的每个像素,在标记或分割过程中,仅将该像素的正上方和正下方像素视为其邻域像素。

Example

在此处,我们已经定义了一个自定义结构元素来对图像进行标记。

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image_rgb = mh.imread('sea.bmp')
image = image_rgb[:,:,0]
# Applying gaussian filtering
image = mh.gaussian_filter(image, 4)
image = (image > image.mean())
# Creating a custom structuring element
binary_closure = np.array([[0, 1, 0],
[0, 1, 0],
[0, 1, 0]])
# Converting it to a labeled image
labeled, num_objects = mh.labeled.label(image, Bc=binary_closure)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original RGB image
axes[0].imshow(image_rgb)
axes[0].set_title('RGB Image')
axes[0].set_axis_off()
# Displaying the labeled image
axes[1].imshow(labeled)
axes[1].set_title('Labeled Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figure
mtplt.show()

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

rgb label image2