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>
52 #define CHECK_STATUS(STMT) \
55 VPIStatus status = (STMT); \
56 if (status != VPI_SUCCESS) \
58 char buffer[VPI_MAX_STATUS_MESSAGE_LENGTH]; \
59 vpiGetLastStatusMessage(buffer, sizeof(buffer)); \
60 std::ostringstream ss; \
61 ss << vpiStatusGetName(status) << ": " << buffer; \
62 throw std::runtime_error(ss.str()); \
66 int main(
int argc,
char *argv[])
85 throw std::runtime_error(std::string(
"Usage: ") + argv[0] +
" <cpu|cuda> <input_video>");
89 std::string strBackend = argv[1];
90 std::string strInputVideo = argv[2];
93 if (strBackend ==
"cpu")
97 else if (strBackend ==
"cuda")
103 throw std::runtime_error(
"Backend '" + strBackend +
"' not recognized.");
107 cv::VideoCapture invid;
108 if (!invid.open(strInputVideo))
110 throw std::runtime_error(
"Can't open '" + strInputVideo +
"'");
113 #if CV_MAJOR_VERSION >= 3
114 int32_t width = invid.get(cv::CAP_PROP_FRAME_WIDTH);
115 int32_t height = invid.get(cv::CAP_PROP_FRAME_HEIGHT);
117 int fourcc = cv::VideoWriter::fourcc(
'a',
'v',
'c',
'1');
118 double fps = invid.get(cv::CAP_PROP_FPS);
119 std::string extOutputVideo =
".mp4";
121 int32_t width = invid.get(CV_CAP_PROP_FRAME_WIDTH);
122 int32_t height = invid.get(CV_CAP_PROP_FRAME_HEIGHT);
126 int fourcc = CV_FOURCC(
'M',
'P',
'E',
'G');
127 double fps = invid.get(CV_CAP_PROP_FPS);
128 std::string extOutputVideo =
".avi";
144 cv::VideoWriter outVideo(
"fgmask_" + strBackend + extOutputVideo, fourcc, fps, cv::Size(width, height),
false);
145 if (!outVideo.isOpened())
147 throw std::runtime_error(
"Can't create output video");
150 cv::VideoWriter bgimageVideo(
"bgimage_" + strBackend + extOutputVideo, fourcc, fps, cv::Size(width, height));
151 if (!outVideo.isOpened())
153 throw std::runtime_error(
"Can't create output video");
159 while (invid.read(cvCurFrame))
161 printf(
"Processing frame %d\n", idxFrame++);
163 if (imgCurFrame == NULL)
190 outVideo << outFrame;
202 bgimageVideo << outFrame;
208 catch (std::exception &e)
210 std::cerr << e.what() << std::endl;
Functions and structures for dealing with VPI arrays.
Declares functions that implement background subtractor(GMM) algorithms.
Functions and structures for dealing with VPI images.
Functions for handling OpenCV interoperability with VPI.
Functions and structures for dealing with VPI pyramids.
Declaration of VPI status codes handling functions.
Declares functions dealing with VPI streams.
float learningRate
Learning rate that indicates how fast the background model is learnt.
VPIStatus vpiCreateBackgroundSubtractor(uint32_t backends, int32_t imageWidth, int32_t imageHeight, VPIImageFormat inputFormat, VPIPayload *payload)
Creates payload for vpiSubmitBackgroundSubtractor.
VPIStatus vpiInitBackgroundSubtractorParams(VPIBackgroundSubtractorParams *params)
Initializes VPIBackgroundSubtractorParams with default values.
VPIStatus vpiSubmitBackgroundSubtractor(VPIStream stream, uint32_t backend, VPIPayload payload, VPIImage inFrame, VPIImage outFGMask, VPIImage outBGImage, const VPIBackgroundSubtractorParams *params)
Submits a background subtractor operation to the stream associated with the given payload.
Structure that defines the parameters for vpiCreateBackgroundSubtractor.
VPIStatus vpiImageLock(VPIImage img, VPILockMode mode, VPIImageData *hostData)
Acquires the lock on an image object and returns a pointer to the image planes.
void vpiImageDestroy(VPIImage img)
Destroy an image instance.
struct VPIImageImpl * VPIImage
A handle to an image.
VPIStatus vpiImageCreate(int32_t width, int32_t height, VPIImageFormat fmt, uint32_t flags, VPIImage *img)
Create an empty image instance with the specified flags.
VPIStatus vpiImageUnlock(VPIImage img)
Releases the lock on an image object.
Stores information about image characteristics and content.
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 vpiImageCreateOpenCVMatWrapper(const cv::Mat &mat, VPIImageFormat fmt, uint32_t flags, VPIImage *img)
Wraps a cv::Mat in an VPIImage with the given image format.
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(uint32_t flags, VPIStream *stream)
Create a stream instance.
@ VPI_BACKEND_CUDA
CUDA backend.
@ VPI_BACKEND_CPU
CPU backend.
@ VPI_LOCK_READ
Lock memory only for reading.