Mahotas 简明教程

Mahotas - Removing Bordered Labelled

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

Removing bordered labeled refers to removing border regions in a labeled image. A labeled image consists of distinct regions that are assigned unique labels.

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

A border region of a labeled image refers to regions present along the edges (boundaries) of an image.

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

Border regions often make it difficult to analyze an image and, in some cases, represent significant noise.

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

Hence, it is important to remove border regions to improve accuracy of image segmentation algorithms and to reduce the overall size of the image.

Removing Bordered Labeled in Mahotas

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

In Mahotas, we can use mahotas.labeled.remove_bordering() function to remove border labels from an image. It analyzes the image to check for presence of any border label. If it finds a border label, it determines the value associated with that border label.

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

The value of the border label is then updated to 0, to remove it from the image. Since, value 0 is associated with the background, all the border labels become part of the background.

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

Sometimes, a border label can be far away from the image boundary. If we want to retain these border labels, we need to specify the minimum distance between a border region and the boundary. Any border region that exceeds this distance will be retained by the function.

The mahotas.labeled.remove_bordering() function

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

The mahotas.labeled.remove_bordering() function takes a labeled image as an input and returns a labeled image as output without having any border regions.

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

The function remove borders of all sizes, hence the output image consumes significantly less space than the input image.

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

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

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

其中,

Where,

  1. labeled − It is the input labeled image.

  2. rsize (optional) − It determines the minimum distance that regions must have from the image boundary in order to avoid being removed (default is 1).

  3. out (optional) − It specifies where to store the output image (default is an array of same size as labeled).

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

In the following example, we are removing border regions from an image by using the mh.labeled.remove_bordering() function.

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

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

Following is the output of the above code −

removing bordered labeled

Removing Regions at a Specific Distance

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

We can also remove bordered regions that are at a specific distance, away from the image boundary. This allows us to remove any region that may have been considered as the bordered region because of its closeness to the image boundary.

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

In mahotas, the rsize parameter determines how far away a border region must be to be retained in the image. We need to set an integer value for this parameter and then pass it to the mh.labeled.remove_bordering() function.

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

For example, let’s say we have set ‘200’ as the value for rsize. Then, only the bordered regions that are at least 200 pixels away from the image boundary will be retained.

Example

在下面提到的示例中,我们正在移除图像边界内特定距离的边界区域。

In the example mentioned below, we are removing border regions that are within a specific distance of the image boundary.

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

上述代码的输出如下:

Output of the above code is as follows −

removing bordered labeled1

Removing Regions from a Specific part of an Image

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

Another way of removing bordered regions is to remove them from a specific part of an image.

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

Specific parts of an image refer to a small portion of the larger image obtained by cropping the larger image.

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

In mahotas, to remove regions from a specific part of an image, we first identify a region of interest from the original image.

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

Then, we crop the identified part of the image. We then remove the border regions from this part.

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

For example, if we specify the values as [:800, :800], then the region will start from 0 pixel and go up to 800 pixels in both the vertical (y−axis) and horizontal (x−axis) direction.

Example

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

In here, we are removing bordered regions from a specific part of an image.

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

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

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

removing bordered regions