Perspective 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.
The perspective transform matrix maps the source image into the destination image. The transform can be described mathematically by the equation below:
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.
VPI_BORDER_ZERO - Sampling outside source image border 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
Language:
Import VPI module
import vpi
Define the 3x3 perspective transform to be applied.
xform = [[ 0.5386, 0.1419, -74 ],
[ -0.4399, 0.8662, 291.5 ],
[ -0.0005, 0.0003, 1 ]]
Applies the perspective transform in the VPI image input, returning the result in a new VPI image.
with vpi.Backend.CUDA:
output = input.perspwarp(xform)
Initialization phase
Include the header that defines the Perspective Warp function.
Create the output image. In this particular case both input and output have same dimensions, but they could be different. The image formats must match, though.