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 3×3 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 −
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 −
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 −