Bounding Shapes
Wrap every contour in the cheapest box, tightest rotated box, or smallest circle that holds it. Detectors speak boxes, so this is the translation layer.
Why Does This Exist?
Contours are clouds of points, but trackers, detectors, and robot grippers want four numbers: position plus size. Bounding shapes compress any loop into that contract, trading tightness for cost across three options. Every detector output you have ever seen, including modern object detection boxes, descends from the upright variant.
This page covers the upright box, the rotated minimum-area rectangle, and the enclosing circle, with the angle convention that trips up every beginner. Rotation-proof identity rather than fitted geometry is shape matching.
Think of It Like This
Shipping a ladder
Ship a ladder in an axis-aligned crate and you pay for a box nearly as wide as the ladder is long, mostly air. Rotate the crate to lie along the ladder and the box shrinks to the ladder's true footprint. The diagonal crate is the minimum-area rectangle: same ladder, far less air, one extra measurement (the angle).
It stops holding for round cargo. A beach ball ships best in a cylinder, not any rectangle, the same way circular parts want the minimum enclosing circle. Box-shaped answers fit box-shaped questions only.
How It Actually Works
Three fits, three prices
The upright box takes min and max of and : two passes, four numbers, always axis-aligned. The minimum-area rectangle rotates with the shape, returning center, width, height, and angle; it is the tightest rectangle possible. The minimum enclosing circle returns center and radius via an iterative expansion, and suits round parts where any rectangle wastes the corners.
The rotated-rectangle payoff, in numbers
Take a bar rotated degrees. Its upright box spans wide and tall, an area of about against the bar's true . The minimum-area rectangle recovers the true and area , over three times tighter. That gap is pure air billed as object, and on a conveyor it decides whether neighbouring parts merge into one detection.
The angle convention trap
OpenCV reports the rotated angle in with width and height swapping roles as the box crosses in some versions, so the "width" of an upright bar reads as height plus a angle. Read the angle together with both side lengths, never alone, and normalize to a center-plus-long-axis format before comparing across frames.
Code
Upright versus rotated extents for the fixture bar:
import math
w, h, deg = 100.0, 20.0, 30.0t = math.radians(deg)up_w = w * math.cos(t) + h * math.sin(t)up_h = w * math.sin(t) + h * math.cos(t)print(f"upright {up_w:.1f} x {up_h:.1f} area {up_w * up_h:.0f}, rotated area {w * h:.0f}")# -> upright 96.6 x 67.3 area 6503, rotated area 2000The upright box wastes square pixels of air; the rotated fit wastes none.
Watch Out For
Comparing raw OpenCV angles across frames
Symptom: a slowly rotating part shows angle jumps of with width and height swapping, breaking Kalman filters and orientation plots. The angle convention wraps and swaps sides near square shapes. Convert every detection to an unwrapped long-axis angle in immediately after fitting, and track that instead.
Letting one outlier size the box
Symptom: a single stray contour point stretches the box across half the image, dwarfing the real part. Min and max listen to extremes by definition. Hull or approximate the contour first, or fit to a high percentile of points, before taking extrema.
The Quick Version
- The upright box is cheapest; the rotated rectangle is tightest; the enclosing circle suits round parts.
- A bar at fills a -pixel upright box but only rotated pixels.
- OpenCV angles live in with width-height swaps, so normalize to long-axis form before tracking.
- Outliers stretch min-max boxes, so clean the contour before fitting.
- Boxes answer where and how big; they never answer which shape, which needs matching instead.