The closing process is reverse of the opening process. In this, first a dilation operation is performed, followed by an erosion operation. Dilation examines each pixel and its neighboring pixels.
If any neighboring pixel is white, the center pixel is also turned white. This step helps expand or thicken the image and can fill in small gaps or holes.
After dilation, erosion examines each pixel and its neighboring pixels. If any neighboring pixel is black, the center pixel is also turned black. This step helps shrink or remove unwanted details, such as thin protrusions and internal noise.
The closing operation is effectively closes small holes or gaps in binary images, and smoothens or eliminates the small foreground regions.
Closing Process on Image in Mahotas
To perform the closing process in Mahotas, we use the mahotas.close() function. This method allows for the sequential application of dilation and erosion operations.
Dilation expands the remaining structures while preserving their key features by replacing each pixel with the maximum value in its neighborhood.
Subsequently, erosion reduces noise and eliminates small structures by considering each pixel”s neighborhood and replacing it with the minimum value.
The mahotas.close() function
The close() function in Mahotas takes two main arguments− the binary image and the structuring element (kernel). The function first applies a dilation operation to the input binary image. It then applies an erosion operation to the dilated image.
The close() function returns the resulting closed image after the dilation and erosion operations have been performed.
Syntax
Following is the basic syntax of the close() function in mahotas −
mahotas.close(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 dilation and erosion 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 closing process on an image −
import mahotas as mh import numpy as np import matplotlib.pyplot as plt image = mh.imread(''sun.png'') closed_image = mh.close(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 closed image axes[1].imshow(closed_image, cmap=''gray'') axes[1].set_title(''Closed 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 aperform closing 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 closing by using a structuring element. It will define the shape and size of the neighborhood for the dilation and erosion operations.
Additionally, we create an empty array with the same shape as the input image to store the result. Finally, we get the resultant closed image.
Example
In here, we are trying to perform closing 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 closing with a 3x3 cross structuring element result = mh.close(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 closing 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 closing on the grayscale image.
Example
Now, we are performing the closing 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 closing with a 3x3 cross structuring element result = mh.close(image, Bc, out=np.empty_like(image)) # Show the result imshow(result) show()
Output
Following is the output of the above code −