From the course: Hands-On AI: Image Processing with Python

Unlock this course with a free trial

Join today to access over 24,900 courses taught by industry experts.

Erosion and dilation

Erosion and dilation

- [Instructor] Erosion and dilation make white blobs shrink and grow respectively. Now, the usual algorithm for this is similar to the median filter. For erosion, a pixel is turned black if there are black pixels in its neighborhood area. For dilation, a pixel is turned white if there are white pixels in its neighborhood area. Let's get to know OpenCV's erode and dilate functions. First, let's open a black and white picture with some text. This image has white text over a black background. This is the usual case in computer vision. In line three, I have defined a three by three kernel with all ones, which I'm using in the erode function in line five. This function takes as arguments the image, the kernel and the number of erosions to perform repeatedly. Also, notice in nine seven that I'm saving this picture for later in img3. Let's see the results of one three by three erosion. The text at the top lost a lot of vital pixels while the text at the bottom is still readable. Notice that…

Contents