Mahotas 简明教程

Mahotas - Removing Bordered Labelled

移除有边界的标签是指移除标记图像中的边界区域。标记图像包含分配了唯一标签的不同区域。

标记图像的边界区域是指图像边缘(边界)上出现的区域。

边界区域通常会使图像分析变得困难,并且在某些情况下,它们代表了显著的噪声。

因此,移除边界区域对提高图像分割算法的准确性以及缩小图像总大小非常重要。

Removing Bordered Labeled in Mahotas

在 Mahotas 中,我们可以使用 mahotas.labeled.remove_bordering() 函数从图像中移除边界标签。它会分析图像以检查是否存在任何边界标签。如果找到边界标签,它将确定与此边界标签关联的值。

然后将边界标签的值更新为 0,以将其从图像中移除。因为值 0 与背景相关联,所以所有边界标签都会变为背景的一部分。

有时,边界标签可能远离图像边界。如果我们想保留这些边界标签,我们需要指定边界区域与边界之间的最小距离。任何超过此距离的边界区域都将被该函数保留。

The mahotas.labeled.remove_bordering() function

mahotas.labeled.remove_bordering() 函数将标记图像作为输入,并将一个不包含任何边界区域的标记图像作为输出返回。

该函数会移除所有大小的边界,因此输出图像占用的空间远小于输入图像。

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

mahotas.labeled.remove_bordering(labeled, rsize=1, out={np.empty_like(im)})

其中,

  1. labeled − 这是输入标记图像。

  2. rsize (optional) - 它确定区域必须与图像边界保持的最小距离才能避免被移除(默认值为 1)。

  3. out (optional) - 它指定存储输出图像的位置(默认值是与 labeled 大小相同的数组)。

在以下示例中,我们通过使用 mh.labeled.remove_bordering() 函数从图像中移除边界区域。

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('nature.jpeg')
# Converting it to grayscale
image = mh.colors.rgb2gray(image)
# Applying gaussian filtering
image = mh.gaussian_filter(image, 4)
# Thresholding the image
image = image > image.mean()
# Labeling the image
labeled, num_objects = mh.label(image)
# Removing bordering labels
remove_border = mh.labeled.remove_bordering(labeled)
# 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 image without borders
axes[1].imshow(remove_border)
axes[1].set_title('Border Removed Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

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

removing bordered labeled

Removing Regions at a Specific Distance

我们还可以移除距离图像边界特定距离的边界区域。这使我们可以移除由于靠近图像边界而被视为边界区域的任何区域。

在 mahotas 中, rsize 参数确定边界区域必须远离图像多远才能保留在图像中。我们需要为此参数设置一个整数值,然后将其传递给 mh.labeled.remove_bordering() 函数。

例如,假设我们已将 rsize 的值为设置为“200”。那么,只有距离图像边界至少为 200 像素的边界区域才会被保留。

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 = mh.colors.rgb2gray(image)
# Applying gaussian filtering
image = mh.gaussian_filter(image, 4)
# Thresholding the image
image = image > image.mean()
# Labeling the image
labeled, num_objects = mh.label(image)
# Removing bordering labels
remove_border = mh.labeled.remove_bordering(labeled, rsize=200)
# 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 image without borders
axes[1].imshow(remove_border)
axes[1].set_title('Border Removed Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

上述代码的输出如下:

removing bordered labeled1

Removing Regions from a Specific part of an Image

移除边界区域的另一种方法是从图像的特定部分移除它们。

图像的特定部分是指通过裁剪较大图像获得的较大图像的一小部分。

在 mahotas 中,要从图像的特定部分移除区域,我们首先从原始图像中识别目标区域。

然后,我们将图像中标识的部分裁剪出来。然后移除该部分的边界区域。

举例而言,如果我们指定值 [:800, :800],那么该区域将从 0 像素开始,并且在垂直(y 轴)和水平(x 轴)方向上均达到 800 像素。

Example

在这里,我们从图像的特定部分移除边界区域。

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('tree.tiff')
# Converting it to grayscale
image = mh.colors.rgb2gray(image)
image = image[:800, :800]
# Applying gaussian filtering
image = mh.gaussian_filter(image, 4)
# Thresholding the image
image = image > image.mean()
# Labeling the image
labeled, num_objects = mh.label(image)
# Removing bordering labels
remove_border = mh.labeled.remove_bordering(labeled)
# 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 image without borders
axes[1].imshow(remove_border)
axes[1].set_title('Border Removed Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

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

removing bordered regions