Sub topic 2
Bounding boxes: xyxy, xywh and normalized YOLO coordinates
This chapter assumes you know basic Python, images as pixel arrays, and the idea of training a neural network with a loss function. The goal is to move safely between the box formats used by detection libraries and YOLO label files.
What a bounding box represents
A bounding box is a rectangle around one object. Image coordinates usually start at the top-left corner:
xincreases from left to right.yincreases from top to bottom.Wis the image width in pixels.His the image height in pixels.
The same rectangle can have several representations. A common pixel-based format is xyxy, which stores the left, top, right, and bottom edges:
(x1, y1, x2, y2)Another format is xywh, which stores the box center, width, and height:
(x_center, y_center, width, height)The values in xyxy and pixel-based xywh are measured in pixels. YOLO label files use xywh, but normalize every value to the range from 0 to 1.
Comparing the formats
| Format | Values | Units | Example |
|---|---|---|---|
| `xyxy` | left, top, right, bottom | pixels | `(100, 50, 300, 250)` |
| pixel `xywh` | center x, center y, width, height | pixels | `(200, 150, 200, 200)` |
| normalized YOLO | center x, center y, width, height | fractions of image size | `(0.25, 0.25, 0.25, 0.25)` |
For an image that is 800 pixels wide and 600 pixels high, the pixel box (100, 50, 300, 250) becomes:
- Center:
(200, 150) - Size:
(200, 200) - Normalized center:
(200 / 800, 150 / 600) = (0.25, 0.25) - Normalized size:
(200 / 800, 200 / 600) = (0.25, 0.3333)
Converting between formats
The conversion from xyxy to pixel xywh is:
x_center = (x1 + x2) / 2
y_center = (y1 + y2) / 2
width = x2 - x1
height = y2 - y1To normalize the result, divide horizontal values by W and vertical values by H. To convert normalized YOLO values back to pixels, multiply horizontal values by W and vertical values by H.
def xyxy_to_yolo(box, image_width, image_height):
x1, y1, x2, y2 = box
x_center = (x1 + x2) / 2 / image_width
y_center = (y1 + y2) / 2 / image_height
width = (x2 - x1) / image_width
height = (y2 - y1) / image_height
return x_center, y_center, width, height
def yolo_to_xyxy(box, image_width, image_height):
x_center, y_center, width, height = box
x_center *= image_width
y_center *= image_height
width *= image_width
height *= image_height
x1 = x_center - width / 2
y1 = y_center - height / 2
x2 = x_center + width / 2
y2 = y_center + height / 2
return x1, y1, x2, y2
yolo_box = xyxy_to_yolo((100, 50, 300, 250), 800, 600)
print(yolo_box)
print(yolo_to_xyxy(yolo_box, 800, 600))YOLO label files
Each object has one line in its label file:
class_id x_center y_center width heightThe class index starts at zero. Coordinates are normalized, and there are no commas or brackets.
1 0.2500 0.2500 0.2500 0.3333This line describes class 1 in the example above. Values can be rounded for storage, but excessive rounding can move small boxes enough to hurt training.
Common coordinate errors
- Treating
x2andy2as width and height instead of the bottom-right corner. - Dividing every value by the image width.
- Writing pixel coordinates into a YOLO label file.
- Using a one-based class index when YOLO expects zero-based indices.
- Forgetting that resizing or cropping an image also requires updating its boxes.
Exercise
An image is 1000 pixels wide and 500 pixels high. Convert the pixel box (200, 100, 600, 300) to normalized YOLO coordinates.
Show answer
The center is (400, 200) and the size is (400, 200). Divide x values by 1000 and y values by 500: (0.4, 0.4, 0.4, 0.4).