CMP362 Image Processing and Pattern Recognition

Image Processing and Pattern RecognitionUnit 710 min read

Morphological Image Processing: Erosion, Dilation, Opening, Closing, Hit-or-Miss

Unit 7 of Image Processing and Pattern Recognition explores morphological operations—erosion, dilation, opening, closing, and hit-or-miss transforms—using structuring elements to analyze and modify binary/grayscale images for shape extraction, noise removal, and segmentation.

What is Morphological Image Processing?

Morphological image processing is a non-linear technique used to analyze and modify shapes in images by probing them with a structuring element (SE). It is particularly useful for:

  • Shape extraction (e.g., identifying objects in medical imaging).
  • Noise removal (e.g., removing salt-and-pepper noise).
  • Skeletonization (e.g., thinning objects to their central axis).
  • Boundary extraction (e.g., detecting edges of objects).

Unlike linear operations (e.g., convolution), morphological operations use set theory (for binary images) or min/max operations (for grayscale images).


Key Definitions

  1. Structuring Element (SE): A small matrix (e.g., 3×3) defining the shape and size of the probe. Common shapes:

    • Square, disk, cross, diamond.
    • Example: A 3×3 square SE for erosion/dilation.
  2. Binary vs. Grayscale Morphology:

    • Binary: Uses set operations (union, intersection, complement).
    • Grayscale: Uses min/max operations with the SE.

Core Operations

-1-0.8-0.6-0.4-0.20.20.40.60.810.20.40.60.81xyAbsolute Value (Grayscale Dilation)Threshold
Grayscale erosion (minimum) vs. dilation (maximum) operations

1. Erosion

Definition: Shrinks the foreground objects (white regions in binary images) by eroding their boundaries. For grayscale, it replaces each pixel with the minimum value in its SE neighborhood.

Mathematical Formulation (Binary): Where:

  • = input image,
  • = structuring element,
  • = SE translated by .

Visualization:

1,1,101,1,111,1,12
3×3 square structuring element (SE) for erosion

Example (Binary):

Worked Example (Grayscale): Consider a 3×3 grayscale image patch and a 3×3 SE:

Original Patch:
[ 50  60  70 ]
[ 40  55  65 ]
[ 30  45  50 ]

SE (3×3 square):
[ 1  1  1 ]
[ 1  1  1 ]
[ 1  1  1 ]

Erosion replaces the center pixel (55) with the minimum value in the neighborhood:


2. Dilation

Definition: Expands the foreground objects by adding pixels to their boundaries. For grayscale, it replaces each pixel with the maximum value in its SE neighborhood.

Mathematical Formulation (Binary):

Visualization:

1,1,101,1,111,1,12
3×3 square structuring element (SE) for dilation (foreground expansion)

Example (Binary):

Worked Example (Grayscale): Using the same patch as above:


3. Opening and Closing

Opening = Erosion followed by Dilation (removes small noise and detaches connected objects). Closing = Dilation followed by Erosion (fills small holes and smooths contours).

Visualization:

graph TD
  A["Original Image"] -->|"Erosion"| B["Eroded"]
  B -->|"Dilation"| C["Opened Image"]
  D["Original Image"] -->|"Dilation"| E["Dilated"]
  E -->|"Erosion"| F["Closed Image"]
  subgraph SE1["3×3 Square SE"]
    direction TB
    S1["1 1 1"]
    S2["1 1 1"]
    S3["1 1 1"]
  end
  subgraph Example
    Original["Original Image"] -->|"Opening"| Opened["Opened Image"]
    Original -->|"Closing"| Closed["Closed Image"]
  end

Example (Opening):

Example (Closing):


4. Hit-or-Miss Transform

Definition: Detects specific shapes in an image by combining erosion with two SEs (one for the shape, one for its complement). Used for template matching (e.g., detecting corners or edges).

Mathematical Formulation: Where:

  • = SE for the shape,
  • = SE for the background,
  • = complement of .

Example: Detecting a cross shape in a binary image:

graph LR
    A["Original Image"] -->|"Hit-or-Miss"| B["Detected Crosses"]
    subgraph SE1["Cross SE"]
        direction TB
        S1["0 1 0"]
        S2["1 1 1"]
        S3["0 1 0"]
    end
    subgraph SE2["Background SE"]
        direction TB
        S1["1 0 1"]
        S2["0 0 0"]
        S3["1 0 1"]
    end

Applications in Real World

1. Medical Imaging (e.g., NAMS Hospital Systems in Nepal)

  • Use Case: Detecting tumor boundaries in MRI scans.
  • How: Opening removes noise, while closing fills gaps in the tumor region.
  • Example: A 3×3 SE is used to smooth the edges of a detected tumor before segmentation.

2. Traffic Monitoring (e.g., NTC’s Smart Traffic Systems)

  • Use Case: Vehicle detection in surveillance footage.
  • How: Dilation expands detected vehicle blobs, while erosion removes small noise (e.g., shadows or dust).
  • Example: A binary image of a road with white vehicles is dilated to merge partially detected cars.

3. Fingerprint Recognition (e.g., eSewa or Khalti Biometric Login)

  • Use Case: Thinning ridges in fingerprint images.
  • How: Morphological skeletonization (repeated erosion) reduces ridges to 1-pixel width for feature extraction.
  • Example: A fingerprint image is eroded iteratively until only the central axis remains.

Comparison Table: Morphological Operations

Operation Effect on Objects Effect on Noise Grayscale Operation Key Use Case
Erosion Shrinks objects Removes small noise Min operation Object boundary extraction
Dilation Expands objects Fills small gaps Max operation Connecting disjoint objects
Opening Smooths contours Removes small noise Erosion + Dilation Noise removal
Closing Fills holes Removes small holes Dilation + Erosion Hole filling
Hit-or-Miss Detects specific shapes N/A Custom SE Template matching

Advantages and Limitations

Advantages:

  1. Shape Preservation: Maintains geometric properties (e.g., area, perimeter) better than linear filters.
  2. Noise Reduction: Effective for salt-and-pepper noise and speckle noise.
  3. No Parameter Tuning: Unlike Gaussian filters, SE shape/size is intuitive to choose.
  4. Parallelizable: Operations can be implemented efficiently in hardware (e.g., FPGAs).

Limitations:

  1. Sensitivity to SE Choice: Poor SE selection can distort objects.
  2. Not for All Noise Types: Ineffective against Gaussian noise (use filtering first).
  3. Computational Cost: Slower than linear operations for large images (though optimizations exist).
  4. Over-Smoothing: Aggressive erosion/dilation can disconnect objects or merge unrelated regions.

Worked Example: Traffic Sign Detection

Scenario: NTC uses morphological processing to detect red circular traffic signs in surveillance images. The steps:

  1. Preprocessing: Convert RGB to grayscale, apply thresholding to get a binary image.
  2. Noise Removal: Apply opening (erosion + dilation) with a 5×5 disk SE to remove small noise.
  3. Shape Detection: Use hit-or-miss with a circular SE to detect red circles.
  4. Post-Processing: Apply closing to fill gaps in detected signs.

Step-by-Step Trace:

  1. Original Image:

  2. After Opening (5×5 Disk SE):

    • Small noise (e.g., dust) is removed.
    • Circles remain intact.
  3. Hit-or-Miss for Circles:

    • SE for circle: A 3×3 ring (center black, border white).
    • SE for background: Inverted ring.
    • Detected circles are marked.
  4. Final Output:


Exam Tip

What to Expect:

  1. Theory Questions (30%):

    • Define erosion, dilation, opening, closing, and hit-or-miss.
    • Explain the role of the structuring element (SE).
    • Compare binary vs. grayscale morphology.
    • Draw SEs for common shapes (e.g., cross, diamond).
  2. Problem Solving (50%):

    • Given a binary/grayscale image patch and an SE, compute erosion/dilation.
    • Design a sequence of operations (e.g., "How would you remove noise from a fingerprint?").
    • Interpret results: "Why does opening remove small objects?"
  3. Applications (20%):

    • Relate operations to real-world scenarios (e.g., medical imaging, traffic monitoring).
    • Explain why morphological processing is preferred over linear filters in specific cases.

Key Formulas to Memorize:

Operation Binary Formula Grayscale Operation
Erosion Min over SE neighborhood
Dilation Max over SE neighborhood
Opening Erosion → Dilation
Closing Dilation → Erosion

Common Pitfalls:

  1. SE Size Matters: A large SE can disconnect objects; a small SE may miss details.
  2. Order in Opening/Closing: Opening = erosion then dilation; reversing the order changes the result.
  3. Grayscale vs. Binary: Confusing min/max operations in grayscale with set operations in binary.
  4. Hit-or-Miss Misuse: Forgetting to use the complement for background detection.

Practice Questions for Self-Assessment:

  1. Given a 3×3 binary image and a 3×3 square SE, compute the erosion and dilation.
  2. Why is opening used before closing in noise removal pipelines?
  3. Design a morphological pipeline to:
    • Remove small holes in a document scan.
    • Detect all rectangular objects in an image.
  4. Compare the effect of a disk SE vs. a square SE on a circular object. Which preserves shape better? Why?
  5. How would you use morphological operations to measure the area of blood cells in a microscope image?

Based on the PU BE Computer (PU) syllabus for Image Processing and Pattern Recognition (CMP362), unit 7.

Discussion

Loading…