- Mahotas Tutorial
- Mahotas - Home
- Mahotas - Introduction
- Mahotas - Computer Vision
- Mahotas - History
- Mahotas - Features
- Mahotas - Installation
- Mahotas Handling Images
- Mahotas - Handling Images
- Mahotas - Loading an Image
- Mahotas - Loading Image as Grey
- Mahotas - Displaying an Image
- Mahotas - Displaying Shape of an Image
- Mahotas - Saving an Image
- Mahotas - Centre of Mass of an Image
- Mahotas - Convolution of Image
- Mahotas - Creating RGB Image
- Mahotas - Euler Number of an Image
- Mahotas - Fraction of Zeros in an Image
- Mahotas - Getting Image Moments
- Mahotas - Local Maxima in an Image
- Mahotas - Image Ellipse Axes
- Mahotas - Image Stretch RGB
- Mahotas Color-Space Conversion
- Mahotas - Color-Space Conversion
- Mahotas - RGB to Gray Conversion
- Mahotas - RGB to LAB Conversion
- Mahotas - RGB to Sepia
- Mahotas - RGB to XYZ Conversion
- Mahotas - XYZ to LAB Conversion
- Mahotas - XYZ to RGB Conversion
- Mahotas - Increase Gamma Correction
- Mahotas - Stretching Gamma Correction
- Mahotas Labeled Image Functions
- Mahotas - Labeled Image Functions
- Mahotas - Labeling Images
- Mahotas - Filtering Regions
- Mahotas - Border Pixels
- Mahotas - Morphological Operations
- Mahotas - Morphological Operators
- Mahotas - Finding Image Mean
- Mahotas - Cropping an Image
- Mahotas - Eccentricity of an Image
- Mahotas - Overlaying Image
- Mahotas - Roundness of Image
- Mahotas - Resizing an Image
- Mahotas - Histogram of Image
- Mahotas - Dilating an Image
- Mahotas - Eroding Image
- Mahotas - Watershed
- Mahotas - Opening Process on Image
- Mahotas - Closing Process on Image
- Mahotas - Closing Holes in an Image
- Mahotas - Conditional Dilating Image
- Mahotas - Conditional Eroding Image
- Mahotas - Conditional Watershed of Image
- Mahotas - Local Minima in Image
- Mahotas - Regional Maxima of Image
- Mahotas - Regional Minima of Image
- Mahotas - Advanced Concepts
- Mahotas - Image Thresholding
- Mahotas - Setting Threshold
- Mahotas - Soft Threshold
- Mahotas - Bernsen Local Thresholding
- Mahotas - Wavelet Transforms
- Making Image Wavelet Center
- Mahotas - Distance Transform
- Mahotas - Polygon Utilities
- Mahotas - Local Binary Patterns
- Threshold Adjacency Statistics
- Mahotas - Haralic Features
- Weight of Labeled Region
- Mahotas - Zernike Features
- Mahotas - Zernike Moments
- Mahotas - Rank Filter
- Mahotas - 2D Laplacian Filter
- Mahotas - Majority Filter
- Mahotas - Mean Filter
- Mahotas - Median Filter
- Mahotas - Otsu's Method
- Mahotas - Gaussian Filtering
- Mahotas - Hit & Miss Transform
- Mahotas - Labeled Max Array
- Mahotas - Mean Value of Image
- Mahotas - SURF Dense Points
- Mahotas - SURF Integral
- Mahotas - Haar Transform
- Highlighting Image Maxima
- Computing Linear Binary Patterns
- Getting Border of Labels
- Reversing Haar Transform
- Riddler-Calvard Method
- Sizes of Labelled Region
- Mahotas - Template Matching
- Speeded-Up Robust Features
- Removing Bordered Labelled
- Mahotas - Daubechies Wavelet
- Mahotas - Sobel Edge Detection
Mahotas - Getting Border of Labels
Getting the border of labels refers to extracting the border pixels of a labeled image. A border can be defined as a region whose pixels are located at the edges of an image. A border represents the transition between different regions of an image.
Getting borders of labels involves identifying the border regions in the labeled image and separating them from the background.
Since a labeled image consists of only the foreground pixels and the background pixels, the borders can be easily identified as they are present adjacent to the background regions.
Getting Border of labels in Mahotas
In Mahotas, we can use the mahotas.labeled.borders() function to get the border of labels. It analyzes the neighboring pixels of the labeled image and considers the connectivity patterns to get the borders.
The mahotas.labeled.borders() function
The mahotas.labeled.borders() function takes a labeled image as input and returns an image with the highlighted borders.
In the resultant image, the border pixels have a value of 1 and are part of the foreground.
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 labeled image.
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 the following example, we are getting the borders of labels using the mh.labeled.borders() function.
import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt # Loading the image image = mh.imread('nature.jpeg', as_grey=True) # Applying thresholding image = image > image.mean() # Converting it to a labeled image labeled, num_objects = mh.label(image) # Geting border of labels borders = mh.labeled.borders(labeled) # Creating a figure and axes for subplots fig, axes = mtplt.subplots(1, 2) # Displaying the labeled image axes[0].imshow(labeled) axes[0].set_title('Labeled Image') axes[0].set_axis_off() # Displaying the borders axes[1].imshow(borders) axes[1].set_title('Border Labels') axes[1].set_axis_off() # Adjusting spacing between subplots mtplt.tight_layout() # Showing the figures mtplt.show()
Output
Following is the output of the above code −
Getting Borders by using a Custom Structuring Element
We can also get borders of labels by using a custom structuring element. A structuring element is an array which consists of only 1s and 0s. It is used to define the connectivity structure of the neighboring pixels.
Pixels that are included in the connectivity analysis have the value 1, while the pixels that are excluded have the value 0.
In mahotas, we create a custom structuring element using the mh.disk() function. Then, we set this custom structuring element as the Bc parameter in the borders() function to get the borders of labels.
Example
Here, we are getting borders of labels using a custom structuring element.
import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt # Loading the image image = mh.imread('sea.bmp', as_grey=True) # Applying thresholding image = image > image.mean() # Converting it to a labeled image labeled, num_objects = mh.label(image) # Geting border of labels borders = mh.labeled.borders(labeled, mh.disk(5)) # Creating a figure and axes for subplots fig, axes = mtplt.subplots(1, 2) # Displaying the labeled image axes[0].imshow(labeled) axes[0].set_title('Labeled Image') axes[0].set_axis_off() # Displaying the borders axes[1].imshow(borders) axes[1].set_title('Border Labels') 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 −