VPI - Vision Programming Interface

0.3.7 Release

Perspective Image Warp

Overview

Perspective Image Warp algorithm allows for correcting perspective distortion caused by camera misalignment with respect to the object plane being captured. This is the case when the camera is, for instance, pointing to a frame hanging on a wall, but looking from below. The resulting image won't have opposite sides that are parallel.

If the camera position, tilt and pan relative to the frame are known, a 3x3 pespective transform can be derived, which will warp the image in order to keep frame's opposite sides parallel to each other, as shown below.

Input Transform Corrected

\begin{bmatrix} 0.5386 & 0.1419 & -74\\ -0.4399 & 0.8662 & 291.5\\ -0.0005 & 0.0003 & 1 \end{bmatrix}

Implementation

The perspective transform matrix maps the source image into the destination image. The transform can be described mathematically by the equation below:

\begin{align*} \mathsf{y} = \mathsf{H}_p \mathsf{x} = \begin{bmatrix} \mathsf{A} & \mathsf{t} \\ \mathsf{p}^\intercal & p \end{bmatrix} \mathsf{x} \end{align*}

or, expanding the matrices and vectors:

\begin{align*} \begin{bmatrix} y_u \\ y_v \\ y_w \end{bmatrix} &= \begin{bmatrix} a_{11} & a_{12} & t_u \\ a_{21} & a_{22} & t_v \\ p_0 & p_1 & p \end{bmatrix} \begin{bmatrix}x_u \\ x_v \\ 1 \end{bmatrix} \\ \end{align*}

In these equations,

  • \(\mathsf{H}_p\) is the projection matrix
  • \(\mathsf{x}\) is the homogeneous coordinates in the source image.
  • \(\mathsf{y}\) is the homogeneous coordinates in the destination image.
  • \(\mathsf{A}\) is a 2x2 non-singular matrix with the linear component.
  • \(\mathsf{t}\) is the translation component.
  • \(\mathsf{p},p\) are the projective components. \(p\) is usually 1.

The projection of \(\mathsf{y}\) onto the output image is then given by:

\begin{align*} \begin{bmatrix} y'_u \\ y'_v \end{bmatrix} &= \begin{bmatrix} y_u/y_w \\ y_v/y_w \end{bmatrix} \end{align*}

These equations are efficiently implemented by doing the reverse operation, i.e, applying the inverse transform on destination pixels and sample the corresponding values from source image. If flag VPI_WARP_INVERSE is passed, the operation will assume that user's matrix is already inverted and won't try to invert it again. Pass zero if matrix must be inverted by VPI.

\begin{align*} \mathsf{H}_p^{-1} &= \begin{bmatrix}h_{11} & h_{12} & h_{13} \\ h_{21} & h_{22} & h_{23} \\ h_{31} & h_{32} & h_{33}\end{bmatrix} \\ \mathrm{dst}(u,v) &= \mathrm{src}\left(\frac{h_{11}u+h_{12}v+h_{13}}{h_{31}u+h_{32}v+h_{33}},\frac{h_{21}u+h_{22}v+h_{23}}{h_{31}u+h_{32}v+h_{33}}\right), \forall (u,v) \in \mathrm{dst} \end{align*}

The source and destination images may have different dimensions. They must have the same type, though.

Interpolation is used when the source coordinate doesn't fall exactly on a pixel. Perspective Image Warp accepts the following interpolation modes:

The only boundary mode currently supported is:

  • VPI_BOUNDARY_COND_ZERO - Sampling outside source image boundary will return a black value. This can be seen in the corrected image on top of this page, the black regions fall outside source image.

Usage

  1. Initialization phase
    1. Include the header that defines the Perspective Image Warp function.
    2. Define the stream on which the algorithm will be executed and the input image.
      VPIStream stream = /*...*/;
      VPIImage input = /*...*/;
    3. Create the output image. In this particular case both input and output have same dimensions, but this couldn't be the case.
      uint32_t w, h;
      vpiImageGetSize(input, &w, &h);
      vpiImageGetType(input, &type);
      VPIImage output;
      vpiImageCreate(w, h, type, 0, &output);
    4. Create the algorithm payload.
  2. Processing phase
    1. Define the perspective transform to be applied. Note that the transform doesn't have to be the same in every call.
      {
      { 0.5386, 0.1419, -74 },
      {-0.4399, 0.8662, 291.5},
      {-0.0005, 0.0003, 1 }
      };
    2. Submit the algorithm to the stream, along with all parameters.
    3. Optionally, wait until the processing is done.
      vpiStreamSync(stream);

For more details, consult the API reference.

Limitations and Constraints

Constraints for specific backends supersede the ones specified for all backends.

PVA

  • The following image types are accepted:
  • Only available on Jetson Xavier family.

CUDA

  • Not implemented

CPU

  • Not implemented

Performance

For further information on how performance was benchmarked, see Performance Measurement. Currently this algorithm is only available on Jetson Xavier family.

Jetson AGX Xavier
sizetypeinterp.CPUCUDAPVA
1920x1080nv12nearest n/an/a2.290 ms
1920x1080nv12linear n/an/a2.283 ms
1920x1080nv12catmull n/an/a2.284 ms
vpiSubmitPerspectiveImageWarp
VPIStatus vpiSubmitPerspectiveImageWarp(VPIPayload payload, VPIImage input, const VPIPerspectiveTransform xform, VPIImage output, VPIInterpolationType interp, VPIBoundaryCond bcond, uint32_t flags)
Submits the Perspective Image Warp operation to the stream associated with the payload.
VPIImageType
VPIImageType
Image formats.
Definition: Types.h:206
vpiCreatePerspectiveImageWarp
VPIStatus vpiCreatePerspectiveImageWarp(VPIStream stream, VPIPayload *payload)
Creates a payload for Perspective Image Warp algorithm.
vpiStreamSync
VPIStatus vpiStreamSync(VPIStream stream)
Blocks the calling thread until all submitted commands in this stream queue are done (queue is empty)...
VPIStream
struct VPIStreamImpl * VPIStream
A handle to a stream.
Definition: Types.h:177
VPI_INTERP_LINEAR
@ VPI_INTERP_LINEAR
Alias to fast linear interpolation.
Definition: Types.h:334
VPIImage
struct VPIImageImpl * VPIImage
A handle to an image.
Definition: Types.h:183
vpiImageGetSize
VPIStatus vpiImageGetSize(VPIImage img, uint32_t *width, uint32_t *height)
Get the image size in pixels.
vpiImageGetType
VPIStatus vpiImageGetType(VPIImage img, VPIImageType *type)
Get the image type.
PerspectiveImageWarp.h
VPI_BOUNDARY_COND_ZERO
@ VPI_BOUNDARY_COND_ZERO
All pixels outside the image are considered to be zero.
Definition: Types.h:270
VPIPayload
struct VPIPayloadImpl * VPIPayload
A handle to an algorithm payload.
Definition: Types.h:195
vpiImageCreate
VPIStatus vpiImageCreate(uint32_t width, uint32_t height, VPIImageType type, uint32_t flags, VPIImage *img)
Create an empty image instance with the specified flags.
VPIPerspectiveTransform
float VPIPerspectiveTransform[3][3]
Represents a 2D perspective transform.
Definition: Types.h:608