Skip to content

Commit

Permalink
Merge pull request #2869 from dimitrisPs:QDS_getDisparity
Browse files Browse the repository at this point in the history
Remove normalization in cv::stereo::QuasiDenseStereo getDisparity().

* Remove normalization in getDisparity. The function returns CV_32F disparity maps with nan values for unknown matches.

* Switch to GTEST and replace license header
  • Loading branch information
dimitrisPs authored Mar 1, 2021
1 parent 974f90f commit 0165d48
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 45 deletions.
3 changes: 1 addition & 2 deletions modules/stereo/include/opencv2/stereo/quasi_dense_stereo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,12 @@ class CV_EXPORTS_W QuasiDenseStereo

/**
* @brief Compute and return the disparity map based on the correspondences found in the "process" method.
* @param[in] disparityLvls The level of detail in output disparity image.
* @note Default level is 50
* @return cv::Mat containing a the disparity image in grayscale.
* @sa computeDisparity
* @sa quantizeDisparity
*/
CV_WRAP virtual cv::Mat getDisparity(uint8_t disparityLvls=50) = 0;
CV_WRAP virtual cv::Mat getDisparity() = 0;


CV_WRAP static cv::Ptr<QuasiDenseStereo> create(cv::Size monoImgSize, cv::String paramFilepath = cv::String());
Expand Down
3 changes: 1 addition & 2 deletions modules/stereo/samples/dense_disparity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ int main()


//! [disp]
uint8_t displvl = 80;
cv::Mat disp;
disp = stereo->getDisparity(displvl);
disp = stereo->getDisparity();
cv::namedWindow("disparity map");
cv::imshow("disparity map", disp);
//! [disp]
Expand Down
2 changes: 1 addition & 1 deletion modules/stereo/samples/sample_quasi_dense.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

stereo = cv.stereo.QuasiDenseStereo_create(frame_size[::-1])
stereo.process(left_img, right_img)
disp = stereo.getDisparity(80)
disp = stereo.getDisparity()
cv.imshow("disparity", disp)
cv.waitKey()
dense_matches = stereo.getDenseMatches()
Expand Down
42 changes: 3 additions & 39 deletions modules/stereo/src/quasi_dense_stereo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class QuasiDenseStereoImpl : public QuasiDenseStereo
ssum1 = cv::Mat_<double>(integralSize);
// the disparity image.
disparity = cv::Mat_<float>(monoImgSize);
disparityImg = cv::Mat_<uchar>(monoImgSize);
// texture images.
textureDescLeft = cv::Mat_<int> (monoImgSize);
textureDescRight = cv::Mat_<int> (monoImgSize);
Expand All @@ -55,7 +54,6 @@ class QuasiDenseStereoImpl : public QuasiDenseStereo
ssum1.release();
// the disparity image.
disparity.release();
disparityImg.release();
// texture images.
textureDescLeft.release();
textureDescRight.release();
Expand Down Expand Up @@ -248,7 +246,6 @@ class QuasiDenseStereoImpl : public QuasiDenseStereo
* @param[in] matchMap A matrix of points, the same size as the left channel. Each cell of this
* matrix stores the location of the corresponding point in the right image.
* @param[out] dispMat The disparity map.
* @sa quantizeDisparity
* @sa getDisparity
*/
void computeDisparity(const cv::Mat_<cv::Point2i> &matchMap,
Expand All @@ -262,7 +259,7 @@ class QuasiDenseStereoImpl : public QuasiDenseStereo

if (matchMap.at<cv::Point2i>(tmpPoint) == NO_MATCH)
{
dispMat.at<float>(tmpPoint) = 200;
dispMat.at<float>(tmpPoint) = NAN;
continue;
}
//if a match is found, compute the difference in location of the match and current
Expand All @@ -276,37 +273,6 @@ class QuasiDenseStereoImpl : public QuasiDenseStereo
}


/**
* @brief Disparity map normalization for display purposes. If needed specify the quantization
* level as input argument.
* @param[in] dispMat The disparity Map.
* @param[in] lvls The quantization level of the output disparity map.
* @return Disparity image.
* @note Stores the output in the disparityImage class variable.
* @sa computeDisparity
* @sa getDisparity
*/
cv::Mat quantiseDisparity(const cv::Mat_<float> &dispMat, const int lvls)
{
float tmpPixelVal ;
double min, max;
// minMaxLoc(disparity, &min, &max);
min = 0;
max = lvls;
for(int row=0; row<height; row++)
{
for(int col=0; col<width; col++)
{
tmpPixelVal = dispMat.at<float>(row, col);
tmpPixelVal = (float) (255. - 255.0*(tmpPixelVal-min)/(max-min));

disparityImg.at<uchar>(row, col) = (uint8_t) tmpPixelVal;
}
}
return disparityImg;
}


/**
* @brief Compute the Zero-mean Normalized Cross-correlation.
*
Expand Down Expand Up @@ -635,10 +601,10 @@ class QuasiDenseStereoImpl : public QuasiDenseStereo
return refMap.at<cv::Point2i>(y, x);
}

cv::Mat getDisparity(uint8_t disparityLvls) override
cv::Mat getDisparity() override
{
computeDisparity(refMap, disparity);
return quantiseDisparity(disparity, disparityLvls);
return disparity;
}

// Variables used at sparse feature extraction.
Expand All @@ -663,8 +629,6 @@ class QuasiDenseStereoImpl : public QuasiDenseStereo
cv::Mat_<double> ssum1;
// Container to store the disparity un-normalized
cv::Mat_<float> disparity;
// Container to store the disparity image.
cv::Mat_<uchar> disparityImg;
// Containers to store textures descriptors.
cv::Mat_<int> textureDescLeft;
cv::Mat_<int> textureDescRight;
Expand Down
2 changes: 1 addition & 1 deletion modules/stereo/test/test_block_matching.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ void CV_SGBlockMatchingTest::run(int )
image2 = imread(ts->get_data_path() + "stereomatching/datasets/tsukuba/im6.png", IMREAD_GRAYSCALE);
gt = imread(ts->get_data_path() + "stereomatching/datasets/tsukuba/disp2.png", IMREAD_GRAYSCALE);


ts->printf(cvtest::TS::LOG,(ts->get_data_path() + "stereomatching/datasets/tsukuba/im2.png").c_str());
if(image1.empty() || image2.empty() || gt.empty())
{
ts->printf(cvtest::TS::LOG, "Wrong input data \n");
Expand Down
69 changes: 69 additions & 0 deletions modules/stereo/test/test_qds_matching.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.


#include "test_precomp.hpp"

namespace opencv_test { namespace {


static float disparity_MAE(const Mat &reference, const Mat &estimation)
{
int elems=0;
float error=0;
float ref_invalid=0;
for (int row=0; row< reference.rows; row++){
for (int col=0; col<reference.cols; col++){
float ref_val = reference.at<float>(row, col);
float estimated_val = estimation.at<float>(row, col);
if (ref_val == 0){
ref_invalid++;
}
// filter out pixels with unknown reference value and pixels whose disparity did not get estimated.
if (estimated_val == 0 || ref_val == 0 || std::isnan(estimated_val)){
continue;
}
else{
error+=abs(ref_val - estimated_val);
elems+=1;
}
}
}
return error/elems;
}


// void CV_QdsMatchingTest::run(int)
TEST(qds_getDisparity, accuracy)
{
//load data
Mat image1, image2, gt;
image1 = imread(cvtest::TS::ptr()->get_data_path() + "stereomatching/datasets/cones/im2.png", IMREAD_GRAYSCALE);
image2 = imread(cvtest::TS::ptr()->get_data_path() + "stereomatching/datasets/cones/im6.png", IMREAD_GRAYSCALE);
gt = imread(cvtest::TS::ptr()->get_data_path() + "stereomatching/datasets/cones/disp2.png", IMREAD_GRAYSCALE);

// reference scale factor is based on this https://github.com/opencv/opencv_extra/blob/master/testdata/cv/stereomatching/datasets/datasets.xml
gt.convertTo(gt, CV_32F);
gt =gt/4;

//test inputs
ASSERT_FALSE(image1.empty() || image2.empty() || gt.empty()) << "Issue with input data";

//configure disparity algorithm
cv::Size frameSize = image1.size();
Ptr<stereo::QuasiDenseStereo> qds_matcher = stereo::QuasiDenseStereo::create(frameSize);


//compute disparity
qds_matcher->process(image1, image2);
Mat outDisp = qds_matcher->getDisparity();

// test input output size consistency
ASSERT_EQ(gt.size(), outDisp.size()) << "Mismatch input/output dimensions";
ASSERT_LT(disparity_MAE(gt, outDisp),2) << "EPE should be 1.1053 for this sample/hyperparamters (Tested on version 4.5.1)";
}



}} // namespace

0 comments on commit 0165d48

Please sign in to comment.