Sub topic 1
Representing Boxes: Corners, Width-Height, and Normalization
Prerequisites
You should know basic Python and lists. You should also know that an object detector returns a bounding box, a class label, and a confidence score. This chapter assumes you understand precision and recall at a basic level.
A bounding box is a rectangle that marks where an object appears in an image. Before measuring detection quality, you must represent every box in a consistent coordinate system. A mismatch in box format can make correct predictions look wrong.
Corner Coordinates
The most common format stores the top-left and bottom-right corners:
(x1, y1, x2, y2)
Here, x measures horizontal position from left to right, and y measures vertical position from top to bottom. The point (x1, y1) is the top-left corner. The point (x2, y2) is the bottom-right corner.
For a valid box:
- x1 is less than x2.
- y1 is less than y2.
- All coordinates refer to the same image.
- Coordinates use the same pixel convention.
The box width is x2 - x1. The box height is y2 - y1. Its area is width multiplied by height.
This format is often called xyxy because it stores two x coordinates followed by two y coordinates. It is convenient for calculating overlap because the intersection boundaries can be found with maximum and minimum operations.
Center, Width, and Height
Another format stores the center and size:
(cx, cy, width, height)
The center is the midpoint of the rectangle. To convert this format to corner coordinates:
- x1 = cx - width / 2
- y1 = cy - height / 2
- x2 = cx + width / 2
- y2 = cy + height / 2
To convert in the other direction:
- cx = (x1 + x2) / 2
- cy = (y1 + y2) / 2
- width = x2 - x1
- height = y2 - y1
This format is often called cxcywh. It is useful for model outputs and box regression because the model predicts an object center and its size.
A related format starts at the top-left corner and stores size:
(x, y, width, height)
This is sometimes called xywh. Do not confuse its first two values with a center. In xywh, x and y identify the top-left corner. In cxcywh, cx and cy identify the center.
def xywh_to_xyxy(box):
"""Convert (top_left_x, top_left_y, width, height) to xyxy."""
x, y, width, height = box
return (x, y, x + width, y + height)
def cxcywh_to_xyxy(box):
"""Convert (center_x, center_y, width, height) to xyxy."""
cx, cy, width, height = box
return (
cx - width / 2,
cy - height / 2,
cx + width / 2,
cy + height / 2,
)
print(xywh_to_xyxy((10, 20, 40, 30)))
print(cxcywh_to_xyxy((30, 35, 40, 30)))Both calls describe the same rectangle and produce (10, 20, 50, 50).
Pixel and Normalized Coordinates
Pixel coordinates use the image dimensions directly. For an image that is 640 pixels wide and 480 pixels high, a box might be (100, 50, 300, 250).
Normalized coordinates divide positions and sizes by the image width or height. They usually range from 0 to 1:
- Normalize x values and widths by image width.
- Normalize y values and heights by image height.
For the pixel box above, the normalized xyxy box is:
(100 / 640, 50 / 480, 300 / 640, 250 / 480)
This gives approximately (0.15625, 0.10417, 0.46875, 0.52083).
Normalization makes annotations independent of a particular image resolution. A box can be resized with the image without changing its relative location. However, you must retain the image width and height when converting normalized values back to pixels.
def normalize_xyxy(box, image_width, image_height):
x1, y1, x2, y2 = box
return (
x1 / image_width,
y1 / image_height,
x2 / image_width,
y2 / image_height,
)
def denormalize_xyxy(box, image_width, image_height):
x1, y1, x2, y2 = box
return (
x1 * image_width,
y1 * image_height,
x2 * image_width,
y2 * image_height,
)
pixel_box = (100, 50, 300, 250)
normalized = normalize_xyxy(pixel_box, 640, 480)
print(normalized)
print(denormalize_xyxy(normalized, 640, 480))Coordinate Conventions
A frequent source of errors is mixing conventions between annotation tools, models, and evaluation code. Check these details before computing IoU:
- Is the format xyxy, xywh, or cxcywh?
- Does x and y describe a corner or a center?
- Are values pixels or normalized fractions?
- Are coordinates floats or integers?
- Are image dimensions ordered as width, height?
- Does the system use inclusive or half-open pixel boundaries?
For continuous geometry, the formulas above are normally sufficient. For discrete image slicing, a box may use half-open ranges such as x1 ≤ x < x2. In that convention, the width is still x2 - x1. Converting between systems without checking this rule can create one-pixel differences.
The next step is to use a common box format to measure how much two boxes overlap.
Exercise
An image is 800 pixels wide and 600 pixels high. Convert the normalized xyxy box (0.25, 0.10, 0.75, 0.60) to pixel coordinates, then compute its pixel width and height.
Show answer
The pixel box is (200, 60, 600, 360). Its width is 400 pixels and its height is 300 pixels.