Dilation of an image is the process by which the object area in the image is increased. This process is used to accentuate features in the image. It increases the white region in the image or the size of the foreground object increases.
Working of dilation:
OpenCV is an open-source library that has a large number of computer vision algorithms.
To install the Python interface for OpenCV, we can use pip
.
pip install opencv-python
dilate()
functionThe dilate()
function of OpenCV is used to apply the dilation operation on the given image with the specified kernel.
cv2.dilate(img, kernel, iterations)
img
: The image to apply the dilation on.kernel
: The kernel to use.iterations
: The number of iterations of dilations to be performed.Refer to the following coding example to understand more.
import cv2, numpy as npimg = cv2.imread("/test.png")kernel = np.ones((7, 7), np.uint8)dilated_img = cv2.dilate(img, kernel, iterations=1)cv2.imwrite("output/Test-image.png", img)cv2.imwrite("output/dilated-image.png", dilated_img)
opencv
and numpy
packages are imported.test.png
image is read into memory.cv2.dilate()
method.The output image dilated-image.png
shows a reduction in the object area.