DIP+CV · Programming Exercise

EP04_08 — 🌋 Morphological Gradient, Top-hat, and Black-hat

4.9.8 EP04_08 🌋 Morphological Gradient, Top-hat, and Black-hat

In automatic inspection of printed circuit boards, three questions arise all the time: where are the edges of the components? Which small bright details (such as solder points) stand out from the background? What dark recesses (such as cracks) does the background conceal? A single erosion/dilation pair answers all three: the morphological gradient highlights contours, the top-hat reveals narrow peaks, and the black-hat reveals narrow valleys — three tools, one single neighborhood. See Figure 4.37 for a simulation of this EP.

4.9.8.1 📋 Implementation Guidelines

  1. Image dimensions: Read the integers \(L\) (rows) and \(C\) (columns) from \(f\).
  2. Dimensions of \(B\): Read the integers \(L_B\) (rows) and \(C_B\) (columns) of the structuring element.
  3. Structuring element: Read the matrix \(B\) with values \(0\) or \(1\), row by row.
  4. Data: Read the matrix \(f\) (the original image, in grayscale), row by row.
  5. Basic operators: Compute, exactly as in EPs 04_03 through 04_06:
    • \(d = f \oplus B\) (dilation),
    • \(e = f \ominus B\) (erosion),
    • \(\text{opening} = e \oplus B\),
    • \(\text{closing} = d \ominus B\).
  6. Morphological gradient: \(\text{grad}(y,x) = d(y,x) - e(y,x)\).
  7. Top-hat: \(\text{tophat}(y,x) = f(y,x) - \text{opening}(y,x)\).
  8. Black-hat: \(\text{blackhat}(y,x) = \text{closing}(y,x) - f(y,x)\).
  9. Output: Display, in this order, the three complete matrices: gradient, top-hat, black-hat.

4.9.8.2 📌 Computational Constraints

  • No padding at any intermediate stage — dilation, erosion, opening, and closing follow the same neighborhood rules as in the previous EPs.
  • No clipping: the three outputs may contain any integer value (the gradient is always \(\geq 0\), but top-hat and black-hat may also be so).
  • Reuse: \(d\) and \(e\) must be computed only once and reused to assemble opening, closing, and gradient.

4.9.8.3 🧠 Theoretical Foundation

Operator Formula What it reveals
Gradient \(d - e\) Edges: zero in flat regions, high at transitions
Top-hat \(f - \text{opening}(f)\) Bright, thin elements, smaller than \(B\)
Black-hat \(\text{closing}(f) - f\) Dark, thin elements, smaller than \(B\)

4.9.8.4 📦 Input and Output Specification (VPL)

Input:

  • Line 1: Integer \(L\).
  • Line 2: Integer \(C\).
  • Line 3: Integer \(L_B\).
  • Line 4: Integer \(C_B\).
  • Next \(L_B\) lines: integer elements (\(0\) or \(1\)) of matrix \(B\).
  • Next \(L\) lines: integer elements of matrix \(f\).

Output:

  • Gradient matrix with \(L\) rows and \(C\) columns.
  • Top-hat matrix with \(L\) rows and \(C\) columns.
  • Black-hat matrix with \(L\) rows and \(C\) columns.

4.9.8.5 📌 Examples

Input Output Observation
9
9
3
3
1 1 1
1 1 1
1 1 1
10 10 10 10 10 10 10 10 10
10 10 10 10 10 10 10 10 10
10 10 80 10 10 10 10 10 10
10 10 10 10 10 10 10 10 10
10 10 10 10 10 10 10 10 10
10 10 10 10 10 10 10 10 10
10 10 10 10 10 10 2 10 10
10 10 10 10 10 10 10 10 10
10 10 10 10 10 10 10 10 10
(gradient: \(3\times3\) halo of \(70\) around \((2,2)\) and \(3\times3\) halo of \(8\) around \((6,6)\), rest \(0\))
(top-hat: single \(70\) at \((2,2)\), rest \(0\))
(black-hat: single \(8\) at \((6,6)\), rest \(0\))
Isolated peak becomes top-hat; isolated valley becomes black-hat; both appear in the gradient
🌋 EP04_08 Simulator: Gradient / Top-hat / Black-hat 3 operators, 1 neighborhood

Add peaks or valleys to matrix f and observe the simultaneous behavior of the gradient, top-hat, and black-hat operators.

f (Input)
Gradient
Top-hat
Black-hat
Figure 4.37: EP04_08 Simulator: Morphological Gradient, Top-hat and Black-hat
%%writefile EP04_08.py
# Python code
Overwriting EP04_08.py
TestSuite("EP04_08.py").run()
✔️ EP04_08.cases already exists in casos/
📋 4 case(s) loaded from casos/EP04_08.cases

🔍 Testing Python: EP04_08.py
⚠️ EP04_08.py: Empty file (fewer than 3 lines). Tests skipped.