test_reconstruct.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. /* Check projection errors */
  38. static void
  39. check_projection_errors(const cv::Mat& X_estimated, const std::vector<Matx34d>& Ps,
  40. const std::vector<Mat_<double> >& xs, float err_max2d)
  41. {
  42. cv::Mat X;
  43. euclideanToHomogeneous(X_estimated, X); // 3D point
  44. for (int m = 0; m < xs.size(); ++m)
  45. {
  46. cv::Mat x;
  47. homogeneousToEuclidean(cv::Mat(Ps[m]) * X, x); // 2d projection
  48. cv::Mat projerr = xs[m] - x;
  49. for (int n = 0; n < projerr.cols; ++n)
  50. {
  51. double d = cv::norm(projerr.col(n));
  52. EXPECT_NEAR(0, d, err_max2d);
  53. }
  54. }
  55. }
  56. #if CERES_FOUND
  57. TEST(Sfm_reconstruct, twoViewProjectiveOutliers)
  58. {
  59. float err_max2d = 1e-7;
  60. int nviews = 2;
  61. int npoints = 50;
  62. bool is_projective = true;
  63. std::vector<Mat_<double> > points2d;
  64. std::vector<cv::Matx33d> Rs;
  65. std::vector<cv::Vec3d> ts;
  66. std::vector<cv::Matx34d> Ps;
  67. Matx33d K;
  68. Mat_<double> points3d;
  69. generateScene(nviews, npoints, is_projective, K, Rs, ts, Ps, points3d, points2d);
  70. Mat_<double> points3d_estimated;
  71. std::vector<cv::Mat> Ps_estimated;
  72. reconstruct(points2d, Ps_estimated, points3d_estimated, K, is_projective);
  73. /* Check projection errors on GT */
  74. check_projection_errors(points3d, Ps, points2d, err_max2d);
  75. /* Check projection errors on estimates */
  76. std::vector<cv::Matx34d> Ps_estimated_d;
  77. Ps_estimated_d.resize(Ps_estimated.size());
  78. for(size_t i=0; i<Ps_estimated.size(); ++i)
  79. Ps_estimated_d[i] = Ps_estimated[i];
  80. check_projection_errors(points3d_estimated, Ps_estimated_d, points2d, err_max2d);
  81. }
  82. #endif /* CERES_FOUND */
  83. }} // namespace