Learn Mahotas – Closing Holes in an Image work project make money

Mahotas – Closing Holes in an Image



Closing holes in an image refers to removing small gaps or holes in the foreground regions of an image. In the closing process, first a dilation operation is performed, followed by an erosion operation.

Dilation expands the boundaries of the foreground regions. For each pixel, the dilation operation examines its neighbors according to the structuring element.

If any neighboring pixel is white, the center pixel is also turned white. This process helps to fill in gaps or holes within the foreground regions.

After dilation, erosion shrinks the boundaries of the foreground regions. Again, using the structuring element, erosion examines each pixel and its neighbors. If any neighboring pixel is black, the center pixel is turned black.

This step helps to refine and smooth the contours of the foreground regions while preserving the main structures.

Closing Holes in an Image in Mahotas

To perform the closing holes process in Mahotas, we use the mahotas.close_holes() function. This method allows for the sequential application of dilation and erosion operations.

By applying the dilation operation first (to expand the regions) and then the erosion operation (to refine the boundaries), the closing operation effectively closes small holes or gaps in the foreground regions of the binary image.

The mahotas.close_holes() function

The close_holes() function in Mahotas takes the binary image as an input and returns the resulting image with the holes closed.

The close_holes() function automatically detects and closes the holes without the need for explicitly defining a structuring element.

Syntax

Following is the basic syntax of the close_holes() function in mahotas −

mahotas.close_holes(ref, Bc=None)

Where,

  • ref − It is the input binary image.

  • Bc − It specifies the structuring element or kernel used for the closing operation. If not provided (set to None), a default structuring element will be used.

Example

In the following example, we are closing the holes in an image −

import mahotas as mh
import numpy as np
import matplotlib.pyplot as plt
image = mh.imread(''pic.jpg'',as_grey = True)
closed_holes_image = mh.close_holes(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 holes image
axes[1].imshow(closed_holes_image, cmap=''gray'')
axes[1].set_title(''Closed Holes 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 −

Closing Holes Image

Using Random Binary Image

We can also perform closing holes’ process in 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. Finally, we get the resultant closed holes in an image.

Example

In here, we are trying to perform closing holes’ process in 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 holes with a 3x3 cross structuring element
result = mh.close_holes(image, Bc)
# Show the result
imshow(result)
show()

Output

The output produced is as shown below −

Closing Holes Image

Closing Holes with Multiple Iterations

For closing holes with multiple iterations, we aim to gradually fill in smaller holes and refine the result repeatedly.

We start by creating a copy of the original binary image. This copy will serve as the starting point for each iteration of the hole closing process. Then, using a for loop, we iterate a specified number of times.

During each iteration, the mahotas.close_holes() function is applied to the current version of the image. The function identifies holes in the image and fills them, gradually improving the connectivity and continuity of the foreground regions.

Example

Now, we are closing the holes in an image with multiple iterations −

import mahotas
import numpy as np
import matplotlib.pyplot as plt
image = mahotas.imread(''pic.jpg'', as_grey = True)
# Close the holes in the binary image with multiple iterations
closed_image = image.copy()
for i in range(3):
   closed_image = mahotas.close_holes(closed_image)
# Display the original and closed images
fig, axes = plt.subplots(1, 2, figsize=(10, 5))
axes[0].imshow(image, cmap=''gray'')
axes[0].set_title(''Original Image'')
axes[1].imshow(closed_image, cmap=''gray'')
axes[1].set_title(''Closed Image'')
plt.tight_layout()
plt.show()

Output

Following is the output of the above code −

Closing Holes Multiple Images

Leave a Reply

Your email address will not be published. Required fields are marked *