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 << "line " << __LINE__ << " " << 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;
162 int stereoWidth = inputWidth;
163 int stereoHeight = inputHeight;
168 int outputWidth = inputWidth;
169 int outputHeight = inputHeight;
172 if (strBackend.find(
"ofa") != std::string::npos)
177 if (strBackend ==
"ofa")
190 if (strBackend.find(
"pva") != std::string::npos)
193 outputWidth = std::max(64, minWidth);
194 outputHeight = (inputHeight * outputWidth) / inputWidth;
206 CHECK_STATUS(
vpiImageCreate(outputWidth, outputHeight, disparityFormat, 0, &disparity));
209 CHECK_STATUS(
vpiImageCreate(stereoWidth, stereoHeight, stereoFormat, 0, &stereoLeft));
210 CHECK_STATUS(
vpiImageCreate(stereoWidth, stereoHeight, stereoFormat, 0, &stereoRight));
213 if (strBackend ==
"ofa-pva-vic" || strBackend ==
"cuda")
219 bool const isRescaleRequired = (stereoWidth != inputWidth) || (stereoHeight != inputHeight);
220 if (isRescaleRequired)
222 CHECK_STATUS(
vpiImageCreate(inputWidth, inputHeight, stereoFormat, 0, &tmpLeft));
223 CHECK_STATUS(
vpiImageCreate(inputWidth, inputHeight, stereoFormat, 0, &tmpRight));
232 if (strBackend ==
"ofa-pva-vic")
239 else if (strBackend ==
"cuda")
247 if (isRescaleRequired)
275 confidenceMap, &submitParams));
293 cvDisparity.convertTo(cvDisparity, CV_8UC1, 255.0 / (32 * createParams.
maxDisparity), 0);
297 cv::Mat cvDisparityColor;
298 applyColorMap(cvDisparity, cvDisparityColor, cv::COLORMAP_JET);
309 cv::Mat cvConfidence;
313 cvConfidence.convertTo(cvConfidence, CV_8UC1, 255.0 / 65535, 0);
314 imwrite(
"confidence_" + strBackend +
".png", cvConfidence);
320 threshold(cvConfidence, cvMask, 1, 255, cv::THRESH_BINARY);
321 cvtColor(cvMask, cvMask, cv::COLOR_GRAY2BGR);
322 bitwise_and(cvDisparityColor, cvMask, cvDisparityColor);
325 imwrite(
"disparity_" + strBackend +
".png", cvDisparityColor);
327 catch (std::exception &e)
329 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.
VPIStereoDisparityConfidenceType confidenceType
Computation type to produce the confidence output.
int32_t confidenceThreshold
Confidence threshold above which disparity values are considered valid.
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 vpiInitStereoDisparityEstimatorParams(VPIStereoDisparityEstimatorParams *params)
Initializes VPIStereoDisparityEstimatorParams with default values.
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.
@ VPI_STEREO_CONFIDENCE_INFERENCE
The confidence value of a pixel is on a 0:UINT16_MAX scale, mapping from 0% to 100%.
Structure that defines the parameters for vpiCreateStereoDisparityEstimator.
Structure that defines the parameters for vpiSubmitStereoDisparityEstimator.
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.