![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 - Opening Process on Image
Opening is a two−step process used in image processing. First, an erosion operation is performed, followed by a dilation operation.
Erosion shrinks or removes unwanted details by examining each pixel and its neighboring pixels. If any neighboring pixel is black, the center pixel is also turned black.
This step helps eliminate thin protrusions and internal noise. Protrusions are thin or elongated structures that extend outward from a surface.
After erosion, dilation is performed to expand or thicken the image. It looks at each pixel and its neighbors, and if any neighboring pixel is white, the center pixel is turned white.
Opening effectively removes small details and noise while preserving the main structures of the image.
Opening Process on Image in Mahotas
To perform the opening process in Mahotas, we use the mahotas.open() function. This method allows for the sequential application of erosion and dilation operations.
Erosion reduces noise and eliminates small structures by considering each pixel's neighborhood and replacing it with the minimum value.
Subsequently, dilation expands the remaining structures while preserving their key features by replacing each pixel with the maximum value in its neighborhood.
The mahotas.open() function
The open() function in Mahotas takes two main arguments − the binary image and the structuring element (kernel). The function first applies an erosion operation to the input binary image. It then applies a dilation operation to the eroded image.
The open() function returns the resulting opened image after the erosion and dilation operations have been performed.
Syntax
Following is the basic syntax of the open() function in mahotas −
mahotas.open(f, Bc={3x3 cross}, out={np.empty_like(f)})
Where,
f − It is the input binary image represented as a NumPy array.
Bc − It is the structuring element used for both the erosion and dilation operations. Default is 3x3 cross-shaped structuring element.
out − It is the output array. The result will be stored in a new array with the same shape and data type as the input image f.
Example
In the following example, we are performing the opening process on an image −
import mahotas as mh import numpy as np import matplotlib.pyplot as plt image = mh.imread('sun.png') opened_image = mh.open(image) # Create a figure with subplots fig, axes = plt.subplots(1, 2, figsize=(7,5 )) # Display the original image axes[0].imshow(image) axes[0].set_title('Original Image') axes[0].axis('off') # Display the opened image axes[1].imshow(opened_image, cmap='gray') axes[1].set_title('Opened Image') axes[1].axis('off') # Adjust the layout and display the plot plt.tight_layout() plt.show()
Output
After executing the above code, we get the following output −
![Opening Process Image](/mahotas/images/opening_process_image.jpg)
Using Random Binary Image
We can also perform opening process on an image by creating a random binary image. To achieve this, first we create a random binary image using NumPy, where the pixels are either 0 (background) or 1 (foreground).
Once we have our binary image, we can proceed to perform morphological opening by using a structuring element. It will define the shape and size of the neighborhood for the erosion and dilation operations.
Additionally, we create an empty array with the same shape as the input image to store the result. Finally, we get the resultant opened image.
Example
In here, we are trying to perform opening process on an image by creating a random binary image −
import mahotas as mh import numpy as np from pylab import imshow, show # Create a random binary image image = np.random.randint(0, 2, size=(100, 50), dtype=np.bool_) Bc=np.ones((3,3)) # Perform morphological opening with a 3x3 cross structuring element result = mh.open(image, Bc, out=np.empty_like(image)) # Show the result imshow(result) show()
Output
The output produced is as shown below −
![Random Binary Image](/mahotas/images/random_binary_image.jpg)
Using a Grayscale Image
We can also perform opening process on a grayscale image in mahotas. To achive this, we simply pass as_grey=True parameter while reading the image, ensuring that the image is loaded as a grayscale image. Next, we will perform morphological opening on the grayscale image.
Example
Now, we are performing the opening process on a grayscale image in mahotas −
import mahotas as mh import numpy as np from pylab import imshow, show # Create a grayscale image image = mh.imread('nature.jpeg', as_grey = True).astype(np.uint8) Bc=np.ones((20,15)) # Perform morphological opening with a 3x3 cross structuring element result = mh.open(image, Bc, out=np.empty_like(image)) # Show the result imshow(result) show()
Output
Following is the output of the above code −
![Using Grayscale Image](/mahotas/images/using_grayscale_image.jpg)