test_fundamental.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Software License Agreement (BSD License)
  3. *
  4. * Copyright (c) 2009, Willow Garage, Inc.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above
  14. * copyright notice, this list of conditions and the following
  15. * disclaimer in the documentation and/or other materials provided
  16. * with the distribution.
  17. * * Neither the name of Willow Garage, Inc. nor the names of its
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. */
  35. #include "test_precomp.hpp"
  36. namespace opencv_test { namespace {
  37. TEST(Sfm_fundamental, fundamentalFromProjections)
  38. {
  39. double tolerance_prop = 1e-7;
  40. double tolerance_near = 1e-15;
  41. Matx34d P1_gt, P2_gt;
  42. P1_gt << 1, 0, 0, 0,
  43. 0, 1, 0, 0,
  44. 0, 0, 1, 0;
  45. P2_gt << 1, 1, 1, 3,
  46. 0, 2, 0, 3,
  47. 0, 1, 1, 0;
  48. Matx33d F_gt;
  49. fundamentalFromProjections(P1_gt, P2_gt, F_gt);
  50. Matx34d P1, P2;
  51. projectionsFromFundamental(F_gt, P1, P2);
  52. Matx33d F;
  53. fundamentalFromProjections(P1, P2, F);
  54. Matx33d F_gt_norm, F_norm;
  55. normalizeFundamental(F_gt, F_gt_norm);
  56. normalizeFundamental(F, F_norm);
  57. EXPECT_MATRIX_PROP(F_gt, F, tolerance_prop);
  58. EXPECT_MATRIX_NEAR(F_gt_norm, F_norm, tolerance_near);
  59. }
  60. TEST(Sfm_fundamental, normalizedEightPointSolver)
  61. {
  62. double tolerance = 1e-14;
  63. TwoViewDataSet d;
  64. generateTwoViewRandomScene( d );
  65. Matx33d F;
  66. normalizedEightPointSolver( d.x1, d.x2, F );
  67. expectFundamentalProperties( F, d.x1, d.x2, tolerance );
  68. }
  69. TEST(Sfm_fundamental, motionFromEssential)
  70. {
  71. double tolerance = 1e-8;
  72. TwoViewDataSet d;
  73. generateTwoViewRandomScene(d);
  74. Matx33d E;
  75. essentialFromRt(d.R1, d.t1, d.R2, d.t2, E);
  76. Matx33d R;
  77. cv::Vec3d t;
  78. relativeCameraMotion(d.R1, d.t1, d.R2, d.t2, R, t);
  79. cv::normalize(t, t);
  80. std::vector<Mat> Rs;
  81. std::vector<cv::Mat> ts;
  82. motionFromEssential(E, Rs, ts);
  83. bool one_solution_is_correct = false;
  84. for ( int i = 0; i < Rs.size(); ++i )
  85. {
  86. if ( (cvtest::norm(Rs[i], R, NORM_L2) < tolerance) && (cvtest::norm(ts[i], t, NORM_L2) < tolerance) )
  87. {
  88. one_solution_is_correct = true;
  89. break;
  90. }
  91. }
  92. EXPECT_TRUE(one_solution_is_correct);
  93. }
  94. TEST(Sfm_fundamental, fundamentalToAndFromEssential)
  95. {
  96. double tolerance = 1e-15;
  97. TwoViewDataSet d;
  98. generateTwoViewRandomScene(d);
  99. Matx33d F, E;
  100. essentialFromFundamental(d.F, d.K1, d.K2, E);
  101. fundamentalFromEssential(E, d.K1, d.K2, F);
  102. Matx33d F_gt_norm, F_norm;
  103. normalizeFundamental(d.F, F_gt_norm);
  104. normalizeFundamental(F, F_norm);
  105. EXPECT_MATRIX_NEAR(F_gt_norm, F_norm, tolerance);
  106. }
  107. TEST(Sfm_fundamental, essentialFromFundamental)
  108. {
  109. TwoViewDataSet d;
  110. generateTwoViewRandomScene(d);
  111. Matx33d E_from_Rt;
  112. essentialFromRt(d.R1, d.t1, d.R2, d.t2, E_from_Rt);
  113. Matx33d E_from_F;
  114. essentialFromFundamental(d.F, d.K1, d.K2, E_from_F);
  115. EXPECT_MATRIX_PROP(E_from_Rt, E_from_F, 1e-6);
  116. }
  117. TEST(Sfm_fundamental, motionFromEssentialChooseSolution)
  118. {
  119. TwoViewDataSet d;
  120. generateTwoViewRandomScene(d);
  121. Matx33d E;
  122. essentialFromRt(d.R1, d.t1, d.R2, d.t2, E);
  123. Matx33d R;
  124. cv::Vec3d t;
  125. relativeCameraMotion(d.R1, d.t1, d.R2, d.t2, R, t);
  126. normalize(t, t);
  127. std::vector < Mat > Rs;
  128. std::vector < cv::Mat > ts;
  129. motionFromEssential(E, Rs, ts);
  130. cv::Vec2d x1(d.x1(0, 0), d.x1(1, 0));
  131. cv::Vec2d x2(d.x2(0, 0), d.x2(1, 0));
  132. int solution = motionFromEssentialChooseSolution(Rs, ts, d.K1, x1, d.K2, x2);
  133. EXPECT_LE(0, solution);
  134. EXPECT_LE(solution, 3);
  135. EXPECT_LE(cvtest::norm(Rs[solution], Mat(R), NORM_L2), 1e-8);
  136. EXPECT_LE(cvtest::norm(ts[solution], Mat(t), NORM_L2), 1e-8);
  137. }
  138. }} // namespace