Erosion and Dilation of images using OpenCV in Python

Last Updated : 12 Jun, 2026

Morphological operations process images based on the arrangement of neighboring pixels. Erosion and dilation are two fundamental techniques used to modify object boundaries, remove noise, fill gaps, and refine shapes in binary and grayscale images.

  • Boundary Refinement: Modify object size and shape by shrinking or expanding regions.
  • Noise Reduction: Remove small unwanted regions and improve image quality.

Erosion

Erosion shrinks foreground regions by removing pixels from object boundaries. It is commonly used to eliminate small white noise, separate connected objects, and reduce the size of foreground features in an image.

Code Implementation:

Used image can be downloaded from here.

  • Create a 5×5 kernel that defines the neighborhood used for the erosion operation.
  • Apply cv2.erode() to remove pixels from object boundaries, reducing the size of foreground regions and eliminating small white noise.
  • Display the processed image to observe the effect of boundary shrinking.
Python
import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread('input.png', 0)

kernel = np.ones((5, 5), np.uint8)

img_erosion = cv2.erode(img, kernel, iterations=1)

plt.figure(figsize=(10, 5))

plt.subplot(1, 2, 1)
plt.imshow(img, cmap='gray')
plt.title("Original Image")
plt.axis('off')

plt.subplot(1, 2, 2)
plt.imshow(img_erosion, cmap='gray')
plt.title("After Erosion")
plt.axis('off')

plt.tight_layout()
plt.show()

Output:

erorion

The output image contains thinner foreground regions because pixels along object boundaries are removed.

Dilation

Dilation expands foreground regions by adding pixels to object boundaries. It is commonly used to fill small gaps, connect nearby objects, and enhance the visibility of foreground features.

Code Implementation:

  • Create a 5×5 kernel that defines the neighborhood used for the dilation operation.
  • Apply cv2.dilate() to add pixels around object boundaries, increasing the size of foreground regions and filling small gaps.
  • Display the processed image to observe the effect of boundary expansion.
Python
import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread('input.png', 0)

kernel = np.ones((5, 5), np.uint8)

img_dilation = cv2.dilate(img, kernel, iterations=1)

plt.figure(figsize=(10, 5))

plt.subplot(1, 2, 1)
plt.imshow(img, cmap='gray')
plt.title("Original Image")
plt.axis('off')

plt.subplot(1, 2, 2)
plt.imshow(img_dilation, cmap='gray')
plt.title("After Dilation")
plt.axis('off')

plt.tight_layout()
plt.show()

Output:

dilation

The output displays the original image alongside the dilated image, where foreground regions appear larger due to the addition of boundary pixels.

Difference Between Erosion and Dilation

FeatureErosionDilation
Main PurposeShrinks foreground objectsExpands foreground objects
Visual EffectObjects appear smaller and thinnerObjects appear larger and thicker
Edge BehaviorRemoves pixels from object boundariesAdds pixels to object boundaries
Noise HandlingRemoves small white noise and unwanted detailsFills small gaps and holes in objects
ApplicationsNoise removal, object separation, boundary refinementGap filling, object enhancement, object connection
Comment