## 1. Overview

In this article, we’ll talk about the Intersection over Union (IoU), a popular metric for evaluating object detection techniques.

First, we’ll explain the geometrical meaning of IoU. Then, we’ll illustrate how to calculate this metric. Finally, we’ll provide the pseudocode and a simple example of the IoU computation.

## 2. What Is Intersection Over Union?

**In object detection, our task is to locate and classify objects in an image.** To do so, we capture them with bounding boxes, each with a class label representing the object we detected in the box.

The IoU measures the accuracy of our detections. Given a ground-truth bounding box and a detected bounding box, **we compute the IoU as the ratio of the overlap and union areas**:

Here are some examples of different IoU values:

**The IoU can have any value between 0 and 1**. If two boxes do not intersect, the IoU is 0. On the other hand, if they completely overlap, the intersection and the union areas are equal. So, in that case, the IoU is 1.

**Therefore, the higher the IoU, the better the prediction of an object detection system**.

## 3. How to Compute IoU?

We assume a coordinate system with the positive x-axis moving to the right and the positive y-axis moving downward. A bounding box is defined by the left (L), right (R), top (T), and bottom (B).

**Let’s start by calculating the coordinates of the intersection rectangle**. Given a pair of bounding boxes, there are two different ways to name the coordinates (cases 1 and 2):

In both cases, the left side of the intersection is the rightmost left margin of the two bounding boxes. Similarly, the top of the intersection is the lower top margin of the two boxes. Therefore, the intersection’s left-top coordinates are:

(1)

(2)

The right side of the intersection is the leftmost right margin of the two boxes. Similarly, the bottom of the intersection is the higher bottom margin of the two boxes. Hence, the interesection’s bottom-right coordinates are:

(3)

(4)

The intersection area can be easily computed from the obtained coordinates:

(5)

**Let’s now compute the area of the union.** First we calculate the area of the two boxes:

(6)

(7)

The union area is computed as:

(8)

it is worth noting that intersection area is include in both and , hence is subtracted to in order to count the intersection area only once.

**Finally, we can calculate the IoU:**

(9)

## 4. Pseudocode

Here’s the pseudocode of the IoU computation:

The algorithm requires 8 input values, i.e., the coordinates of the two bounding boxes. The intersection and the union areas are computed as described previously. When or , the boxes do not intersect, hence the algorithm returns 0.

## 5. Example

Let’s consider the two boxes with coordinates:

The coordinates of the intersection rectangle are:

So, the intersection area is:

Each box has an area of , so the union area is:

Finally, the IoU is:

## 6. Computational Complexity

The algorithm always requires 8 input values. **Hence, the spatial complexity and time complexity are both constant**, i.e., .

## 7. Conclusion

In this article, we explained the IoU metric for object detection in images. We defined it geometrically and explained how to calculate it. Finally, we provided the pseudocode and an example of the IoU computation.