test_registration.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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_WillowGarage.md file found in this module's directory
  5. #include "test_precomp.hpp"
  6. namespace opencv_test { namespace {
  7. class CV_RgbdDepthRegistrationTest: public cvtest::BaseTest
  8. {
  9. public:
  10. CV_RgbdDepthRegistrationTest()
  11. {
  12. }
  13. ~CV_RgbdDepthRegistrationTest()
  14. {
  15. }
  16. protected:
  17. void
  18. run(int)
  19. {
  20. // Test all three input types for no-op registrations (where a depth image is registered to itself)
  21. int code = noOpRandomRegistrationTest<unsigned short>(100, 2500);
  22. if( code != cvtest::TS::OK )
  23. {
  24. ts->set_failed_test_info(code);
  25. return;
  26. }
  27. code = noOpRandomRegistrationTest<float>(0.1f, 2.5f);
  28. if( code != cvtest::TS::OK )
  29. {
  30. ts->set_failed_test_info(code);
  31. return;
  32. }
  33. code = noOpRandomRegistrationTest<double>(0.1, 2.5);
  34. if( code != cvtest::TS::OK )
  35. {
  36. ts->set_failed_test_info(code);
  37. return;
  38. }
  39. // Test sentinel value handling, occlusion, and dilation
  40. {
  41. // K from a VGA Kinect
  42. Mat K = (Mat_<float>(3, 3) << 525., 0., 319.5, 0., 525., 239.5, 0., 0., 1.);
  43. int width = 640, height = 480;
  44. // All elements are zero except for first two along the diagonal
  45. Mat_<unsigned short> vgaDepth(height, width, (unsigned short)0);
  46. vgaDepth(0,0) = 1001;
  47. vgaDepth(1,1) = 1000;
  48. Mat_<unsigned short> registeredDepth;
  49. registerDepth(K, K, Mat(), Matx44f::eye(), vgaDepth, Size(width, height), registeredDepth, true);
  50. // We expect the closer depth of 1000 to occlude the more distant depth and occupy the
  51. // upper four left pixels in the depth image because of dilation
  52. Mat_<unsigned short> expectedResult(height, width, (unsigned short)0);
  53. expectedResult(0,0) = 1000;
  54. expectedResult(0,1) = 1000;
  55. expectedResult(1,0) = 1000;
  56. expectedResult(1,1) = 1000;
  57. int cmpResult = cvtest::cmpEps2( ts, registeredDepth, expectedResult, 0, true, "Dilation and occlusion");
  58. if( cmpResult != cvtest::TS::OK )
  59. {
  60. ts->set_failed_test_info(cmpResult);
  61. return;
  62. }
  63. }
  64. ts->set_failed_test_info(cvtest::TS::OK);
  65. }
  66. private:
  67. template <class DepthDepth>
  68. int noOpRandomRegistrationTest(DepthDepth minDepth, DepthDepth maxDepth)
  69. {
  70. // K from a VGA Kinect
  71. Mat K = (Mat_<float>(3, 3) << 525., 0., 319.5, 0., 525., 239.5, 0., 0., 1.);
  72. // Create a random depth image
  73. RNG rng;
  74. Mat_<DepthDepth> randomVGADepth(480, 640);
  75. rng.fill(randomVGADepth, RNG::UNIFORM, minDepth, maxDepth);
  76. Mat registeredDepth;
  77. registerDepth(K, K, Mat(), Matx44f::eye(), randomVGADepth, Size(640, 480), registeredDepth);
  78. // See if registeredDepth == depth
  79. return cvtest::cmpEps2( ts, registeredDepth, randomVGADepth, 1e-5, true, "No-op registration");
  80. }
  81. };
  82. TEST(Rgbd_DepthRegistration, compute)
  83. {
  84. CV_RgbdDepthRegistrationTest test;
  85. test.safe_run();
  86. }
  87. TEST(Rgbd_DepthRegistration, issue_2234)
  88. {
  89. Matx33f intrinsicsDepth(100, 0, 50, 0, 100, 50, 0, 0, 1);
  90. Matx33f intrinsicsColor(100, 0, 200, 0, 100, 50, 0, 0, 1);
  91. Mat_<float> depthMat(100, 100, (float)0.);
  92. for(int i = 1; i <= 100; i++)
  93. {
  94. for(int j = 1; j <= 100; j++)
  95. depthMat(i-1,j-1) = (float)j;
  96. }
  97. Mat registeredDepth;
  98. registerDepth(intrinsicsDepth, intrinsicsColor, Mat(), Matx44f::eye(), depthMat, Size(400, 100), registeredDepth);
  99. Rect roi( 150, 0, 100, 100 );
  100. Mat subM(registeredDepth,roi);
  101. EXPECT_EQ(0, cvtest::norm(subM, depthMat, NORM_INF));
  102. }
  103. }} // namespace