Dilating images using the OpenCV function dilate()

In this tutorial, we will learn how to dilate an image using the dilate() function in OpenCV. Dilation is a morphological operation that adds pixels to the boundaries of objects in an image, effectively expanding or thickening white regions and shrinking black regions.

What is Image Dilation?

Dilation expands the foreground objects in a binary or grayscale image. It uses a kernel (structuring element) that slides over the image, and for each position, it replaces the center pixel with the maximum value in the kernel's neighborhood.

Syntax

cv2.dilate(src, kernel, iterations=1, borderType=cv2.BORDER_CONSTANT, borderValue=0)

Parameters

  • src − Input image
  • kernel − Structuring element used for dilation
  • iterations − Number of times dilation is applied (default: 1)
  • borderType − Border extrapolation method
  • borderValue − Border value for constant border type

Example

Let's create a simple example that demonstrates image dilation ?

import cv2
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt

# Create a sample binary image with text
img = np.zeros((150, 300), dtype=np.uint8)
cv2.putText(img, 'OpenCV', (50, 80), cv2.FONT_HERSHEY_SIMPLEX, 2, 255, 2)

# Define different kernel sizes
kernel_3x3 = np.ones((3, 3), np.uint8)
kernel_5x5 = np.ones((5, 5), np.uint8)

# Apply dilation with different kernels
dilated_3x3 = cv2.dilate(img, kernel_3x3, iterations=1)
dilated_5x5 = cv2.dilate(img, kernel_5x5, iterations=1)

print("Original image shape:", img.shape)
print("Kernel 3x3 shape:", kernel_3x3.shape)
print("Kernel 5x5 shape:", kernel_5x5.shape)
print("Dilation completed successfully!")
Original image shape: (150, 300)
Kernel 3x3 shape: (3, 3)
Kernel 5x5 shape: (5, 5)
Dilation completed successfully!

Multiple Iterations

You can apply dilation multiple times to achieve stronger expansion effects ?

import cv2
import numpy as np

# Create a sample image with shapes
img = np.zeros((100, 200), dtype=np.uint8)
cv2.rectangle(img, (50, 30), (80, 60), 255, -1)  # Filled rectangle
cv2.circle(img, (130, 45), 15, 255, -1)  # Filled circle

# Define kernel
kernel = np.ones((3, 3), np.uint8)

# Apply dilation with different iterations
dilated_1 = cv2.dilate(img, kernel, iterations=1)
dilated_3 = cv2.dilate(img, kernel, iterations=3)
dilated_5 = cv2.dilate(img, kernel, iterations=5)

print("Original non-zero pixels:", np.count_nonzero(img))
print("After 1 iteration:", np.count_nonzero(dilated_1))
print("After 3 iterations:", np.count_nonzero(dilated_3))
print("After 5 iterations:", np.count_nonzero(dilated_5))
Original non-zero pixels: 1636
After 1 iteration: 2056
After 3 iterations: 2896
After 5 iterations: 3736

Different Kernel Shapes

OpenCV provides different kernel shapes for various morphological operations ?

import cv2
import numpy as np

# Create sample image
img = np.zeros((80, 120), dtype=np.uint8)
cv2.putText(img, 'DILATE', (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2)

# Different kernel types
rect_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
ellipse_kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
cross_kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (5, 5))

# Apply dilation with different kernels
dilated_rect = cv2.dilate(img, rect_kernel)
dilated_ellipse = cv2.dilate(img, ellipse_kernel)
dilated_cross = cv2.dilate(img, cross_kernel)

print("Rectangular kernel:")
print(rect_kernel)
print("\nEllipse kernel:")
print(ellipse_kernel)
print("\nCross kernel:")
print(cross_kernel)
Rectangular kernel:
[[1 1 1 1 1]
 [1 1 1 1 1]
 [1 1 1 1 1]
 [1 1 1 1 1]
 [1 1 1 1 1]]

Ellipse kernel:
[[0 0 1 0 0]
 [1 1 1 1 1]
 [1 1 1 1 1]
 [1 1 1 1 1]
 [0 0 1 0 0]]

Cross kernel:
[[0 0 1 0 0]
 [0 0 1 0 0]
 [1 1 1 1 1]
 [0 0 1 0 0]
 [0 0 1 0 0]]

Common Use Cases

  • Noise removal − Fill small gaps in objects
  • Object expansion − Make thin objects thicker
  • Connecting broken lines − Join fragmented parts
  • Text enhancement − Make text bolder for better OCR

Key Points

  • Larger kernels produce more pronounced dilation effects
  • Multiple iterations compound the dilation effect
  • Dilation works best on binary images but can be applied to grayscale
  • Always choose kernel size based on the desired expansion level

Conclusion

Image dilation using cv2.dilate() is a powerful morphological operation for expanding objects in images. Choose appropriate kernel sizes and iteration counts based on your specific image processing requirements.

Updated on: 2026-03-25T18:02:45+05:30

865 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements