Introduction to OpenCV erosion
OpenCV erosion() is a command which is present in the OpenCV public domain library, which is enabled to help the user to morphologically process the images that the user wants to modify. The procedure is used for making modifications in the geometric structure for the provided image. During this process, the system finds the exact shape or structural integrity along with the size of the object present in the image. This operation is majorly defined with respect to images present in binary format, but their extended use can even be made on gray scale images.
Syntax of OpenCV erosion()
Following is the syntax which can to be used in order to make utilization of the function OpenCV erosion():
cv2 . erode ( src , dst , kernel )
The OpenCV erosion function accepts the following parameters:
|Parameter||Description of Parameter|
|src||This parameter is used for representing the image which is being inputted by the user or the source image. It is an object belonging to the class Mat.|
|dst||This parameter is used to represent the final output image produced by the system after the function has been performed on it or, Alternatively, the destination image. It is an object belonging to the class Mat.|
|kernel||This parameter is a variable having an integer value which is used for representing the kernel value.|
|Return Value||This method is responsible for returning an output image it is to be loaded from the file which has been specified.|
What is the Action Performed by erosion Function, and How does it Work?
In the process of erosion, the total area which is used by the object is decreased. Moreover, with respect to the dilation function, the erosion function has a greater advantage in that it has the capability to remove white noise that is present within the image, and at the same time, results in shrinkage of the size of the image. Ford, the user, to get even more effective results, they can first perform the erosion function using the OpenCV erode function and then use the dilation function to remove the image and increase the total area occupied by the object present in it the image. This function is also utilized at times to join some of the parts of a specific object present within the image.
- Generally, the first process is the formation of a kernel with respect to the image that the user has provided. Usually, the matrix with respect to the kernel formed has an odd value order (for instance, 1, 3, 5, 7, 9, etc.).
- For the provided image, pixel size is chosen. For an image where all the pixels are seen to be under the kernel weather order value is 1; for such an image, the pixel to be chosen is 1.
- The function is also responsible for increasing the overall white regions present in the image or the evidence size with respect to the objects in the foreground present within the provided image.
Example of OpenCV erosion
Given below is the example which can be used to illustrate how to use the OpenCV erosion command in the coding initiate:
# command used to import the OpenCV library to utilize OpenCV erosion function
# command used to import the numpy library to utilize np_array commands
import numpy as np1 * * * * * *
# command used for reading an image from the disk that has been instructed by the user
img1 = cv2.imread(r'C:\Users\PRIYANKA BANERJEE\Edu CBA logo.jpg', 1)
# declaring the kernel value * * * * * *
kernel1 = np1.ones((50,50), np1.uint8)
# using the OpenCV erode command to morphologically process the images that user wants to modify * * * * * *
img1_erosion1 = cv2.erode(img1, kernel1, iterations=1) * * * * * *
# convoluting along with kernels * * * * * *
img1_dilation1 = cv2.dilate(img1, kernel1, iterations=1) * * * * * *
# displaying the original image being processed by the user
print ("The original image being processed by the user :"; /n)
cv2.imshow('Input', img1) * * * * * *
# displaying the final image being processed by the user
print ("The final image being processed by the user : "; /n)
cv2.imshow('Erosion', img1_erosion1) * * * * * *
The user processes the original image:
The final image being processed by the user:
How is the Output Different while using OpenCV erosion Command?
- The output performed by OpenCV erode() is quite similar to OpenCV dilate function. The major change that can be noted is that the calculated pixel value is the minimum value in contrast to the maximum value, which is calculated when using the OpenCV dilate function.
- The processed image is substituted underneath the anchor terminal with respect to the calculated value for the minimum pixel outstanding range. Also, the resultant image after the processing can be seen to have comparatively darker regions or shades with increasing contrast or hue, and the resultant image will be comparatively less bright, and the white objects would look washed off.
The OpenCV erosion function is seen to be widely used in removing the noise from the images that are being processed. This command is also used to identify the intensity collisions or holes present in the images being processed and isolate individual components present in an image, simultaneously assembling dissimilar rudiments present in an image being processed by the designers.
This is a guide to OpenCV erosion. Here we discuss action performed by erosion and how does it work? Example & how is the output different. You may also have a look at the following articles to learn more –