Erosion and Dilation of images using OpenCV in python - GeeksforGeeks

25 Jun, 2021
Morphological operations are a set of operations that process images based on shapes. They apply a structuring element to an input image and generate an output image. The most basic morphological operations are two: Erosion and Dilation Basics of Erosion:  
Erodes away the boundaries of the foreground object
Used to diminish the features of an image.
Working of erosion:  
A kernel(a matrix of odd size(3,5,7) is convolved with the image.A pixel in the original image (either 1 or 0) will be considered 1 only if all the pixels under the kernel are 1, otherwise, it is eroded (made to zero).Thus all the pixels near the boundary will be discarded depending upon the size of the kernel.So the thickness or size of the foreground object decreases or simply the white region decreases in the image.
A kernel(a matrix of odd size(3,5,7) is convolved with the image.
A pixel in the original image (either 1 or 0) will be considered 1 only if all the pixels under the kernel are 1, otherwise, it is eroded (made to zero).
Thus all the pixels near the boundary will be discarded depending upon the size of the kernel.
So the thickness or size of the foreground object decreases or simply the white region decreases in the image.
Basics of dilation:  
Increases the object area
Used to accentuate features
Working of dilation: 
A kernel(a matrix of odd size(3,5,7) is convolved with the imageA pixel element in the original image is ‘1’ if at least one pixel under the kernel is ‘1’.It increases the white region in the image or the size of the foreground object increases  
A kernel(a matrix of odd size(3,5,7) is convolved with the image
A pixel element in the original image is ‘1’ if at least one pixel under the kernel is ‘1’.
It increases the white region in the image or the size of the foreground object increases  
 
Python
# Python program to demonstrate erosion and# dilation of images.import cv2import numpy as np # Reading the input imageimg = cv2.imread('input.png', 0) # Taking a matrix of size 5 as the kernelkernel = np.ones((5,5), np.uint8) # The first parameter is the original image,# kernel is the matrix with which image is# convolved and third parameter is the number# of iterations, which will determine how much# you want to erode/dilate a given image.img_erosion = cv2.erode(img, kernel, iterations=1)img_dilation = cv2.dilate(img, kernel, iterations=1) cv2.imshow('Input', img)cv2.imshow('Erosion', img_erosion)cv2.imshow('Dilation', img_dilation) cv2.waitKey(0)
The second image is the eroded form of the original image and the third image is the dilated form.  
Uses of Erosion and Dilation:  
Erosion: It is useful for removing small white noises.Used to detach two connected objects etc.Dilation:In cases like noise removal, erosion is followed by dilation. Because, erosion removes white noises, but it also shrinks our object. So we dilate it. Since noise is gone, they won’t come back, but our object area increases.It is also useful in joining broken parts of an object.
Erosion: It is useful for removing small white noises.Used to detach two connected objects etc.
It is useful for removing small white noises.
Used to detach two connected objects etc.
Dilation:In cases like noise removal, erosion is followed by dilation. Because, erosion removes white noises, but it also shrinks our object. So we dilate it. Since noise is gone, they won’t come back, but our object area increases.It is also useful in joining broken parts of an object.
In cases like noise removal, erosion is followed by dilation. Because, erosion removes white noises, but it also shrinks our object. So we dilate it. Since noise is gone, they won’t come back, but our object area increases.
It is also useful in joining broken parts of an object.
This article is contributed by Pratima Upadhyay. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
Kashif_Iftikhar
Image-Processing
OpenCV
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Read JSON file using Python
Adding new column to existing DataFrame in Pandas
Python map() function
How to get column names in Pandas dataframe
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python
Different ways to create Pandas Dataframe
Python program to convert a list to string