VPI - Vision Programming Interface
0.4.4 Release
Overview
The Perspective Warp sample application takes an input video and outputs a video where for each frame, a different perspective warp is applied. The result is a perspective bouncy effect. Sample application could be modified to get the input from a camera and apply the effect in real-time.
This sample shows the following:
Creating and destroying a VPI stream.
Wrapping an OpenCV image to be used by VPI.
Wrapping a VPI-managed image into an OpenCV's cv::Mat.
Use OpenCV to fetch frames from a video file.
Use OpenCV save frames into a video file.
Create pipeline that does Convert Image Format to convert from/to NV12 format and run Perspective Warp .
Create a perspective transform matrix with origin on the center of input frame.
Instructions
The usage is:
./vpi_sample_10_perspwarp <backend> <input video> <output video>
where
backend: currently only pva , as other backends don't have an implementation available. It defines the backend that will perform the processing.
input video: video file to the effect applied on; it accepts .mp4, .avi and possibly others, depending on OpenCV's support.
output video: file to write the resulting video. It'll use the same codec and fps as input video.
VPI samples installer includes some sample videos that can be used as input. They are found in /opt/nvidia/vpi-0.4/samples/assets/ directory.
Here's one invocation example:
./vpi_sample_10_perspwarp pva ../assets/noisy.mp4 persp_effect.mp4
Results
Input video Perspective effect
Source code
For convenience, here's the code that is also installed in the samples directory.
29 #include <opencv2/core/version.hpp>
30 #if CV_MAJOR_VERSION >= 3
31 # include <opencv2/imgcodecs.hpp>
32 # include <opencv2/videoio.hpp>
34 # include <opencv2/highgui/highgui.hpp>
37 #include <opencv2/imgproc/imgproc.hpp>
53 #define CHECK_STATUS(STMT) \
56 VPIStatus status = (STMT); \
57 if (status != VPI_SUCCESS) \
59 char buffer[VPI_MAX_STATUS_MESSAGE_LENGTH]; \
60 vpiGetLastStatusMessage(buffer, sizeof(buffer)); \
61 std::ostringstream ss; \
62 ss << vpiStatusGetName(status) << ": " << buffer; \
63 throw std::runtime_error(ss.str()); \
70 for (
int i = 0; i < 3; ++i)
72 for (
int j = 0; j < 3; ++j)
74 r[i][j] = a[i][0] * b[0][j];
75 for (
int k = 1; k < 3; ++k)
77 r[i][j] += a[i][k] * b[k][j];
87 memset(&imgData, 0,
sizeof (imgData));
101 throw std::runtime_error(
"Frame type not supported" );
111 if (image ==
nullptr )
130 switch (imgData.
type )
148 throw std::runtime_error(
"Frame type not supported" );
154 int main(
int argc,
char *argv[])
167 throw std::runtime_error(std::string(
"Usage: " ) + argv[0] +
" <cpu|vic|cuda> <input_video> <output>" );
170 std::string strBackend = argv[1];
171 std::string strInputVideo = argv[2];
172 std::string strOutputVideo = argv[3];
175 cv::VideoCapture invid;
176 if (!invid.open(strInputVideo))
178 throw std::runtime_error(
"Can't open '" + strInputVideo +
"'" );
190 if (strBackend ==
"cpu" )
194 else if (strBackend ==
"cuda" )
198 else if (strBackend ==
"vic" )
204 throw std::runtime_error(
"Backend '" + strBackend +
205 "' not recognized, it must be either cpu, cuda or vic." );
212 #if CV_MAJOR_VERSION >= 3
213 int w = invid.get(cv::CAP_PROP_FRAME_WIDTH);
214 int h = invid.get(cv::CAP_PROP_FRAME_HEIGHT);
215 int fourcc = invid.get(cv::CAP_PROP_FOURCC);
216 double fps = invid.get(cv::CAP_PROP_FPS);
218 int w = invid.get(CV_CAP_PROP_FRAME_WIDTH);
219 int h = invid.get(CV_CAP_PROP_FRAME_HEIGHT);
220 int fourcc = invid.get(CV_CAP_PROP_FOURCC);
221 double fps = invid.get(CV_CAP_PROP_FPS);
224 cv::VideoWriter outVideo(strOutputVideo, fourcc, fps, cv::Size(w, h));
235 memset(&xform, 0,
sizeof (xform));
241 while (invid.read(cvFrame))
243 printf(
"Frame: %d\n" , curFrame++);
245 frameBGR = ToVPIImage(frameBGR, cvFrame);
255 float v1 = sin(curFrame / 30.0 * 2 * M_PI / 2) * 0.0005f;
256 float v2 = cos(curFrame / 30.0 * 2 * M_PI / 3) * 0.0005f;
264 MatrixMultiply(tmp, P, t1);
265 MatrixMultiply(xform, t2, tmp);
279 outVideo << ToCV(imgdata);
283 catch (std::exception &e)
285 std::cerr << e.what() << std::endl;
uint32_t height
Height of this plane in pixels.
struct VPIContextImpl * VPIContext
A handle to a context.
uint32_t width
Width of this plane in pixels.
VPIStatus vpiStreamCreate(uint32_t flags, VPIStream *stream)
Create a stream instance.
VPIBackend
VPI Backend types.
VPIStatus vpiContextCreate(uint32_t flags, VPIContext *ctx)
Create a context instance.
VPIStatus vpiContextSetCurrent(VPIContext ctx)
Sets the context for the calling thread.
@ VPI_LOCK_READ
Lock memory only for reading.
VPIStatus vpiImageUnlock(VPIImage img)
Releases the lock on an image object.
Declares functions that implement the Perspective Warp algorithm.
VPIStatus vpiStreamSync(VPIStream stream)
Blocks the calling thread until all submitted commands in this stream queue are done (queue is empty)...
@ VPI_BACKEND_CUDA
CUDA backend.
Stores information about image characteristics and content.
struct VPIStreamImpl * VPIStream
A handle to a stream.
@ VPI_INTERP_LINEAR
Alias to fast linear interpolation.
void vpiContextDestroy(VPIContext ctx)
Destroy a context instance as well as all resources it owns.
float VPIPerspectiveTransform[3][3]
Represents a 2D perspective transform.
VPIImagePlane planes[VPI_MAX_PLANE_COUNT]
Data of all image planes.
VPIStatus vpiImageCreate(uint32_t width, uint32_t height, VPIImageFormat fmt, uint32_t flags, VPIImage *img)
Create an empty image instance with the specified flags.
Functions and structures for dealing with VPI images.
uint32_t pitchBytes
Difference in bytes of beginning of one row and the beginning of the previous.
struct VPIImageImpl * VPIImage
A handle to an image.
int32_t numPlanes
Number of planes.
@ VPI_BACKEND_VIC
VIC backend.
@ VPI_BOUNDARY_COND_ZERO
All pixels outside the image are considered to be zero.
struct VPIPayloadImpl * VPIPayload
A handle to an algorithm payload.
VPIStatus vpiImageLock(VPIImage img, VPILockMode mode, VPIImageData *hostData)
Acquires the lock on an image object and returns a pointer to the image planes.
VPIImageFormat type
Image type.
Declaration of VPI status codes handling functions.
@ VPI_BACKEND_CPU
CPU backend.
VPIStatus vpiImageCreateHostMemWrapper(const VPIImageData *hostData, uint32_t flags, VPIImage *img)
Create an image object by wrapping around an existing host memory block.
Declares functions dealing with VPI streams.
VPIStatus vpiImageSetWrappedHostMem(VPIImage img, const VPIImageData *hostData)
Redefines the wrapped host memory in an existing VPIImage wrapper.
void * data
Pointer to the first row of this plane.
VPIStatus vpiCreatePerspectiveWarp(VPIBackend backend, VPIPayload *payload)
Creates a payload for Perspective Warp algorithm.
VPIStatus vpiSubmitPerspectiveWarp(VPIStream stream, VPIPayload payload, VPIImage input, const VPIPerspectiveTransform xform, VPIImage output, VPIInterpolationType interp, VPIBoundaryCond bcond, uint32_t flags)
Submits the Perspective Warp operation to the stream associated with the payload.
Functions and structures for dealing with VPI contexts.