Computer >> Computer tutorials >  >> Programming >> Python

Dilating images using the OpenCV function dilate()


In this program, we will dilate an image using the dilate function in the OpenCV library. Dilation adds pixels to the boundaries of objects in an image, i.e., it expands the image on all sides.

Original Image

Dilating images using the OpenCV function dilate()

Algorithm

Step 1: Import cv2 and numpy.
Step 2: Read the image using opencv.imread().
Step 3: Define the kernel using np.ones() function.
Step 4: Pass the image and kernel to the dilate() function.
Step 5: Display the image

Example Code

import cv2
import numpy as np

image = cv2.imread('testimage.jpg')
kernel = np.ones((3,3), np.uint8)

image = cv2.dilate(image, kernel)
cv2.imshow('Dilated Image', image)

Output

Dilating images using the OpenCV function dilate()

Explanation

As you can see, the image is expanded, i.e., the pixels of the image are expanded and hence, the image looks a bit distorted.