0
0
Computer-visionConceptBeginner · 3 min read

What is IoU in Object Detection in Computer Vision

In object detection, IoU (Intersection over Union) is a metric that measures the overlap between two bounding boxes: the predicted box and the true box. It is calculated as the area of overlap divided by the area of union, helping to evaluate how well the model detects objects.
⚙️

How It Works

Imagine you draw two rectangles on a piece of paper: one shows where the computer thinks an object is, and the other shows where the object actually is. IoU measures how much these two rectangles overlap compared to their total combined area.

Think of it like comparing two puzzle pieces: the more they fit together, the higher the IoU score. If they perfectly match, the IoU is 1 (or 100%). If they don't overlap at all, the IoU is 0.

This simple ratio helps computers understand how accurate their guesses are when finding objects in images.

💻

Example

This example shows how to calculate IoU between two bounding boxes using Python. Each box is defined by its top-left and bottom-right corners.

python
def calculate_iou(box1, box2):
    # box = [x1, y1, x2, y2]
    x_left = max(box1[0], box2[0])
    y_top = max(box1[1], box2[1])
    x_right = min(box1[2], box2[2])
    y_bottom = min(box1[3], box2[3])

    if x_right <= x_left or y_bottom <= y_top:
        return 0.0  # No overlap

    intersection_area = (x_right - x_left) * (y_bottom - y_top)

    box1_area = (box1[2] - box1[0]) * (box1[3] - box1[1])
    box2_area = (box2[2] - box2[0]) * (box2[3] - box2[1])

    union_area = box1_area + box2_area - intersection_area

    iou = intersection_area / union_area
    return iou

# Example boxes
predicted_box = [50, 50, 150, 150]
true_box = [100, 100, 200, 200]

iou_score = calculate_iou(predicted_box, true_box)
print(f"IoU score: {iou_score:.2f}")
Output
IoU score: 0.14
🎯

When to Use

IoU is used to check how well an object detection model finds objects in images. It helps decide if a predicted box is good enough to count as a correct detection.

For example, in self-driving cars, IoU helps the system know if it correctly spotted a pedestrian or another vehicle. In medical imaging, it can check if a model accurately finds tumors.

IoU is also used to set thresholds: if the IoU between predicted and true boxes is above a certain value (like 0.5), the prediction is considered correct.

Key Points

  • IoU measures overlap between predicted and true bounding boxes.
  • It is a ratio of intersection area over union area.
  • Values range from 0 (no overlap) to 1 (perfect overlap).
  • Used to evaluate and improve object detection models.
  • Helps set thresholds for deciding correct detections.

Key Takeaways

IoU measures how much predicted and true bounding boxes overlap in object detection.
It is calculated as the area of overlap divided by the area of union between two boxes.
IoU values range from 0 (no overlap) to 1 (perfect match).
It helps decide if a detection is accurate enough to be counted as correct.
IoU is widely used in real-world applications like self-driving cars and medical imaging.