VPI - Vision Programming Interface
0.4.4 Release
Overview
The Temporal Noise Reduction sample application takes a noisy input video, reduces its noise and writes the result to disk. The user can define what backend will be used for processing.
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 Temporal Noise Reduction .
Shows how to properly use Temporal Noise Reduction to process a video sequence.
Instructions
The usage is:
./vpi_sample_09_tnr <backend> <input video> <output video>
where
backend: either cuda or pva , as cpu isn't available yet. It defines the backend that will perform the processing.
input video: video file to have noise reduced; it accepts .mp4, .avi and possibly others, depending on OpenCV's support.
output video: file to write the de-noised result. It'll use the same codec and fps as input video.
VPI samples installer includes a sample video with noise added artificially, found in /opt/nvidia/vpi-0.4/samples/assets/noisy.mp4 .
Here's one invocation example:
./vpi_sample_09_tnr cuda ../assets/noisy.mp4 denoised.mp4
Results
Input video De-noised video
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>
54 #define CHECK_STATUS(STMT) \
57 VPIStatus status = (STMT); \
58 if (status != VPI_SUCCESS) \
60 char buffer[VPI_MAX_STATUS_MESSAGE_LENGTH]; \
61 vpiGetLastStatusMessage(buffer, sizeof(buffer)); \
62 std::ostringstream ss; \
63 ss << vpiStatusGetName(status) << ": " << buffer; \
64 throw std::runtime_error(ss.str()); \
72 memset(&imgData, 0,
sizeof (imgData));
86 throw std::runtime_error(
"Frame type not supported" );
115 switch (imgData.
type )
133 throw std::runtime_error(
"Frame type not supported" );
139 int main(
int argc,
char *argv[])
152 throw std::runtime_error(std::string(
"Usage: " ) + argv[0] +
" <cpu|vic|cuda> <input_video> <output>" );
155 std::string strBackend = argv[1];
156 std::string strInputVideo = argv[2];
157 std::string strOutputVideo = argv[3];
160 cv::VideoCapture invid;
161 if (!invid.open(strInputVideo))
163 throw std::runtime_error(
"Can't open '" + strInputVideo +
"'" );
175 if (strBackend ==
"cpu" )
179 else if (strBackend ==
"cuda" )
183 else if (strBackend ==
"vic" )
189 throw std::runtime_error(
"Backend '" + strBackend +
190 "' not recognized, it must be either cpu, cuda or vic." );
202 #if CV_MAJOR_VERSION >= 3
203 int w = invid.get(cv::CAP_PROP_FRAME_WIDTH);
204 int h = invid.get(cv::CAP_PROP_FRAME_HEIGHT);
205 int fourcc = invid.get(cv::CAP_PROP_FOURCC);
206 double fps = invid.get(cv::CAP_PROP_FPS);
208 int w = invid.get(CV_CAP_PROP_FRAME_WIDTH);
209 int h = invid.get(CV_CAP_PROP_FRAME_HEIGHT);
210 int fourcc = invid.get(CV_CAP_PROP_FOURCC);
211 double fps = invid.get(CV_CAP_PROP_FPS);
214 cv::VideoWriter outVideo(strOutputVideo, fourcc, fps, cv::Size(w, h));
216 VPIImage imgPrevious, imgCurrent, imgOutput;
231 while (invid.read(cvFrame))
233 printf(
"Frame: %d\n" , ++curFrame);
235 frameBGR = ToVPIImage(frameBGR, cvFrame);
255 outVideo << ToCV(imgdata);
260 std::swap(imgPrevious, imgOutput);
263 catch (std::exception &e)
265 std::cerr << e.what() << std::endl;
uint32_t height
Height of this plane in pixels.
struct VPIContextImpl * VPIContext
A handle to a context.
Declares functions that implement the Temporal Noise Reduction algorithm.
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.
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.
void vpiContextDestroy(VPIContext ctx)
Destroy a context instance as well as all resources it owns.
VPIImagePlane planes[VPI_MAX_PLANE_COUNT]
Data of all image planes.
@ VPI_TNR_DEFAULT
Chooses the version with best quality available in the current device and given backend.
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.
VPIStatus vpiSubmitTemporalNoiseReduction(VPIStream stream, VPIPayload payload, VPIImage prevFrame, VPIImage curFrame, VPIImage outFrame)
Submits a Temporal Noise Reduction operation to the stream associated with the given payload.
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_TNR_PRESET_INDOOR_LOW_LIGHT
Low light indoor scene.
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 vpiCreateTemporalNoiseReduction(VPIBackend backend, uint32_t width, uint32_t height, VPIImageFormat imgFormat, VPITNRVersion version, VPITNRPreset preset, float strength, VPIPayload *payload)
Creates a payload for Temporal Noise Reduction algorithm.
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.
Functions and structures for dealing with VPI contexts.