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>");
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")
118 else if (strBackend ==
"ofa")
122 else if (strBackend ==
"ofa-pva-vic")
128 throw std::runtime_error(
129 "Backend '" + strBackend +
130 "' not recognized, it must be either cpu, cuda, pva, ofa, ofa-pva-vic or pva-nvenc-vic.");
135 cvImageLeft = cv::imread(strLeftFileName);
136 if (cvImageLeft.empty())
138 throw std::runtime_error(
"Can't open '" + strLeftFileName +
"'");
141 cvImageRight = cv::imread(strRightFileName);
142 if (cvImageRight.empty())
144 throw std::runtime_error(
"Can't open '" + strRightFileName +
"'");
150 int32_t inputWidth = cvImageLeft.cols;
151 int32_t inputHeight = cvImageLeft.rows;
173 int stereoWidth = inputWidth;
174 int stereoHeight = inputHeight;
175 int outputWidth = inputWidth;
176 int outputHeight = inputHeight;
179 if (strBackend ==
"pva-nvenc-vic")
188 convParams.
scale = 256;
198 else if (strBackend.find(
"ofa") != std::string::npos)
203 if (strBackend ==
"ofa")
209 if (strBackend.find(
"pva") != std::string::npos)
213 outputWidth = std::max(64, minWidth);
214 outputHeight = (inputHeight * stereoWidth) / inputWidth;
222 else if (strBackend ==
"pva")
225 stereoWidth = outputWidth = 480;
226 stereoHeight = outputHeight = 270;
238 CHECK_STATUS(
vpiImageCreate(outputWidth, outputHeight, disparityFormat, 0, &disparity));
241 CHECK_STATUS(
vpiImageCreate(stereoWidth, stereoHeight, stereoFormat, 0, &stereoLeft));
242 CHECK_STATUS(
vpiImageCreate(stereoWidth, stereoHeight, stereoFormat, 0, &stereoRight));
245 if (strBackend ==
"pva-nvenc-vic")
256 else if (strBackend.find(
"ofa") != std::string::npos)
262 if (strBackend.find(
"pva") != std::string::npos)
268 else if (strBackend ==
"pva")
271 CHECK_STATUS(
vpiImageCreate(inputWidth, inputHeight, stereoFormat, 0, &tmpLeft));
272 CHECK_STATUS(
vpiImageCreate(inputWidth, inputHeight, stereoFormat, 0, &tmpRight));
274 else if (strBackend ==
"cuda")
284 if (strBackend ==
"pva-nvenc-vic" || strBackend ==
"pva" || strBackend ==
"ofa" || strBackend ==
"ofa-pva-vic")
308 confidenceMap, NULL));
326 cvDisparity.convertTo(cvDisparity, CV_8UC1, 255.0 / (32 * stereoParams.
maxDisparity), 0);
330 cv::Mat cvDisparityColor;
331 applyColorMap(cvDisparity, cvDisparityColor, cv::COLORMAP_JET);
344 cv::Mat cvConfidence;
349 cvConfidence.convertTo(cvConfidence, CV_8UC1, 255.0 / 65535, 0);
350 imwrite(
"confidence_" + strBackend +
".png", cvConfidence);
357 threshold(cvConfidence, cvMask, 1, 255, cv::THRESH_BINARY);
358 cvtColor(cvMask, cvMask, cv::COLOR_GRAY2BGR);
359 bitwise_and(cvDisparityColor, cvMask, cvDisparityColor);
362 imwrite(
"disparity_" + strBackend +
".png", cvDisparityColor);
364 catch (std::exception &e)
366 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.