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 int main(
int argc,
char *argv[])
80 throw std::runtime_error(std::string(
"Usage: ") + argv[0] +
" <cpu|cuda> <input_video>");
84 std::string strBackend = argv[1];
85 std::string strInputVideo = argv[2];
88 if (strBackend ==
"cpu")
92 else if (strBackend ==
"cuda")
98 throw std::runtime_error(
"Backend '" + strBackend +
"' not recognized.");
102 cv::VideoCapture invid;
103 if (!invid.open(strInputVideo))
105 throw std::runtime_error(
"Can't open '" + strInputVideo +
"'");
108 int32_t width = invid.get(cv::CAP_PROP_FRAME_WIDTH);
109 int32_t height = invid.get(cv::CAP_PROP_FRAME_HEIGHT);
124 int fourcc = cv::VideoWriter::fourcc(
'M',
'P',
'E',
'G');
125 double fps = invid.get(cv::CAP_PROP_FPS);
127 cv::VideoWriter outVideo(
"fgmask_" + strBackend +
".mp4", fourcc, fps, cv::Size(width, height),
false);
128 if (!outVideo.isOpened())
130 throw std::runtime_error(
"Can't create output video");
133 cv::VideoWriter bgimageVideo(
"bgimage_" + strBackend +
".mp4", fourcc, fps, cv::Size(width, height));
134 if (!outVideo.isOpened())
136 throw std::runtime_error(
"Can't create output video");
142 while (invid.read(cvCurFrame))
144 printf(
"Processing frame %d\n", idxFrame++);
146 if (imgCurFrame == NULL)
173 outVideo << outFrame;
185 bgimageVideo << outFrame;
191 catch (std::exception &e)
193 std::cerr << e.what() << std::endl;
Functions and structures for dealing with VPI arrays.
Declares functions that implement background subtractor 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 vpiSubmitBackgroundSubtractor(VPIStream stream, uint64_t backend, VPIPayload payload, VPIImage inFrame, VPIImage outFGMask, VPIImage outBGImage, const VPIBackgroundSubtractorParams *params)
Submits a Background Subtractor operation to the stream.
VPIStatus vpiInitBackgroundSubtractorParams(VPIBackgroundSubtractorParams *params)
Initializes VPIBackgroundSubtractorParams with default values.
VPIStatus vpiCreateBackgroundSubtractor(uint64_t backends, int32_t imageWidth, int32_t imageHeight, VPIImageFormat inputFormat, VPIPayload *payload)
Creates payload for vpiSubmitBackgroundSubtractor.
Structure that defines the parameters for vpiCreateBackgroundSubtractor.
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.
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_CPU
CPU backend.
@ VPI_LOCK_READ
Lock memory only for reading.