DIP+CV · Programming Exercise

EP04_10 — 🪙 Blob Separation, Labeling, and Descriptors

4.9.10 EP04_10 🪙 Blob Separation, Labeling, and Descriptors

In a coin production line, it is common for pieces to touch one another on the conveyor belt, forming a single connected blob in the image—a naive count would yield the wrong total. The classic solution combines morphological operations and connectivity analysis: first, an erosion reduces or breaks fragile connections between objects, and then connected component labeling separates each object into a distinct region. Finally, geometric descriptors (area and bounding box) summarize each detected component.

See Figure 4.39 for a simulation of this EP.

4.9.10.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\), containing values \(0\) or \(1\), row by row.

  4. Data: read the binary matrix \(f\) (values \(0\) or \(1\)), row by row.

  5. Separation: compute \[ f_{ero} = f \ominus B \] using flat binary erosion (as in EP04_04), eliminating fragile connections between objects.

  6. Labeling: on \(f_{ero}\), identify connected components using connectivity defined by the neighborhood \(B\). Labeling must follow raster scanning: upon finding an unlabeled pixel with value \(1\), assign a new increasing integer label starting from 1 and propagate that label to the entire connected region.

  7. Descriptors: for each label \(k\), compute:

    • Area: number of pixels belonging to the label;
    • Bounding box: \[(y_{min}, x_{min}, y_{max}, x_{max})\]
  8. Output: display the total number of labels and then one line per label in the format: \[ k,\ \text{area},\ y_{min},\ x_{min},\ y_{max},\ x_{max} \]

4.9.10.2 📌 Computational Constraints

  • Erosion must be applied before labeling.
  • Connectivity is fixed and defined by the neighborhood above.
  • The structuring element \(B\) does not interfere with labeling connectivity.
  • No padding in any step.
  • The order of labels follows first discovery in raster scanning.

4.9.10.3 🧠 Theoretical Background

Concept Meaning Impact
Thin bridge Narrow connection between objects Can be removed by morphological erosion
Connectivity Defined by the set \[\mathcal{N}(y,x)\] Determines which pixels belong to the same component
Area Number of pixels per component Direct estimate of object size
Bounding box Spatial extent of the label Geometric summary of the component

4.9.10.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: matrix \(B\)
  • Next \(L\) lines: matrix \(f\)

Output:

  • Line 1: total number of labels found
  • Following lines: \[ k,\ \text{area},\ y_{min},\ x_{min},\ y_{max},\ x_{max} \]
🪙 EP04_10 Simulator: Coins Stuck Together → Separated → Counted erosion + labeling + descriptors

Adjust the bridge thickness between the coins and observe how morphological erosion separates the objects for counting and descriptor extraction (area and bounding box).


1 px
Original (Connected)
After Erosion + Labels
Figure 4.39: Simulador EP04_10: Blob Separation, Labeling, and Descriptors
%%writefile EP04_10.cpp
// your solution
Overwriting EP04_10.cpp
TestSuite("EP04_10.cpp").run()
✔️ EP04_10.cases already exists in casos/
📋 4 case(s) loaded from casos/EP04_10.cases

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