![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 - Morphological Operators
Morphological operators are a set of image processing techniques that help modify the shape, size, and spatial relationships of objects in an image. They are like tools used to reshape or manipulate the objects within an image.
Imagine you have a picture with various objects like circles, squares, and lines. Morphological operators allow you to change the appearance of these objects in different ways.
For example, you can make them bigger or smaller, smooth their edges, remove tiny details, or fill in gaps.
To achieve these transformations, morphological operators use mathematical operations that involve scanning the image with a special pattern called a structuring element. This structuring element defines the shape and size of the neighborhood around each pixel that will be considered during the operation.
Mahotas and Morphological Operators
Mahotas provides a comprehensive set of morphological operators that can be applied to binary, grayscale, or multichannel images. These operators are implemented efficiently using optimized algorithms, making them suitable for real−world applications with largescale datasets.
Let us look at few morphological operations which can be done in mahotas −
Dilation
Dilation is a morphological operation that expands the pixels on the boundaries of objects in an image. It involves scanning the image with a structuring element and replacing each pixel with the maximum value within the corresponding neighborhood defined by the structuring element.
Let us look at the dilated image along with the original image below −
![Dilation Image](/mahotas/images/dilation_image.jpg)
Erosion
Erosion is a basic morphological operator that shrinks or erodes the pixels on boundaries of objects in an image.
Following is the eroded image along with its original image −
![Erosion Image](/mahotas/images/erosion_image.jpg)
Opening
Opening is a combination of erosion followed by dilation. It removes small objects and fills in small holes in the foreground while preserving the overall shape and connectivity of larger objects.
Let us look at the opened image in mahotas −
![Opening Image](/mahotas/images/opening_image.jpg)
Closing
Closing is the reverse of opening, consisting of dilation followed by erosion. It fills small gaps and holes within the foreground, ensuring the connectivity of objects.
Now, let us look at the closed image in mahotas −
![Closing Image](/mahotas/images/closing_image.jpg)
Conditional Erosion
Conditional erosion is a morphological operation that preserves structures in an image based on a user−defined condition. It selectively erodes regions of an image based on the condition specified by a second image called the marker image.
The operation erodes the image only where the marker image has non−zero values.
Following image shows conditional erosion −
![Conditional Erosion Image](/mahotas/images/conditional_erosion_image.jpg)
Conditional Dilation
Conditional dilation is similar to conditional erosion but operates in the opposite way. It selectively dilates regions of an image based on the condition specified by the marker image.
The operation dilates the image only where the marker image has non−zero values.
Following image shows conditional dilation −
![Conditional Dilation Image](/mahotas/images/conditional_dilation_image.jpg)
Regional Maxima
Regional maxima are points in an image that have the highest intensity within their neighborhood. They represent local peaks or salient features in the image.
The regional maxima operator in Mahotas identifies these local maxima and marks them as foreground pixels.
Following image represents regional maxima −
![Regional Maxima Image](/mahotas/images/regional_maxima_image.jpg)
Regional Minima
Regional minima are points in an image that have the lowest intensity within their neighborhood. They represent local basins or depressions in the image.
The regional minima operator in Mahotas identifies these local minima and marks them as background pixels.
Following image represents regional minima −
![Regional Minima Image](/mahotas/images/regional_minima_image.jpg)
Example
In the following example, we are trying to perform all the above explained morphological operations −
import mahotas as mh import matplotlib.pyplot as plt import numpy as np image = mh.imread('nature.jpeg', as_grey=True).astype(np.uint8) # Dilation dilated_image = mh.dilate(image, Bc=mh.disk(8)) plt.title("Dilated Image") plt.imshow(dilated_image) plt.show() # Erosion plt.title("Eroded Image") eroded_image = mh.erode(image, Bc=mh.disk(8)) plt.imshow(eroded_image) plt.show() # Opening plt.title("Opened Image") opened_image = mh.open(image) plt.imshow(opened_image) plt.show() # Closing plt.title("Closed Image") closed_image = mh.close(image) plt.imshow(closed_image) plt.show() # Conditional Dilation plt.title("Conditional Dilation") g = image * 6 cdilated_image = mh.cdilate(image, g) plt.imshow(cdilated_image) plt.show() # Conditional Erosion plt.title("Conditional Erosion") scaled_image = image * 0.5 scaled_image = scaled_image.astype(np.uint8) ceroded_image = mh.cerode(image, scaled_image) plt.imshow(ceroded_image) plt.show() # Regional Maxima plt.title("Regional Maxima") regional_maxima = mh.regmax(image) plt.imshow(regional_maxima) plt.show() # Regional Minima plt.title("Regional Minima") regional_minima = mh.regmin(image) plt.imshow(regional_minima) plt.show()
Output
The output obtained is as shown below −
Dilation:
![Dilation Image1](/mahotas/images/dilation_image1.jpg)
Erosion:
![Erosion Image1](/mahotas/images/erosion_image1.jpg)
Opened Image:
![Opened Image](/mahotas/images/opened_image.jpg)
Closed Image:
![Closed Image](/mahotas/images/closed_image.jpg)
Conditional Dilation:
![Conditional Dilation Image1](/mahotas/images/conditional_dilation_image1.jpg)
Conditional Erosion:
![Conditional Erosion Image1](/mahotas/images/conditional_erosion_image1.jpg)
Regional Maxima:
![Regional Maxima Image1](/mahotas/images/regional_maxima_image1.jpg)
Regional Minima:
![Regional Minima Image1](/mahotas/images/regional_minima_image1.jpg)
We will discuss about all the morphological operators in detail in the remaining chapters of this section.