diff --git a/modules/stereo/include/opencv2/stereo/quasi_dense_stereo.hpp b/modules/stereo/include/opencv2/stereo/quasi_dense_stereo.hpp index 197d762b6c7..b2290e3768c 100644 --- a/modules/stereo/include/opencv2/stereo/quasi_dense_stereo.hpp +++ b/modules/stereo/include/opencv2/stereo/quasi_dense_stereo.hpp @@ -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 create(cv::Size monoImgSize, cv::String paramFilepath = cv::String()); diff --git a/modules/stereo/samples/dense_disparity.cpp b/modules/stereo/samples/dense_disparity.cpp index e7822366623..b72d567588a 100644 --- a/modules/stereo/samples/dense_disparity.cpp +++ b/modules/stereo/samples/dense_disparity.cpp @@ -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] diff --git a/modules/stereo/samples/sample_quasi_dense.py b/modules/stereo/samples/sample_quasi_dense.py index a6059e70179..bd11e1d2898 100644 --- a/modules/stereo/samples/sample_quasi_dense.py +++ b/modules/stereo/samples/sample_quasi_dense.py @@ -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() diff --git a/modules/stereo/src/quasi_dense_stereo.cpp b/modules/stereo/src/quasi_dense_stereo.cpp index a4d196ae11b..dfc48f52937 100644 --- a/modules/stereo/src/quasi_dense_stereo.cpp +++ b/modules/stereo/src/quasi_dense_stereo.cpp @@ -34,7 +34,6 @@ class QuasiDenseStereoImpl : public QuasiDenseStereo ssum1 = cv::Mat_(integralSize); // the disparity image. disparity = cv::Mat_(monoImgSize); - disparityImg = cv::Mat_(monoImgSize); // texture images. textureDescLeft = cv::Mat_ (monoImgSize); textureDescRight = cv::Mat_ (monoImgSize); @@ -55,7 +54,6 @@ class QuasiDenseStereoImpl : public QuasiDenseStereo ssum1.release(); // the disparity image. disparity.release(); - disparityImg.release(); // texture images. textureDescLeft.release(); textureDescRight.release(); @@ -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_ &matchMap, @@ -262,7 +259,7 @@ class QuasiDenseStereoImpl : public QuasiDenseStereo if (matchMap.at(tmpPoint) == NO_MATCH) { - dispMat.at(tmpPoint) = 200; + dispMat.at(tmpPoint) = NAN; continue; } //if a match is found, compute the difference in location of the match and current @@ -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_ &dispMat, const int lvls) - { - float tmpPixelVal ; - double min, max; -// minMaxLoc(disparity, &min, &max); - min = 0; - max = lvls; - for(int row=0; row(row, col); - tmpPixelVal = (float) (255. - 255.0*(tmpPixelVal-min)/(max-min)); - - disparityImg.at(row, col) = (uint8_t) tmpPixelVal; - } - } - return disparityImg; - } - - /** * @brief Compute the Zero-mean Normalized Cross-correlation. * @@ -635,10 +601,10 @@ class QuasiDenseStereoImpl : public QuasiDenseStereo return refMap.at(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. @@ -663,8 +629,6 @@ class QuasiDenseStereoImpl : public QuasiDenseStereo cv::Mat_ ssum1; // Container to store the disparity un-normalized cv::Mat_ disparity; - // Container to store the disparity image. - cv::Mat_ disparityImg; // Containers to store textures descriptors. cv::Mat_ textureDescLeft; cv::Mat_ textureDescRight; diff --git a/modules/stereo/test/test_block_matching.cpp b/modules/stereo/test/test_block_matching.cpp index 159be4907cb..c678d42d724 100644 --- a/modules/stereo/test/test_block_matching.cpp +++ b/modules/stereo/test/test_block_matching.cpp @@ -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"); diff --git a/modules/stereo/test/test_qds_matching.cpp b/modules/stereo/test/test_qds_matching.cpp new file mode 100644 index 00000000000..9b550a80da2 --- /dev/null +++ b/modules/stereo/test/test_qds_matching.cpp @@ -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(row, col); + float estimated_val = estimation.at(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 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 \ No newline at end of file