Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Dilation and erosion are the morphological operations in image processing. These operations are applied to an image in order to manipulate the shape of an object within the image. They are used to smoothen the edges, remove noises, and detect patterns in an image.
In this blog, we will discuss the difference between dilation and erosion in detail. So without any further wait, let us start learning!
Morphological Operations
Morphological operations are simple transformations applied to an image. These operations are applied to shapes and structures inside the image. They solely rely on the relative order of pixel values rather than numeric values; thus, they are useful in order to process binary images. But we can apply them to grayscale images as well.
We can use these operations to increase as well as decrease the size of objects in an image. Also, we can close gaps between objects as well as open them. In short, the morphological operation is a great way to transform an image however we want.
Two main types of morphological operations:
Dilation
Erosion
Let us first understand the term structuring element before discussing the process of dilation and erosion.
Structuring Element
The structuring element is a binary image that is used to probe an image to find the region of interest. They are defined by their size, which determines the neighbourhood around each pixel. They can have different shapes, such as squares, rectangles, circles, or custom shapes. For example, if we want to detect a simple line in an image, we can create a linear structuring element in this case.
Fit: In this case, all pixels of the structural element completely overlap the object's pixels.
Hit: In this case, at least one pixel of the structural element overlaps the object's pixels.
Miss: In this case, not a single pixel of structural element overlaps the object's pixel.
Now, let’s see what is Dilation and Erosion.
Dilation
As the name suggests, dilation is a process of enlarging. It is a technique where we expand the boundaries of an object in an image. In this process, the structuring element is used to decide the size and shape of the dilation. It adds the number of pixels to the boundaries of the object in an image and outputs a new image where pixels in the original image are expanded.
In Python, OpenCV provides us with the method cv2.dilate() to perform dilation operations.
Syntax
cv2.dilate(src, dst, kernel, iterations)
You can also try this code with Online Python Compiler
src: It represents our input image. It can be colour or grayscale.
dst: It represents the output image.
kernel: It represents the kernel. It represents the structuring element for our dilation operation.
iterations: It represents the number of iterations to be performed.
Return value: It returns the output image.
Now that we are aware of the syntax; let’s try to use this method on a sample image. We will be using the below image as an input image in all our further examples.
Example
Code
#Importing libraries
import cv2
import numpy as np
#Variable to store the path
path = r'C:\Users\sohai\Downloads\Coding Ninjas.jpg'
#Reading and storing the input image
img = cv2.imread(path)
#Defining structuring element, aka kernel
kernel = np.ones((5,5), np.uint8)
#Performing dilation
img_dilation = cv2.dilate(img, kernel, iterations=1)
#Display the image in window
cv2.imshow('Dilated output image', img_dilation)
#Pause the screen until any key is pressed
cv2.waitKey(0)
cv2.destroyAllWindows()
You can also try this code with Online Python Compiler
Now, you must be wondering what difference the dilate() function makes, right? So for that, let’s increase the count of iterations and analyze the output.
After running the same code of iterations 2, 4, and 6, you will get the below output.
Explanation
Let us now see the step-by-step explanation of the above example:
Step 1: First of all, we imported the necessary libraries for our operation.
Step 2: Then we read the input image using the imread() method and stored it in the img variable.
Step 3: Now, we defined the structure of the structuring element of our operation, which is a 5x5 matrix of ones.
Step 4: We used the dilate() method to do the dilation process and displayed it using the imshow() method.
Step 5: We paused the screen using the waitKey(0) method and finally closed the windows whenever the user pressed any key.
We can clearly see from the output that the dilate() method is expanding the brighter shades of our input image.
Advantages of Dilation
Below are a few pros of dilation operation.
Fill small holes or gaps in the image.
Enlarge the object and join the broken part of an object in an image.
It can be used in feature extraction to identify specific patterns in an image.
Dilation can assist in object detection by finding specific regions of interest.
Disadvantages of Dilation
Below are some cons of dilation operation.
It can amplify noises which may cause in loss of information.
Excess dilation can cause the merging of nearby regions.
Excess dependence on the complexity of the image as well as the nature of the object.
Dilation has high computational complexity; thus, it may take large time to process large images.
Erosion
Erosion is somewhat similar to dilation. Here, the only difference is that instead of enlarging, we shrink the pixel value of the image. In erosion, the region of darker shades increases compared to dilation, where the darker shades expand.
OpenCV provides the cv2.erode() method to perform erosion operations.
Syntax
cv2.erode(src, dst, kernel)
You can also try this code with Online Python Compiler
Again, let’s run the same code for different iterations.
Explanation
In the above example, we can clearly see that the erode() method is shrinking the brighter region of the object in our input image.
Advantages of Erosion
Below are some pros of erosion() method.
Separate overlapping objects and find boundaries of objects in an image.
Effectively remove noise and thin out regions.
Erosion is used in object counting and analysis tasks.
It reduces complex shapes to a simpler and more compact form.
Disadvantages of Erosion
Below are a few cons of using erosion().
It can cause the loss of smaller objects, thus causing the loss of important details.
It can introduce roughness to object boundaries and can erode away pixels of complex shapes.
Lack of control over the shrinking of objects in the image.
It can diminish the contrast between the object and it’s background.
Dilation Vs Erosion
The below table gives explains the difference between dilation and erosion operations.
Dilation
Erosion
It increases the size of an object in the image.
It decreases the size of an object in the image.
It expands the boundaries of the object.
It shrinks the boundaries of the object.
It can amplify noise or unwanted artifacts in the image.
It can cause the loss of objects smaller than the structuring element.
It affects the pixels of objects at the boundaries more compared to objects in between.
It affects the pixels of objects in between the image more than the objects at the boundaries.
It has more chance of introducing noises.
It has less chance of introducing noises.
It increases the brightness of the objects in the image.
It decreases the brightness of the objects in the image.
It is used prior to closing and later in opening operations.
It is used prior to opening and later in closing operations.
Frequently Asked Questions
What is a dilation operation?
The dilation operation is an operation in which we expand the boundaries of objects in an image.
What is erosion operation?
The erosion operation is an operation in which the pixel value of objects is shrunk or reduced.
What is a structuring element?
The structuring element is a small, pre-defined object which is used to process the image in dilation and erosion operation.
What are the most used structuring elements in dilation and erosion?
Some most used structuring elements are square, rectangle, circle, and elliptical kernels.
Can we use dilation and erosion together?
Yes, we can use dilation and erosion together in operations like opening and closing.
Conclusion
This article discusses the difference between dilation and erosion. We discussed both operations with an example, along with their pros and cons. We hope this blog has helped you enhance your knowledge about dilation and erosion. If you want to learn more, then check out our articles.
But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problems, interview experiences, and interview bundles for placement preparations.
However, you may consider our paid courses to give your career an edge over others!