29 #include <opencv2/core/version.hpp>
30 #if CV_MAJOR_VERSION >= 3
31 # include <opencv2/imgcodecs.hpp>
33 # include <opencv2/contrib/contrib.hpp>
34 # include <opencv2/highgui/highgui.hpp>
37 #include <opencv2/imgproc/imgproc.hpp>
51 #define CHECK_STATUS(STMT) \
54 VPIStatus status = (STMT); \
55 if (status != VPI_SUCCESS) \
57 char buffer[VPI_MAX_STATUS_MESSAGE_LENGTH]; \
58 vpiGetLastStatusMessage(buffer, sizeof(buffer)); \
59 std::ostringstream ss; \
60 ss << vpiStatusGetName(status) << ": " << buffer; \
61 throw std::runtime_error(ss.str()); \
65 int main(
int argc,
char *argv[])
69 cv::Mat cvImageLeft, cvImageRight;
92 throw std::runtime_error(std::string(
"Usage: ") + argv[0] +
93 " <cpu|pva|cuda|pva-nvenc-vic> <left image> <right image>");
96 std::string strBackend = argv[1];
97 std::string strLeftFileName = argv[2];
98 std::string strRightFileName = argv[3];
102 if (strBackend ==
"cpu")
106 else if (strBackend ==
"cuda")
110 else if (strBackend ==
"pva")
114 else if (strBackend ==
"pva-nvenc-vic")
120 throw std::runtime_error(
"Backend '" + strBackend +
121 "' not recognized, it must be either cpu, cuda, pva or pva-nvenc-vic.");
126 cvImageLeft = cv::imread(strLeftFileName);
127 if (cvImageLeft.empty())
129 throw std::runtime_error(
"Can't open '" + strLeftFileName +
"'");
132 cvImageRight = cv::imread(strRightFileName);
133 if (cvImageRight.empty())
135 throw std::runtime_error(
"Can't open '" + strRightFileName +
"'");
141 int32_t inputWidth = cvImageLeft.cols;
142 int32_t inputHeight = cvImageLeft.rows;
163 int stereoWidth, stereoHeight;
164 if (strBackend ==
"pva-nvenc-vic")
174 convParams.
scale = 256;
183 if (strBackend ==
"pva")
190 stereoWidth = inputWidth;
191 stereoHeight = inputHeight;
205 if (strBackend ==
"pva-nvenc-vic")
214 CHECK_STATUS(
vpiImageCreate(stereoWidth, stereoHeight, stereoFormat, 0, &stereoLeft));
215 CHECK_STATUS(
vpiImageCreate(stereoWidth, stereoHeight, stereoFormat, 0, &stereoRight));
223 if (strBackend ==
"pva")
225 CHECK_STATUS(
vpiImageCreate(inputWidth, inputHeight, stereoFormat, 0, &tmpLeft));
226 CHECK_STATUS(
vpiImageCreate(inputWidth, inputHeight, stereoFormat, 0, &tmpRight));
228 else if (strBackend ==
"cuda")
234 CHECK_STATUS(
vpiImageCreate(stereoWidth, stereoHeight, stereoFormat, 0, &stereoLeft));
235 CHECK_STATUS(
vpiImageCreate(stereoWidth, stereoHeight, stereoFormat, 0, &stereoRight));
243 if (strBackend ==
"pva-nvenc-vic" || strBackend ==
"pva")
267 confidenceMap, NULL));
285 cvDisparity.convertTo(cvDisparity, CV_8UC1, 255.0 / (32 * stereoParams.
maxDisparity), 0);
289 cv::Mat cvDisparityColor;
290 applyColorMap(cvDisparity, cvDisparityColor, cv::COLORMAP_JET);
303 cv::Mat cvConfidence;
308 cvConfidence.convertTo(cvConfidence, CV_8UC1, 255.0 / 65535, 0);
309 imwrite(
"confidence_" + strBackend +
".png", cvConfidence);
316 threshold(cvConfidence, cvMask, 1, 255, cv::THRESH_BINARY);
317 cvtColor(cvMask, cvMask, cv::COLOR_GRAY2BGR);
318 bitwise_and(cvDisparityColor, cvMask, cvDisparityColor);
321 imwrite(
"disparity_" + strBackend +
".png", cvDisparityColor);
323 catch (std::exception &e)
325 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 Rescale algorithm.
Declaration of VPI status codes handling functions.
Declares functions that implement stereo disparity estimation algorithms.
Declares functions dealing with VPI streams.
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 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.
VPIStatus vpiSubmitRescale(VPIStream stream, uint32_t backend, VPIImage input, VPIImage output, VPIInterpolationType interpolationType, VPIBorderExtension border, uint32_t flags)
Changes the size and scale of a 2D image.
int32_t maxDisparity
Maximum disparity for matching search.
VPIStatus vpiInitStereoDisparityEstimatorCreationParams(VPIStereoDisparityEstimatorCreationParams *params)
Initializes VPIStereoDisparityEstimatorCreationParams with default values.
VPIStatus vpiSubmitStereoDisparityEstimator(VPIStream stream, uint32_t backend, VPIPayload payload, VPIImage left, VPIImage right, VPIImage disparity, VPIImage confidenceMap, const VPIStereoDisparityEstimatorParams *params)
Runs stereo processing on a pair of images and outputs a disparity map.
VPIStatus vpiCreateStereoDisparityEstimator(uint32_t backends, int32_t imageWidth, int32_t imageHeight, VPIImageFormat inputFormat, const VPIStereoDisparityEstimatorCreationParams *params, VPIPayload *payload)
Creates payload for vpiSubmitStereoDisparityEstimator.
Structure that defines the parameters for vpiCreateStereoDisparityEstimator.
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)...
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_PVA
PVA backend.
@ VPI_BACKEND_NVENC
NVENC backend.
@ VPI_BACKEND_VIC
VIC backend.
@ VPI_BACKEND_CPU
CPU backend.
@ VPI_BORDER_CLAMP
Border pixels are repeated indefinitely.
@ VPI_INTERP_LINEAR
Linear interpolation.
@ VPI_LOCK_READ
Lock memory only for reading.