From b1688a20ae6677106f787056e925ed3f300b4a75 Mon Sep 17 00:00:00 2001 From: Dimitrios Psychogyios Date: Sat, 13 Feb 2021 23:17:04 +0000 Subject: [PATCH 1/2] Remove normalization in getDisparity. The function returns CV_32F disparity maps with nan values for unknown matches. --- .../opencv2/stereo/quasi_dense_stereo.hpp | 3 +- modules/stereo/samples/dense_disparity.cpp | 3 +- modules/stereo/samples/sample_quasi_dense.py | 2 +- modules/stereo/src/quasi_dense_stereo.cpp | 42 +---- modules/stereo/test/test_block_matching.cpp | 2 +- modules/stereo/test/test_qds_matching.cpp | 144 ++++++++++++++++++ 6 files changed, 151 insertions(+), 45 deletions(-) create mode 100644 modules/stereo/test/test_qds_matching.cpp 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..f21983c03c5 --- /dev/null +++ b/modules/stereo/test/test_qds_matching.cpp @@ -0,0 +1,144 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// Intel License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2000, Intel Corporation, all rights reserved. +// Third party copyrights are property of their respective owners. +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of Intel Corporation may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors "as is" and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ + +#include "test_precomp.hpp" + +namespace opencv_test { namespace { + +class CV_QdsMatchingTest : public cvtest::BaseTest +{ +public: + CV_QdsMatchingTest(); + ~CV_QdsMatchingTest(); +protected: + void run(int /* idx */); +}; + +CV_QdsMatchingTest::CV_QdsMatchingTest(){} +CV_QdsMatchingTest::~CV_QdsMatchingTest(){} + +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) +{ + //load data + Mat image1, image2, gt; + image1 = imread(ts->get_data_path() + "stereomatching/datasets/cones/im2.png", IMREAD_GRAYSCALE); + image2 = imread(ts->get_data_path() + "stereomatching/datasets/cones/im6.png", IMREAD_GRAYSCALE); + gt = imread(ts->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 + if(image1.empty() || image2.empty() || gt.empty()) + { + ts->printf(cvtest::TS::LOG, "Wrong input data \n"); + ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA); + return; + } + if(image1.rows != image2.rows || image1.cols != image2.cols || gt.cols != image1.cols || gt.rows != image1.rows) + { + ts->printf(cvtest::TS::LOG, "Wrong input / output dimension \n"); + ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA); + return; + } + //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 + if(gt.rows != outDisp.rows || gt.cols != outDisp.cols) + { + ts->printf(cvtest::TS::LOG, "Missmatch input output dimension \n"); + ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT); + return; + } + + // test error level, for this sample/hyperparameters, EPE(MAE) should be ~1.1 in version 4.5.1 + double error_mae = disparity_MAE(gt, outDisp); + if(error_mae > 2) + { + ts->printf( cvtest::TS::LOG,("Disparity Mean Absolute Error: "+std::to_string(error_mae)+" pixels > 2\n").c_str()); + ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY); + return; + } +} + + +TEST(qds_matching_simple_test, accuracy) { CV_QdsMatchingTest test; test.safe_run(); } + + +}} // namespace \ No newline at end of file From 0909bb47983fef72024135b4c1f6dad6be048815 Mon Sep 17 00:00:00 2001 From: Dimitrios Psychogyios Date: Sun, 28 Feb 2021 20:39:39 +0000 Subject: [PATCH 2/2] Switch to GTEST and replace license header --- modules/stereo/test/test_qds_matching.cpp | 107 ++++------------------ 1 file changed, 16 insertions(+), 91 deletions(-) diff --git a/modules/stereo/test/test_qds_matching.cpp b/modules/stereo/test/test_qds_matching.cpp index f21983c03c5..9b550a80da2 100644 --- a/modules/stereo/test/test_qds_matching.cpp +++ b/modules/stereo/test/test_qds_matching.cpp @@ -1,77 +1,27 @@ -/*M/////////////////////////////////////////////////////////////////////////////////////// -// -// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. -// -// By downloading, copying, installing or using the software you agree to this license. -// If you do not agree to this license, do not download, install, -// copy or use the software. -// -// -// Intel License Agreement -// For Open Source Computer Vision Library -// -// Copyright (C) 2000, Intel Corporation, all rights reserved. -// Third party copyrights are property of their respective owners. -// -// Redistribution and use in source and binary forms, with or without modification, -// are permitted provided that the following conditions are met: -// -// * Redistribution's of source code must retain the above copyright notice, -// this list of conditions and the following disclaimer. -// -// * Redistribution's in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// * The name of Intel Corporation may not be used to endorse or promote products -// derived from this software without specific prior written permission. -// -// This software is provided by the copyright holders and contributors "as is" and -// any express or implied warranties, including, but not limited to, the implied -// warranties of merchantability and fitness for a particular purpose are disclaimed. -// In no event shall the Intel Corporation or contributors be liable for any direct, -// indirect, incidental, special, exemplary, or consequential damages -// (including, but not limited to, procurement of substitute goods or services; -// loss of use, data, or profits; or business interruption) however caused -// and on any theory of liability, whether in contract, strict liability, -// or tort (including negligence or otherwise) arising in any way out of -// the use of this software, even if advised of the possibility of such damage. -// -//M*/ +// 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 { -class CV_QdsMatchingTest : public cvtest::BaseTest -{ -public: - CV_QdsMatchingTest(); - ~CV_QdsMatchingTest(); -protected: - void run(int /* idx */); -}; - -CV_QdsMatchingTest::CV_QdsMatchingTest(){} -CV_QdsMatchingTest::~CV_QdsMatchingTest(){} 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)) - { + if (estimated_val == 0 || ref_val == 0 || std::isnan(estimated_val)){ continue; } else{ @@ -84,31 +34,22 @@ static float disparity_MAE(const Mat &reference, const Mat &estimation) } -void CV_QdsMatchingTest::run(int) +// void CV_QdsMatchingTest::run(int) +TEST(qds_getDisparity, accuracy) { //load data Mat image1, image2, gt; - image1 = imread(ts->get_data_path() + "stereomatching/datasets/cones/im2.png", IMREAD_GRAYSCALE); - image2 = imread(ts->get_data_path() + "stereomatching/datasets/cones/im6.png", IMREAD_GRAYSCALE); - gt = imread(ts->get_data_path() + "stereomatching/datasets/cones/disp2.png", IMREAD_GRAYSCALE); + 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 - if(image1.empty() || image2.empty() || gt.empty()) - { - ts->printf(cvtest::TS::LOG, "Wrong input data \n"); - ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA); - return; - } - if(image1.rows != image2.rows || image1.cols != image2.cols || gt.cols != image1.cols || gt.rows != image1.rows) - { - ts->printf(cvtest::TS::LOG, "Wrong input / output dimension \n"); - ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA); - return; - } + 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); @@ -118,27 +59,11 @@ void CV_QdsMatchingTest::run(int) qds_matcher->process(image1, image2); Mat outDisp = qds_matcher->getDisparity(); - // test input output size consistency - if(gt.rows != outDisp.rows || gt.cols != outDisp.cols) - { - ts->printf(cvtest::TS::LOG, "Missmatch input output dimension \n"); - ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT); - return; - } - - // test error level, for this sample/hyperparameters, EPE(MAE) should be ~1.1 in version 4.5.1 - double error_mae = disparity_MAE(gt, outDisp); - if(error_mae > 2) - { - ts->printf( cvtest::TS::LOG,("Disparity Mean Absolute Error: "+std::to_string(error_mae)+" pixels > 2\n").c_str()); - ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY); - return; - } + 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)"; } -TEST(qds_matching_simple_test, accuracy) { CV_QdsMatchingTest test; test.safe_run(); } - }} // namespace \ No newline at end of file