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 VPI features:
- Creating and destroying a VPI device.
- 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 device.
- Simple device 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.
Source code
For convenience, here's the code that is also installed in the samples directory.
#include <cstring>
#include <iostream>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#define CHECK_STATUS(STMT) \
do \
{ \
VPIStatus status = (STMT); \
if (status != VPI_SUCCESS) \
{ \
throw std::runtime_error(vpiStatusGetName(status)); \
} \
} while (0);
int main(int argc, char *argv[])
{
int retval = 0;
try
{
if (argc != 4)
{
throw std::runtime_error(std::string("Usage: ") + argv[0] + " <cpu|pva|cuda> <left image> <right image>");
}
std::string strDevType = argv[1];
std::string strLeftFileName = argv[2];
std::string strRightFileName = argv[3];
cv::Mat cvImageLeft = cv::imread(strLeftFileName, cv::IMREAD_GRAYSCALE);
if (cvImageLeft.empty())
{
throw std::runtime_error("Can't open '" + strLeftFileName + "'");
}
cv::Mat cvImageRight = cv::imread(strRightFileName, cv::IMREAD_GRAYSCALE);
if (cvImageRight.empty())
{
throw std::runtime_error("Can't open '" + strRightFileName + "'");
}
cvImageLeft.convertTo(cvImageLeft, CV_16UC1);
cvImageRight.convertTo(cvImageRight, CV_16UC1);
if(strDevType == "cpu")
{
}
else if(strDevType == "cuda")
{
}
else if(strDevType == "pva")
{
}
else
{
throw std::runtime_error("Backend '" + strDevType + "' not recognized, it must be either cpu, cuda or pva.");
}
{
memset(&imgData, 0, sizeof(imgData));
}
{
double min, max;
minMaxLoc(cvOut, &min, &max);
cvOut.convertTo(cvOut, CV_8UC1, 255.0 / (max - min), -min);
imwrite("disparity_"+strDevType+".png", cvOut);
}
}
catch (std::exception &e)
{
std::cerr << e.what() << std::endl;
retval = 1;
}
return retval;
}
Results
Left input image | Right input image |
| |
Stereo disparity |
|