![Mahotas](/mahotas/images/mahotas-mini-logo.jpg)
- 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 - 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](/mahotas/images/border_pixels_image.jpg)
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](/mahotas/images/border_pixels_image1.jpg)
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](/mahotas/images/border_pixels_image2.jpg)