Scikit Image - Erosion



Erosion is a fundamental operation in mathematical morphology that reduces the size of bright regions and enlarges dark regions in an image. It works by setting each pixel in the output image to the minimum value over all pixels within a local neighborhood centered around it.

Erosion operation is widely used in image processing for various applications, including image segmentation, feature extraction, and object detection.

The scikit-image library provides the functions like morphology.erosion() and morphology.binary_erosion() to perform erosion and binary erosion operations on images.

Using the skimage.morphology.erosion() function

The skimage.morphology.erosion() function returns a grayscale morphological erosion of an input image.

Syntax

Following is the syntax of this function −

skimage.morphology.erosion(image, footprint=None, out=None, shift_x=False, shift_y=False)

Parameters

  • image (ndarray): The input image on which erosion will be performed.
  • footprint (ndarray or tuple, optional): The neighborhood to be used for erosion. It is expressed as a 2-D array of 1’s and 0’s. If None is provided, a cross-shaped footprint (connectivity=1) is used. You can also specify a custom footprint as a sequence of smaller footprints. For example, footprint=[(np.ones((9, 1)), 1), (np.ones((1, 9)), 1)] would apply a 9x1 footprint followed by a 1x9 footprint, resulting in the same effect as footprint=np.ones((9, 9)). This can help reduce the computational cost for certain footprints.
  • out (ndarray, optional): The output array where the result of the morphology will be stored. If None is passed, a new array will be allocated for the result.
  • shift_x and shift_y (bool, optional): These parameters control whether the footprint should be shifted about the center point. This only affects eccentric footprints (i.e., footprints with even-numbered sides).

Return Value

It returns an eroded array, which has the same shape as the input image.

Example

The following example demonstrates how to use the erosion() function on an image.

import numpy as np
import matplotlib.pyplot as plt
from skimage.morphology import square, erosion
from skimage.color import gray2rgb
from skimage import io

# Create an example input image 
bright_square = np.array([[0, 0, 0, 0, 0],
                          [0, 1, 1, 1, 0],
                          [0, 1, 1, 1, 0],
                          [0, 1, 1, 1, 0],
                          [0, 0, 0, 0, 0]], dtype=np.uint8)

# Perform erosion using a square-shaped footprint of size 3x3
result = erosion(bright_square, square(3))

# Create subplots to display the input and eroded images
fig, axes = plt.subplots(1, 2, figsize=(10, 5))
ax = axes.ravel()

# Display the input image
ax[0].imshow(bright_square, cmap=plt.cm.gray)
ax[0].set_title('Input Image')
ax[0].set_axis_off()

# Display the eroded image
ax[1].imshow(result, cmap=plt.cm.gray)
ax[1].set_title('Eroded Image')
ax[1].set_axis_off()
plt.tight_layout()
plt.show()
Output

On executing the above program, you will get the following output −

Using the skimage.morphology.binary_erosion() function

The morphology.binary_erosion() function is used for fast binary morphological erosion of a binary image. Binary erosion is similar to grayscale erosion, but it works faster for binary images. It shrinks the bright regions and enlarges the dark regions.

Syntax

Following is the syntax of this function −

skimage.morphology.binary_erosion(image, footprint=None, out=None)

Parameters

  • image (ndarray): This is the binary input image on which erosion will be performed.
  • footprint (ndarray or tuple, optional): The neighborhood to be used for erosion, expressed as a 2-D array of 1’s and 0’s. If None is provided, a cross-shaped footprint (connectivity=1) is used. Similar to grayscale erosion, you can also specify a custom footprint as a sequence of smaller footprints, which can help reduce the computational cost for certain footprints.
  • out (ndarray, optional): The output array where the result of the morphology will be stored. If None is passed, a new array will be allocated for the result.

Return Value

It returns an eroded binary image with values in [False, True].

Example

Here is an example that demonstrates how to use the binary_erosion() function on an image.

import numpy as np
import matplotlib.pyplot as plt
from skimage.morphology import square, binary_erosion

# Create an example input image 
binary_image = np.array([[0, 0, 0, 0, 0],
                          [0, 1, 1, 1, 0],
                          [0, 1, 1, 1, 0],
                          [0, 1, 1, 1, 0],
                          [0, 0, 0, 0, 0]], dtype=np.uint8)

# Perform binary erosion using a square-shaped footprint of size 3x3
result = binary_erosion(binary_image, square(3))

# Display the input and eroded binary images
fig, axes = plt.subplots(1, 2, figsize=(10, 5))
ax = axes.ravel()

# Display the input image
ax[0].imshow(binary_image, cmap=plt.cm.gray)
ax[0].set_title('Input Image')
ax[0].set_axis_off()

# Display the eroded image
ax[1].imshow(result, cmap=plt.cm.gray)
ax[1].set_title('Eroded Binary Image')
ax[1].set_axis_off()
plt.tight_layout()
plt.show()
Output

On executing the above program, you will get the following output −

Example

The following example performs both the grayscale and binary erosion on an image using scikit-image.

import matplotlib.pyplot as plt
from skimage.morphology import disk, binary_erosion, erosion
from skimage import io

# Load an input image
image = io.imread('Images/Circle2.jpg', as_gray=True)

# Create a binary image
threshold_value = 0.5 
binary_image = image > threshold_value

# Perform grayscale erosion 
grayscale_eroded = erosion(image)

# Perform binary erosion using a disk-shaped structuring element of radius 3
binary_eroded = binary_erosion(binary_image)

# Create subplots to display the input, grayscale eroded, and binary eroded images
fig, axes = plt.subplots(1, 3, figsize=(15, 5))
ax = axes.ravel()

# Display the input binary image
ax[0].imshow(image, cmap=plt.cm.gray)
ax[0].set_title('Input Binary Image')
ax[0].set_axis_off()

# Display the grayscale eroded image
ax[1].imshow(grayscale_eroded, cmap=plt.cm.gray)
ax[1].set_title('Grayscale Eroded Image')
ax[1].set_axis_off()

# Display the binary eroded image
ax[2].imshow(binary_eroded, cmap=plt.cm.gray)
ax[2].set_title('Binary Eroded Image')
ax[2].set_axis_off()

plt.tight_layout()
plt.show() 
Output

On executing the above program, you will get the following output −

Advertisements