Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
1.
Introduction
2.
Morphological Operations
3.
Structuring Element
4.
Dilation
4.1.
Syntax
4.2.
Example
4.2.1.
Code
4.2.2.
Output
4.2.3.
Explanation
4.3.
4.4.
5.
Erosion
5.1.
Syntax
5.2.
Example
5.2.1.
Code
5.2.2.
Output
5.2.3.
Explanation
5.3.
5.4.
6.
Dilation Vs Erosion
7.
7.1.
What is a dilation operation?
7.2.
What is erosion operation?
7.3.
What is a structuring element?
7.4.
What are the most used structuring elements in dilation and erosion?
7.5.
Can we use dilation and erosion together?
8.
Conclusion
Last Updated: Mar 27, 2024
Medium

# Difference between Dilation and Erosion

Sohail Ali
1 upvote
Create a resume that lands you SDE interviews at MAANG
Speaker
Anubhav Sinha
SDE-2 @
12 Jun, 2024 @ 01:30 PM

## 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.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

## 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 PythonOpenCV provides us with the method cv2.dilate() to perform dilation operations.

### Syntax

``cv2.dilate(src, dst, kernel, iterations)``

Parameters

• 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

#Reading and storing the input image

#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()``````

#### Output

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)``

Parameters

• src: It represents the source image.

• dst: It represents the output image.

• kernel: It represents the kernel. It is the structuring element for our dilation operation.

• iterations: It represents the number of iterations to be performed.

• Return value: It returns the output image.

### Example

#### Code

``````#Importing libraries
import cv2
import numpy as np

#Variable to store the path

#Reading and storing the input image

#Defining structuring element, aka kernel
kernel = np.ones((5,5), np.uint8)

#Performing erosion
img_dilation = cv2.erode(img, kernel, iterations=1)

#Display the image in the window
# cv2.imshow('Input image', img)
cv2.imshow('Erosion output image', img_dilation)

#Pause the screen until any key is pressed
cv2.waitKey(0)
cv2.destroyAllWindows()``````

#### Output

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.

## 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.

And many more on our platform Coding Ninjas.

Refer to our Guided Path to upskill yourself in DSACompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your coding ability, you may check out the mock test series and participate in the contests hosted on Coding Ninjas!

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 problemsinterview experiences, and interview bundles for placement preparations.

However, you may consider our paid courses to give your career an edge over others!

Happy Learning!

Live masterclass