| id | class | area | solidity | vertices | bbox det (x,y,w,h) | bbox gt (x,y,w,h) | IoU | status (IoU ≥ 0.5) |
|---|
8.14.11 EP08_11 🔴 Classification and Validation of Objects with Bounding Box Ground Truth
In this exercise, the goal is to process a grayscale image containing multiple geometric objects, extract their properties using mm.measure, and validate the detected bounding boxes against a real ground truth (GT) provided as input, using the IoU (Intersection over Union) metric.
8.14.11.1 📋 Implementation Guidelines
Image Reading: Read the dimensions \(H \times W\) and the \(H \times W\) pixel matrix of the grayscale image.
Morphological Pipeline: Binarize the image using the Otsu method (
mm.threshold) and render the resulting binarized mask usingmm.drawImg.Ground Truth Reading:
- Read the number \(G\) of ground truth bounding boxes.
- If \(G > 0\), read \(G\) lines each containing 5 values:
id xmin_norm ymin_norm xmax_norm ymax_norm. - Coordinate Conversion: The ground truth coordinates are normalized in the range \([0.0, 1.0]\). To convert them to pixels on the image grid:
\[x_{\min} = \lfloor \text{xmin\_norm} \times W \rfloor, \quad y_{\min} = \lfloor \text{ymin\_norm} \times H \rfloor\]
\[w = \lfloor \text{xmax\_norm} \times W \rfloor - x_{\min}, \quad h = \lfloor \text{ymax\_norm} \times H \rfloor - y_{\min}\]
- Metric Extraction and IoU Calculation:
- Extract instance properties using
mm.measure(img_bin, precision=0.02). - For each detected bounding box \((x, y, w, h)\), compute the IoU overlap with respect to the ground truth boxes and set
hits = 1if there exists any match with \(\text{IoU} \ge 0.50\), otherwise sethits = 0.
- Extract instance properties using
- Output: Sort the instances by position
(bbox[0], bbox[1])and print the CSV table with the additional columnhits.- For sorting, use
medidas.sort(key=lambda m: (m['bbox'][1], m['bbox'][0])), withmedidas = mm.measure(img).
- For sorting, use
8.14.11.2 🧠 Theoretical Foundation and Conversion
| Concept | Formula / Operation | Description |
|---|---|---|
| Detected BBox | \((x, y, w, h)\) via mm.measure |
Bounding box computed on the discrete grid in integer pixels. |
| Ground Truth BBox (GT) | \((x_{\min}, y_{\min}, w, h)\) converted | Real box provided as input in relative coordinates \([0.0, 1.0]\). |
| IoU (Intersection over Union) | \(\text{IoU} = \frac{\text{Area}(B_{\text{DET}} \cap B_{\text{GT}})}{\text{Area}(B_{\text{DET}} \cup B_{\text{GT}})}\) | Evaluates the overlap rate of the boxes. It is considered valid if \(\text{IoU} \ge 0.50\). |
Validation Status (hits) |
\(1\) if \(\max(\text{IoU}) \ge 0.50\), otherwise \(0\) | Binary indicator of detector correctness relative to the ground truth. |
8.14.11.3 📦 Input and Output Specification (VPL)
Input:
- Line 1: Integers \(H\) and \(W\) (dimensions of the matrix).
- Next \(H\) lines: \(W\) integers (\(0\) to \(255\)) representing the grayscale image.
- Line \(H + 2\): Integer \(G\) (number of true ground truth boxes).
- Next \(G\) lines: 5 numeric values per line:
id xmin_norm ymin_norm xmax_norm ymax_norm(where the coordinates are floating-point values between \(0.0\) and \(1.0\)).
Output:
- Rendered binarized matrix via
mm.drawImg(img_bin). - CSV header:
id,area,perimeter,cx,cy,x,y,w,h,circularity,solidity,vertices,hits - One CSV line per detected object containing its formatted properties and the
hitsindicator (\(1\) or \(0\)).
8.14.11.4 📌 Examples
| Input | Output |
|---|---|
| 10 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 180 0 0 0 0 0 0 0 0 180 180 180 0 0 0 0 0 0 0 180 180 180 0 0 0 0 0 0 0 180 180 180 0 0 0 0 0 0 0 0 180 0 0 0 0 0 0 0 0 180 180 180 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 1 0.10 0.30 0.25 0.60 2 0.60 0.30 0.75 0.60 |
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 id area perimeter cx cy x y w h circularity solidity vertices hits 1 2.0 5.7 3.0 4.0 2 3 3 3 0.79 1.000 4 1 2 4.0 8.0 13.0 4.0 12 3 3 3 0.79 1.000 4 1 |
%%writefile EP08_11.py
# Python codeOverwriting EP08_11.py
TestSuite("EP08_11.py").run()✔️ EP08_11.cases already exists in casos/
📋 3 case(s) loaded from casos/EP08_11.cases
🔍 Testing Python: EP08_11.py
⚠️ EP08_11.py: Empty file (fewer than 3 lines). Tests skipped.