Image Dilation with OpenCV


Python OpenCV - Image Dilation

In this tutorial, you will learn how to use the cv2.dilate() function to dilate a given image, with examples.

Dilated image using Python OpenCV

What is image dilation?

Dilation is a process that adds pixels to the boundaries of objects in an image.

What does dilation do to an image?

Dilation fills in small holes or gaps, to join broken segments of lines, and to make objects more uniform in size.

What does dilation do to pixels in an image?

In dilation, the value of a pixel is changed to the maximum value of its neighboring pixels.

How to dilate an image with OpenCV?

To dilate an image with OpenCV in Python, you can use the cv2.dilate() function.

Syntax of cv2.dilate()

The syntax of cv2.dilate() function to dilate an image is

cv2.dilate(src, kernel, dts, anchor, iterations, borderType, borderValue)

where

ParameterDescription
srcInput/source image array.
kernelA matrix. Kernel used for dilation.
dts[Optional] Output image array of the same size and type as src.
anchor[Optional] Position of the anchor within the element; default value (-1, -1) means that the anchor is at the element center.
iterations[Optional] The number of times dilation is applied.
borderType[Optional] Pixel extrapolation method.
borderValue[Optional] Border value in case of a constant border.

In the examples that we cover, we do not use the optional parameters. We shall cover some examples covering iterations parameter.

cv2.dilate(src, kernel, iterations=1)

Steps to dilate an image

  1. Read an image into an array using cv2.imread() function.
  2. Define a kernel for dilation. Kernel is a matrix, usually with an odd number of rows and odd number of columns.
  3. Call cv2.dilate() function and pass the input image array and kernel as arguments. The function returns the dilated image array.
  4. Save the dilated image using cv2.imwrite() function.

Examples

In the following examples, we shall use the below input image for dilation.

test_image_house.jpg

1. Dilate image using 3x3 kernel

In the following program, we dilate the given image with a 3x3 kernel of ones.

Python Program

import cv2
import numpy as np

# Load the image
img = cv2.imread('test_image_house.jpg')

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

# Perform image dilation
dilated = cv2.dilate(img, kernel)

# Save dilated image
cv2.imwrite('dilated.jpg', dilated)

dilated.jpg

Python OpenCV - Dilate image with kernel of size 3x3

2. Dilate image using 5x5 kernel

In the following program, we dilate the given image with a 5x5 kernel of ones.

Python Program

import cv2
import numpy as np

# Load the image
img = cv2.imread('test_image_house.jpg')

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

# Perform image dilation
dilated = cv2.dilate(img, kernel)

# Save dilated image
cv2.imwrite('dilated.jpg', dilated)

dilated.jpg

Python OpenCV - Dilate image with kernel of size 5x5

3. Dilate image using kernel with different number of rows and columns

In the following program, we take a kernel of size 3x7, and apply dilation to the given image.

Python Program

import cv2
import numpy as np

# Load the image
img = cv2.imread('test_image_house.jpg')

# Define the structuring element
kernel = np.ones((3,7),np.uint8)

# Perform image dilation
dilated = cv2.dilate(img, kernel)

# Save dilated image
cv2.imwrite('dilated.jpg', dilated)

dilated.jpg

Python OpenCV - Dilate image with rectangular kernel

4. Dilate image with multiple iterations

By default, only one iteration of dilation is done. But, we can increase the number of iterations, by specifying the required iterations (integer) value to the cv2.dilate() function.

Python Program

import cv2
import numpy as np

# Load the image
img = cv2.imread('test_image_house.jpg')

# Define the structuring element
kernel = np.ones((5,5),np.uint8)

# Perform image dilation
dilated = cv2.dilate(img, kernel, iterations=3)

# Save dilated image
cv2.imwrite('dilated.jpg', dilated)

dilated.jpg

Python OpenCV - Dilate image with multiple iterations

Summary

In this Python OpenCV Tutorial, we did image dilation on a given image using cv2.dilate() function, with the help of examples.