Interactive lab · Introductory · 10 min read

YOLO bounding box coordinates explained

Turn pixel corners into a normalized YOLO row and back, see how many decimals you need, and find the exporter bugs that pass every range check, in a lab you can drag.

anastig editorial teamPublished

At a glance for instructors

Suggested class time
10 to 15 minutes
Level
Introductory (first computer vision or deep learning course; engineers writing a label exporter)
Prerequisites
Pixel coordinates with the origin at the top-left corner; Division and fractions

Learning objectives

  • Compute the YOLO row for a box given its pixel corners and the image size, and recover the pixel corners from a row.
  • Bound the round-trip error caused by writing d decimals (0.75 × 10^−d × image width) and choose a precision for a given image size.
  • Identify the bug behind a wrong row (swapped axes, corner instead of centre, wrong dimension, missing normalization, integer division) and explain why a range check misses three of them.

Free to use in class without an account. Jump to the teaching notes and worksheet.

A YOLO label row is class x_center y_center width height. The four coordinates describe the box by its centre and size, and each one is divided by the image width (the x values) or the image height (the y values), so every value lies between 0 and 1. To write a row from pixel corners, average the corners to get the centre, subtract them to get the size, and divide. To read a row back, multiply by the image size and step half the width and height out from the centre. Drag the box in the lab below to see every step update, change the image size, and see where common exporter bugs put the box.

Worked example

The image is W = 640 by H = 480 pixels. Coordinates are measured from the top-left corner of the image, x to the right and y down. The box runs from the corner (x1, y1) = (120, 150) to the corner (x2, y2) = (300, 330).

QuantityPixelsDivide byYOLO value
centre x: (120 + 300) / 2210W = 6400.328125
centre y: (150 + 330) / 2240H = 4800.500000
width: 300 − 120180W = 6400.281250
height: 330 − 150180H = 4800.375000

With class 0, the row is 0 0.328125 0.500000 0.281250 0.375000. Reading it back: x1 = (0.328125 − 0.28125 / 2) × 640 = 120 and x2 = (0.328125 + 0.28125 / 2) × 640 = 300; y1 and y2 work the same way with 480.

A 640 by 480 image with a box from (120, 150) to (300, 330) and its centre (210, 240). cx = (120 + 300) / 2 = 210, divided by 640 gives 0.328125; cy = 240, divided by 480 gives 0.5; w = 180, divided by 640 gives 0.28125; h = 180, divided by 480 gives 0.375. Row: 0 0.328125 0.500000 0.281250 0.375000.
DiagramThe box, its centre and the arithmetic that turns pixel corners into the YOLO row.

The lab starts with this box. Its results are hidden at first: work each value out, then reveal it, or start a challenge (the challenges use other boxes).

Static version — the interactive lab loads with JavaScript

Static version of the lab. The interactive lab lets you drag the box, change the image size and the number of decimals, see where exporter bugs put the box and decode rows typed by hand. Without it, use the worked example above, the tables below and the worksheet at the end of the page; worksheet questions 1 to 5 are the lab's challenges in written form.

What the row means

The Ultralytics YOLO detection format, as documented for its trainers, has one .txt file per image and one row per object. Each row is class x_center y_center width height. The coordinates are normalized to the range 0 to 1, x values divided by the image width and y values by the image height. Class numbers start at 0, and an image with no objects needs no label file [1]. The class number is a position in the list of names in the dataset's YAML file; the format comparison explains why that list must never be reordered.

Three conventions sit underneath every number in this page:

  • Origin and axes. (0, 0) is the top-left corner of the image, x grows to the right and y grows downwards. A W×H image spans x from 0 to W and y from 0 to H.
  • Continuous coordinates. A box edge is a position between pixels, not a pixel number. The pixel in column 0 covers x from 0 to 1, so a box that covers columns 120 to 299 has x1 = 120 and x2 = 300, and its width is x2 − x1 = 180, with no "+ 1".
  • The displayed image. Ultralytics notes that coordinates refer to the image as viewers display it, with its EXIF orientation applied [1]. A phone photo stored sideways with a rotation flag is labelled in its upright orientation.

From pixel corners to the row

For a box with corners (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2) in a W×HW \times H image:

xc=x1+x22W,yc=y1+y22H,wn=x2−x1W,hn=y2−y1Hx_c = \frac{x_1 + x_2}{2W}, \qquad y_c = \frac{y_1 + y_2}{2H}, \qquad w_n = \frac{x_2 - x_1}{W}, \qquad h_n = \frac{y_2 - y_1}{H}

If your box is in COCO form, [x,y,w,h][x, y, w, h] with (x,y)(x, y) the top-left corner, the centre is x+w/2x + w/2, so xc=(x+w/2)/Wx_c = (x + w/2)/W and wn=w/Ww_n = w/W. Either way, every x quantity is divided by W and every y quantity by H. That is also why the worked example's 180 × 180-pixel box has two different normalized sizes: 180 / 640 = 0.28125 and 180 / 480 = 0.375. A square box in pixels is not square in YOLO numbers unless the image is square.

torchvision calls the corner form xyxy and the centre form cxcywh, and torchvision.ops.box_convert converts between them in whatever units you pass [2]. It does not normalize; dividing by the image size is a separate step.

Back to pixels: the inverse

Multiply each value by the dimension it was divided by, then step out from the centre:

x1=(xc−wn2)W,x2=(xc+wn2)W,y1=(yc−hn2)H,y2=(yc+hn2)Hx_1 = \left(x_c - \tfrac{w_n}{2}\right) W, \quad x_2 = \left(x_c + \tfrac{w_n}{2}\right) W, \quad y_1 = \left(y_c - \tfrac{h_n}{2}\right) H, \quad y_2 = \left(y_c + \tfrac{h_n}{2}\right) H

Example: the row 0 0.25 0.6 0.2 0.3 on a 1280×720 image gives x1 = (0.25 − 0.1) × 1280 = 192, x2 = 448, y1 = (0.6 − 0.15) × 720 = 324 and y2 = 540. The same row on a 640×360 image gives a box exactly half the size in the same relative place. That is the point of normalizing: a plain resize of the image needs no change to its labels. Cropping and padding do; resizing images without breaking boxes covers those cases.

How many decimals?

The format does not fix a precision. Each written value is off by at most half a step, 0.5×10−d0.5 \times 10^{-d} for dd decimals. Since x1=(xc−wn/2) Wx_1 = (x_c - w_n/2)\,W, an edge can be off by at most 0.75×10−d×W0.75 \times 10^{-d} \times W pixels. We wrote the box (500, 300) to (900, 700) on a 1920×1080 image with different precisions and decoded it again:

DecimalsRow (class omitted)Decoded x1, y1, x2, y2 (px)Largest edge errorUpper bound
20.36 0.46 0.21 0.37489.6, 297, 892.8, 696.610.4 px14.4 px
30.365 0.463 0.208 0.370501.12, 300.24, 900.48, 699.841.12 px1.44 px
40.3646 0.4630 0.2083 0.3704500.064, 300.024, 900, 700.0560.064 px0.144 px
60.364583 0.462963 0.208333 0.370370499.9997, 300.0002, 899.999, 699.99980.001 px0.0014 px

Six decimals keeps the error far below a pixel even on large images. Two decimals moves a box edge by 10 pixels on a full-HD frame, which is enough to cut off a small object.

Valid rows

A correct row satisfies four checks: the class is a whole number that exists in the names list; all four values are between 0 and 1; the width and height are greater than 0; and the box stays inside the image, xc−wn/2≥0x_c - w_n/2 \ge 0 and xc+wn/2≤1x_c + w_n/2 \le 1 (and the same for y). Clip boxes to the image before writing them, and drop boxes that have no area left after clipping.

Trainers differ in how strictly they apply this. We ran the Ultralytics label checker from release 8.4.220 (the current release on 2026-11-28; the main branch has the same code) and from the older 8.4.24 [3]:

  • values from −0.01 to 1.01 are tolerated, as long as width and height are not negative (8.4.24 has no width/height condition);
  • values above 1.01 fail with "non-normalized or out of bounds coordinates", and larger negative values with "negative label values" (8.4.24 words it "negative class labels or coordinate");
  • a class number at or above the number of classes fails with "Label class … exceeds dataset class count";
  • a zero-size row such as 0 0 0 0 0 is accepted without any message.

A failing file does not stop training: that image is skipped as corrupt with a warning. Read the warnings, or check the dataset before training (validating a YOLO dataset turns this into a checklist).

Common bugs, and which ones a check catches

All rows below were written for the worked-example box, (120, 150) to (300, 330) on 640×480, and decoded the way a trainer decodes them.

BugWhat was writtenRowDecoded box (px)IoU with the true boxCaught by the four checks above?
Nonecentre and size, x ÷ W, y ÷ H0 0.328125 0.500000 0.281250 0.375000(120, 150) to (300, 330)1n/a
x and y swappedrow and column order (y first), as in array indexing0 0.500000 0.328125 0.375000 0.281250(200, 90) to (440, 225)0.131No
Corner instead of centrethe COCO top-left corner, normalized0 0.187500 0.312500 0.281250 0.375000(30, 60) to (210, 240)0.143No
Wrong dimensionx ÷ H and y ÷ W0 0.437500 0.375000 0.375000 0.281250(160, 112.5) to (400, 247.5)0.267No
Not normalizedpixels0 210 240 180 180far outside the image0Yes: values above 1 (Ultralytics also rejects it)
Integer division210 / 640 in C, Java or Python 20 0 0 0 0zero size at (0, 0)0Yes: zero size (Ultralytics accepts it silently, so the object becomes background)

IoU, intersection over union, is the overlap area divided by the combined area: 1 for a perfect match, 0 for no overlap.

The true box (120, 150) to (300, 330) and three decoded buggy boxes: x and y swapped gives (200, 90) to (440, 225), IoU 0.131; corner instead of centre gives (30, 60) to (210, 240), IoU 0.143; x divided by H and y by W gives (160, 112.5) to (400, 247.5), IoU 0.267. Unnormalized pixels and integer division are caught by range and size checks (Ultralytics accepts the zero row silently); these three are not.
DiagramThe three bugs that pass every range check put the box in the wrong place. Only drawing the decoded boxes over the images reveals them.

The wrong-dimension bug deserves a warning of its own. On a square image, W = H, so dividing by the wrong one changes nothing: for the worked-example box on a 416×416 image the decoded box is exact. Resize those images to 640×480, with the box scaling along, and the same exporter gives IoU 0.267, as in the table. A pipeline tested only on square images will pass every test and then fail on the first camera that is not square.

The conversion in code

These two functions are all a converter needs. We ran them with Python 3 on 2026-11-28; the outputs are in the comments.

def to_yolo(x1, y1, x2, y2, W, H, cls=0, decimals=6):
    """Pixel corners (origin top-left, x right, y down) -> YOLO detect row."""
    xc, yc = (x1 + x2) / 2 / W, (y1 + y2) / 2 / H
    wn, hn = (x2 - x1) / W, (y2 - y1) / H
    return f"{cls} {xc:.{decimals}f} {yc:.{decimals}f} {wn:.{decimals}f} {hn:.{decimals}f}"

def from_yolo(row, W, H):
    """YOLO detect row -> (class, x1, y1, x2, y2) in pixels."""
    c, xc, yc, wn, hn = row.split()
    xc, yc, w, h = float(xc) * W, float(yc) * H, float(wn) * W, float(hn) * H
    return int(c), xc - w / 2, yc - h / 2, xc + w / 2, yc + h / 2

row = to_yolo(120, 150, 300, 330, 640, 480)
print(row)                       # 0 0.328125 0.500000 0.281250 0.375000
print(from_yolo(row, 640, 480))  # (0, 120.0, 150.0, 300.0, 330.0)

If your annotation tool gives inclusive pixel numbers instead of edges, convert them first: for 1-based numbers, as in Pascal VOC, x1 = xmin − 1 and x2 = xmax; for 0-based inclusive numbers, x1 = xmin and x2 = xmax + 1. The format comparison explains the VOC convention. Bounding-box annotation covers drawing boxes that are tight in the first place.

Common misconceptions

  • "The first two numbers are the top-left corner." They are the centre. Writing the corner shifts every box left and up by half its size.
  • "A square box has equal width and height in the row." Only on a square image; the width is divided by W and the height by H.
  • "If no value is above 1, the labels are fine." Swapped axes, corners instead of centres and the wrong dimension all produce valid-looking numbers. Draw the boxes.
  • "More decimals is wasted space." Two decimals can move an edge by more than 10 pixels on a 1920-pixel image. Six decimals cost a few bytes per row.

Worksheet

Use these with or without the lab. Questions 1 to 5 are the lab's challenges.

  1. A 640×480 image has a box from (100, 60) to (420, 300). What x_center goes into its YOLO row?
  2. A label file for a 1280×720 image contains 0 0.5 0.4 0.25 0.3. What is x1 in pixels?
  3. On a 1280×720 image the box (500, 300) to (900, 610) was written with 2 decimals as 0 0.55 0.63 0.31 0.43. What x1 does that row decode to, and how far is it from 500?
  4. A 1280×720 image has the box (256, 180) to (640, 420). An exporter wrote 0 0.200000 0.250000 0.300000 0.333333. Which mistake produced it?
  5. An exporter divides x values by H and y values by W. On a 416×416 image with the box (50, 100) to (250, 300) its rows decode exactly. After resizing the images to 640×480 (the box scales along), what IoU does the decoded box have with the true box?
  6. Same 640×480 image as the worked example, box from (0, 0) to (640, 480). What is the row?
  7. Why are the width and height of the worked-example box equal in pixels (180) but different in the row (0.28125 and 0.375)?
  8. A 1280×720 image has a box from (100, 200) to (420, 560). Write the row for class 0 with 6 decimals.
  9. Decode the row 0 0.25 0.75 0.1 0.2 for an 800×600 image into pixel corners.
  10. A COCO box [50, 40, 100, 60] belongs to a 500×400 image. Write the YOLO values.
  11. The row 3 0.9 0.5 0.3 0.2 passes a "values between 0 and 1" check. Is the box inside the image?
  12. What is the largest edge error you can get from 2 decimals on a 4000-pixel-wide image? From 6 decimals?
  13. On a 1000×500 image, the correct row for a box is 0 0.2 0.3 0.2 0.4. An exporter wrote 0 0.3 0.2 0.4 0.2. Which bug is it, and where does a trainer put the box?
Show the answers
  1. (100 + 420) / 2 = 260, and 260 / 640 = 0.40625.
  2. (0.5 − 0.25 / 2) × 1280 = 480.
  3. (0.55 − 0.31 / 2) × 1280 = 505.6, which is 5.6 px from the true 500.
  4. Top-left corner instead of centre: 0.2 × 1280 = 256 and 0.25 × 720 = 180 are the corner; the centre would be 0.35 and 0.416667.
  5. 0.401. The bug is invisible on square images and appears with the first non-square one.
  6. 0 0.500000 0.500000 1.000000 1.000000.
  7. The width is divided by W = 640 and the height by H = 480.
  8. Centre (260, 380), size 320 × 360: 0 0.203125 0.527778 0.250000 0.500000.
  9. Centre (200, 450), size 80 × 120, so the corners are (160, 390) and (240, 510).
  10. Centre (50 + 50, 40 + 30) = (100, 70): 0.200000 0.175000 0.200000 0.150000.
  11. No. The right edge is 0.9 + 0.3 / 2 = 1.05, past the image border. A range check on the four values alone misses it; check the edges.
  12. 0.75 × 10⁻² × 4000 = 30 px with 2 decimals; 0.75 × 10⁻⁶ × 4000 = 0.003 px with 6.
  13. x and y were swapped (each pair is reversed). The trainer decodes centre (300, 100) and size 400 × 100, so the box runs from (100, 50) to (500, 150) instead of (100, 50) to (300, 250).

Teaching notes

Suggested sequence

  1. 2 min: show the worked example and figure above the lab, then give the class the box (100, 60) to (420, 300) on 640×480 and ask for its x_center. The lab opens with every result hidden.
  2. 3 min: start challenge 1 in the lab, collect predictions and press Check (the outputs are now shown). Drag the box and focus a line in the arithmetic panel to highlight the matching guide on the image.
  3. 3 min: challenge 2 (decode a row; the decoder is off until Check), then type a row into the decoder and press Draw this box.
  4. 3 min: challenge 3 (2 decimals on 1280×720): after Check, compare the decoded x1 with the bound shown under the round-trip table.
  5. 2 to 4 min: challenges 4 and 5 on bugs, including the square-image counterexample, then hand out the worksheet (questions 6 to 13 are new material).

Discussion questions

  1. Why does the YOLO row not change when an image is resized from 1280×720 to 640×360? (Answer: every value is a fraction of the width or height, and a plain resize keeps all fractions.)
  2. A colleague's exporter passes all checks on a 640×640 dataset but fails on a 1920×1080 one. Which bug would you suspect first? (Answer: dividing by the wrong dimension, which is invisible when W = H.)
  3. Which of the five bugs in the table would a check of 'all values between 0 and 1' catch, and why not the others? (Answer: only the unnormalized pixels; integer division gives zeros, which need the size check, and swapped axes, corner-for-centre and wrong dimension produce valid-looking fractions.)
  4. How many decimals would you require for 4K frames (3840 pixels wide) if no edge may move by more than 0.1 pixel? (Answer: 5. The bound 0.75 × 10⁻ᵈ × 3840 is 0.288 px for d = 4 and 0.029 px for d = 5.)

Print this page for a worksheet: the printed version shows the static exercise, tables and answers.

References

  1. [1] Ultralytics documentation: Object detection datasets overview (Ultralytics YOLO format). Accessed 2026-11-28. docs.ultralytics.com/datasets/detect/
  2. [2] torchvision documentation: torchvision.ops.box_convert (stable). Accessed 2026-11-28. docs.pytorch.org/vision/stable/generated/torchvision.ops.box_convert.html
  3. [3] Ultralytics source: ultralytics/data/utils.py, verify_image_label (main branch, read 2026-11-28; behaviour tested on releases 8.4.220 and 8.4.24). github.com/ultralytics/ultralytics/blob/main/ultralytics/data/utils.py

Where to go next

How to cite: anastig editorial team (2026). YOLO bounding box coordinates explained. anastig Learn. https://anastig.ai/learn/yolo-bounding-box-coordinates

How this resource was prepared and checked: editorial standards. Found an error? Tell us.

YOLO Bounding Box Coordinates Explained | anastig