py123d_detection.torch.data.images

How a camera image becomes network input: one affine map, applied once.

Every step between the sensor and the network is an axis-aligned affine map of pixel coordinates: the rig’s fixed row crop and resize (rigs), the scale that makes the image cover the network input, the crop window, the horizontal flip. So the whole chain is one 3x3 matrix A, and it is used twice:

image_out  = warpAffine(image, A)     # one resampling of the source pixels
intrinsics = A @ intrinsics           # the camera that would have taken image_out

Because both come from the same matrix, pixels and projection cannot disagree. The thesis pipeline resized twice (to the rig’s size, then by the augmentation scale), which cost a second interpolation and left the intrinsics and the pixels a fraction of a pixel apart, because cv2.resize samples at pixel centres and the matrix scaled about the corner.

What is chosen per sample lives in ImageDraw, a zoom, a horizontal crop position and a flip, shared by every camera of the frame. ImagePipeline.resolve() turns a draw into the concrete transform for one camera’s size. The validation path is the default draw: no zoom, centred, not flipped.

Functions

warp(image, matrix, size)

Resample image through the 3x3 matrix into size = (height, width), once.

Classes

ImageDraw([zoom, crop_x, flip])

The per-sample choices of the image transform, independent of any camera's size.

ImagePipeline([size, mode, pad_divisor])

The network input every camera is mapped to.

ImageTransform(resize, crop, flip)

The resize, crop and flip for one camera, resolved against its size.

class py123d_detection.torch.data.images.ImageDraw(zoom=1.0, crop_x=0.5, flip=False)[source]

Bases: object

The per-sample choices of the image transform, independent of any camera’s size.

zoom: float = 1.0

Scale relative to the smallest one at which the image covers the network input. Above one the network sees a magnified part of the view; the validation path uses exactly one.

crop_x: float = 0.5

Where the crop window sits in the horizontal slack, from 0 (left) to 1 (right).

flip: bool = False
class py123d_detection.torch.data.images.ImageTransform(resize, crop, flip)[source]

Bases: object

The resize, crop and flip for one camera, resolved against its size.

resize: Tuple[float, float]

Scale applied to width and height. Equal in crop mode, independent in stretch mode.

crop: Tuple[int, int, int, int]

(x0, y0, x1, y1) of the window taken out of the resized image. It may reach past the image’s edge; what lies outside is black.

flip: bool
property size: Tuple[int, int]

(height, width) of the result.

matrix()[source]

The 3x3 map from the input’s pixel coordinates to the output’s.

Pixel centres sit on integer coordinates, so a window W pixels wide mirrors as x -> W - 1 - x, which is also what cv2.flip does. PETR mirrors as x -> W - x in the intrinsics while flipping the pixels with cv2.flip, which left every flipped camera’s projection one pixel off its image.

Return type:

ndarray

class py123d_detection.torch.data.images.ImagePipeline(size=(320, 800), mode='crop', pad_divisor=32)[source]

Bases: object

The network input every camera is mapped to.

Parameters:

mode (str) – "crop" scales by one factor until the image covers size, then cuts size out, bottom-aligned so the road is kept and the sky is lost. It keeps the pixel scale the same across cameras and is what PETR does. "stretch" resizes straight to size, distorting the aspect ratio but keeping the whole field of view; it ignores the zoom and the crop position of a draw.

size: Tuple[int, int] = (320, 800)

(height, width) of the network input, before padding.

mode: str = 'crop'
pad_divisor: int = 32

Pad up to a multiple of this, so a convolutional backbone’s strides divide it.

resolve(draw, height, width)[source]

The transform draw means for an image of height by width pixels.

An ImageTransform is already resolved and comes back unchanged, which is how a test or a debugging session replays one exact transform.

Return type:

ImageTransform

padded_size(height, width)[source]
Return type:

Tuple[int, int]

py123d_detection.torch.data.images.warp(image, matrix, size)[source]

Resample image through the 3x3 matrix into size = (height, width), once.

Output pixels whose source lies outside the image are black, as in PETR’s crop.

Return type:

ndarray