Updated April 3, 2023
Introduction to OpenCV erode
The operations performed on the binary images based on the shape of the images are called morphological operations and there are two basic morphological operations namely erosion and dilation where the process of eroding away the boundaries of the foreground object is called erosion using which the small white noises in the image can be removed, the two connected objects can be detached, etc. and we make use of a function called erode() function by providing kernel value to perform erosion of the given image in OpenCV and the erode() function returns an image with erosion performed on it.
Syntax to define erode() function in OpenCV is as follows:
erode(input_image, kernel, anchor, iterations, bordertype, bordervalue)
where input_image is the image that is to be eroded,
- kernel represents the kernel matrix,
- anchor represents integer type variable representing anchor point,
- iterations represent the number of times erosion is applied,
- bordertype represents the type of border to be added and
- bordervalue represents the value of the constant border.
How erode() function works in OpenCV?
Working of erode() function in OpenCV is as follows:
- The erode() function in OpenCV is used to perform erosion of images.
- The erosion of images is mostly performed on binary images.
- A kernel is a small matrix used for blurring of images, sharpening of images, detection of edges in the images, etc. in image processing.
- The working of erode() function begins by the kernel sliding over the image.
- If the value of the pixels under the image is 1, the pixel in the original image is also made 1.
- If the value of the pixels under the image is not 1, the pixel in the original image is made 0, also called eroded.
- Hence the white region in a given image decreases as the pixels near the region of boundary will be discarded.
- The erode() function returns an image with erosion performed on the image.
Examples
Let us discuss examples of OpenCV erode.
Example #1
OpenCV program in python to demonstrate erode() function to read the given image using imread() function, perform erosion of the image using erode() function, and display the eroded image as the output on the screen:
#importing the required modules
import cv2
import numpy as np
#reading the image which is to be eroded using imread() function
imageread = cv2.imread('C:/Users/admin/Desktop/images/lo.jpg')
#defining the kernel matrix
kernel = np.ones((5,5),np.uint8)
#using erode function on the input image to be eroded
erodedimage = cv2.erode(imageread,kernel,iterations = 1)
#displaying the eroded image as the output on the screen
cv2.imshow('Eroded_image', erodedimage)
cv2.waitKey(0)
The output of the given program is shown in the snapshot below:
In the above program, we are importing the required modules. Then we are reading the image which is to be eroded using imread() function. Then we are defining the kernel matrix. Then we are making use of erode() function to perform erosion of images. Then we are displaying the eroded image as the output on the screen. The output is shown in the snapshot above.
Example #2
OpenCV program in python to demonstrate erode() function to read the given image using imread() function, perform erosion of the image using erode() function, and display the eroded image as the output on the screen:
#importing the required modules
import cv2
import numpy as np
#reading the image which is to be eroded using imread() function
imageread = cv2.imread('C:/Users/admin/Desktop/images/educba.jpg')
#defining the kernel matrix
kernel = np.ones((5,5),np.uint8)
#using erode function on the input image to be eroded
erodedimage = cv2.erode(imageread,kernel,iterations = 1)
#displaying the eroded image as the output on the screen
cv2.imshow('Eroded_image', erodedimage)
cv2.waitKey(0)
The output of the given program is shown in the snapshot below:
In the above program, we are importing the required modules. Then we are reading the image which is to be eroded using imread() function. Then we are defining the kernel matrix. Then we are making use of erode() function to perform erosion of images. Then we are displaying the eroded image as the output on the screen. The output is shown in the snapshot above.
Example #3
OpenCV program in python to demonstrate erode() function to read the given image using imread() function, perform erosion of the image using erode() function, and display the eroded image as the output on the screen:
#importing the required modules
import cv2
import numpy as np
#reading the image which is to be eroded using imread() function
imageread = cv2.imread('C:/Users/admin/Desktop/images/educbalogo.jpg')
#defining the kernel matrix
kernel = np.ones((5,5),np.uint8)
#using erode function on the input image to be eroded
erodedimage = cv2.erode(imageread,kernel,iterations = 1)
#displaying the eroded image as the output on the screen
cv2.imshow('Eroded_image', erodedimage)
cv2.waitKey(0)
The output of the given program is shown in the snapshot below:
In the above program, we are importing the required modules. Then we are reading the image which is to be eroded using imread() function. Then we are defining the kernel matrix. Then we are making use of erode() function to perform erosion of images. Then we are displaying the eroded image as the output on the screen. The output is shown in the snapshot above.
Example #4
OpenCV program in python to demonstrate erode() function to read the given image using imread() function, perform erosion of the image using erode() function, and display the eroded image as the output on the screen:
#importing the required modules
import cv2
import numpy as np
#reading the image which is to be eroded using imread() function
imageread = cv2.imread('C:/Users/admin/Desktop/images/educbalogo.jpg')
#defining the kernel matrix
kernel = np.ones((5,5),np.uint8)
#using erode function on the input image to be eroded
erodedimage = cv2.erode(imageread,kernel,iterations = 1)
#displaying the eroded image as the output on the screen
cv2.imshow('Eroded_image', erodedimage)
cv2.waitKey(0)
The output of the given program is shown in the snapshot below:
In the above program, we are importing the required modules. Then we are reading the image which is to be eroded using imread() function. Then we are defining the kernel matrix. Then we are making use of erode() function to perform erosion of images. Then we are displaying the eroded image as the output on the screen. The output is shown in the snapshot above.
Conclusion
In this article, we have learned the concept of erosion using erode() function with corresponding programming examples and their outputs to demonstrate them.
Recommended Articles
This is a guide to OpenCV erode. Here we discuss the Introduction, syntax, Working of erode() function in OpenCV, and examples. You may also have a look at the following articles to learn more –