29 #include <opencv2/core/version.hpp>
30 #include <opencv2/imgcodecs.hpp>
31 #include <opencv2/imgproc/imgproc.hpp>
32 #include <opencv2/videoio.hpp>
48 #define CHECK_STATUS(STMT) \
51 VPIStatus status = (STMT); \
52 if (status != VPI_SUCCESS) \
54 char buffer[VPI_MAX_STATUS_MESSAGE_LENGTH]; \
55 vpiGetLastStatusMessage(buffer, sizeof(buffer)); \
56 std::ostringstream ss; \
57 ss << vpiStatusGetName(status) << ": " << buffer; \
58 throw std::runtime_error(ss.str()); \
65 for (
int i = 0; i < 3; ++i)
67 for (
int j = 0; j < 3; ++j)
69 r[i][j] = a[i][0] * b[0][j];
70 for (
int k = 1; k < 3; ++k)
72 r[i][j] += a[i][k] * b[k][j];
78 int main(
int argc,
char *argv[])
86 VPIImage imgInput = NULL, imgOutput = NULL;
98 throw std::runtime_error(std::string(
"Usage: ") + argv[0] +
" <cpu|vic|cuda> <input_video>");
101 std::string strBackend = argv[1];
102 std::string strInputVideo = argv[2];
107 if (strBackend ==
"cpu")
111 else if (strBackend ==
"cuda")
115 else if (strBackend ==
"vic")
121 throw std::runtime_error(
"Backend '" + strBackend +
122 "' not recognized, it must be either cpu, cuda or vic.");
129 cv::VideoCapture invid;
130 if (!invid.open(strInputVideo))
132 throw std::runtime_error(
"Can't open '" + strInputVideo +
"'");
136 int w = invid.get(cv::CAP_PROP_FRAME_WIDTH);
137 int h = invid.get(cv::CAP_PROP_FRAME_HEIGHT);
138 int fourcc = cv::VideoWriter::fourcc(
'M',
'P',
'E',
'G');
139 double fps = invid.get(cv::CAP_PROP_FPS);
141 cv::VideoWriter outVideo(
"perspwarp_" + strBackend +
".mp4", fourcc, fps, cv::Size(w, h));
142 if (!outVideo.isOpened())
144 throw std::runtime_error(
"Can't create output video");
157 memset(&xform, 0,
sizeof(xform));
163 while (invid.read(cvFrame))
165 printf(
"Frame: %d\n", curFrame++);
167 if (frameBGR == NULL)
185 float v1 = sin(curFrame / 30.0 * 2 * M_PI / 2) * 0.0005f;
186 float v2 = cos(curFrame / 30.0 * 2 * M_PI / 3) * 0.0005f;
194 MatrixMultiply(tmp, P, t1);
195 MatrixMultiply(xform, t2, tmp);
212 outVideo << outFrame;
217 catch (std::exception &e)
219 std::cerr << e.what() << std::endl;
Functions and structures for dealing with VPI images.
Functions for handling OpenCV interoperability with VPI.
Declares functions that implement the Perspective Warp algorithm.
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 vpiSubmitPerspectiveWarp(VPIStream stream, uint64_t backend, VPIImage input, const VPIPerspectiveTransform xform, VPIImage output, const VPIWarpGrid *grid, VPIInterpolationType interp, VPIBorderExtension border, uint64_t flags)
Submits a Perspective Warp operation to the stream.
float VPIPerspectiveTransform[3][3]
Represents a 2D perspective transform.
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_VIC
VIC backend.
@ VPI_BACKEND_CPU
CPU backend.
@ VPI_BORDER_ZERO
All pixels outside the image are considered to be zero.
@ VPI_INTERP_LINEAR
Linear interpolation.
@ VPI_LOCK_READ
Lock memory only for reading.