From the course: Building Computer Vision Applications with Python

Unlock the full course today

Join today to access over 24,800 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 in it. This image has white text over a black background. This is the usual case in computer vision. In line 3, I've defined that 3 by 3 kernel of all ones, which I'm using in the erode function in line 5. This function takes us arguments the image, the kernel, and the number of erosions to perform repeatedly. Also notice in line 7 that I'm saving this picture for later in img3. Let's see the result of one 3 by 3 erosion. The text at the top lost a lot of vital pixels, while the text at…

Contents