DIP+CV · Programming Exercise

EP06_01 — 🟢 Segmentation Evaluation by IoU ( Intersection over Union )

6.14.1 EP06_01 🟢 Segmentation Evaluation by IoU (Intersection over Union)

Throughout this chapter, several stages of the pipeline produce binary masks, such as in document segmentation, QRCode localization, and defect detection. To objectively evaluate the quality of these segmentations, it is necessary to compare them with a reference mask (ground truth).

One of the most widely used metrics for this purpose is the IoU (Intersection over Union), defined as the ratio between the intersection area and the union area of two binary masks. The higher the IoU value, the greater the agreement between the segmentation produced by the algorithm and the reference.

6.14.1.1 📋 Implementation Guidelines

  1. Dimensions: Read the integers \(L\) (number of rows) and \(C\) (number of columns).
  2. Reference mask: Read the \(L \times C\) binary elements (0 or 1) of the ref matrix.
  3. Predicted mask: Read the \(L \times C\) binary elements (0 or 1) of the pred matrix.
  4. Intersection: Count the number of positions \((i,j)\) for which ref[i][j] = 1 and pred[i][j] = 1.
  5. Union: Count the number of positions \((i,j)\) for which ref[i][j] = 1 or pred[i][j] = 1.
  6. Degenerate case: If the union equals \(0\), set \(\mathrm{IoU}=1{,}0\), since both masks are empty.
  7. Calculation: If the union is greater than zero, compute

\[ \mathrm{IoU}= \frac{|\mathrm{Intersecao}|} {|\mathrm{Uniao}|}. \]

  1. Classification: Determine the qualitative classification using the IoU value before rounding.
  2. Rounding: Display the IoU with four decimal places.
  3. Output: Print, in this order, the intersection, union, IoU, and classification.

6.14.1.2 📌 Computational Restrictions

  • If the union equals \(0\), the division must not be performed; the IoU must be defined as \(1{,}0\).
  • The classification ranges use non-strict comparisons (\(\geq\)).
  • The classification must be performed using the IoU value in full precision, before rounding for display.

6.14.1.3 🧠 Theoretical Foundation

The IoU is defined by

\[ \mathrm{IoU}= \frac{|R\cap P|} {|R\cup P|}, \]

where:

  • \(R\) represents the set of pixels belonging to the reference mask;
  • \(P\) represents the set of pixels belonging to the predicted mask;
  • \(|R\cap P|\) corresponds to the number of pixels belonging simultaneously to both masks;
  • \(|R\cup P|\) corresponds to the number of pixels belonging to at least one of the masks.
IoU Range Classification Interpretation
\(\mathrm{IoU}\geq0{,}90\) EXCELENTE Very high agreement between the masks.
\(0{,}70\leq\mathrm{IoU}<0{,}90\) BOM Small differences between the masks.
\(0{,}50\leq\mathrm{IoU}<0{,}70\) ACEITAVEL Partial agreement between the masks.
\(\mathrm{IoU}<0{,}50\) RUIM Low agreement between the masks.

The IoU depends only on the overlap between the masks and is therefore independent of the image size.

6.14.1.4 📦 Input and Output Specification (VPL)

Input:

  • Line 1: integer \(L\).
  • Line 2: integer \(C\).
  • Next \(L\) lines: binary elements (0 or 1) of the ref matrix.
  • Next \(L\) lines: binary elements (0 or 1) of the pred matrix.

Output:

  • Line 1: Intersecao: X
  • Line 2: Uniao: Y
  • Line 3: IoU: Z
  • Line 4: Classificacao: NOME

The IoU value must be printed with four decimal places.

6.14.1.5 📌 Examples

Input Output Observation
2
2
1 1
0 0
1 0
0 0
Intersecao: 1
Uniao: 2
IoU: 0.5000
Classificacao: ACEITAVEL
Half of the reference region was correctly segmented.
2
2
0 0
0 0
0 0
0 0
Intersecao: 0
Uniao: 0
IoU: 1.0000
Classificacao: EXCELENTE
Both masks are empty; by convention, \(\mathrm{IoU}=1{,}0\).
🎮 Simulator EP06_01: IoU (Intersection over Union) IoU = |A ∩ B| / |A ∪ B|
0
0
6
Move and resize the predicted mask to evaluate alignment.
Reference (A)
Predicted (B)
Overlap (A ∩ B)
–
Figure 6.21: EP06_01 Simulator: IoU between reference mask and predicted mask
%%writefile EP06_01.py
# Python code
Overwriting EP06_01.py
TestSuite("EP06_01.py").run()
✔️ EP06_01.cases already exists in casos/
📋 5 case(s) loaded from casos/EP06_01.cases

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