29 #include <opencv2/core/version.hpp>
30 #include <opencv2/imgcodecs.hpp>
31 #include <opencv2/imgproc/imgproc.hpp>
32 #include <opencv2/videoio.hpp>
47 #define CHECK_STATUS(STMT) \
50 VPIStatus status = (STMT); \
51 if (status != VPI_SUCCESS) \
53 char buffer[VPI_MAX_STATUS_MESSAGE_LENGTH]; \
54 vpiGetLastStatusMessage(buffer, sizeof(buffer)); \
55 std::ostringstream ss; \
56 ss << vpiStatusGetName(status) << ": " << buffer; \
57 throw std::runtime_error(ss.str()); \
61 static void ProcessMotionVector(
VPIImage mvImg, cv::Mat &outputImage)
72 cv::Mat flow(mvImage.size(), CV_32FC2);
73 mvImage.convertTo(flow, CV_32F, 1.0f / (1 << 5));
81 cv::Mat magnitude, angle;
83 cv::Mat flowChannels[2];
84 split(flow, flowChannels);
85 cv::cartToPolar(flowChannels[0], flowChannels[1], magnitude, angle,
true);
89 cv::threshold(magnitude, magnitude, clip, clip, cv::THRESH_TRUNC);
92 cv::Mat _hsv[3], hsv, bgr;
94 _hsv[1] = cv::Mat::ones(angle.size(), CV_32F);
95 _hsv[2] = magnitude / clip;
98 cv::cvtColor(hsv, bgr, cv::COLOR_HSV2BGR);
99 bgr.convertTo(outputImage, CV_8U, 255.0);
102 int main(
int argc,
char *argv[])
106 cv::Mat cvPrevFrame, cvCurFrame;
125 throw std::runtime_error(std::string(
"Usage: ") + argv[0] +
" <nvenc> <input_video> <low|medium|high>");
129 std::string strBackend = argv[1];
130 std::string strInputVideo = argv[2];
131 std::string strQuality = argv[3];
134 if (strQuality ==
"low")
138 else if (strQuality ==
"medium")
142 else if (strQuality ==
"high")
148 throw std::runtime_error(
"Unknown quality provided");
152 if (strBackend ==
"nvenc")
158 throw std::runtime_error(
"Backend '" + strBackend +
"' not recognized, it must be nvenc.");
162 cv::VideoCapture invid;
163 if (!invid.open(strInputVideo))
165 throw std::runtime_error(
"Can't open '" + strInputVideo +
"'");
173 if (!invid.read(cvPrevFrame))
175 throw std::runtime_error(
"Cannot read frame from input video");
187 int32_t width = cvPrevFrame.cols;
188 int32_t height = cvPrevFrame.rows;
198 CHECK_STATUS(
vpiImageCreate(width, height, imgFmt, 0, &imgPrevFrameTmp));
199 CHECK_STATUS(
vpiImageCreate(width, height, imgFmt, 0, &imgCurFrameTmp));
203 CHECK_STATUS(
vpiImageCreate(width, height, imgFmtBL, 0, &imgPrevFrameBL));
204 CHECK_STATUS(
vpiImageCreate(width, height, imgFmtBL, 0, &imgCurFrameBL));
207 int32_t mvWidth = (width + 3) / 4;
208 int32_t mvHeight = (height + 3) / 4;
211 int fourcc = cv::VideoWriter::fourcc(
'M',
'P',
'E',
'G');
212 double fps = invid.get(cv::CAP_PROP_FPS);
214 cv::VideoWriter outVideo(
"denseoptflow_mv_" + strBackend +
".mp4", fourcc, fps, cv::Size(mvWidth, mvHeight));
215 if (!outVideo.isOpened())
217 throw std::runtime_error(
"Can't create output video");
228 cv::Mat mvOutputImage;
232 while (invid.read(cvCurFrame))
234 printf(
"Processing frame %d\n", idxFrame++);
249 ProcessMotionVector(imgMotionVecBL, mvOutputImage);
252 outVideo << mvOutputImage;
255 std::swap(cvPrevFrame, cvCurFrame);
256 std::swap(imgPrevFramePL, imgCurFramePL);
257 std::swap(imgPrevFrameBL, imgCurFrameBL);
260 catch (std::exception &e)
262 std::cerr << e.what() << std::endl;
Functions and structures for dealing with VPI arrays.
Functions and structures for dealing with VPI images.
Functions for handling OpenCV interoperability with VPI.
Declares functions that implement the dense optical flow.
Functions and structures for dealing with VPI pyramids.
Declaration of VPI status codes handling functions.
Declares functions dealing with VPI streams.
void vpiImageDestroy(VPIImage img)
Destroy an image instance.
struct VPIImageImpl * VPIImage
A handle to an image.
VPIStatus vpiImageLockData(VPIImage img, VPILockMode mode, VPIImageBufferType bufType, VPIImageData *data)
Acquires the lock on an image object and returns the image contents.
VPIStatus vpiImageCreate(int32_t width, int32_t height, VPIImageFormat fmt, uint64_t flags, VPIImage *img)
Create an empty image instance with the specified flags.
VPIStatus vpiImageUnlock(VPIImage img)
Releases the lock on an image object.
@ VPI_IMAGE_BUFFER_HOST_PITCH_LINEAR
Host-accessible with planes in pitch-linear memory layout.
Stores information about image characteristics and content.
VPIStatus vpiImageCreateWrapperOpenCVMat(const cv::Mat &mat, VPIImageFormat fmt, uint64_t flags, VPIImage *img)
Wraps a cv::Mat in an VPIImage with the given image format.
VPIStatus vpiImageDataExportOpenCVMat(const VPIImageData &imgData, cv::Mat *mat)
Fills an existing cv::Mat with data from VPIImageData coming from a locked VPIImage.
VPIStatus vpiImageSetWrappedOpenCVMat(VPIImage img, const cv::Mat &mat)
Redefines the wrapped cv::Mat of an existing VPIImage wrapper.
VPIStatus vpiCreateOpticalFlowDense(uint64_t backends, int32_t width, int32_t height, VPIImageFormat inputFmt, VPIOpticalFlowQuality quality, VPIPayload *payload)
Creates payload for vpiSubmitOpticalFlowDense.
VPIStatus vpiSubmitOpticalFlowDense(VPIStream stream, uint64_t backend, VPIPayload payload, VPIImage prevImg, VPIImage curImg, VPIImage mvImg)
Runs dense Optical Flow on two frames.
struct VPIPayloadImpl * VPIPayload
A handle to an algorithm payload.
void vpiPayloadDestroy(VPIPayload payload)
Deallocates the payload object and all associated resources.
struct VPIStreamImpl * VPIStream
A handle to a stream.
VPIStatus vpiStreamSync(VPIStream stream)
Blocks the calling thread until all submitted commands in this stream queue are done (queue is empty)...
VPIBackend
VPI Backend types.
void vpiStreamDestroy(VPIStream stream)
Destroy a stream instance and deallocate all HW resources.
VPIStatus vpiStreamCreate(uint64_t flags, VPIStream *stream)
Create a stream instance.
@ VPI_BACKEND_CUDA
CUDA backend.
@ VPI_BACKEND_NVENC
NVENC backend.
@ VPI_BACKEND_VIC
VIC backend.
VPIOpticalFlowQuality
Defines the quality of the optical flow algorithm.
@ VPI_OPTICAL_FLOW_QUALITY_LOW
Fast but low quality optical flow implementation.
@ VPI_OPTICAL_FLOW_QUALITY_HIGH
Slow but high quality optical flow implementation.
@ VPI_OPTICAL_FLOW_QUALITY_MEDIUM
Speed and quality in between of VPI_OPTICAL_FLOW_QUALITY_LOW and VPI_OPTICAL_FLOW_QUALITY_HIGH.
@ VPI_LOCK_READ
Lock memory only for reading.