OpenCV  4.2.0
Open Source Computer Vision
Quasi dense Stereo

Goal

In this tutorial you will learn how to

  • Configure a QuasiDenseStero object
  • Compute dense Stereo correspondences.
#include <opencv2/core.hpp>
#include <fstream>
using namespace cv;
using namespace std;
int main()
{
cv::Mat rightImg, leftImg;
leftImg = imread("./imgLeft.png", IMREAD_COLOR);
rightImg = imread("./imgRight.png", IMREAD_COLOR);
cv::Size frameSize = leftImg.size();
stereo->process(leftImg, rightImg);
uint8_t displvl = 80;
cv::Mat disp;
disp = stereo->getDisparity(displvl);
cv::namedWindow("disparity map");
cv::imshow("disparity map", disp);
cv::namedWindow("right channel");
cv::namedWindow("left channel");
cv::imshow("left channel", leftImg);
cv::imshow("right channel", rightImg);
vector<stereo::Match> matches;
stereo->getDenseMatches(matches);
std::ofstream dense("./dense.txt", std::ios::out);
for (uint i=0; i< matches.size(); i++)
{
dense << matches[i].p0 << matches[i].p1 << endl;
}
dense.close();
return 0;
}

Explanation:

The program loads a stereo image pair.

After importing the images.

cv::Mat rightImg, leftImg;
leftImg = imread("./imgLeft.png", IMREAD_COLOR);
rightImg = imread("./imgRight.png", IMREAD_COLOR);

We need to know the frame size of a single image, in order to create an instance of a QuasiDesnseStereo object.

cv::Size frameSize = leftImg.size();
Ptr<stereo::QuasiDenseStereo> stereo = stereo::QuasiDenseStereo::create(frameSize);

Because we didn't specify the second argument in the constructor, the QuasiDesnseStereo object will load default parameters.

We can then pass the imported stereo images in the process method like this

stereo->process(leftImg, rightImg);

The process method contains most of the functionality of the class and does two main things.

  • Computes a sparse stereo based in "Good Features to Track" and "pyramidal Lucas-Kanade" flow algorithm
  • Based on those sparse stereo points, densifies the stereo correspondences using Quasi Dense Stereo method.

After the execution of process() we can display the disparity Image of the stereo.

uint8_t displvl = 80;
cv::Mat disp;
disp = stereo->getDisparity(displvl);
cv::namedWindow("disparity map");
cv::imshow("disparity map", disp);

At this point we can also extract all the corresponding points using getDenseMatches() method and export them in a file.

vector<stereo::Match> matches;
stereo->getDenseMatches(matches);
std::ofstream dense("./dense.txt", std::ios::out);
for (uint i=0; i< matches.size(); i++)
{
dense << matches[i].p0 << matches[i].p1 << endl;
}
dense.close();
cv::stereo::QuasiDenseStereo::create
static cv::Ptr< QuasiDenseStereo > create(cv::Size monoImgSize, cv::String paramFilepath=cv::String())
cv::IMREAD_COLOR
@ IMREAD_COLOR
If set, always convert image to the 3 channel BGR color image.
Definition: imgcodecs.hpp:67
cv::stereo::QuasiDenseStereo::getDisparity
virtual cv::Mat getDisparity(uint8_t disparityLvls=50)=0
Compute and return the disparity map based on the correspondences found in the "process" method.
cv::waitKey
int waitKey(int delay=0)
Waits for a pressed key.
stereo.hpp
cv::uint8_t
::uint8_t uint8_t
Definition: cvdef.h:767
cv::Size_
Template class for specifying the size of an image or rectangle.
Definition: types.hpp:316
highgui.hpp
cv::namedWindow
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
Creates a window.
core.hpp
cv::stereo::QuasiDenseStereo::process
virtual void process(const cv::Mat &imgLeft, const cv::Mat &imgRight)=0
Main process of the algorithm. This method computes the sparse seeds and then densifies them.
cv::imread
Mat imread(const String &filename, int flags=IMREAD_COLOR)
Loads an image from a file.
cv::Mat::size
MatSize size
Definition: mat.hpp:2108
cv::Ptr
std::shared_ptr< _Tp > Ptr
Definition: cvstd_wrapper.hpp:23
uint
uint32_t uint
Definition: interface.h:42
cv::imshow
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
cv::Mat
n-dimensional dense array class
Definition: mat.hpp:792
cv
"black box" representation of the file storage associated with a file on disk.
Definition: affine.hpp:52
cv::stereo::QuasiDenseStereo::getDenseMatches
virtual void getDenseMatches(std::vector< stereo::Match > &denseMatches)=0
Get The dense corresponding points.