test_robust.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. #include "opencv2/sfm/robust.hpp"
  37. namespace opencv_test { namespace {
  38. TEST(Sfm_robust, fundamentalFromCorrespondences8PointRobust)
  39. {
  40. double tolerance = 1e-8;
  41. const int n = 16;
  42. Mat_<double> x1(2,n);
  43. x1 << 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5,
  44. 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 5;
  45. Mat_<double> x2 = x1.clone();
  46. for (int i = 0; i < n; ++i)
  47. {
  48. x2(0,i) += i % 2; // Multiple horizontal disparities.
  49. }
  50. x2(0,n - 1) = 10;
  51. x2(1,n - 1) = 10; // The outlier has vertical disparity.
  52. Matx33d F;
  53. vector<int> inliers;
  54. fundamentalFromCorrespondences8PointRobust(x1, x2, 0.1, F, inliers);
  55. // F should be 0, 0, 0,
  56. // 0, 0, -1,
  57. // 0, 1, 0
  58. EXPECT_NEAR(0.0, F(0,0), tolerance);
  59. EXPECT_NEAR(0.0, F(0,1), tolerance);
  60. EXPECT_NEAR(0.0, F(0,2), tolerance);
  61. EXPECT_NEAR(0.0, F(1,0), tolerance);
  62. EXPECT_NEAR(0.0, F(1,1), tolerance);
  63. EXPECT_NEAR(0.0, F(2,0), tolerance);
  64. EXPECT_NEAR(0.0, F(2,2), tolerance);
  65. EXPECT_NEAR(F(1,2), -F(2,1), tolerance);
  66. EXPECT_EQ(n - 1, inliers.size());
  67. }
  68. TEST(Sfm_robust, fundamentalFromCorrespondences8PointRealisticNoOutliers)
  69. {
  70. double tolerance = 1e-8;
  71. TwoViewDataSet d;
  72. generateTwoViewRandomScene(d);
  73. Matx33d F_estimated;
  74. vector<int> inliers;
  75. fundamentalFromCorrespondences8PointRobust(d.x1, d.x2, 3.0, F_estimated, inliers);
  76. EXPECT_EQ(d.x1.cols, inliers.size());
  77. // Normalize.
  78. Matx33d F_gt_norm, F_estimated_norm;
  79. normalizeFundamental(d.F, F_gt_norm);
  80. normalizeFundamental(F_estimated, F_estimated_norm);
  81. EXPECT_MATRIX_NEAR(F_gt_norm, F_estimated_norm, tolerance);
  82. // Check fundamental properties.
  83. expectFundamentalProperties( F_estimated, d.x1, d.x2, tolerance);
  84. }
  85. TEST(Sfm_robust, fundamentalFromCorrespondences7PointRobust)
  86. {
  87. double tolerance = 1e-8;
  88. const int n = 16;
  89. Mat_<double> x1(2,n);
  90. x1 << 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5,
  91. 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 5;
  92. Mat_<double> x2 = x1.clone();
  93. for (int i = 0; i < n; ++i)
  94. {
  95. x2(0,i) += i % 2; // Multiple horizontal disparities.
  96. }
  97. x2(0,n - 1) = 10;
  98. x2(1,n - 1) = 10; // The outlier has vertical disparity.
  99. Matx33d F;
  100. vector<int> inliers;
  101. fundamentalFromCorrespondences7PointRobust(x1, x2, 0.1, F, inliers);
  102. // F should be 0, 0, 0,
  103. // 0, 0, -1,
  104. // 0, 1, 0
  105. EXPECT_NEAR(0.0, F(0,0), tolerance);
  106. EXPECT_NEAR(0.0, F(0,1), tolerance);
  107. EXPECT_NEAR(0.0, F(0,2), tolerance);
  108. EXPECT_NEAR(0.0, F(1,0), tolerance);
  109. EXPECT_NEAR(0.0, F(1,1), tolerance);
  110. EXPECT_NEAR(0.0, F(2,0), tolerance);
  111. EXPECT_NEAR(0.0, F(2,2), tolerance);
  112. EXPECT_NEAR(F(1,2), -F(2,1), tolerance);
  113. EXPECT_EQ(n - 1, inliers.size());
  114. }
  115. TEST(Sfm_robust, fundamentalFromCorrespondences7PointRealisticNoOutliers)
  116. {
  117. double tolerance = 1e-8;
  118. TwoViewDataSet d;
  119. generateTwoViewRandomScene(d);
  120. Matx33d F_estimated;
  121. vector<int> inliers;
  122. fundamentalFromCorrespondences7PointRobust(d.x1, d.x2, 3.0, F_estimated, inliers);
  123. EXPECT_EQ(d.x1.cols, inliers.size());
  124. // Normalize.
  125. Matx33d F_gt_norm, F_estimated_norm;
  126. normalizeFundamental(d.F, F_gt_norm);
  127. normalizeFundamental(F_estimated, F_estimated_norm);
  128. EXPECT_MATRIX_NEAR(F_gt_norm, F_estimated_norm, tolerance);
  129. // Check fundamental properties.
  130. expectFundamentalProperties( F_estimated, d.x1, d.x2, tolerance);
  131. }
  132. }} // namespace