liveBook · Manning

Bounding box

As explained in Deep Learning with Python, Third Edition

A rectangle representation used to localize an object in an image, typically encoded by coordinates and size.

A bounding box is a rectangle that localizes an object in an image. Object detection combines each box with a class label, making it possible to count, track, or crop objects without pixel-level masks. Boxes are cheaper to annotate and usually faster to process than segmentation masks. section 12.0

A COCO annotation

COCO stores each box as x, y, width, and height in pixel coordinates measured from the image’s top-left corner. For one sample, normalized boxes such as [0.12, 0.27, 0.57, 0.33] accompany labels such as cat, bench, and bicycle. Drawing these rectangles over the image makes the annotation immediately inspectable. section 12.2.1

Choose the representation according to the model or tool consuming the box:

  • Prompt a segmentation model with the top-left and bottom-right corner coordinates of the rectangle. section 11.3.5
  • Normalize COCO-style x, y, width, and height values to a unit square so image dimensions no longer affect manipulation. section 12.2.1
  • For a YOLO grid, assign responsibility to the cell containing the box center; encode center offsets, image-relative width and height, and confidence. section 12.2.3
def to_grid(box):
    x, y, w, h = box
    cx, cy = (x + w / 2) * grid_size, (y + h / 2) * grid_size
    ix, iy = int(cx), int(cy)
    return (ix, iy), (cx - ix, cy - iy, w, h)

def from_grid(loc, box):
    (xi, yi), (x, y, w, h) = loc, box
    x = (xi + x) / grid_size - w / 2
    y = (yi + y) / grid_size - h / 2
    return (x, y, w, h)

The conversion utilities show how to move between normalized image coordinates and YOLO grid-cell coordinates, including center offsets and reconstruction. section 12.2.3

How Deep Learning with Python, Third Edition covers it

How deep it goes
Explains corner, pixel, normalized, and grid-relative encodings with preprocessing code, but defers detector architecture details.
Examples
Uses a mango box prompt, COCO cat-bench-bicycle annotations, visualization helpers, and YOLO grid targets.
What you can do after
Build it from scratch by parsing, normalizing, visualizing, and converting box annotations into YOLO training targets.
How it connects
Connects bounding boxes to object detection, image segmentation, YOLO, RetinaNet, COCO, class labels, confidence, and Intersection over Union.

Also explained in

Compare how 8 books explain bounding box →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with Python, Third Edition. The section links point to the parts of the book it draws on.