Chapter 162 — Image Erosion and Dilation Explained: Morphological Operations in Computer Vision
1. Introduction to Morphological Image Processing
In previous computer vision topics, we examined feature extraction techniques such as edge detection filters (e.g., Sobel and Laplacian). Those classical filters operate directly on pixel intensity gradients to identify boundaries without modifying the underlying structural form of the image.
In contrast, Morphological Image Processing consists of filters and operations designed specifically to modify and refine the physical shapes and structures within an image.
┌─────────────────────────────────────────────────────────────┐
│ Feature Extraction vs. Morphological Processing │
├───────────────────────────────┬─────────────────────────────┤
│ Feature Extraction (Edges) │ Morphological Processing │
├───────────────────────────────┼─────────────────────────────┤
│ • Operates on pixel intensity │ • Operates on geometric │
│ gradients. │ shapes & structures. │
│ • Extracts features without │ • Modifies, refines, or │
│ altering shapes. │ morphs objects. │
│ • Identifies boundaries. │ • Performs noise removal & │
│ │ shape analysis. │
└───────────────────────────────┴─────────────────────────────┘
Core Principles of Morphological Processing:
- Binary & Grayscale Processing: Operates primarily on binary (black and white) images or grayscale images.
- Mathematical Foundation: Rooted in mathematical set theory and spatial geometry.
- Shape-Based Transformations: Focuses on structural transformations of foreground shapes rather than raw pixel color values.
- Primary Objectives: Enables shape analysis, noise removal, and object refinement prior to downstream computer vision tasks like segmentation or contour extraction.
2. The Structuring Element (Kernel)
Every morphological operation requires a probe called a Structuring Element (or kernel).
┌─────────────────────────────────────────────────────────────┐
│ Structuring Element Properties │
├─────────────────────────────────────────────────────────────┤
│ • Small 2D binary matrix (kernel). │
│ • Odd Dimensions: 3×3, 5×5, 7×7, etc. (has clear center) │
│ • Slides across the image in a sliding window process. │
│ • Defines the neighborhood for evaluating foreground pixels│
└─────────────────────────────────────────────────────────────┘
Common Kernel Shapes:
Square (3×3) Cross Shape Circular Shape
┌───┬───┬───┐ ┌───┬───┬───┐ ┌───┬───┬───┐
│ 1 │ 1 │ 1 │ │ 0 │ 1 │ 0 │ │ 0 │ 1 │ 0 │
├───┼───┼───┤ ├───┼───┼───┤ ├───┼───┼───┤
│ 1 │ 1 │ 1 │ │ 1 │ 1 │ 1 │ │ 1 │ 1 │ 1 │
├───┼───┼───┤ ├───┼───┼───┤ ├───┼───┼───┤
│ 1 │ 1 │ 1 │ │ 0 │ 1 │ 0 │ │ 0 │ 1 │ 0 │
└───┴───┴───┘ └───┴───┴───┘ └───┴───┴───┘
Kernel Shapes and Function:
- Odd Dimensions: Structured with odd dimensions (such as 3 × 3, 5 × 5, or 7 × 7) so that the kernel possesses an exact, well-defined center pixel.
- Neighborhood Definition: Acts as a probe sliding across every pixel location, defining the local spatial neighborhood used to update the target pixel.
- Common Shapes: Configured as squares, crosses, or circular matrices depending on the geometric requirements of the target objects.
3. Erosion: Shrinking & Boundary Stripping
Erosion is a fundamental morphological operation that shrinks or strips away pixels from the boundaries of foreground objects.
┌─────────────────────────────────────────────────────────────┐
│ Erosion Mathematical Rule │
├─────────────────────────────────────────────────────────────┤
│ Condition: STRICT "FIT" RULE │
│ │
│ • Set Center Pixel = 1 (Foreground): │
│ ONLY if ALL pixels under the kernel are 1. │
│ │
│ • Set Center Pixel = 0 (Background): │
│ If EVEN A SINGLE pixel under the kernel is 0. │
└─────────────────────────────────────────────────────────────┘
Erosion Example (3×3 Kernel Over Object Boundary):
Image Patch under Kernel: Erosion Output:
┌───┬───┬───┐
│ 1 │ 1 │ 0 │ ◄── Contains a '0' ──► Center Pixel
├───┼───┼───┤ (Kernel fails) becomes 0 (Stripped!)
│ 1 │ 1 │ 1 │
├───┼───┼───┤
│ 1 │ 1 │ 1 │
└───┴───┴───┘
Key Applications of Erosion:
- Removing Small Noise: Eliminates isolated noise artifacts such as salt-and-pepper noise.
- Salt Noise: Light noise pixels scattered over a dark background.
- Pepper Noise: Dark noise pixels scattered over a light background.
- Disconnecting Overlapping Objects: Separates distinct foreground objects that were accidentally joined or merged by noise.
- Object Shrinking: Reduces object thickness to isolate core structural lines.
4. Dilation: Expanding & Hole Filling
Dilation is the exact inverse operation of erosion—it expands or grows the outer boundaries of foreground objects.
┌─────────────────────────────────────────────────────────────┐
│ Dilation Mathematical Rule │
├─────────────────────────────────────────────────────────────┤
│ Condition: RELAXED "HIT" RULE │
│ │
│ • Set Center Pixel = 1 (Foreground): │
│ If AT LEAST ONE pixel under the kernel is 1. │
│ │
│ • Set Center Pixel = 0 (Background): │
│ ONLY if ALL pixels under the kernel are 0. │
└─────────────────────────────────────────────────────────────┘
Dilation Example (3×3 Kernel Over Background Near Boundary):
Image Patch under Kernel: Dilation Output:
┌───┬───┬───┐
│ 0 │ 0 │ 0 │ ◄── Contains '1's ──► Center Pixel
├───┼───┼───┤ (At least one 1) becomes 1 (Expanded!)
│ 0 │ 1 │ 1 │
├───┼───┼───┤
│ 0 │ 1 │ 1 │
└───┴───┴───┘
Key Applications of Dilation:
- Filling Internal Holes: Fills unwanted gaps, holes, or missing pixels inside foreground objects.
- Connecting Broken Components: Joins disconnected or fragmented object segments back into a single continuous shape.
- Enlarging Small Features: Increases the spatial size and emphasis of small foreground details.
5. Advanced Morphological Operations: Opening & Closing
By combining erosion and dilation sequentially, computer vision engineers build advanced morphological operations that clean images without permanently distorting overall object sizes.
┌─────────────────────────────────────────────────────────────┐
│ Advanced Morphological Transformations │
├─────────────────────────────────────────────────────────────┤
│ 1. OPENING (Erosion ──► Dilation): │
│ • First erodes, then dilates. │
│ • Primary Use: Removes background noise & small │
│ spurs without altering main object size. │
│ │
│ 2. CLOSING (Dilation ──► Erosion): │
│ • First dilates, then erodes. │
│ • Primary Use: Fills internal holes & bridges gaps │
│ without enlarging main object size. │
└─────────────────────────────────────────────────────────────┘
A. Opening (Erode then Dilate):
- Sequence: Applies Erosion first, followed by Dilation.
- Function: Erosion strips away small background noise spots and thin bridges. Dilation then restores the primary foreground objects back to their original dimensions, leaving the noise removed.
B. Closing (Dilate then Erode):
- Sequence: Applies Dilation first, followed by Erosion.
- Function: Dilation bridges small internal gaps, fills holes, and joins nearby broken segments. Erosion then shrinks the outer boundaries back to their original size, keeping the internal holes closed.
6. Summary Comparison Table
| Morphological Operation |
Execution Sequence |
Mathematical Condition |
Primary Practical Use Case |
| Erosion |
Single-pass sliding kernel. |
Strict ("Fit"): All kernel pixels must be 1. |
Strips boundaries, removes salt noise, separates joined objects. |
| Dilation |
Single-pass sliding kernel. |
Relaxed ("Hit"): At least one kernel pixel is 1. |
Grows boundaries, fills internal holes, connects broken parts. |
| Opening |
Erosion → Dilation. |
Combined strict then relaxed pass. |
Removes background noise without altering overall object size. |
| Closing |
Dilation → Erosion. |
Combined relaxed then strict pass. |
Fills internal holes & bridges gaps without enlarging object size. |