|
VPI - Vision Programming Interface
0.4.4 Release
|
Overview
The Stereo Disparity application receives left and right stereo pair images and returns the disparity between them, which is a function of image depth. The result is saved into disparity.png.
This sample shows the following:
- Creating and destroying a VPI stream.
- Wrapping an image hosted on CPU (the input) to be used by VPI.
- Creating a VPI-managed 2D image where output will be written to
- Create a properly-configured StereoProcess algorithm and submit it to the stream.
- Simple stream synchronization.
- Image locking to access its contents from CPU side.
- Error handling.
- Environment clean up.
Instructions
The usage is:
./vpi_sample_02_stereo_disparity <backend> <input image>
where
- backend: either cpu, cuda or pva; it defines the backend that will perform the processing.
- left image: left input image of a stereo pair, it accepts png, jpeg and possibly others.
- right image: right input image of a stereo pair.
Here's one example:
./vpi_sample_02_stereo_disparity cuda ../assets/chair_stereo_left.png ../assets/chair_stereo_right.png
This is using the CUDA backend and the provided sample images. You can try with other stereo pair images, respecting the constraints imposed by the algorithm.
Results
Left input image | Right input image |
| |
Stereo disparity |
|
Source code
For convenience, here's the code that is also installed in the samples directory.
29 #include <opencv2/core/version.hpp>
30 #if CV_MAJOR_VERSION >= 3
31 # include <opencv2/imgcodecs.hpp>
33 # include <opencv2/highgui/highgui.hpp>
36 #include <opencv2/imgproc/imgproc.hpp>
47 #define CHECK_STATUS(STMT) \
50 VPIStatus status = (STMT); \
51 if (status != VPI_SUCCESS) \
53 char buffer[VPI_MAX_STATUS_MESSAGE_LENGTH]; \
54 vpiGetLastStatusMessage(buffer, sizeof(buffer)); \
55 std::ostringstream ss; \
56 ss << vpiStatusGetName(status) << ": " << buffer; \
57 throw std::runtime_error(ss.str()); \
61 int main(
int argc,
char *argv[])
75 throw std::runtime_error(std::string(
"Usage: ") + argv[0] +
" <cpu|pva|cuda> <left image> <right image>");
78 std::string strBackend = argv[1];
79 std::string strLeftFileName = argv[2];
80 std::string strRightFileName = argv[3];
83 cv::Mat cvImageLeft = cv::imread(strLeftFileName, cv::IMREAD_GRAYSCALE);
84 if (cvImageLeft.empty())
86 throw std::runtime_error(
"Can't open '" + strLeftFileName +
"'");
89 cv::Mat cvImageRight = cv::imread(strRightFileName, cv::IMREAD_GRAYSCALE);
90 if (cvImageRight.empty())
92 throw std::runtime_error(
"Can't open '" + strRightFileName +
"'");
96 cvImageLeft.convertTo(cvImageLeft, CV_16UC1);
97 cvImageRight.convertTo(cvImageRight, CV_16UC1);
102 if (strBackend ==
"cpu")
106 else if (strBackend ==
"cuda")
110 else if (strBackend ==
"pva")
116 throw std::runtime_error(
"Backend '" + strBackend +
117 "' not recognized, it must be either cpu, cuda or pva.");
127 memset(&imgData, 0,
sizeof(imgData));
176 minMaxLoc(cvOut, &min, &max);
177 cvOut.convertTo(cvOut, CV_8UC1, 255.0 / (max - min), -min);
179 imwrite(
"disparity_" + strBackend +
".png", cvOut);
185 catch (std::exception &e)
187 std::cerr << e.what() << std::endl;
uint32_t height
Height of this plane in pixels.
uint32_t width
Width of this plane in pixels.
VPIStatus vpiStreamCreate(uint32_t flags, VPIStream *stream)
Create a stream instance.
VPIBackend
VPI Backend types.
VPIStatus vpiSubmitStereoDisparityEstimator(VPIStream stream, VPIPayload payload, VPIImage left, VPIImage right, VPIImage disparity, const VPIStereoDisparityEstimatorParams *params)
Runs stereo processing on a pair of images and outputs a disparity map.
@ VPI_LOCK_READ
Lock memory only for reading.
VPIStatus vpiImageUnlock(VPIImage img)
Releases the lock on an image object.
void vpiPayloadDestroy(VPIPayload payload)
Deallocates the payload object and all associated resources.
VPIStatus vpiStreamSync(VPIStream stream)
Blocks the calling thread until all submitted commands in this stream queue are done (queue is empty)...
@ VPI_BACKEND_CUDA
CUDA backend.
Stores information about image characteristics and content.
struct VPIStreamImpl * VPIStream
A handle to a stream.
VPIStatus vpiCreateStereoDisparityEstimator(VPIBackend backend, uint32_t imageWidth, uint32_t imageHeight, const VPIImageFormat inputFormat, const uint32_t maxDisparity, VPIPayload *payload)
Creates payload for vpiSubmitStereoDisparityEstimator.
void vpiStreamDestroy(VPIStream stream)
Destroy a stream instance and deallocate all HW resources.
VPIImagePlane planes[VPI_MAX_PLANE_COUNT]
Data of all image planes.
VPIStatus vpiImageCreate(uint32_t width, uint32_t height, VPIImageFormat fmt, uint32_t flags, VPIImage *img)
Create an empty image instance with the specified flags.
void vpiImageDestroy(VPIImage img)
Destroy an image instance.
Functions and structures for dealing with VPI images.
Declares functions that implement stereo disparity estimation algorithms.
uint32_t windowSize
Width of Census Transform window for disparity features.
uint32_t pitchBytes
Difference in bytes of beginning of one row and the beginning of the previous.
struct VPIImageImpl * VPIImage
A handle to an image.
int32_t numPlanes
Number of planes.
struct VPIPayloadImpl * VPIPayload
A handle to an algorithm payload.
VPIStatus vpiImageLock(VPIImage img, VPILockMode mode, VPIImageData *hostData)
Acquires the lock on an image object and returns a pointer to the image planes.
VPIImageFormat type
Image type.
Declaration of VPI status codes handling functions.
@ VPI_BACKEND_CPU
CPU backend.
uint32_t maxDisparity
Maximum disparity for matching search.
Structure that defines the parameters for vpiCreateStereoDisparityEstimator.
VPIStatus vpiImageCreateHostMemWrapper(const VPIImageData *hostData, uint32_t flags, VPIImage *img)
Create an image object by wrapping around an existing host memory block.
Declares functions dealing with VPI streams.
void * data
Pointer to the first row of this plane.
@ VPI_BACKEND_PVA
PVA backend.