Mahotas 简明教程

Mahotas - Border Pixels

边界像素是指位于图像边界或边缘上的像素。边界像素至少有一个属于不同区域或具有不同值的相邻像素,表示感兴趣区域和背景之间的过渡。

Border pixels are the pixels lying on the boundaries or edges of an image. A border pixel has at least one neighboring pixel that belongs to a different region or has a different value, indicating the transition between regions of interest and the background.

例如,在对象由白色像素表示而背景由黑色像素表示的二值图像中,边界像素将与黑色像素相邻的那些白色像素。

For example, in a binary image where objects are represented by white pixels and the background is represented by black pixels, the border pixels would be those white pixels that are adjacent to black pixels.

Border Pixels in Mahotas

在 Mahotas 中,我们可以使用 labeled.border()labeled.borders() 函数提取边界像素。这些函数通过检查具有不同标记的相邻像素并同时考虑由结构元素指定的连通性来检测边界。

In Mahotas, we can extract border pixels using the labeled.border() and labeled.borders() functions. These functions detect borders by examining neighboring pixels that have different labels while also considering the connectivity specified by the structuring element.

Using the mahotas.labeled.border() Function

mahotas.labeled.border() 函数以标记图像作为输入,并返回相同大小的二值图像,显示边界像素。此函数提取标记图像中两个指定区域之间的边界像素。

The mahotas.labeled.border() function take a labeled image as input and return a binary image of the same size showing the border pixels. This function extracts border pixels between two specified regions of a labeled image.

在生成图像中,边界像素标记为 True(或 1),而非边界像素标记为 False(或 0)。

In the resultant image, the border pixels are marked as True (or 1) and non−border pixels as False (or 0).

下面是 mahotas 中 border() 函数的基本语法−

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

mahotas.labeled.border(labeled, i, j, Bc={3x3 cross},
out={np.zeros(labeled.shape, bool)}, always_return=True)

其中,

where,

  1. labeled − It is the input array.

  2. i − It is the label of the first region.

  3. j − It is the label of the second region.

  4. Bc (optional) − It is the structuring element used for connectivity.

  5. out (optional) − It is the output array (defaults to new array of same shape as labeled).

  6. always_return (optional) − It is a flag to indicate whether to return an output if no border pixels are present (defaults to True).

在以下示例中,我们使用 mh.labeled.border() 函数提取标记区域 1 的边界像素。

In the following example, we are extracting the border pixels of labeled region 1 using the mh.labeled.border() function.

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.label(image)
# Getting border pixels
border_pixels = mh.labeled.border(labeled, 0, 1)
# 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 border pixels
axes[1].imshow(border_pixels)
axes[1].set_title('Border Pixels')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Show the figure
mtplt.show()

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

Following is the output of the above code −

border pixels image

Using the mahotas.labeled.borders() Function

mahotas.labeled.borders() 函数从标记图像中提取 all 边界像素。它类似于 mahotas.labeled.border() 函数,它检查具有不同标记的相邻像素以检测边界。

The mahotas.labeled.borders() function extracts all the border pixels from a labeled image. It is similar to mahotas.labeled.border() function in that it examines neighboring pixels that have different labels to detect borders.

它还生成一个二值图像,其中边界像素标记为 True(或 1),而非边界像素标记为 False(或 0)。

It also produces a binary image where the border pixels are marked as True (or 1) and non−border pixels as False (or 0).

以下是 mahotas 中 borders() 函数的基本语法 −

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

mahotas.labeled.borders(labeled, Bc={3x3 cross}, out={np.zeros(labeled.shape,
bool)})

其中,

where,

  1. labeled − It is the input array.

  2. Bc (optional) − It is the structuring element used for connectivity.

  3. out (optional) − It is the output array (defaults to new array of same shape as labeled).

在这里,我们使用 mh.labeled.borders() 函数提取标记图像的所有边界像素。

In here, we are extracting all the border pixels of a labeled image using the mh.labeled.borders() function.

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image_rgb = mh.imread('nature.jpeg')
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)
# Get border pixels
border_pixels = mh.labeled.borders(labeled)
# 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 border pixels
axes[1].imshow(border_pixels)
axes[1].set_title('Border Pixels')
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 −

border pixels image1

Using Custom Structuring Element

我们还可以使用自定义结构元素更准确地检测边界像素。结构元素是由 1 和 0 组成的一个奇数维二进制数组。

We can also use a custom structuring element to detect border pixels more accurately. A structuring element is a binary array of odd dimensions consisting of ones and zeroes.

它定义了识别边界像素时邻域像素的连通性模式。我们可以使用 numpy 库中的 array() 函数定义一个自定义结构元素。

It defines the connectivity pattern of the neighborhood pixels when identifying border pixels.We can define a custom structuring element by using the array() function in the numpy library.

例如,让我们考虑二进制数组: [[0, 1, 0],[0, 1, 0],[0, 1, 0]] 作为结构元素。此结构元素表示垂直连通性,这意味着对于每个像素,只有正上方和正下方的像素(在同一列中)被视为其邻居。

For example let’s consider the binary array: [[0, 1, 0],[0, 1, 0],[0, 1, 0]] as the structuring element. This structuring element implies vertical connectivity, meaning that for each pixel only the pixels directly above and below it (in the same column) are considered as its neighbors.

Example

以下示例显示了使用自定义结构元素提取边界像素。

The following example shows the extraction of border pixels using a custom structuring element.

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 to a labeled image
labeled, num_objects = mh.label(image)
# Creating a custom structuring element
binary_closure = np.array([[0, 1, 0],
[0, 1, 0],
[0, 1, 0]])
# Getting border pixels
border_pixels = mh.labeled.borders(labeled, Bc=binary_closure)
# Create 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 border pixels
axes[1].imshow(border_pixels)
axes[1].set_title('Border Pixels')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

下面显示了产生的输出:

The output produced is as shown below −

border pixels image2