Image ProcessingUnit 410 min read

Morphological Image Processing: Erosion, Dilation, Opening, Closing

Unit 4 of Image Processing introduces morphological operations—erosion, dilation, opening, and closing—using structuring elements to modify image shapes, remove noise, and segment objects. Learn how these operations work, their mathematical foundations, and real-world applications in medical imaging, document processin

TAKEAWAYS:

  • Morphological operations use structuring elements (kernels) to probe and transform images via erosion (shrinking) and dilation (expanding).
  • Opening (erosion followed by dilation) removes small objects/bridges, while closing (dilation followed by erosion) fills gaps and smooths contours.
  • These operations preserve shape topology (connectivity) and are computationally efficient for binary and grayscale images.
  • Applications include license plate recognition (segmenting text), medical imaging (tumor boundary extraction), and traffic sign detection (noise removal).
  • Structuring element shape and size critically affect the output (e.g., a disk vs. a cross for edge detection).
  • Hit-or-miss transform combines erosion/dilation to detect specific patterns (e.g., corners or lines).

Core Concepts: Structuring Elements and Basic Operations

1. Structuring Elements (SE)

Morphological operations rely on a structuring element (SE), a small matrix (e.g., 3×3) that defines the "neighborhood" for probing the image. The SE can be:

  • Binary (black/white pixels, e.g., a cross or diamond).
  • Grayscale (weighted values for more complex transformations).

Why it matters: The SE acts like a "stamp" that slides over the image, modifying pixels based on overlap rules. Visual:

slides overDilationErosionOriginal ImageBinary SE (3×3 cross)Dilation (SE OR image)Erosion (SE AND image)
Structuring Element (SE) sliding over a binary image (3×3 cross example)

2. Erosion

Erosion shrinks bright regions (objects) and removes small noise. For a binary image:

  • A pixel in the output is 1 only if all pixels in the SE overlap with 1 in the input.
  • Mathematically: (For binary images, replace min with AND over the SE.)

Worked Example: Removing Noise from a License Plate Input: A binary image of a license plate with salt-and-pepper noise (random black/white pixels). SE: 3×3 square of 1s. Step-by-step erosion:

  1. Slide the SE over the image. For each position, check if all 9 pixels under the SE are 1 (white).
  2. If yes, the center pixel in the output is 1; else, 0.
  3. Result: Small noise (isolated 1s or 0s) disappears because they cannot "cover" the entire SE.

3. Dilation

Dilation expands bright regions and connects nearby objects. For binary images:

  • A pixel in the output is 1 if any pixel in the SE overlaps with 1 in the input.
  • Mathematically: (For binary images, replace max with OR.)

Worked Example: Connecting Broken Traffic Sign Edges Input: A traffic sign with broken edges (due to low resolution). SE: 3×3 diamond shape (favors diagonal connections). Steps:

  1. Slide the SE. If any pixel under the SE is 1, the output center is 1.
  2. Result: Gaps ≤ 1 pixel wide are filled, making the sign edges continuous.

Visual:

graph TD
    A["Original\n(Broken Sign)"] -->|"Dilation"| B["Dilated\n(Edges Connected)"]
    B --> C["SE: Diamond\n(preserves angles)"]

Advanced Operations: Opening and Closing

4. Opening (Erosion → Dilation)

  • Purpose: Remove small objects/bridges while preserving the shape of larger objects.
  • Steps:
    1. Erode the image (shrinks objects).
    2. Dilate the result (expands back, but small objects vanish).
  • Mathematically: .

Real-World Example: Daraz Order Processing Scenario: Daraz’s warehouse scans use opening to:

  1. Remove speckle noise in barcode images (e.g., dust on the scanner).
  2. Separate overlapping product labels (erosion breaks connections; dilation reconnects valid labels). Worked Example:
  • Input: A barcode with 2-pixel-wide noise lines.
  • SE: 3×3 square.
  • Erosion: Noise lines disappear (they’re too small to survive).
  • Dilation: Valid barcode lines re-expand to original size.

5. Closing (Dilation → Erosion)

  • Purpose: Fill gaps and smooth contours in objects.
  • Steps:
    1. Dilate the image (fills small holes).
    2. Erode the result (shrinks back, but holes are gone).
  • Mathematically: .

Real-World Example: NTC’s Vehicle Plate Recognition Scenario: NTC’s automated number plate readers use closing to:

  1. Fix broken characters (e.g., a torn "8" in "KTM 8234").
  2. Merge adjacent pixels caused by lighting variations. Worked Example:
  • Input: A plate with a 3-pixel gap in the letter "A".
  • SE: 5×5 square.
  • Dilation: The gap is bridged (SE covers the gap).
  • Erosion: The shape returns to near-original, but now continuous.

Visual:

flowchart LR
  A["Original
(Gapped 'A')"] -->|Dilation| B["Filled Gap
(SE: 5×5 square)"]
  B -->|"Erosion"| C["Closed
(Continuous 'A')"]
Closing operation: Dilation → Erosion with 5×5 square SE

Hit-or-Miss Transform: Pattern Detection

The hit-or-miss transform detects specific patterns (e.g., corners, lines) using two SEs:

  • SE₁: Marks where the pattern’s "foreground" should be.
  • SE₂: Marks where the pattern’s "background" should be.
  • Output: 1 only where both conditions are met.
MatchExcludeHitRoad MapCorner SE₁Background SE₂Detected Corners
Hit-or-Miss transform logic for corner detection (SE₁ = white corner, SE₂ = background)

Example: Detecting Corners in Kathmandu Traffic Routes Scenario: Analyzing satellite images of Kathmandu’s roads to find road intersections (corners). SEs:

  • SE₁: A "plus" shape (detects bright pixels forming a corner).
  • SE₂: The inverse (detects dark pixels around the corner). Worked Example:
  • Input: A binary road map (white = road, black = background).
  • Output: Pixels where a white corner (road intersection) is detected.

Visual:


Grayscale Morphological Operations

For grayscale images, erosion/dilation use min/max over the SE:

  • Erosion:
  • Dilation:
-5-4-3-2-112345-4-22468xyGrayscale ErosionGrayscale DilationCenter pixel
Grayscale operation effect on pixel intensity (x-axis: SE position, y-axis: output value)

Example: Enhancing a Medical X-Ray Scenario: A chest X-ray with faint tumor boundaries. Steps:

  1. Dilation with a disk-shaped SE to brighten edges.
  2. Erosion to sharpen the tumor outline. Result: Radiologists can more easily identify the tumor’s shape.

Structuring Element Design: Shape Matters

The SE’s shape affects the output:

SE Shape Use Case Effect
Square General smoothing Isotropic (same in all directions)
Diamond Diagonal feature preservation Favors 45° lines
Cross Edge detection Highlights horizontal/vertical edges
Disk Circular object analysis Smooths curves

Worked Example: SE Choice for NEPSE Stock Chart Analysis Scenario: Analyzing candlestick charts to detect support/resistance levels.

  • SE: Vertical line (1×3 rectangle).
  • Operation: Opening removes "noise" (small price spikes).
  • Result: Clearer trends for traders.

Advantages and Limitations

Advantages

  • Topology preservation: Connectivity of objects is maintained.
  • Noise reduction: Effective for salt-and-pepper noise.
  • Computational efficiency: Simple operations (min/max over SE).
  • No parameter tuning: Unlike filters (e.g., Gaussian), SE size/shape is intuitive.

Limitations

  • Shape bias: SE shape can distort objects (e.g., squares vs. disks).
  • Size sensitivity: Small SEs miss large features; large SEs blur details.
  • Not for all noise: Gaussian noise requires other methods (e.g., Wiener filtering).

## In the Real World

  1. eSewa’s Document Verification

    • Operation: Opening (erosion + dilation) removes scratches/noise from scanned ID photos before OCR (optical character recognition).
    • SE: 3×3 square to preserve text edges while cleaning artifacts.
  2. Pathao’s Delivery Route Optimization

    • Operation: Hit-or-miss transform detects road intersections in map data to optimize delivery paths.
    • SEs: Custom shapes for T-junctions and crossroads.
  3. Ncell’s Tower Signal Analysis

    • Operation: Grayscale dilation with a disk SE enhances signal coverage areas in heatmaps, helping plan new towers.
    • Result: Clearer "hotspots" for signal strength.

## Exam Tip

  1. Always draw the SE: Examiners expect you to show the structuring element used in operations.
  2. Order matters: Opening = erosion → dilation; closing = dilation → erosion. Reverse the order, and the result changes drastically.
  3. Binary vs. grayscale: For binary images, use AND/OR; for grayscale, use min/max.
  4. Hit-or-miss is two SEs: Remember to use both foreground and background SEs for pattern detection.
  5. Real-world tie-ins: Questions often ask how operations are used in license plates, medical imaging, or document processing. Relate your answer to these domains.
  6. Pseudocode is key: For trace questions, show step-by-step pixel checks (e.g., "For pixel (2,2), the SE overlaps with...").

Practice Question: Given a binary image of a broken circle (a ring with a 3-pixel gap), design a sequence of morphological operations to close the gap without distorting the circle’s size. Justify your SE choice.

Based on the TU BIT syllabus for Image Processing, unit 4.

Discussion

Loading…