DIP+CV · Programming Exercise

EP04_06 — 🧩 Morphological Closing (Filling of Gaps)

4.9.6 EP04_06 🧩 Morphological Closing (Filling of Gaps)

In fingerprint digitalization, skin ridges sometimes become interrupted by dirt or dryness, creating small gaps in the continuous curve that should exist. Closing — dilation followed by erosion with the same structuring element — is the dual operator of opening: it fills small holes and narrow indentations, without significantly altering the external contour of the object. It is the standard step before extracting the skeleton of a fingerprint. See Figure 4.35 for a simulation of this EP.

4.9.6.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 binary matrix \(f\) (values \(0\) or \(1\)), row by row.
  5. Dilation: Compute \(d = f \oplus B\), using exactly the algorithm from EP04_03 (reflecting \(B\), without padding).
  6. Erosion: Compute \(g = d \ominus B\), using exactly the algorithm from EP04_04 (without reflecting \(B\), without padding) — now applied to \(d\), not to \(f\).
  7. Output: Display the resulting matrix \(g\) (the closing of \(f\) by \(B\)) with dimensions \(L \times C\).

4.9.6.2 📌 Computational Constraints

  • Fixed order: It is always dilation first, then erosion — the reverse order corresponds to the opening from EP04_05.
  • Same \(B\): The structuring element used in the dilation and in the erosion must be identical.
  • No padding in either of the two stages.

4.9.6.3 🧠 Theoretical Foundation

Concept Meaning Visual Impact
Extensivity \(g \supseteq f\) always Closing never removes a pixel, only adds
Idempotence \(\text{close}(\text{close}(f)) = \text{close}(f)\) Applying it again changes nothing further
Small holes Smaller than \(B\) They are completely filled
Duality \(\text{close}(f) = \overline{\text{open}(\bar f)}\) It is the opening applied to the “negative” of the image

4.9.6.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 (\(0\) or \(1\)) of matrix \(f\).

Output:

  • Resulting matrix in \(L\) rows and \(C\) columns, values \(0\) or \(1\).

4.9.6.5 📌 Examples

Input Output Observation
8
8
3
3
1 1 1
1 1 1
1 1 1
0 0 0 0 0 0 0 0
0 0 1 1 1 1 0 0
0 0 1 1 1 1 0 0
0 0 1 0 1 1 0 0
0 0 1 1 0 1 0 0
0 0 1 1 1 1 0 0
0 0 1 1 1 1 0 0
0 0 0 0 0 0 0 0
0 0 1 1 1 1 0 0
0 0 1 1 1 1 0 0
0 0 1 1 1 1 0 0
0 0 1 1 1 1 0 0
0 0 1 1 1 1 0 0
0 0 1 1 1 1 0 0
0 0 1 1 1 1 0 0
0 0 1 1 1 1 0 0
The two non-adjacent internal holes are completely filled
🧩 Simulator EP04_06: Morphological Closing g = (f ⊕ B) ⊖ B

Click on cells of f original to turn pixels on or off (fill in internal holes!) and adjust the size of structuring element B.


3×3
f Original (Clickable)
d = f ⊕ B (Dilation)
g = d ⊖ B (Closing)
Figure 4.35: EP04_06 Simulator: Morphological Closing (g = (f ⊕ B) ⊖ B)
%%writefile EP04_06.py
# Python code
Overwriting EP04_06.py
TestSuite("EP04_06.py").run()
✔️ EP04_06.cases already exists in casos/
📋 5 case(s) loaded from casos/EP04_06.cases

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