Sub topic 4
Pixel Coordinates, Origins and Normalisation
Prerequisites and the working model
Assume you know basic Python, JSON, and that an image is a rectangular array of pixels. You should also understand that a labelled object has a position and size inside that array.
An image coordinate system maps a pixel location to two numbers:
xmeasures position from left to right.ymeasures position from top to bottom.- The origin
(0, 0)is normally the top-left corner. - Image width is the number of columns.
- Image height is the number of rows.
For an image with width 640 and height 480, valid pixel indices are usually x = 0 through 639 and y = 0 through 479.
Pixel positions and box boundaries
A bounding box is often stored as:
[x_min, y_min, width, height]
Here, x_min and y_min identify the top-left corner. The width and height describe the box size in pixels. For example:
[50, 40, 100, 50]
means that the box starts at (50, 40), is 100 pixels wide, and is 50 pixels high.
Its other corner is:
x_max = 50 + 100 = 150y_max = 40 + 50 = 90
Many libraries instead use (x_min, y_min, x_max, y_max). Always check whether the final coordinates describe a corner or a size.
A second source of errors is boundary convention. Some systems treat a box as covering pixels from x_min up to, but not including, x_max. This is called a half-open interval. Other tools describe the last included pixel explicitly. The difference is usually one pixel, but it matters when converting labels or calculating areas.
Normalising coordinates
Pixel coordinates depend on image size. A box at (50, 40) means something different in a 640-pixel image than in a 1280-pixel image. Normalisation expresses coordinates as fractions of the image dimensions.
YOLO-style detection labels usually store:
class_id, x_center, y_center, box_width, box_height
All four geometric values are divided by the image width or height:
x_center = (x_min + width / 2) / image_widthy_center = (y_min + height / 2) / image_heightbox_width = width / image_widthbox_height = height / image_height
The result should normally be between 0 and 1.
def coco_bbox_to_yolo(bbox, img_w, img_h):
"""Convert [x_min, y_min, width, height] to normalised YOLO values."""
x, y, w, h = bbox
x_center = (x + w / 2) / img_w
y_center = (y + h / 2) / img_h
return x_center, y_center, w / img_w, h / img_h
values = coco_bbox_to_yolo([50, 40, 100, 50], 640, 480)
print([round(value, 4) for value in values])This prints approximately:
[0.1562, 0.1354, 0.1562, 0.1042]
The centre is used because YOLO labels describe an object relative to the image, rather than storing the top-left corner directly.
Normalising points and masks
A point (x, y) can be normalised with:
x_normalised = x / image_widthy_normalised = y / image_height
Polygon vertices use the same rule, one vertex at a time. Do not normalise a mask by dividing its pixel values. A binary mask is already a raster representation: 0 means background and 1 means object. Its shape remains (height, width).
When converting between formats, keep the image dimensions attached to every annotation. Without them, pixel coordinates cannot be converted safely.
Common errors
- Swapping width and height when dividing coordinates.
- Using the top-left corner where a centre coordinate is required.
- Treating
[x, y, width, height]as[x_min, y_min, x_max, y_max]. - Mixing coordinates from a resized image with dimensions from the original image.
- Rounding too early, which can shift small objects by several pixels.
- Allowing negative coordinates or boxes outside the image without an explicit clipping policy.
Exercise
An image is 800 pixels wide and 600 pixels high. Convert the box [200, 100, 160, 120] from top-left-plus-size format to normalised centre-plus-size format.
Show answer
The centre is (280, 160). Divide by the image dimensions: xcenter = 280/800 = 0.35, ycenter = 160/600 ≈ 0.2667, width = 160/800 = 0.20, and height = 120/600 = 0.20. The result is [0.35, 0.2667, 0.20, 0.20].