cv2 erode method Implementation in Python with Steps

cv2 erode method Implementation in Python with Steps

Opencv is the best library for image processing. It is widely used for solving computer vision problems. There are many functions in it for manipulating images. The cv2.erode() is one of them. This method is used to perform erosion on the image. It allows you to erode away the boundaries from the foreground of the image. In this entire tutorial you will learn how to implement cv2.erode() method with steps.

Syntax of the cv2 erode

Before going to the coding demonstration parts let’s know the syntax of the erode() method.

cv2.erode( src, kernel[, dst[, anchor[, iterations[, borderType[, borderValue]]]]] )

Explanation of the parameter

src: Input image.

kernel: structuring element used for erosion.

dst: output image of the same size and type as src.

anchor: position of the anchor within the element; default value (-1, -1) means that the anchor is at the element center.

iterations: number of times erosion is applied.

borderType: pixel extrapolation method. It can be cv2.BORDER_CONSTANT, cv2.BORDER_REFLECT, etc.

borderValue : border value in case of a constant border

Steps to implement cv2 erode

Step 1 : Import required packages

The first step is to import all the nessary python libraries. In this entire tutorial I will use only numpy and openCV. So lets import them using the import statement.

import cv2
import numpy as np

Step 2: Read the sample image

After importing the packages lets read the sample image you want to implement. You can read an image in OpenCV using the cv2.imread() method. Add  the below lines of code.

img = cv2.imread("thank_you.png")

You are reading the following image.

Sample image for implementing cv2 erode method
Sample image for implementing cv2 erode method

Step 3: Create a Kernel for the image

The third step is to create a kernel for the image using the NumPy module. Kernel transform the pixel values of the input image according to the dimensions defined in it. Append the following lines of code.

kernel_img = np.ones((5,5),dtype="uint8")

Step 4:  Apply the cv2 erode method

After reading the image and creating a kernel for the image let’s apply the cv2.erode() method to the input image. Just pass the input image and the kernel image inside the method. Execute the below lines of code and see the output.

Simple implementation of cv2 erode method
Simple implementation of cv2 erode method

You can also define the border type for the input image. To do so you have to just pass the borderType parameters. Here i am using cv2.BORDER_REFLECT.  Run the following lines of code and see the output.

import cv2
import numpy as np
img = cv2.imread("thank_you.png")
kernel_img = np.ones((5,5),dtype="uint8")
image = cv2.erode(img,kernel_img,cv2.BORDER_REFLECT)
cv2.imshow("Original Image",img)
cv2.imshow("Eroded Image",image)
cv2.waitKey(0)

Here I have used cv2.imshow() method for displaying the image and and cv2.waitKey(0) for showing the image indefinelty until you press any key on the keyboard

Output

Adding borderType parameter to the cv2 erode method
Adding borderType parameter to the cv2 erode method

Conclusion

Opencv is the best library for solving computer vision problems. You can use cv2.erode() method for manipulating the border in the text of the image for text extraction. I hope you have liked this tutorial. If you have any queries then you can contact us for more help.

Source:

OpenCv Documentation

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner