def iou(box1, box2):
    """Compute the Intersection-Over-Union of two given boxes.

    Args:
      box1: array of 4 elements [cx, cy, width, height].
      box2: same as above
    Returns:
      iou: a float number in range [0, 1]. iou of the two boxes.
    """

    lr = min(box1[0] + 0.5 * box1[2], box2[0] + 0.5 * box2[2]) - \
         max(box1[0] - 0.5 * box1[2], box2[0] - 0.5 * box2[2])
    if lr > 0:
        tb = min(box1[1] + 0.5 * box1[3], box2[1] + 0.5 * box2[3]) - \
             max(box1[1] - 0.5 * box1[3], box2[1] - 0.5 * box2[3])
        if tb > 0:
            intersection = tb * lr
            union = box1[2] * box1[3] + box2[2] * box2[3] - intersection

            return intersection / union

    return 0