CSC332 Image Processing

Image ProcessingUnit 79 min read

Morphological Image Processing: Erosion, Dilation, Opening, Closing & Applications

Unit 7 of Image Processing covers morphological operations (erosion, dilation, opening, closing), structuring elements, hit-or-miss transforms, and their applications in noise removal, segmentation, and skeletonization—with real-world examples from medical imaging, satellite analysis, and OCR systems.

TAKEAWAYS:

  • Morphological operations use structuring elements to probe and modify images via set theory (union, intersection, complement).
  • Erosion shrinks objects (removes borders) while dilation expands them (fills gaps).
  • Opening (erosion + dilation) removes small noise; closing (dilation + erosion) fills small holes.
  • Hit-or-miss transforms detect specific patterns (e.g., corners, lines) by combining erosion with two structuring elements.
  • Applications include license plate recognition (erosion to separate characters), medical imaging (segmenting tumors), and satellite cloud removal.
  • Trade-offs: small structuring elements preserve details but may under-segment; large ones smooth but lose fine structures.

Core Concepts: Morphology as Set Theory on Images

Morphological image processing treats an image as a set of pixels (e.g., foreground = 1, background = 0) and applies set operations using a structuring element (SE). The SE acts like a "probe" or "stamp" that slides over the image, modifying it based on overlaps.

1. Structuring Elements (SEs)

The SE defines the neighborhood for operations. Common shapes:

  • Flat SEs: Binary (0/1) or grayscale (intensity values).
  • Shapes: Disk, square, cross, line segment, or custom (e.g., a "T" shape for detecting junctions).

Example SEs:

graph LR
    SE1["Disk-shaped SE"] -->|"Shape"| circle("30")
    SE2["Square SE"] -->|"Shape"| square("30")
    SE3["Cross-shaped SE"] -->|"Shape"| plus("30")

Real-world SE choice:

  • Medical imaging: Disk-shaped SEs for tumor segmentation (smooth boundaries).
  • OCR (e.g., eSewa forms): Cross-shaped SEs to separate handwritten digits.

2. Basic Operations: Erosion and Dilation

Erosion (θ)

  • Definition: A pixel in the output is 1 only if all pixels in the SE are 1 in the input.
  • Effect: Shrinks foreground objects, removes small noise, and separates touching objects.
  • Mathematically: where = image, = SE.

Worked Example: Erosion with a 3×3 Square SE Input Image (3×3):

1 1 1
1 1 0
1 0 0

SE (3×3 square):

1 1 1
1 1 1
1 1 1

Output:

  • Center pixel (1,1): SE overlaps (0,0) to (2,2). All 9 pixels in input? No (input has 0 at (2,1) and (2,2)).
  • Result: Only the top-left 1 survives (since its SE fits entirely in the input’s 1s).
1 0 0
0 0 0
0 0 0

Visual Trace:

Dilation (⊕)

  • Definition: A pixel in the output is 1 if it overlaps with any 1 in the SE.
  • Effect: Expands foreground objects, fills gaps, and connects nearby objects.
  • Mathematically:

Worked Example: Dilation with a 3×3 Square SE Input Image (same as above):

1 1 1
1 1 0
1 0 0

Output:

  • Any pixel whose SE overlaps at least one 1 becomes 1.
  • Result:
1 1 1
1 1 1
1 1 1

Comparison Table:

Operation Effect on Objects Effect on Noise Use Case
Erosion Shrinks/breaks apart Removes small noise Separating touching characters
Dilation Expands/connects Fills gaps Closing holes in license plates

3. Advanced Operations: Opening and Closing

Opening (∘)

  • Definition: Erosion followed by dilation (A ∘ B = (A ⊖ B) ⊕ B).
  • Effect:
    • Removes small noise and fine details.
    • Preserves the shape and size of larger objects.
  • Example: Cleaning up text in a scanned document (e.g., Daraz’s product images).

Worked Example: Opening on Noisy Text Input (noisy "A"):

0 1 0
1 1 1
0 1 0

After Erosion (3×3 SE):

0 0 0
0 1 0
0 0 0

After Dilation:

0 1 0
1 1 1
0 1 0

Result: Small noise removed; "A" preserved.

Closing (•)

  • Definition: Dilation followed by erosion (A • B = (A ⊕ B) ⊖ B).
  • Effect:
    • Fills small holes and gaps.
    • Connects nearby objects.
  • Example: Restoring broken lines in satellite images (e.g., NTC’s road network maps).

Worked Example: Closing a Broken Line Input (broken line):

1 1 1
1 0 1
1 1 1

After Dilation (3×3 SE):

1 1 1
1 1 1
1 1 1

After Erosion:

1 1 1
1 1 1
1 1 1

Result: Gap filled.

Mermaid Diagram of Operations:

flowchart LR
    A["Original Image"] --> B["Erosion"]
    B --> C["Opening\n(A ⊖ B ⊕ B)"]
    A --> D["Dilation"]
    D --> E["Closing\n(A ⊕ B ⊖ B)"]

4. Hit-or-Miss Transform: Detecting Specific Patterns

  • Definition: Detects pixels that match both a foreground SE (B) and a background SE (C). where = complement of .

  • Use Case: Finding corners, line endings, or specific shapes (e.g., "T" junctions in Kathmandu traffic maps).

Example: Detecting Corners

  • Foreground SE (B):
    0 1 0
    1 1 1
    0 1 0
    
  • Background SE (C):
    1 1 1
    1 0 1
    1 1 1
    
  • Result: Pixels where the foreground matches B and the background matches C are marked as corners.

Real-world Application:

  • Pathao’s route optimization: Detects "dead ends" (corners) in Kathmandu’s narrow streets using hit-or-miss transforms to avoid traffic jams.

5. Grayscale Morphology

For grayscale images, operations compare pixel values to the minimum/maximum in the SE neighborhood:

  • Erosion: Replace pixel with the minimum value in the SE.
  • Dilation: Replace pixel with the maximum value in the SE.

Example: Smoothing a Grayscale Image Input (3×3 grayscale patch):

50 60 70
40 50 60
30 40 50

Erosion (3×3 SE):

  • Center pixel (1,1) = min(50,60,70,40,50,60,30,40,50) = 30. Dilation:
  • Center pixel = max(...) = 70.

Effect: Erosion darkens edges; dilation brightens them.


## In the Real World

  1. eSewa’s License Plate Recognition

    • Operation: Opening (erosion + dilation) with a disk-shaped SE to separate overlapping characters on Nepal’s license plates.
    • Why? Erosion breaks apart touching digits (e.g., "8" and "B"), while dilation reconnects the skeleton of each character.
  2. NTC’s Satellite Imagery for Flood Mapping

    • Operation: Closing with a large SE to fill small gaps in cloud-covered areas, revealing submerged roads.
    • Example: During monsoon floods, closing helps distinguish rivers from clouds in raw satellite data.
  3. Khalti’s Document Verification

    • Operation: Hit-or-Miss Transform to detect forged signatures by matching known patterns (e.g., "Khalti" logo corners).
    • How? The transform flags pixels where the foreground matches the logo’s shape and the background matches the expected empty space around it.
  4. Medical Imaging: Tumor Segmentation

    • Operation: Morphological Gradient (A ⊕ B - A ⊖ B) to highlight edges of brain tumors in MRI scans.
    • Example: At Tribhuvan University Hospital, erosion with a disk SE shrinks the tumor region, while dilation expands it; subtracting the two enhances the boundary.

## Worked Example: Traffic Sign Segmentation

Scenario: Segmenting circular traffic signs (e.g., "No Entry") from a blurry road image. Steps:

  1. Convert to binary: Threshold at gray level = 128.
  2. Erode with disk SE (radius=5): Removes small noise (e.g., dust on the lens).
  3. Dilate with same SE: Reconnects broken sign edges.
  4. Hit-or-Miss with circular SE: Detects only circular objects (ignores rectangular signs).

Input Image (simplified):

1 1 1 1 1
1 0 0 0 1
1 0 1 0 1
1 0 0 0 1
1 1 1 1 1

After Erosion (3×3 disk SE):

0 0 0 0 0
0 0 0 0 0
0 0 1 0 0
0 0 0 0 0
0 0 0 0 0

After Dilation:

0 0 1 0 0
0 1 1 1 0
0 1 1 1 0
0 0 1 0 0
0 0 0 0 0

Result: Only the circular sign’s skeleton remains.


## Exam Tip

  1. Memorize Definitions:

    • Erosion = "shrinks," Dilation = "expands."
    • Opening = "remove small noise," Closing = "fill small holes."
    • Hit-or-Miss = "detect specific patterns."
  2. Structuring Element Choice:

    • Square/disk SEs: For isotropic smoothing (e.g., medical images).
    • Cross-shaped SEs: For directional operations (e.g., separating text lines).
    • Always state the SE size/shape in answers (e.g., "3×3 square SE").
  3. Common Pitfalls:

    • Order matters: Opening ≠ Closing. Always erosion first, then dilation.
    • Grayscale vs. Binary: Grayscale uses min/max; binary uses set operations.
    • Edge artifacts: Erosion can remove entire small objects; dilation can merge nearby objects.
  4. Exam Questions Pattern:

    • Theory (30%): Define erosion/dilation, explain opening/closing.
    • Calculation (40%): Given an image and SE, compute erosion/dilation manually (use 3×3 examples).
    • Application (30%): Match operations to real-world tasks (e.g., "How would you clean noise from a Daraz product image?" → Opening).
  5. Quick Trick for Manual Calculation:

    • For erosion: All pixels in the SE must be 1 in the input.
    • For dilation: At least one pixel in the SE must be 1 in the input.
    • Use a template overlay method (draw the SE over the image and check overlaps).

Based on the TU BSc CSIT syllabus for Image Processing (CSC332), unit 7.

Discussion

Loading…