Bounding Boxes
A bounding box is the rectangle that marks the location of a detected object. Understanding how they are represented and drawn is essential for building any detection system.
8 min•By Priygop Team•Updated 2026
Bounding Box Formats
Bounding Box Formats
import cv2
import numpy as np
# BOUNDING BOX FORMATS
# There are several ways to represent a bounding box:
# FORMAT 1: (x, y, w, h) — top-left corner + width + height
# Used by: OpenCV, COCO dataset
x, y, w, h = 100, 50, 200, 150
# FORMAT 2: (x1, y1, x2, y2) — top-left and bottom-right corners
# Used by: PyTorch, many deep learning frameworks
x1, y1, x2, y2 = 100, 50, 300, 200
# FORMAT 3: (cx, cy, w, h) — centre + width + height
# Used by: YOLO (internally)
cx, cy = 200, 125
# Convert between formats:
# (x, y, w, h) → (x1, y1, x2, y2)
x2 = x + w
y2 = y + h
# (x1, y1, x2, y2) → (x, y, w, h)
w_out = x2 - x1
h_out = y2 - y1
# DRAWING BOUNDING BOXES
image = np.zeros((400, 500, 3), dtype=np.uint8) # Black canvas
# Draw rectangle: (x, y) top-left, (x+w, y+h) bottom-right
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
# Add label
label = "car: 93%"
(label_w, label_h), _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
cv2.rectangle(image, (x, y-label_h-5), (x+label_w, y), (0, 255, 0), -1)
cv2.putText(image, label, (x, y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,0), 1)
cv2.imwrite("bounding_box_demo.jpg", image)Diagram
Loading diagram…
Machine Learning follows a structured pipeline from data to deployment