Computer Vision Lesson 16 – Erosion & Dilation | Dataplexa

Erosion and Dilation

In the previous lesson, you learned about kernels and filters — how small matrices can change an image. Now we take that idea further and apply it to shape-based image processing.

Erosion and Dilation are called morphological operations. They do not focus on color or intensity alone, but on the structure and shape of objects in an image.

These operations are extremely important in real-world systems like document scanning, medical imaging, object detection, and noise cleanup.


What Are Morphological Operations?

Morphological operations process images based on:

  • Object shapes
  • Object boundaries
  • Spatial structure

They are most commonly applied on:

  • Binary images (black & white)
  • Thresholded images
  • Masks and segmented regions

At the heart of morphology is a kernel, usually called a structuring element.


What Is a Structuring Element?

A structuring element is a small matrix that defines how erosion or dilation behaves.

Think of it as a probe that scans the image and decides how pixels should change.

Common shapes:

  • Square
  • Rectangle
  • Circle
  • Cross

Different shapes produce different effects.


Erosion – Conceptual Understanding

Erosion removes pixels from object boundaries.

When erosion is applied:

  • White regions shrink
  • Thin objects may disappear
  • Small noise points are removed

You can think of erosion like:

  • Sandpaper rubbing an object
  • Edges being eaten away

Only pixels that fully fit under the structuring element survive.


Why Erosion Is Useful

Erosion is commonly used to:

  • Remove small white noise
  • Detach connected objects
  • Refine object boundaries

It is often applied before:

  • Contour detection
  • Shape analysis
  • Object separation

Dilation – Conceptual Understanding

Dilation adds pixels to object boundaries.

When dilation is applied:

  • White regions grow
  • Gaps between objects shrink
  • Thin structures become thicker

Dilation is like:

  • Inflating an object
  • Spreading ink outward

If any part of the structuring element touches a white pixel, the output pixel becomes white.


Why Dilation Is Useful

Dilation is commonly used to:

  • Fill small holes
  • Connect broken components
  • Strengthen object regions

It is often used after erosion to recover useful shapes.


Erosion vs Dilation (Key Difference)

Aspect Erosion Dilation
Main effect Shrinks objects Expands objects
Noise handling Removes small noise Fills small gaps
Boundary effect Edges move inward Edges move outward
Typical use Cleanup, separation Connection, strengthening

Order Matters (Very Important)

Applying erosion then dilation is not the same as dilation then erosion.

This leads to advanced operations like:

  • Opening (erosion → dilation)
  • Closing (dilation → erosion)

You will study these formally in the next lesson.


Real-World Examples

Erosion and dilation appear in many real systems:

  • Removing dust from scanned documents
  • Cleaning binary medical images
  • Separating touching objects
  • Strengthening detected regions

These operations run silently behind the scenes.


Where You Will Implement This

You will implement erosion and dilation using:

  • OpenCV (cv2.erode, cv2.dilate)
  • Binary and grayscale images

Recommended environments:

  • Google Colab (no setup needed)
  • Local Python + OpenCV

You will visually compare before and after images to clearly see the effect.


Practice Questions

Q1. What happens to white objects during erosion?

White regions shrink and small objects may disappear.

Q2. Which operation is better for filling gaps?

Dilation.

Q3. Are erosion and dilation kernel-based?

Yes. They use a structuring element, which is a kernel.

Homework / Observation Task

  • Look at a scanned text image
  • Imagine removing tiny dots → erosion
  • Imagine thickening letters → dilation

Try to visualize the effect before seeing code.


Quick Recap

  • Morphology focuses on shapes
  • Erosion shrinks objects
  • Dilation expands objects
  • Structuring elements control behavior
  • Order of operations matters

In the next lesson, you will learn Opening and Closing, which combine erosion and dilation into powerful tools.