29 #include <opencv2/core/version.hpp>
30 #include <opencv2/imgcodecs.hpp>
31 #include <opencv2/imgproc/imgproc.hpp>
32 #include <opencv2/videoio.hpp>
49 #define CHECK_STATUS(STMT) \
52 VPIStatus status = (STMT); \
53 if (status != VPI_SUCCESS) \
55 char buffer[VPI_MAX_STATUS_MESSAGE_LENGTH]; \
56 vpiGetLastStatusMessage(buffer, sizeof(buffer)); \
57 std::ostringstream ss; \
58 ss << "line " << __LINE__ << ": "; \
59 ss << vpiStatusGetName(status) << ": " << buffer; \
60 throw std::runtime_error(ss.str()); \
64 static void ProcessMotionVector(
VPIImage mvImg, cv::Mat &outputImage)
75 cv::Mat flow(mvImage.size(), CV_32FC2);
76 mvImage.convertTo(flow, CV_32F, 1.0f / (1 << 5));
84 cv::Mat magnitude, angle;
86 cv::Mat flowChannels[2];
87 split(flow, flowChannels);
88 cv::cartToPolar(flowChannels[0], flowChannels[1], magnitude, angle,
true);
92 cv::threshold(magnitude, magnitude, clip, clip, cv::THRESH_TRUNC);
95 cv::Mat _hsv[3], hsv, bgr;
97 _hsv[1] = cv::Mat::ones(angle.size(), CV_32F);
98 _hsv[2] = magnitude / clip;
101 cv::cvtColor(hsv, bgr, cv::COLOR_HSV2BGR);
102 bgr.convertTo(outputImage, CV_8U, 255.0);
105 int main(
int argc,
char *argv[])
109 cv::Mat cvPrevFrame, cvCurFrame;
135 throw std::runtime_error(std::string(
"Usage: ") + argv[0] +
136 " <ofa> <input_video> <low|medium|high> <gridsize> <numlevels>");
140 std::string strBackend = argv[1];
141 std::string strInputVideo = argv[2];
142 std::string strQuality = argv[3];
143 std::string strGridSize = argv[4];
144 std::string strNumLevels = argv[5];
147 if (strQuality ==
"low")
151 else if (strQuality ==
"medium")
155 else if (strQuality ==
"high")
161 throw std::runtime_error(
"Unknown quality provided");
165 if (strBackend ==
"ofa")
171 throw std::runtime_error(
"Backend '" + strBackend +
"' not recognized, it must be ofa.");
175 int gridSize = strtol(strGridSize.c_str(), &endptr, 10);
178 throw std::runtime_error(
"Syntax error parsing gridsize " + strGridSize);
181 int numLevels = strtol(strNumLevels.c_str(), &endptr, 10);
184 throw std::runtime_error(
"Syntax error parsing numlevels " + strNumLevels);
188 cv::VideoCapture invid;
189 if (!invid.open(strInputVideo))
191 throw std::runtime_error(
"Can't open '" + strInputVideo +
"'");
199 if (!invid.read(cvPrevFrame))
201 throw std::runtime_error(
"Cannot read frame from input video");
214 int32_t multiple = (1 << (numLevels - 1));
215 int width = ((cvPrevFrame.cols + multiple - 1) / multiple) * multiple;
216 int height = ((cvPrevFrame.rows + multiple - 1) / multiple) * multiple;
227 std::vector<int32_t> pyrGridSize(numLevels, gridSize);
236 CHECK_STATUS(
vpiImageCreate(width, height, imgFmt, 0, &imgPrevFrameTmp));
237 CHECK_STATUS(
vpiImageCreate(width, height, imgFmt, 0, &imgCurFrameTmp));
242 CHECK_STATUS(
vpiPyramidCreate(width, height, imgFmt, pyrGridSize.size(), 0.5, 0, &prevPyrTmp));
243 CHECK_STATUS(
vpiPyramidCreate(width, height, imgFmt, pyrGridSize.size(), 0.5, 0, &curPyrTmp));
245 CHECK_STATUS(
vpiPyramidCreate(width, height, imgFmtBL, pyrGridSize.size(), 0.5, 0, &prevPyrBL));
246 CHECK_STATUS(
vpiPyramidCreate(width, height, imgFmtBL, pyrGridSize.size(), 0.5, 0, &curPyrBL));
249 int32_t mvWidth = (width + gridSize - 1) / gridSize;
250 int32_t mvHeight = (height + gridSize - 1) / gridSize;
253 int fourcc = cv::VideoWriter::fourcc(
'M',
'P',
'E',
'G');
254 double fps = invid.get(cv::CAP_PROP_FPS);
256 cv::VideoWriter outVideo(
"denseoptflow_mv_" + strBackend +
".mp4", fourcc, fps, cv::Size(mvWidth, mvHeight));
257 if (!outVideo.isOpened())
259 throw std::runtime_error(
"Can't create output video");
273 cv::Mat mvOutputImage;
277 while (invid.read(cvCurFrame))
279 printf(
"Processing frame %d\n", idxFrame++);
304 ProcessMotionVector(imgMotionVecPL, mvOutputImage);
307 outVideo << mvOutputImage;
310 std::swap(cvPrevFrame, cvCurFrame);
311 std::swap(imgPrevFramePL, imgCurFramePL);
312 std::swap(prevPyrBL, curPyrBL);
315 catch (std::exception &e)
317 std::cerr << e.what() << std::endl;
Functions and structures for dealing with VPI arrays.
Declares functions that handle gaussian pyramids.
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.
Declares functions that implement the Rescale algorithm.
Declaration of VPI status codes handling functions.
Declares functions dealing with VPI streams.
VPIStatus vpiSubmitGaussianPyramidGenerator(VPIStream stream, uint64_t backend, VPIImage input, VPIPyramid output, VPIBorderExtension border)
Computes the Gaussian pyramid from the input image.
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, const int32_t *gridSize, int32_t numLevels, VPIOpticalFlowQuality quality, VPIPayload *payload)
Creates payload for vpiSubmitOpticalFlowDense.
VPIStatus vpiSubmitOpticalFlowDensePyramid(VPIStream stream, uint64_t backend, VPIPayload payload, VPIPyramid prevPyr, VPIPyramid curPyr, VPIImage mvImg)
Runs dense Optical Flow on two frames, outputting motion vectors.
struct VPIPayloadImpl * VPIPayload
A handle to an algorithm payload.
void vpiPayloadDestroy(VPIPayload payload)
Deallocates the payload object and all associated resources.
VPIStatus vpiPyramidCreate(int32_t width, int32_t height, VPIImageFormat fmt, int32_t numLevels, float scale, uint64_t flags, VPIPyramid *pyr)
Create an empty image pyramid instance with the specified flags.
struct VPIPyramidImpl * VPIPyramid
A handle to an image pyramid.
void vpiPyramidDestroy(VPIPyramid pyr)
Destroy an image pyramid instance as well as all resources it owns.
VPIStatus vpiSubmitRescale(VPIStream stream, uint64_t backend, VPIImage input, VPIImage output, VPIInterpolationType interpolationType, VPIBorderExtension border, uint64_t flags)
Changes the size and scale of a 2D image.
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_OFA
OFA backend.
@ VPI_BACKEND_VIC
VIC backend.
VPIOpticalFlowQuality
Defines the quality of the optical flow algorithm.
@ VPI_BORDER_ZERO
All pixels outside the image are considered to be zero.
@ VPI_BORDER_CLAMP
Border pixels are repeated indefinitely.
@ VPI_INTERP_LINEAR
Linear interpolation.
@ 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.