scene.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 <opencv2/calib3d.hpp>
  36. #include <opencv2/core.hpp>
  37. #include "test_precomp.hpp"
  38. static cv::Matx33d randomK(bool is_projective)
  39. {
  40. static cv::RNG rng;
  41. cv::Matx33d K = cv::Matx33d::zeros();
  42. K(0, 0) = rng.uniform(100, 1000);
  43. K(1, 1) = rng.uniform(100, 1000);
  44. if (is_projective)
  45. {
  46. K(0, 2) = rng.uniform(-100, 100);
  47. K(1, 2) = rng.uniform(-100, 100);
  48. }
  49. K(2, 2) = 1.0;
  50. return K;
  51. }
  52. void
  53. generateScene(size_t n_views, size_t n_points, bool is_projective, cv::Matx33d & K, std::vector<cv::Matx33d> & R,
  54. std::vector<cv::Vec3d> & t, std::vector<cv::Matx34d> & P, cv::Mat_<double> & points3d,
  55. std::vector<cv::Mat_<double> > & points2d)
  56. {
  57. R.resize(n_views);
  58. t.resize(n_views);
  59. cv::RNG rng;
  60. // Generate a bunch of random 3d points in a 0, 1 cube
  61. points3d.create(3, n_points);
  62. rng.fill(points3d, cv::RNG::UNIFORM, 0, 1);
  63. // Generate random intrinsics
  64. K = randomK(is_projective);
  65. // Generate random camera poses
  66. // TODO deal with smooth camera poses (e.g. from a video sequence)
  67. for (size_t i = 0; i < n_views; ++i)
  68. {
  69. // Get a random rotation axis
  70. cv::Vec3d vec;
  71. rng.fill(vec, cv::RNG::UNIFORM, 0, 1);
  72. // Give a random angle to the rotation vector
  73. vec = vec / cv::norm(vec) * rng.uniform(0.0f, float(2 * CV_PI));
  74. cv::Rodrigues(vec, R[i]);
  75. // Create a random translation
  76. t[i] = cv::Vec3d(rng.uniform(-0.5f, 0.5f), rng.uniform(-0.5f, 0.5f), rng.uniform(1.0f, 2.0f));
  77. // Make sure the shape is in front of the camera
  78. cv::Mat_<double> points3d_transformed = cv::Mat(R[i]) * points3d + cv::Mat(t[i]) * cv::Mat_<double> ::ones(1, n_points);
  79. double min_dist, max_dist;
  80. cv::minMaxIdx(points3d_transformed.row(2), &min_dist, &max_dist);
  81. if (min_dist < 0)
  82. t[i][2] = t[i][2] - min_dist + 1.0;
  83. }
  84. // Compute projection matrices
  85. P.resize(n_views);
  86. for (size_t i = 0; i < n_views; ++i)
  87. {
  88. cv::Matx33d K3 = K, R3 = R[i];
  89. cv::Vec3d t3 = t[i];
  90. cv::sfm::projectionFromKRt(K3, R3, t3, P[i]);
  91. }
  92. // Compute homogeneous 3d points
  93. cv::Mat_<double> points3d_homogeneous(4, n_points);
  94. points3d.copyTo(points3d_homogeneous.rowRange(0, 3));
  95. points3d_homogeneous.row(3).setTo(1);
  96. // Project those points for every view
  97. points2d.resize(n_views);
  98. for (size_t i = 0; i < n_views; ++i)
  99. {
  100. cv::Mat_<double> points2d_tmp = cv::Mat(P[i]) * points3d_homogeneous;
  101. points2d[i].create(2, n_points);
  102. for (unsigned char j = 0; j < 2; ++j)
  103. cv::Mat(points2d_tmp.row(j) / points2d_tmp.row(2)).copyTo(points2d[i].row(j));
  104. }
  105. // TODO: remove a certain number of points per view
  106. // TODO: add a certain number of outliers per view
  107. }