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|ofa|ofa-pva-vic> <left image> <right image>\nNote: "
94 "For pva-nvenc-vic backend the left_image's size must be 1920x1080");
97 std::string strBackend = argv[1];
98 std::string strLeftFileName = argv[2];
99 std::string strRightFileName = argv[3];
103 if (strBackend ==
"cpu")
107 else if (strBackend ==
"cuda")
111 else if (strBackend ==
"pva")
115 else if (strBackend ==
"pva-nvenc-vic")
119 else if (strBackend ==
"ofa")
123 else if (strBackend ==
"ofa-pva-vic")
129 throw std::runtime_error(
130 "Backend '" + strBackend +
131 "' not recognized, it must be either cpu, cuda, pva, ofa, ofa-pva-vic or pva-nvenc-vic.");
136 cvImageLeft = cv::imread(strLeftFileName);
137 if (cvImageLeft.empty())
139 throw std::runtime_error(
"Can't open '" + strLeftFileName +
"'");
142 cvImageRight = cv::imread(strRightFileName);
143 if (cvImageRight.empty())
145 throw std::runtime_error(
"Can't open '" + strRightFileName +
"'");
151 int32_t inputWidth = cvImageLeft.cols;
152 int32_t inputHeight = cvImageLeft.rows;
174 int stereoWidth = inputWidth;
175 int stereoHeight = inputHeight;
176 int outputWidth = inputWidth;
177 int outputHeight = inputHeight;
180 if (strBackend ==
"pva-nvenc-vic")
189 convParams.
scale = 256;
199 else if (strBackend.find(
"ofa") != std::string::npos)
204 if (strBackend ==
"ofa")
210 if (strBackend.find(
"pva") != std::string::npos)
214 outputWidth = std::max(64, minWidth);
215 outputHeight = (inputHeight * stereoWidth) / inputWidth;
223 else if (strBackend ==
"pva")
226 stereoWidth = outputWidth = 480;
227 stereoHeight = outputHeight = 270;
239 CHECK_STATUS(
vpiImageCreate(outputWidth, outputHeight, disparityFormat, 0, &disparity));
242 CHECK_STATUS(
vpiImageCreate(stereoWidth, stereoHeight, stereoFormat, 0, &stereoLeft));
243 CHECK_STATUS(
vpiImageCreate(stereoWidth, stereoHeight, stereoFormat, 0, &stereoRight));
246 if (strBackend ==
"pva-nvenc-vic")
257 else if (strBackend.find(
"ofa") != std::string::npos)
263 if (strBackend.find(
"pva") != std::string::npos)
269 else if (strBackend ==
"pva")
272 CHECK_STATUS(
vpiImageCreate(inputWidth, inputHeight, stereoFormat, 0, &tmpLeft));
273 CHECK_STATUS(
vpiImageCreate(inputWidth, inputHeight, stereoFormat, 0, &tmpRight));
275 else if (strBackend ==
"cuda")
285 if (strBackend ==
"pva-nvenc-vic" || strBackend ==
"pva" || strBackend ==
"ofa" || strBackend ==
"ofa-pva-vic")
309 confidenceMap, NULL));
327 cvDisparity.convertTo(cvDisparity, CV_8UC1, 255.0 / (32 * stereoParams.
maxDisparity), 0);
331 cv::Mat cvDisparityColor;
332 applyColorMap(cvDisparity, cvDisparityColor, cv::COLORMAP_JET);
345 cv::Mat cvConfidence;
350 cvConfidence.convertTo(cvConfidence, CV_8UC1, 255.0 / 65535, 0);
351 imwrite(
"confidence_" + strBackend +
".png", cvConfidence);
358 threshold(cvConfidence, cvMask, 1, 255, cv::THRESH_BINARY);
359 cvtColor(cvMask, cvMask, cv::COLOR_GRAY2BGR);
360 bitwise_and(cvDisparityColor, cvMask, cvDisparityColor);
363 imwrite(
"disparity_" + strBackend +
".png", cvDisparityColor);
365 catch (std::exception &e)
367 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.
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.
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, uint64_t backend, VPIImage input, VPIImage output, VPIInterpolationType interpolationType, VPIBorderExtension border, uint64_t flags)
Changes the size and scale of a 2D image.
int32_t maxDisparity
Maximum disparity for matching search.
int32_t downscaleFactor
Output's downscale factor with respect to the input's resolution.
VPIStatus vpiInitStereoDisparityEstimatorCreationParams(VPIStereoDisparityEstimatorCreationParams *params)
Initializes VPIStereoDisparityEstimatorCreationParams with default values.
VPIStatus vpiCreateStereoDisparityEstimator(uint64_t backends, int32_t imageWidth, int32_t imageHeight, VPIImageFormat inputFormat, const VPIStereoDisparityEstimatorCreationParams *params, VPIPayload *payload)
Creates payload for vpiSubmitStereoDisparityEstimator.
VPIStatus vpiSubmitStereoDisparityEstimator(VPIStream stream, uint64_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.
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(uint64_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_OFA
OFA 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.