test_qds_matching.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. #include "test_precomp.hpp"
  5. namespace opencv_test { namespace {
  6. static float disparity_MAE(const Mat &reference, const Mat &estimation)
  7. {
  8. int elems=0;
  9. float error=0;
  10. float ref_invalid=0;
  11. for (int row=0; row< reference.rows; row++){
  12. for (int col=0; col<reference.cols; col++){
  13. float ref_val = reference.at<float>(row, col);
  14. float estimated_val = estimation.at<float>(row, col);
  15. if (ref_val == 0){
  16. ref_invalid++;
  17. }
  18. // filter out pixels with unknown reference value and pixels whose disparity did not get estimated.
  19. if (estimated_val == 0 || ref_val == 0 || std::isnan(estimated_val)){
  20. continue;
  21. }
  22. else{
  23. error+=abs(ref_val - estimated_val);
  24. elems+=1;
  25. }
  26. }
  27. }
  28. return error/elems;
  29. }
  30. // void CV_QdsMatchingTest::run(int)
  31. TEST(qds_getDisparity, accuracy)
  32. {
  33. //load data
  34. Mat image1, image2, gt;
  35. image1 = imread(cvtest::TS::ptr()->get_data_path() + "stereomatching/datasets/cones/im2.png", IMREAD_GRAYSCALE);
  36. image2 = imread(cvtest::TS::ptr()->get_data_path() + "stereomatching/datasets/cones/im6.png", IMREAD_GRAYSCALE);
  37. gt = imread(cvtest::TS::ptr()->get_data_path() + "stereomatching/datasets/cones/disp2.png", IMREAD_GRAYSCALE);
  38. // reference scale factor is based on this https://github.com/opencv/opencv_extra/blob/master/testdata/cv/stereomatching/datasets/datasets.xml
  39. gt.convertTo(gt, CV_32F);
  40. gt =gt/4;
  41. //test inputs
  42. ASSERT_FALSE(image1.empty() || image2.empty() || gt.empty()) << "Issue with input data";
  43. //configure disparity algorithm
  44. cv::Size frameSize = image1.size();
  45. Ptr<stereo::QuasiDenseStereo> qds_matcher = stereo::QuasiDenseStereo::create(frameSize);
  46. //compute disparity
  47. qds_matcher->process(image1, image2);
  48. Mat outDisp = qds_matcher->getDisparity();
  49. // test input output size consistency
  50. ASSERT_EQ(gt.size(), outDisp.size()) << "Mismatch input/output dimensions";
  51. ASSERT_LT(disparity_MAE(gt, outDisp),2) << "EPE should be 1.1053 for this sample/hyperparamters (Tested on version 4.5.1)";
  52. }
  53. }} // namespace