Affine Warp

Functions to apply affine transformations on 2D images.

enum augpy::WarpScaleMode

Enum whether to scale relative to the shortest or longest side of the image.

Values:

enumerator WARP_SCALE_SHORTEST

Scaling is relative to the shortest side of the image.

enumerator WARP_SCALE_LONGEST

Scaling is relative to the longest side of the image.

int augpy::make_affine_matrix(py::buffer out, size_t source_height, size_t source_width, size_t target_height, size_t target_width, float angle, float scale, float aspect, float shifty, float shiftx, float sheary, float shearx, bool hmirror, bool vmirror, WarpScaleMode scale_mode, int max_supersampling)

Create a \(2 \times 3\) matrix for a set of affine transformations. This matrix is compatible with the warpAffine function of OpenCV with the WARP_INVERSE_MAP flag set.

Transforms are applied in the following order:

  1. shear

  2. scale & aspect ratio

  3. horizontal & vertical mirror

  4. rotation

  5. horizontal & vertical shift

Return

recommended supersampling factor for the warp

Parameters
  • out: output buffer that matrix is written to; must be a writeable \(2 \times 3\) float buffer

  • source_height: \(h_s\) height of the image in pixels

  • source_width: \(w_s\) width of the image in pixels

  • target_height: \(h_t\) height of the output canvas in pixels

  • target_width: \(w_t\) width of the output canvas in pixels

  • angle: clockwise angle in degrees with image center as rotation axis

  • scale: scale factor relative to output size; 1 means fill target height or width wise depending on scale_mode and whichever is longest/shortest; larger values will crop, smaller values leave empty space in the output canvas

  • aspect: controls the aspect ratio; 1 means same as input, values greater 1 increase the width and reduce the height

  • shifty: shift the image in y direction (vertical); 0 centers the image on the output canvas; -1 means shift up as much as possible; 1 means shfit down as much as possible; the maximum distance to shift is \( max(scale \cdot h_s - h_t, h_t - scale \cdot h_s) \)

  • shiftx: same as shifty, but in x direction (horizontal)

  • sheary: controls up/down shear; for every pixel in the x direction move sheary pixels in y direction

  • shearx: same as sheary but controls left/right shear

  • hmirror: if true flip image horizontally

  • vmirror: if true flip image vertically

  • scale_mode: if WARP_SCALE_SHORTEST scale is relative to shortest side; this fills the output canvas, cropping the image if necessary; if WARP_SCALE_LONGEST scale is relative to longest side; this ensures the image is contained inside the output canvas, but leaves empty space

  • max_supersampling: upper limit for recommended supersampling

void augpy::warp_affine(CudaTensor *src, CudaTensor *dst, py::buffer matrix, CudaTensor *background, int supersampling)

Takes an image in channels-last format \( (H, W, C) \) and affine warps it into a given output tensor in channels-first format \( (C, H, W) \). Any blank canvas is filled with a background color. The warp is performed with bi-linear and supersampling.

Parameters
  • src: image tensor

  • dst: target tensor

  • matrix: \(2 \times 3\) float transformation matrix, see make_affine_matrix for details

  • background: background color to fill empty canvas

  • supersampling: supersampling factor, e.g., 3 means 9 samples are taken in a \(3 \times 3\) grid