Eroding an image in image processing refers to shrinking the pixels of an image.
The erosion process will remove pixels around the edges of an image. This operation scans the image and checks if all the pixels within the neighborhood of a particular pixel are foreground pixels. If they are, the pixel is eroded or removed.
Eroding an Image in Mahotas
By eroding an image in Mahotas, we refer to removing the number of pixels to the boundaries of objects or regions in an image. This operation is commonly used to modify the shapes and structures in an image.
We can erode an image in mahotas using the erode() function. It is used to shrink an element A by using structuring element B.
A structuring element is a small matrix or shape that defines the neighborhood around each pixel. It is used to determine which pixels should be considered during the erosion process.
The mahotas.erode() function
The mahotas.erode() function takes the input image and the structuring element as arguments, and returns a new Numpy array.
The value of the output pixel is determined as the minimum value of all the pixels in the neighborhood. A pixel is set to 0, if any neighbourhood pixels have a value of 0.
The erode() function scans the image, pixel by pixel, and checks the neighborhood defined by the structuring element.
If any neighboring pixels are part of an object, the erosion operation removes those pixels to the object”s boundary, making it smaller.
Syntax
Following is the basic syntax of the erode() function in mahotas −
mahotas.erode(A, Bc={3x3 cross}, out={np.empty_as(A)})
where,
-
A − It is the input image on which erosion will be performed. It should be a NumPy array representing a grayscale image.
-
Bc (optional) − It is the structuring element used for erosion. By default, it is set to a 3×3 cross−shaped structuring element.
-
out (optional) − It specifies the output array to store the result.
Example
Following is the basic example to erode an image in mahotas using the erode() function −
import mahotas as mh import matplotlib.pyplot as plt import numpy as np image = mh.imread(''nature.jpeg'', as_grey=True).astype(np.uint8) # Performing erosion with a square kernel of size 3x3 eroded_image = mh.erode(image, Bc=mh.disk(3)) # 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 eroded image axes[1].imshow(eroded_image, cmap=''gray'') axes[1].set_title(''Eroded 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 output as follows −
Erosion with Varying Structuring Element Sizes
We can also erode an image using different structuring element sizes to modify different aspects of the image. These elements are created with different sizes using the Mahotas disk() function.
By choosing different radii or sizes for the structuring elements, we can achieve diverse effects. To begin, we perform the erosion using the largest structuring element. Then, we continue with additional dilations using smaller structuring elements.
Each erosion operation further shrinks the objects in the image by removing pixels from their boundaries.
Example
In here, we are trying to erode an image with varying structuring element sizes −
import mahotas as mh import numpy as np from pylab import imshow, show image = mh.imread(''nature.jpeg'', as_grey=True).astype(np.uint8) # Performing erosion with the largest structuring element largest_se = mh.disk(8) eroded_image = mh.erode(image, Bc=largest_se) # Performing additional erosions with smaller structuring elements smaller_se_1 = mh.disk(2) smaller_se_2 = mh.disk(5) eroded_image = mh.erode(eroded_image, Bc=smaller_se_1) eroded_image = mh.erode(eroded_image, Bc=smaller_se_2) # Displaying the eroded image imshow(eroded_image) show()
Output
The output obtained is as shown below −
Erosion with a Circular-shaped Kernel
To create a circular−shaped kernel, we can use the disk() function from Mahotas. By specifying the desired radius, this function generates a NumPy array representing the circular kernel.
Once we have the image and circular kernel ready, we can proceed to perform erosion. This operation applies the circular kernel to each pixel of the image, shrinking the foreground pixels accordingly.
In simpler terms, it it shrinks the regions based on the connectivity defined by the circular kernel.
Example
Now, we are dilating an image with a circular−shaped kernel −
import mahotas as mh import numpy as np from pylab import imshow, show # Load image image = mh.imread(''sun.png'', as_grey=True).astype(np.uint8) # Circular kernel with radius 10 radius = 10 kernel = mh.disk(radius) eroded_image = mh.erode(image, kernel) # Display the eroded image imshow(eroded_image) show()
Output
Following is the output of the above code −