DIP+CV · Programming Exercise

EP08_10 — 🔴 Low Contrast and Object Separation Topics ( Watershed / Distance)

8.14.10 EP08_10 🔴 Low Contrast and Object Separation Topics (Watershed / Distance)

In this exercise, some geometric objects are slightly touching (overlapping at the edges). Simply extracting contours would treat two objects as one.

8.14.10.1 📋 Implementation Guidelines

  1. Input: an \(H \times W\) grayscale matrix with objects of intensity \(110\dots140\) on a background of \(0\), with noise and a pair of tangent objects.
  2. Preprocessing and Separation:
  • Apply thresholding.
  • Apply the Distance Transform (mm.dist).
  • Obtain distance peaks to serve as markers in the Watershed Transform (mm.watershed), physically separating the touching objects in the mask. Hint: use mm.regmax() to obtain the local maxima and then label them with mm.label0.
  • After the watershed, apply thresholding again with mm.threshold(water,0)//255.
  1. Connected Component Analysis: measure each isolated region after the watershed.
  2. Output: print the components sorted by (bbox[0], bbox[1]) with their individual area, centroid, and solidity metrics.
    • For sorting, use medidas.sort(key=lambda m: (m['bbox'][1], m['bbox'][0])), with medidas = mm.measure(img, precision=0.02).

8.14.10.2 📌 Examples

Input Output
16 16
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
…
(binary image containing two squares)
id area perimeter cx cy x y w h solidity
1 16.0 16.0 5.0 5.0 3 3 5 5 1.000
2 16.0 16.0 11.0 5.0 9 3 5 5 1.000
🧮 Simulator EP08_10: Separation of Tangent Disks (L2 Transform & Watershed) mm.dist L2 → mm.watershed → Measurement
MORPHOLOGICAL PROCESSING STEP
PIXEL MATRIX VISUALIZATION
DISK MEASUREMENTS TABLE AFTER WATERSHED CUT
id area perimeter cx cy x y w h circularity solidity vertices
Figure 8.24: EP08_10 Simulator: Separation of Tangent Disks via L2 Distance Transform and Watershed
%%writefile EP08_10.py
# Python code
Overwriting EP08_10.py
TestSuite("EP08_10.py").run()
✔️ EP08_10.cases already exists in casos/
📋 3 case(s) loaded from casos/EP08_10.cases

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