test_dynafu.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. // This code is also subject to the license terms in the LICENSE_KinectFusion.md file found in this module's directory
  5. #include "test_precomp.hpp"
  6. #ifdef HAVE_OPENGL
  7. namespace opencv_test { namespace {
  8. static std::vector<std::string> readDepth(std::string fileList)
  9. {
  10. std::vector<std::string> v;
  11. std::fstream file(fileList);
  12. if(!file.is_open())
  13. throw std::runtime_error("Failed to read depth list");
  14. std::string dir;
  15. size_t slashIdx = fileList.rfind('/');
  16. slashIdx = slashIdx != std::string::npos ? slashIdx : fileList.rfind('\\');
  17. dir = fileList.substr(0, slashIdx);
  18. while(!file.eof())
  19. {
  20. std::string s, imgPath;
  21. std::getline(file, s);
  22. if(s.empty() || s[0] == '#') continue;
  23. std::stringstream ss;
  24. ss << s;
  25. double thumb;
  26. ss >> thumb >> imgPath;
  27. v.push_back(dir+'/'+imgPath);
  28. }
  29. return v;
  30. }
  31. static const bool display = false;
  32. void flyTest(bool hiDense, bool inequal)
  33. {
  34. Ptr<kinfu::Params> params;
  35. if(hiDense)
  36. params = kinfu::Params::defaultParams();
  37. else
  38. params = kinfu::Params::coarseParams();
  39. if(inequal)
  40. {
  41. params->volumeDims[0] += 32;
  42. params->volumeDims[1] -= 32;
  43. }
  44. std::vector<String> depths = readDepth(cvtest::TS::ptr()->get_data_path() + "dynafu/depth.txt");
  45. CV_Assert(!depths.empty());
  46. Ptr<dynafu::DynaFu> df = dynafu::DynaFu::create(params);
  47. // Check for first 10 frames
  48. CV_Assert(depths.size() >= 10);
  49. Mat currentDepth, prevDepth;
  50. for(size_t i = 0; i < 10; i++)
  51. {
  52. currentDepth = cv::imread(depths[i], IMREAD_ANYDEPTH);
  53. ASSERT_TRUE(df->update(currentDepth));
  54. Mat renderedDepth;
  55. df->renderSurface(renderedDepth, noArray(), noArray());
  56. if(i > 0)
  57. {
  58. // Check if estimated depth aligns with actual depth in the previous frame
  59. Mat depthCvt8, renderCvt8;
  60. convertScaleAbs(prevDepth, depthCvt8, 0.25*256. / params->depthFactor);
  61. convertScaleAbs(renderedDepth, renderCvt8, 0.33*255, -0.5*0.33*255);
  62. Mat diff;
  63. absdiff(depthCvt8, renderCvt8, diff);
  64. Scalar_<float> mu, sigma;
  65. meanStdDev(diff, mu, sigma);
  66. std::cout << "Mean: " << mu[0] << ", Std dev: " << sigma[0] << std::endl;
  67. }
  68. if(display)
  69. {
  70. imshow("depth", currentDepth*(1.f/params->depthFactor/4.f));
  71. Mat rendered;
  72. df->render(rendered);
  73. imshow("render", rendered);
  74. waitKey(10);
  75. }
  76. currentDepth.copyTo(prevDepth);
  77. }
  78. }
  79. /*
  80. #ifdef OPENCV_ENABLE_NONFREE
  81. TEST( DynamicFusion, lowDense )
  82. #else
  83. TEST(DynamicFusion, DISABLED_lowDense)
  84. #endif
  85. {
  86. flyTest(false, false);
  87. }
  88. #ifdef OPENCV_ENABLE_NONFREE
  89. TEST( DynamicFusion, inequal )
  90. #else
  91. TEST(DynamicFusion, DISABLED_inequal)
  92. #endif
  93. {
  94. flyTest(false, true);
  95. }
  96. */
  97. // To enable DynamicFusion tests, uncomment the above lines and delete the following lines
  98. TEST(DynamicFusion, DISABLED)
  99. {
  100. CV_UNUSED(flyTest);
  101. }
  102. }} // namespace
  103. #endif