Skip to main content

Bounding Boxes and Coordinate Systems

Convert between xyxy, xywh and center-size boxes, normalize coordinates correctly, and avoid mistakes when resizing, clipping or flipping detection labels.

A bounding box describes a rectangular region of an image. Four numbers are enough for an axis-aligned box, but those numbers are meaningful only when the format, units, and image dimensions are known. A format mismatch can turn a plausible detection into a box drawn in the wrong place.

Keep the rectangle fixed while changing its description

The explorer starts with a box around the left car. Change the format without moving the rectangle, then switch between pixel and normalized coordinates. All views describe the same boundaries.

One rectangle, three coordinate formats
Box format
Coordinate units
30
100
90
60
x₁
30.0
y₁
100.0
x₂
120.0
y₂
160.0

Image size: 320 × 200. The origin is at the top left; x increases right and y increases down. These are continuous boundary coordinates, so the right boundary may equal 320.

The image is 320 pixels wide and 200 pixels high. Its origin is at the top left, with x increasing rightward and y increasing downward. The box initially spans (30, 100) to (120, 160).

Three common formats

FormatValues for the starting boxMeaning
xyxy(30, 100, 120, 160)Left, top, right, bottom
xywh(30, 100, 90, 60)Left, top, width, height
cxcywh(75, 130, 90, 60)Center x, center y, width, height

The conversion follows directly from the geometry:

w=x2−x1,h=y2−y1,cx=x1+x22,cy=y1+y22w=x_2-x_1,\quad h=y_2-y_1,\quad c_x=\frac{x_1+x_2}{2},\quad c_y=\frac{y_1+y_2}{2}

To recover corners from center-size form, subtract and add half the width and height. Torchvision provides explicit conversion utilities; passing the correct input format is part of their contract. Torchvision box conversion

Normalize x by width and y by height

For normalized coordinates, divide horizontal values by image width and vertical values by image height. The starting xyxy box becomes (0.09375, 0.5, 0.375, 0.8). Using width for all four values would make the vertical coordinates wrong on this rectangular image.

Normalization does not make a box independent of preprocessing. A center crop removes an offset before scaling; letterboxing adds padding after scaling. The coordinate values still refer to a particular image canvas. Record whether labels describe the original image, the resized image, or the padded model input.

Boundaries are different from pixel indices

This page uses continuous image-boundary coordinates. A full-image box is (0, 0, 320, 200), with area 320 × 200. Although the last pixel column has index 319, its right boundary is 320. There is no extra +1 in this area calculation.

Some older annotation or evaluation conventions use inclusive integer endpoints. Do not mix their area formulas with continuous boundaries. A one-pixel discrepancy matters especially for tiny objects, where a small coordinate error can change a large fraction of the box area.

Transform and validate labels with the image

For a horizontal flip across a canvas of width WW, corners transform as:

x1′=W−x2,x2′=W−x1x'_1=W-x_2,\qquad x'_2=W-x_1

The left and right roles exchange. Flipping each x value without exchanging them creates negative width. After a crop, subtract the crop origin, clip to the retained canvas, and remove boxes whose clipped width or height is zero. Keep class labels and any object IDs aligned with the surviving boxes.

Inspect transformed annotations visually before training. Check positive dimensions, finite numbers, the expected coordinate range, and the same transform for every label type. Augmentation demonstrates this correspondence; IoU uses the resulting geometry to measure overlap.

If you found this explanation helpful, consider sharing it with others.

Mastodon