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.
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
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)
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.