Skip to content

Intersection over Union

box_iou

def box_iou(box_true, box_detection)
Compute Intersection over Union of two bounding boxes - box_true and box_detection. Both boxes are expected to be tuples in (x_min, y_min, x_max, y_max) format.

Args
  • box_true: tuple representing ground-truth bounding boxes.

  • box_detection: tuple representing detection bounding boxes.

Returns
  • iou: float value between 0 and 1. None if union is equal to 0.
Example usage
>>> from onemetric.cv.utils.iou import box_iou

>>> iou = box_iou_batch(
...     boxes_true=(0., 0., 1., 1.),
...     boxes_detection=(0.25, 0., 1.25, 1.)
... )

>>> iou
... 0.6

box_iou_batch

def box_iou_batch(boxes_true, boxes_detection)
Compute Intersection over Union of two sets of bounding boxes - boxes_true and boxes_detection. Both sets of boxes are expected to be in (x_min, y_min, x_max, y_max) format.

Args
  • boxes_true: 2d np.ndarray representing ground-truth boxes. shape = (N, 4) where N is number of true objects.

  • boxes_detection: 2d np.ndarray representing detection boxes. shape = (M, 4) where M is number of detected objects.

Returns
  • iou: 2d np.ndarray representing pairwise IoU of boxes from boxes_true and boxes_detection. shape = (N, M) where N is number of true objects and M is number of detected objects.
Example usage
>>> import numpy as np

>>> from onemetric.cv.utils.iou import box_iou_batch

>>> boxes_true = np.array([
...     [0., 0., 1., 1.],
...     [2., 2., 2.5, 2.5]
... ])
>>> boxes_detection = np.array([
...     [0., 0., 1., 1.],
...     [2., 2., 2.5, 2.5]
... ])
>>> iou = box_iou_batch(boxes_true=boxes_true, boxes_detection=boxes_detection)

>>> iou
... np.array([
...     [1., 0.],
...     [0., 1.]
... ])

mask_iou

def mask_iou(mask_true, mask_detection)
Compute Intersection over Union of two masks - mask_true and mask_detection. Shapes of mask_true and mask_detection should be identical. Both arrays are expected to be np.uint8 type and contain binary values (0 or 1).

Args
  • mask_true: 2d np.ndarray representing ground-truth mask.

  • mask_detection: 2d np.ndarray representing detection mask.

Returns
  • iou: float value between 0 and 1. None if union is equal to 0.
Example usage
>>> import numpy as np

>>> from onemetric.cv.utils.iou import mask_iou

>>> full_mask = np.ones((10, 10)).astype('uint8')
>>> quarter_mask = np.zeros((10, 10)).astype('uint8')
>>> quarter_mask[0:5, 0:5] = 1

>>> iou = mask_iou(mask_true=full_mask, mask_detection=quarter_mask)

>>> iou
... 0.25