Updated April 10, 2023
Introduction to OpenCV dilate
The following article provides an outline for OpenCV dilate. The set of operations that can process the images according to the shapes of the images are called morphological operations and performing morphological operations on a given image develops a structural element on the given image which removes noise from the image or settles down any imperfections to make the image very clear and performing convolution with kernel having an anchor point, of a particular shape in a given input image is called dilation using the size of the object in white color in the given image increases or the size of the object in black color in the given image decreases.
Syntax to define dilate() function in OpenCV:
dilate(source_image, kernel)
Where,
- source_image is the image which is to be dilated using dilate() function.
- kernel represents the kernel matrix.
Working of dilate() Function in OpenCV
- The process of performing convolution with kernel having anchor point of a particular shape in a given input image is called dilate() function in OpenCV.
- The dilate() function starts with computing the minimum pixel value by overlapping the kernel over the input image.
- And then the image is replaced by the kernel anchor at the center.
- This causes the white regions in the image to grow bigger in size and the image size increases as well.
- The dilate() function returns an image with increased size of white shade in the given image.
Examples of OpenCV dilate
Given below are the examples of OpenCV dilate:
Example #1
OpenCV program in python to demonstrate dilate() function to read the given image and increase the size of white region in the image and display the resulting image as the output on the screen.
Code:
#importing all the required modules
import numpy as np
import cv2 as cv
#reading the image that is to be dilated using imread() function
imageread = cv.imread('C:/Users/admin/Desktop/logo2.png')
#defining the matrix for kernel to apply dilate() function on the image to dilate the image
kernelmatrix = np.ones((5, 5), np.uint8)
#applying dilate() function on the image to dilate the image and display it as the output on the screen
resultimage = cv.dilate(imageread, kernelmatrix)
cv.imshow('Dilated_image', resultimage)
cv.waitKey(0)
cv.destroyAllWindows()
Output:
In the above program, we are importing the required modules. Then we are reading the image which is to be dilated, using imread() function. Then we are making use of use of dilate() function to dilate the image. Then we are displaying the dilated image as the output on the screen.
Example #2
OpenCV program in python to demonstrate dilate() function to read the given image and increase the size of white region in the image and display the resulting image as the output on the screen.
Code:
#importing all the required modules
import numpy as np
import cv2 as cv
#reading the image that is to be dilated using imread() function
imageread = cv.imread('C:/Users/admin/Desktop/logo1.jpg')
#defining the matrix for kernel to apply dilate() function on the image to dilate the image
kernelmatrix = np.ones((5, 5), np.uint8)
#applying dilate() function on the image to dilate the image and display it as the output on the screen
resultimage = cv.dilate(imageread, kernelmatrix)
cv.imshow('Dilated_image', resultimage)
cv.waitKey(0)
cv.destroyAllWindows()
Output:
In the above program, we are importing the required modules. Then we are reading the image which is to be dilated, using imread() function. Then we are making use of use of dilate() function to dilate the image. Then we are displaying the dilated image as the output on the screen.
Example #3
OpenCV program in python to demonstrate dilate() function to read the given image and increase the size of white region in the image and display the resulting image as the output on the screen.
Code:
#importing all the required modules
import numpy as np
import cv2 as cv
#reading the image that is to be dilated using imread() function
imageread = cv.imread('C:/Users/admin/Desktop/educba1.jpg')
#defining the matrix for kernel to apply dilate() function on the image to dilate the image
kernelmatrix = np.ones((5, 5), np.uint8)
#applying dilate() function on the image to dilate the image and display it as the output on the screen
resultimage = cv.dilate(imageread, kernelmatrix)
cv.imshow('Dilated_image', resultimage)
cv.waitKey(0)
cv.destroyAllWindows()
Output:
In the above program, we are importing the required modules. Then we are reading the image which is to be dilated, using imread() function. Then we are making use of use of dilate() function to dilate the image. Then we are displaying the dilated image as the output on the screen.
Example #4
OpenCV program in python to demonstrate dilate() function to read the given image and increase the size of white region in the image and display the resulting image as the output on the screen.
Code:
#importing all the required modules
import numpy as np
import cv2 as cv
#reading the image that is to be dilated using imread() function
imageread = cv.imread('C:/Users/admin/Desktop/lo.jpg')
#defining the matrix for kernel to apply dilate() function on the image to dilate the image
kernelmatrix = np.ones((5, 5), np.uint8)
#applying dilate() function on the image to dilate the image and display it as the output on the screen
resultimage = cv.dilate(imageread, kernelmatrix)
cv.imshow('Dilated_image', resultimage)
cv.waitKey(0)
cv.destroyAllWindows()
Output:
In the above program, we are importing the required modules. Then we are reading the image which is to be dilated, using imread() function. Then we are making use of use of dilate() function to dilate the image. Then we are displaying the dilated image as the output on the screen.
Recommended Articles
We hope that this EDUCBA information on “OpenCV dilate” was beneficial to you. You can view EDUCBA’s recommended articles for more information.