DIP+CV · Programming Exercise

EP08_11 — 🔴 Classification and Validation of Objects with Bounding Box Ground Truth

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

  1. Image Reading: Read the dimensions \(H \times W\) and the \(H \times W\) pixel matrix of the grayscale image.

  2. Morphological Pipeline: Binarize the image using the Otsu method (mm.threshold) and render the resulting binarized mask using mm.drawImg.

  3. 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}\]

  1. 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 = 1 if there exists any match with \(\text{IoU} \ge 0.50\), otherwise set hits = 0.
  2. Output: Sort the instances by position (bbox[0], bbox[1]) and print the CSV table with the additional column hits.
    • For sorting, use medidas.sort(key=lambda m: (m['bbox'][1], m['bbox'][0])), with medidas = mm.measure(img).

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:

  1. Rendered binarized matrix via mm.drawImg(img_bin).
  2. CSV header: id,area,perimeter,cx,cy,x,y,w,h,circularity,solidity,vertices,hits
  3. One CSV line per detected object containing its formatted properties and the hits indicator (\(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
🧮 Simulator EP08_11: Bounding Boxes and IoU Comparison with Independent Controls GT vs DET BBox Validation
DISPLAY MODE
BOUNDING BOX DISPLAY
BBOX LEGEND:
Real Ground Truth (GT)
Accepted Detection (IoU ≥ 0.5)
Rejected Detection (IoU < 0.5)
BBox Overlap
PIXEL MATRIX VIEW
MEASUREMENTS, GEOMETRIC CLASSIFICATION AND IoU COMPARISON WITH GROUND TRUTH
id class area solidity vertices bbox det (x,y,w,h) bbox gt (x,y,w,h) IoU status (IoU ≥ 0.5)
Figure 8.25: EP08_11 Simulator: Geometric Classification with Independent Controls for BBox Overlays (GT and DET)
%%writefile EP08_11.py
# Python code
Overwriting 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.