Erosion and Dilation are morphological image processing operations. OpenCV morphological image processing is a procedure for modifying the geometric structure in the image. In morphism, we find the shape and size or structure of an object. Both operations are defined for binary images, but we can also use them on a grayscale image. These are widely used in the following way:
Identify intensity bumps or holes in the picture.
Isolation of individual elements and joining disparate elements in image.
In this tutorial, we will explain the erosion and dilation briefly.
Dilation is a technique where we expand the image. It adds the number of pixels to the boundaries of objects in an image. The structuring element controls it. The structuring element is a matrix of 1’s and 0’s.
The size and shape of the structuring element define how many numbers of the pixel should be added or removed from the objects in an image.
It is a matrix of 1’s and 0’s. The center pixel of the image is called the origin.
It contains an image A with some kernel (B), which can have any shape or size, generally a square or circle. Here the kernel B has a defined anchor point. It is the center of the kernel.
In the next step, the kernel is overlapped over the image to calculate maximum pixel values. When the computation is completed, the image is replaced with an anchor at the center. The brighter areas increase in size that made increment in image size.
For example, the size of the object increases in the white shade; on the other side, the size of an object in black shades automatically decreases.
The dilation operation is performed by using the cv2.dilate() method. The syntax is given below:
cv2.dilate(src, dst, kernel)
Parameters: The dilate() function accepts the following argument:
src – It represents the input image.
dst – It represents the output image.
kernel – It represents the kernel.
Consider the following example:
import cv2 import numpy as np img = cv2.imread(r'C:\Users\DEVANSH SHARMA\jtp_flower.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('Dilation', img_dilation) cv2.waitKey(0)
Erosion is much similar to dilation. The difference is that the pixel value calculated minimum rather than the maximum in dilation. The image is replaced under the anchor point with that calculated minimum pixel. Unlikely dilation, the regions of darker shades increase. While it decreases in white shade or brighter side.
OpenCV provides cv2.erode() function to perform this operation. The syntax of the function is the following:
cv2.erode(src, dst, kernel)
src – It represents the source(input) image.
dst – It represents the destination (output) image.
kernel – It represents the Kernel.
Consider the following example:
import cv2 import numpy as np img = cv2.imread(r'C:\Users\DEVANSH SHARMA\baloon.jpg', 1) 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.waitKey(0)
The above program will give the following output. We can see the different between both images.
Erosion operation applied to the input image.