# Boxes Across Frames

On the [ImgTransformations](https://docs.luxonis.com/software-v3/depthai/depthai-components/messages/img_transformations.md) page,
the network found the person with an axis-aligned bounding box in its input image. Mapped to the preview and to the sensor, the
same rectangle came out turned by −20°, exactly like the image:

![frame chain](https://docs.luxonis.com/static/images/software/depthai/img-transformations/frame-chain.webp)

This page looks closely at that rectangle. It explains how a rectangle is described (`RotatedRect`) and what exactly happens to it
when an `ImgTransformation` moves it to another image. Most of the time this just works. The details matter when you draw turned
rectangles, crop by them, track their angle over time, or work with normalized coordinates in images that are not square.

The examples use the scene from the overview page: a room with a parcel, seen in an 800 × 500 image. The parcel is 200 × 100 px.
Its tape stripe and its label sticker show which way it is turned, and every figure is computed with DepthAI. On this page, the
parcel is the object in the image, and a rectangle is the `RotatedRect` that describes it.

In short:

 * A normalized `RotatedRect` is a pixel rectangle divided by the image size (W, H). Its angle stays in pixels.
 * Compute the geometry of normalized rectangles in pixels: use `denormalize(W, H)` or `getOuterRectInFrame(W, H)`.
 * A transformation returns the smallest rectangle around the mapped corners, in the form nearest to the source angle.
 * The width side is not kept. After a turn of more than 45° or a mirror, width and height can swap.

## RotatedRect

A rectangle that can be turned, like the person's bounding box in the preview, is a `RotatedRect`: a center, a size along its own
sides, and an angle.

| Field | Meaning |
| --- | --- |
| `center` | Center of the rectangle (`Point2f`). |
| `size` | Width and height along the sides of the rectangle, not along the image axes. |
| `angle` | Degrees. Positive turns clockwise on screen, because y points down. Any value is valid, for example 350°, −170° or 91°. |
| `getPoints()` | Corners 0–3: (−w/2, −h/2), (+w/2, −h/2), (+w/2, +h/2), (−w/2, +h/2), turned by the angle. Edge 0 → 1 is the width side, drawn orange in all figures. |

![corner order](https://docs.luxonis.com/static/images/software/depthai/img-transformations/corner-order.webp)

### Equal forms

The parcel below can be described in four ways. `(w, h, θ)`, `(h, w, θ ± 90°)` and `(w, h, θ ± 180°)` all enclose it exactly; only
the corner order and the width side differ. This is why a transformation has to choose one of them.

![equal forms](https://docs.luxonis.com/static/images/software/depthai/img-transformations/equal-forms.webp)

## Units

Detections usually store their rectangles normalized, so the same numbers fit any image size. A rectangle is in pixels of the
image that its `ImgTransformation` describes (W × H), or normalized to that image.

 * Normalized means center ÷ (W, H) and size ÷ (W, H). The angle does not change. `denormalize(W, H)` gives the pixel rectangle
   back. `ImageManipConfig.addCropRotatedRect(rect, True)` uses the same meaning. Values can be below 0 or above 1.
 * `Point2f`, `Size2f` and `Rect` carry the flags `normalized` and `hasNormalized`. A `RotatedRect` with mixed units raises an
   error. A `Rect` without flags guesses: normalized when x + w ≤ 1 and y + h ≤ 1.

> **Turned normalized rectangles in non-square images**
> `getPoints()`, `getOuterRect()`, `getOuterXYWH()`, `getOuterCXCYWH()` and the conversion to `Rect` turn the stored values. When W ≠ H, normalizing divides x and y by different numbers, which is a non-uniform scale. Turning a rectangle in that scaled space and scaling it back is the same as shearing it, for the same reason a shear turns the parcel into a parallelogram (see Shapes that are not rectangles below). So the corners and outer rectangle are wrong for a turned normalized rectangle. Call `denormalize(W, H)` first, or use `getOuterRectInFrame(W, H)`.

![normalized trap](https://docs.luxonis.com/static/images/software/depthai/img-transformations/normalized-trap.webp)

The parcel, turned by 30° and stored normalized in the 800 × 500 px image. The correct outer rectangle is 223.21 × 186.60 px.
`getOuterRect()` gives 253.21 × 149.10 px.

```python
rect = detection.getBoundingBox()                        # normalized
xmin, ymin, xmax, ymax = rect.getOuterRectInFrame(W, H)  # correct for any angle
corners = rect.denormalize(W, H).getPoints()             # corners in pixels
```

## Transformations

These are the functions that moved the person's bounding box from the NN input to the preview and the sensor. They move a
rectangle to another image:

| Function | Maps each corner with |
| --- | --- |
| `remapRectTo(to, rect)` | `remapPointTo()`: the 2D steps, distortion and the rotation between cameras. `remapRectFrom()` is the reverse call. |
| `projectRectTo(to, rect, depth)` | A ray to 3D at `depth` mm, the full extrinsics and a projection. Raises an error when the depth is ≤ 0. |
| `transformRect(rect)` | The 2D matrix of the transformation. `invTransformRect()` is the inverse. |

### How the result is made

 1. The rectangle is denormalized with the source size, when it is normalized.
 2. The 4 corners are mapped, and the smallest rectangle around them is fitted.
 3. The equal form whose angle is nearest to the source angle is returned.
 4. The rectangle is normalized with the target size, when the input was normalized.

So the returned angle is always within ±45° of the source angle, and a small turn gives a small change. Turn the whole image by θ
around its center with `addRotation(θ)`, and the parcel and its 200 × 100 px rectangle turn with it:

![turn tiles](https://docs.luxonis.com/static/images/software/depthai/img-transformations/turn-tiles.webp)

> **The width side is not kept**
> The size stays for turns within ±45° of 0° or 180°, and swaps within ±45° of 90° or 270°. A 60° turn gives −30°, 100 × 200. Don't read `size.width` as "the long side" or as a text direction after a transformation. `addCropRotatedRect()` uses width, height and angle, so two equal forms give crops turned by 90° to each other.

At exactly ±45° from the source angle, the returned form flips to the other side: the corners barely move, but the angle jumps by
90° and the sides swap. To track an angle over time, compare corners, or compare angles modulo 90° with a size swap.

### Mirrors

Flip the image horizontally, and the parcel's label sticker moves to its other end. A flip returns the mirror image of the
rectangle in the form nearest to the source angle. For a turned rectangle, this form usually has width and height swapped.

![mirrors](https://docs.luxonis.com/static/images/software/depthai/img-transformations/mirrors.webp)

### Shapes that are not rectangles

Stretch or shear the image, and the turned parcel itself becomes a parallelogram. The same happens with a non-uniform scale, a
shear or a perspective. The mapped rectangle becomes the smallest rectangle around it, with a different angle, so it is larger
than the parcel.

![non rect](https://docs.luxonis.com/static/images/software/depthai/img-transformations/non-rect.webp)

The parcel turned by 30°, in an image scaled by (1, 0.625): the mapped rectangle is 19.84°, 213 × 68 px, and its outer rectangle
is 223.21 × 136.08 px. The parcel itself spans 223.21 × 116.63 px.

> **The true shape**
> A `RotatedRect` cannot describe a parallelogram. When you need the true shape, map the corners with `remapPointTo()`.

## Where rotated rectangles are used

| Message | Rectangles |
| --- | --- |
| [ImgDetections](https://docs.luxonis.com/software-v3/depthai/depthai-components/messages/img_detections.md) | Each detection stores its bounding box as a `RotatedRect`, plus the axis-aligned bounds `xmin..ymax`. `transformTo()` maps them with `remapRectTo()`. |
| [SpatialImgDetections](https://docs.luxonis.com/software-v3/depthai/depthai-components/messages/spatial_img_detections.md) | Same bounding box as `ImgDetections`, plus spatial coordinates. With a known depth, `transformTo()` maps the rectangle with `projectRectTo()`. |
| [Tracklets](https://docs.luxonis.com/software-v3/depthai/depthai-components/messages/tracklets.md) | `roi` is an axis-aligned `Rect`. `transformTo()` maps it as a rectangle and keeps the outer rectangle, so a turn makes it larger. |
| [AprilTags](https://docs.luxonis.com/software-v3/depthai/depthai-components/messages/april_tags.md) | Each tag stores its four corners as points, so `transformTo()` keeps the true shape. |
| [ImageManipConfig](https://docs.luxonis.com/software-v3/depthai/depthai-components/messages/image_manip_config.md) | `addCropRotatedRect()` crops by width, height and angle, so the form of the rectangle sets the crop orientation. |
| [SegmentationMask](https://docs.luxonis.com/software-v3/depthai/depthai-components/messages/segmentation_mask.md) | `getBoundingBoxes()` returns a `RotatedRect` around each labeled region. |

## Good practices

Do:

 * Denormalize before you use corners, outer rectangles or angles.
 * Use `getOuterRectInFrame(W, H)` for an axis-aligned rectangle of a normalized one.
 * Compare rectangles by their corners, or by angle modulo 90° with a size swap.
 * Set unit flags on every new rectangle.
 * Map corners with `remapPointTo()` when you need the true shape.

Avoid:

 * `getPoints()` or `getOuterRect()` on a turned normalized rectangle.
 * Reading `size.width` as the long side after a transformation.

### Need assistance?

Head over to [Discussion Forum](https://discuss.luxonis.com/) for technical support or any other questions you might have.
