| id | area | perimeter | cx | cy | x | y | w | h | circularity | solidity | vertices |
|---|
8.14.7 EP08_07 🟡 Salt-and-Pepper Noise Removal and Object Measurement
In this exercise, you will apply morphological filtering to clean a binary image corrupted by salt-and-pepper noise (isolated pixels of value 1 in the background and 0 inside objects). After cleaning, the program must extract the geometric measurements of the remaining connected components, sort them, and display the final metrics table.
8.14.7.1 📋 Implementation Guidelines
Input: read two integers \(H\) and \(W\) (image height and width) from the first line, followed by \(H\) lines containing the binary matrix with pixels
0and1separated by spaces.Morphological Filtering: apply a chain of Opening (to eliminate salt noise in the background) followed by Closing (to fill pepper noise inside objects) using a \(3 \times 3\) structuring element.
Printing the Cleaned Image: print the resulting matrix with values
0and1separated by spaces.Geometric Measurements: for each object identified in the cleaned matrix, extract:
id: sequential numeric identifier (reassigned after sorting);area: area calculated via contour (cv2.contourArea);perimeter: contour perimeter (cv2.arcLength);cx,cy: center of mass (centroid viacv2.moments);x,y,w,h: coordinates of the bounding rectangle (cv2.boundingRect);circularity: circularity given by \(\frac{4 \pi \cdot \text{area}}{\text{perimeter}^2}\);solidity: solidity given by the ratio \(\frac{\text{area}}{\text{convex hull area}}\);vertices: approximate number of polygon vertices (cv2.approxPolyDPwith \(\epsilon = 0.02 \times \text{perimeter}\)).
- Sorting and Output: sort objects in ascending order by the \(X\) position of the bounding rectangle (
bbox[0]); in case of a tie, use the \(Y\) position (bbox[1]). Reassignids from \(1\) to \(N\) and print the formatted table.- For sorting, use
medidas.sort(key=lambda m: (m['bbox'][1], m['bbox'][0])), withmedidas = mm.measure(img).
- For sorting, use
8.14.7.2 📌 Constraints and Sorting Rules
- Object Sorting Rule:
medidas.sort(key=lambda m: (m['bbox'][0], m['bbox'][1]))- Area Difference: The area calculated by OpenCV (
cv2.contourArea) measures the area of the continuous polygon delimited by the centers of border pixels, resulting in numeric values smaller than the simple discrete count of1pixels (np.sum).
8.14.7.3 🧠 Theoretical Foundation
| Operation / Metric | Function in Filtering and Characterization |
|---|---|
| Morphological Opening (\(\circ\)) | Erosion followed by dilation: removes isolated bright noise (salt). |
| Morphological Closing (\(\bullet\)) | Dilation followed by erosion: fills small dark holes inside objects (pepper). |
cv2.boundingRect |
Returns \((x, y, w, h)\), the smallest axis-aligned rectangle enclosing the object. |
| Circularity and Solidity | Describe the geometric compactness and convexity of the component. |
8.14.7.4 📌 Examples
| Input | Output |
|---|---|
| 8 9 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 1 1 1 1 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 |
id area perimeter cx cy x y w h circularity solidity vertices 1 9.0 12.0 3.5 2.0 3 1 4 3 0.79 1.000 4 2 4.0 8.0 7.5 5.5 7 5 2 2 0.79 1.000 4 |
%%writefile EP08_07.py
# Python codeOverwriting EP08_07.py
TestSuite("EP08_07.py").run()✔️ EP08_07.cases already exists in casos/
📋 4 case(s) loaded from casos/EP08_07.cases
🔍 Testing Python: EP08_07.py
⚠️ EP08_07.py: Empty file (fewer than 3 lines). Tests skipped.