Python OpenCV Erosion and Dilation
In this post, you will learn how to perform erosion and dilation using the Python OpenCV.
Morphological operations apply a structuring element to an input image, creating an output image of the same size.Erosion and Dilation are basic morphological operations used for image processing. Dilation adds pixels to the boundaries of objects in an image, while erosion removes pixels on object boundaries. These operations are generally performed on binary images, but we can also use them for grayscale images.
Erosion: cv2 erode
The erosion operation usually uses a structuring element to gradually erode away the boundary regions of the foreground object. It basically strips out the outermost layer of pixels in a structure. It means that the area of the foreground object will become smaller. It is used to diminish the features of an image. In OpenCV, it is performed by using the cv2.erode() method. The pixel value computed in this way is minimum over the area of the given kernel. A pixel in the original image (either 1 or 0) will be considered 1 if all of the pixels under the kernel are 1, otherwise it will be eroded (turned to zero). The basic syntax is-
cv2.erode(image, kernel, dst, iterations=1)
image- the input image,
kernal- structuring element used for erosion,
dst- the output image is of the same size and type as the input,
iterations- number of times erosion is applied.
Dilation: cv2.dilate
The dilation operation on a binary image is to gradually expand the boundary regions of the input image. It adds an extra layer of pixels on a structure. The pixel value computed this way is the maximum over the area of the given kernel. It means the area of the foreground object will increase and accentuate features. In OpenCV, it is performed by using the cv2.dilate() method. The basic syntax is-
cv2.dilate(image, kernel, dst, iterations=1)
image- the input image,
kernel- structuring element used for dilation,
dst- the output image of the same size and type as input,
iterations- number of times dilation is applied.
Code explanation: OpenCV Erosion and Dilation
These are the steps taken for erosion and dilation of images in Python OpenCV. First, we will load the image using the imread() method.
img = cv2.imread('nature.jpg', 0)
Next, we will take a matrix of size 5 as the kernel. The np.ones() creates a new array of the given shape and type, starting with one.
kernel = np.ones((5,5), np.uint8)
Next, we will erode and dilate the input image using the cv2.erode() and cv2.dilate() function-
img_erosion = cv2.erode(img, kernel, iterations=1)
img_dilation = cv2.dilate(img, kernel, iterations=1)
The cv2.imshow() method is used to display an image in a window. It accepts 'window name' in the first parameter and 'image name' in the second parameter.
cv2.imshow('Input', img)
cv2.imshow('Erosion', img_erosion)
cv2.imshow('Dilation', img_dilation)
The given line waits for a keyboard button press using the cv2.waitKey() method.
cv2.waitKey(0)
Complete Code: OpenCV Erosion and Dilation
import cv2
import numpy as np
# Reading the input image
img = cv2.imread('nature.jpg', 0)
kernel = np.ones((5,5), np.uint8)
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 above code returns the following output -
Original Image
Eroded form of the original image
Dilated form of the original image
Related Articles
Python project ideas for beginnersConvert image to grayscale Python
How to save figure in matplotlib pyplot
Geometric Transformation OpenCV Python
OpenCV bitwise and
How to capture a video in Python OpenCV and save
Python OpenCV Overlaying or Blending Two Images
Contour Detection using Python OpenCV
Harris Corner Detection using Python OpenCV
Pedestrian detection OpenCV
Face Recognition OpenCV Source Code
Canny Edge Detector OpenCV Python
Python NumPy: Overview and Examples
Image processing using Python Pillow
Python OpenCV Histogram Equalization
Python OpenCV Histogram of Color Image
Python OpenCV Histogram of Grayscale Image
Python OpenCV Image Filtering
Python OpenCV ColorMap
Python OpenCV Gaussian Blur Filtering
Python OpenCV Overview and Examples