DIP+CV · Programming Exercise

EP04_05 — 🧹 Morphological Opening (Noise Removal)

4.9.5 EP04_05 🧹 Morphological Opening (Noise Removal)

Images captured by low-cost sensors, such as those on agricultural drones, often come dotted with small noise points—isolated pixels that represent nothing real. Applying erosion followed by dilation with the same structuring element produces the opening: it “cleans” points and thin protrusions, but returns the main object to nearly its original size. This is the classic combination used in satellite image preprocessing before any planted-area counting. See Figure 4.34 for a simulation of this EP.

4.9.5.1 📋 Implementation Guidelines

  1. Image dimensions: Read integers \(L\) (rows) and \(C\) (columns) from \(f\).
  2. Dimensions of \(B\): Read integers \(L_B\) (rows) and \(C_B\) (columns) of the structuring element.
  3. Structuring element: Read matrix \(B\) with values \(0\) or \(1\), row by row.
  4. Data: Read binary matrix \(f\) (values \(0\) or \(1\)), row by row.
  5. Erosion: Compute \(e = f \ominus B\), using exactly the algorithm from EP04_04 (without reflecting \(B\), without padding).
  6. Dilation: Compute \(g = e \oplus B\), using exactly the algorithm from EP04_03 (reflecting \(B\), without padding)—but now applied to \(e\), not to \(f\).
  7. Output: Display the resulting matrix \(g\) (the opening of \(f\) by \(B\)) with dimensions \(L \times C\).

4.9.5.2 📌 Computational Constraints

  • Fixed order: It is always erosion first, then dilation—the reverse order defines another operator (closing, from the next EP).
  • Same \(B\): The structuring element used in erosion and dilation must be identical.
  • No padding in either step.

4.9.5.3 🧠 Theoretical Foundation

Concept Meaning Visual Impact
Anti-extensivity \(g \subseteq f\) always The opening never creates a new pixel, only removes
Idempotence \(\text{opening}(\text{opening}(f)) = \text{opening}(f)\) Applying it again changes nothing further
Isolated points Smaller than \(B\) Are completely eliminated
Object core Larger than \(B\) Is recovered almost intact by the final dilation

4.9.5.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 with \(L\) rows and \(C\) columns, values \(0\) or \(1\).

4.9.5.5 📌 Examples

Input Output Observation
7
7
3
3
1 1 1
1 1 1
1 1 1
0 0 0 0 0 0 0
0 1 0 0 0 1 0
0 0 1 1 1 0 0
0 0 1 1 1 0 0
0 0 1 1 1 1 0
0 0 0 0 0 0 0
0 1 0 0 0 0 1
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 1 1 1 0 0
0 0 1 1 1 0 0
0 0 1 1 1 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
Isolated points and the thin protrusion disappear; the central square survives
🧹 Simulator EP04_05: Morphological Opening g = (f ⊖ B) ⊕ B

Click on the cells of f original to turn pixels on or off (create your own background noise!) and adjust the size of the structuring element B.


3×3
f Original (Clickable)
e = f ⊖ B (Erosion)
g = e ⊕ B (Opening)
Figure 4.34: EP04_05 Simulator: Morphological Opening (g = (f ⊖ B) ⊕ B)
%%writefile EP04_05.cpp
// your solution
Overwriting EP04_05.cpp
TestSuite("EP04_05.cpp").run()
✔️ EP04_05.cases already exists in casos/
📋 5 case(s) loaded from casos/EP04_05.cases

🔍 Testing C++: EP04_05.cpp
⚠️ EP04_05.cpp: Empty file (fewer than 3 lines). Tests skipped.