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 " <cuda|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 ==
"cuda")
106 else if (strBackend ==
"ofa")
110 else if (strBackend ==
"ofa-pva-vic")
116 throw std::runtime_error(
"Backend '" + strBackend +
117 "' not recognized, it must be either cuda, ofa or ofa-pva-vic.");
122 cvImageLeft = cv::imread(strLeftFileName);
123 if (cvImageLeft.empty())
125 throw std::runtime_error(
"Can't open '" + strLeftFileName +
"'");
128 cvImageRight = cv::imread(strRightFileName);
129 if (cvImageRight.empty())
131 throw std::runtime_error(
"Can't open '" + strRightFileName +
"'");
137 int32_t inputWidth = cvImageLeft.cols;
138 int32_t inputHeight = cvImageLeft.rows;
160 int stereoWidth = inputWidth;
161 int stereoHeight = inputHeight;
162 int outputWidth = inputWidth;
163 int outputHeight = inputHeight;
166 if (strBackend.find(
"ofa") != std::string::npos)
171 if (strBackend ==
"ofa")
177 if (strBackend.find(
"pva") != std::string::npos)
181 outputWidth = std::max(64, minWidth);
182 outputHeight = (inputHeight * stereoWidth) / inputWidth;
197 CHECK_STATUS(
vpiImageCreate(outputWidth, outputHeight, disparityFormat, 0, &disparity));
200 CHECK_STATUS(
vpiImageCreate(stereoWidth, stereoHeight, stereoFormat, 0, &stereoLeft));
201 CHECK_STATUS(
vpiImageCreate(stereoWidth, stereoHeight, stereoFormat, 0, &stereoRight));
204 if (strBackend.find(
"ofa") != std::string::npos)
210 if (strBackend.find(
"pva") != std::string::npos)
216 else if (strBackend ==
"cuda")
226 if (strBackend ==
"ofa" || strBackend ==
"ofa-pva-vic")
250 confidenceMap, NULL));
268 cvDisparity.convertTo(cvDisparity, CV_8UC1, 255.0 / (32 * stereoParams.
maxDisparity), 0);
272 cv::Mat cvDisparityColor;
273 applyColorMap(cvDisparity, cvDisparityColor, cv::COLORMAP_JET);
286 cv::Mat cvConfidence;
291 cvConfidence.convertTo(cvConfidence, CV_8UC1, 255.0 / 65535, 0);
292 imwrite(
"confidence_" + strBackend +
".png", cvConfidence);
299 threshold(cvConfidence, cvMask, 1, 255, cv::THRESH_BINARY);
300 cvtColor(cvMask, cvMask, cv::COLOR_GRAY2BGR);
301 bitwise_and(cvDisparityColor, cvMask, cvDisparityColor);
304 imwrite(
"disparity_" + strBackend +
".png", cvDisparityColor);
306 catch (std::exception &e)
308 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_OFA
OFA backend.
@ VPI_BACKEND_VIC
VIC backend.
@ VPI_BORDER_CLAMP
Border pixels are repeated indefinitely.
@ VPI_INTERP_LINEAR
Linear interpolation.
@ VPI_LOCK_READ
Lock memory only for reading.