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
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.
Image dimensions: read the integers \(L\) (rows) and \(C\) (columns) from \(f\).
Dimensions of \(B\): read the integers \(L_B\) (rows) and \(C_B\) (columns) of the structuring element.
Structuring element: read the matrix \(B\), containing values \(0\) or \(1\), row by row.
Data: read the binary matrix \(f\) (values \(0\) or \(1\)), row by row.
Separation: compute \[ f_{ero} = f \ominus B \] using flat binary erosion (as in EP04_04), eliminating fragile connections between objects.
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.
Descriptors: for each label \(k\), compute:
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} \]
| 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 |
Input:
Output:
Adjust the bridge thickness between the coins and observe how morphological erosion separates the objects for counting and descriptor extraction (area and bounding box).
%%writefile EP04_10.py
# Python codeOverwriting EP04_10.py
TestSuite("EP04_10.py").run()✔️ EP04_10.cases already exists in casos/
📋 4 case(s) loaded from casos/EP04_10.cases
🔍 Testing Python: EP04_10.py
⚠️ EP04_10.py: Empty file (fewer than 3 lines). Tests skipped.