Intersection over Union (IoU)
What is IoU?
Intersection Over Union (IoU) is a widely used metric in computer vision that measures the overlap between two bounding boxes.
IoU is a crucial metric in object detection because it provides a clear quantification of how well the bounding boxes predicted by the model match the actual objects in the image.
Definition
IoU (also referred to as the Jaccard Index) between two boxes \(\textbf{A}, \textbf{B} \in \mathbb{R}^4\) is defined as the ratio of their intersection area to their union area.
Mathematically:
How to calculate IoU?
Suppose we need to compute the IoU between two boxes \(\textbf{A}\) and \(\textbf{B}\), given in the xyxy
(to-left, bottom-right corners) format:
To compute the intersection between \(\textbf{A}\) and \(\textbf{B}\) we define:
Then:
To calculate the union between \(A\) and \(B\):
Example
The intersection is calculated as follows:
Therefore:
For the union:
Then:
iscrowd
parameter
OD-Metrics iou
function supports iscrowd
COCOAPI parameter, which indicates whether a ground truth bounding box \(\textbf{y}_{true}\) represents a crowd of objects. For crowd
regions, the IoU metric is computed using a modified criterion: if a \(\textbf{y}_{true}\) object is marked as iscrowd
, it is permissible for a detected object \(\textbf{y}_{pred}\) to match any subregion of the \(\textbf{y}_{true}\). Choosing \(\textbf{y}'_{true}\) in the crowd \(\textbf{y}_{true}\) that best matches the \(\textbf{y}_{pred}\) can be done using:
Since by definition:
computing IoU when iscrowd=True
, is equivalent to:
This modified IoU criterion is applied to crowd
regions in the ground truth.
IoU in OD-Metrics
OD-Metrics supports IoU metrics with the iou
function. The usage is straightfoward.
from od_metrics import iou
a = [[50, 100, 150, 150]]
b = [[105, 120, 185, 160]]
result = iou(a, b, box_format="xyxy")
print(result)
"""
array([[0.19708029]])
"""
For more examples see Usage or API Reference.