test_triangulation.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. static void
  38. checkTriangulation(int nviews, int npoints, bool is_projective, float err_max2d, float err_max3d)
  39. {
  40. std::vector<Mat_<double> > points2d;
  41. std::vector<cv::Matx33d> Rs;
  42. std::vector<cv::Vec3d> ts;
  43. std::vector<cv::Matx34d> Ps;
  44. Matx33d K;
  45. Mat_<double> points3d;
  46. generateScene(nviews, npoints, is_projective, K, Rs, ts, Ps, points3d, points2d);
  47. // get 3d points
  48. cv::Mat X, X_homogeneous;
  49. std::vector<Mat_<double> > Ps_d(Ps.size());
  50. for(size_t i=0; i<Ps.size(); ++i)
  51. Ps_d[i] = cv::Mat_<double>(Ps[i]);
  52. triangulatePoints(points2d, Ps_d, X);
  53. euclideanToHomogeneous(X, X_homogeneous);
  54. for (int i = 0; i < npoints; ++i)
  55. {
  56. for (int k = 0; k < nviews; ++k)
  57. {
  58. cv::Mat x_reprojected;
  59. homogeneousToEuclidean( cv::Mat(Ps[k])*X_homogeneous.col(i), x_reprojected );
  60. // Check reprojection error. Should be nearly zero.
  61. double error = cvtest::norm(x_reprojected, points2d[k].col(i), NORM_L2);
  62. EXPECT_LE(error*error, err_max2d);
  63. }
  64. // Check 3d error. Should be nearly zero.
  65. double error = cvtest::norm(X.col(i), points3d.col(i), NORM_L2);
  66. EXPECT_LE(error*error, err_max3d);
  67. }
  68. }
  69. TEST(Sfm_triangulate, TriangulateDLT)
  70. {
  71. int nviews = 2;
  72. int npoints = 30;
  73. bool is_projective = true;
  74. checkTriangulation(nviews, npoints, is_projective, 1e-7, 1e-9);
  75. }
  76. TEST(Sfm_triangulate, NViewTriangulate_FiveViews)
  77. {
  78. int nviews = 5;
  79. int npoints = 6;
  80. bool is_projective = true;
  81. checkTriangulation(nviews, npoints, is_projective, 1e-7, 1e-9);
  82. }
  83. }} // namespace