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

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

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.

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

Syntax

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,

  • labeled − It is the input array.

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

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

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

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

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

Example

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

Following is the output of the above code −

Border Pixels Image

Using the mahotas.labeled.borders() Function

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.

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

Syntax

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,

  • labeled − It is the input array.

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

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

Example

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

Output of the above code is as follows −

Border Pixels Image1

Using Custom Structuring Element

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.

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.

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.

By default, both mahotas.labeled.border() and mahotas.labeled.borders() uses a 3×3 cross−shaped structuring element. This structuring element considers the four immediate neighbors (top, bottom, left, and right) of each pixel when determining connectivity.

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

Output

The output produced is as shown below −

Border Pixels Image2
Advertisements