simple_bundle_adjuster.cc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2015 Google Inc. All rights reserved.
  3. // http://ceres-solver.org/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. // * Neither the name of Google Inc. nor the names of its contributors may be
  14. // used to endorse or promote products derived from this software without
  15. // specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. // POSSIBILITY OF SUCH DAMAGE.
  28. //
  29. // Author: keir@google.com (Keir Mierle)
  30. //
  31. // A minimal, self-contained bundle adjuster using Ceres, that reads
  32. // files from University of Washington' Bundle Adjustment in the Large dataset:
  33. // http://grail.cs.washington.edu/projects/bal
  34. //
  35. // This does not use the best configuration for solving; see the more involved
  36. // bundle_adjuster.cc file for details.
  37. #include <cmath>
  38. #include <cstdio>
  39. #include <iostream>
  40. #include "ceres/ceres.h"
  41. #include "ceres/rotation.h"
  42. // Read a Bundle Adjustment in the Large dataset.
  43. class BALProblem {
  44. public:
  45. ~BALProblem() {
  46. delete[] point_index_;
  47. delete[] camera_index_;
  48. delete[] observations_;
  49. delete[] parameters_;
  50. }
  51. int num_observations() const { return num_observations_; }
  52. const double* observations() const { return observations_; }
  53. double* mutable_cameras() { return parameters_; }
  54. double* mutable_points() { return parameters_ + 9 * num_cameras_; }
  55. double* mutable_camera_for_observation(int i) {
  56. return mutable_cameras() + camera_index_[i] * 9;
  57. }
  58. double* mutable_point_for_observation(int i) {
  59. return mutable_points() + point_index_[i] * 3;
  60. }
  61. bool LoadFile(const char* filename) {
  62. FILE* fptr = fopen(filename, "r");
  63. if (fptr == nullptr) {
  64. return false;
  65. };
  66. FscanfOrDie(fptr, "%d", &num_cameras_);
  67. FscanfOrDie(fptr, "%d", &num_points_);
  68. FscanfOrDie(fptr, "%d", &num_observations_);
  69. point_index_ = new int[num_observations_];
  70. camera_index_ = new int[num_observations_];
  71. observations_ = new double[2 * num_observations_];
  72. num_parameters_ = 9 * num_cameras_ + 3 * num_points_;
  73. parameters_ = new double[num_parameters_];
  74. for (int i = 0; i < num_observations_; ++i) {
  75. FscanfOrDie(fptr, "%d", camera_index_ + i);
  76. FscanfOrDie(fptr, "%d", point_index_ + i);
  77. for (int j = 0; j < 2; ++j) {
  78. FscanfOrDie(fptr, "%lf", observations_ + 2 * i + j);
  79. }
  80. }
  81. for (int i = 0; i < num_parameters_; ++i) {
  82. FscanfOrDie(fptr, "%lf", parameters_ + i);
  83. }
  84. return true;
  85. }
  86. private:
  87. template <typename T>
  88. void FscanfOrDie(FILE* fptr, const char* format, T* value) {
  89. int num_scanned = fscanf(fptr, format, value);
  90. if (num_scanned != 1) {
  91. LOG(FATAL) << "Invalid UW data file.";
  92. }
  93. }
  94. int num_cameras_;
  95. int num_points_;
  96. int num_observations_;
  97. int num_parameters_;
  98. int* point_index_;
  99. int* camera_index_;
  100. double* observations_;
  101. double* parameters_;
  102. };
  103. // Templated pinhole camera model for used with Ceres. The camera is
  104. // parameterized using 9 parameters: 3 for rotation, 3 for translation, 1 for
  105. // focal length and 2 for radial distortion. The principal point is not modeled
  106. // (i.e. it is assumed be located at the image center).
  107. struct SnavelyReprojectionError {
  108. SnavelyReprojectionError(double observed_x, double observed_y)
  109. : observed_x(observed_x), observed_y(observed_y) {}
  110. template <typename T>
  111. bool operator()(const T* const camera,
  112. const T* const point,
  113. T* residuals) const {
  114. // camera[0,1,2] are the angle-axis rotation.
  115. T p[3];
  116. ceres::AngleAxisRotatePoint(camera, point, p);
  117. // camera[3,4,5] are the translation.
  118. p[0] += camera[3];
  119. p[1] += camera[4];
  120. p[2] += camera[5];
  121. // Compute the center of distortion. The sign change comes from
  122. // the camera model that Noah Snavely's Bundler assumes, whereby
  123. // the camera coordinate system has a negative z axis.
  124. T xp = -p[0] / p[2];
  125. T yp = -p[1] / p[2];
  126. // Apply second and fourth order radial distortion.
  127. const T& l1 = camera[7];
  128. const T& l2 = camera[8];
  129. T r2 = xp * xp + yp * yp;
  130. T distortion = 1.0 + r2 * (l1 + l2 * r2);
  131. // Compute final projected point position.
  132. const T& focal = camera[6];
  133. T predicted_x = focal * distortion * xp;
  134. T predicted_y = focal * distortion * yp;
  135. // The error is the difference between the predicted and observed position.
  136. residuals[0] = predicted_x - observed_x;
  137. residuals[1] = predicted_y - observed_y;
  138. return true;
  139. }
  140. // Factory to hide the construction of the CostFunction object from
  141. // the client code.
  142. static ceres::CostFunction* Create(const double observed_x,
  143. const double observed_y) {
  144. return (new ceres::AutoDiffCostFunction<SnavelyReprojectionError, 2, 9, 3>(
  145. new SnavelyReprojectionError(observed_x, observed_y)));
  146. }
  147. double observed_x;
  148. double observed_y;
  149. };
  150. int main(int argc, char** argv) {
  151. google::InitGoogleLogging(argv[0]);
  152. if (argc != 2) {
  153. std::cerr << "usage: simple_bundle_adjuster <bal_problem>\n";
  154. return 1;
  155. }
  156. BALProblem bal_problem;
  157. if (!bal_problem.LoadFile(argv[1])) {
  158. std::cerr << "ERROR: unable to open file " << argv[1] << "\n";
  159. return 1;
  160. }
  161. const double* observations = bal_problem.observations();
  162. // Create residuals for each observation in the bundle adjustment problem. The
  163. // parameters for cameras and points are added automatically.
  164. ceres::Problem problem;
  165. for (int i = 0; i < bal_problem.num_observations(); ++i) {
  166. // Each Residual block takes a point and a camera as input and outputs a 2
  167. // dimensional residual. Internally, the cost function stores the observed
  168. // image location and compares the reprojection against the observation.
  169. ceres::CostFunction* cost_function = SnavelyReprojectionError::Create(
  170. observations[2 * i + 0], observations[2 * i + 1]);
  171. problem.AddResidualBlock(cost_function,
  172. nullptr /* squared loss */,
  173. bal_problem.mutable_camera_for_observation(i),
  174. bal_problem.mutable_point_for_observation(i));
  175. }
  176. // Make Ceres automatically detect the bundle structure. Note that the
  177. // standard solver, SPARSE_NORMAL_CHOLESKY, also works fine but it is slower
  178. // for standard bundle adjustment problems.
  179. ceres::Solver::Options options;
  180. options.linear_solver_type = ceres::DENSE_SCHUR;
  181. options.minimizer_progress_to_stdout = true;
  182. ceres::Solver::Summary summary;
  183. ceres::Solve(options, &problem, &summary);
  184. std::cout << summary.FullReport() << "\n";
  185. return 0;
  186. }