Skip to course content
Free computer vision course

Computer Vision and Multimodal AI

Unit 08.00: A box is four numbers and a convention

A bounding box is four numbers, and four numbers mean four different things depending on the convention.

xyxy, xywh, cxcywh

The same box in three formats, plus normalised coordinates.

The code converts between them and names which library uses which.

box_xyxy = (10, 20, 60, 90)
x1, y1, x2, y2 = box_xyxy
box_xywh = (x1, y1, x2 - x1, y2 - y1)
cx, cy = x1 + (x2 - x1) / 2, y1 + (y2 - y1) / 2
box_cxcywh = (cx, cy, x2 - x1, y2 - y1)

print(f"{'format':12} {'values':28} used by")
print(f"{'xyxy':12} {str(box_xyxy):28} torchvision, most detectors")
print(f"{'xywh':12} {str(box_xywh):28} COCO annotations")
print(f"{'cxcywh':12} {str(box_cxcywh):28} YOLO (usually normalised 0-1)")

normalised = (cx / 100, cy / 100, (x2 - x1) / 100, (y2 - y1) / 100)
print(f"\nnormalised for a 100x100 image: {tuple(round(v, 2) for v in normalised)}")
print("four numbers, four meanings. Nothing raises if you mix them.")

# Every format is four numbers, so a mismatch produces boxes in the wrong place
# rather than an error. Convert at the boundary of your code and assert the
# format once, loudly.

Every format is four floats, so a mismatch produces boxes in the wrong place rather than an exception. A model fed xywh where it expects xyxy trains, converges to something, and predicts nonsense.

Normalisation adds a fourth possibility: the same four numbers in pixels or in 0-1, indistinguishable without knowing the image size.

The mistake this prevents

The mistake is converting formats wherever they meet. Convert once at the boundary of your code, assert the format loudly there, and let everything internal assume one convention.

Takeaway

Four numbers, four conventions, no error on mismatch. Convert at the boundary and assert; internal code should assume one format.