snavely_reprojection_error.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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: sameeragarwal@google.com (Sameer Agarwal)
  30. //
  31. // Templated struct implementing the camera model and residual
  32. // computation for bundle adjustment used by Noah Snavely's Bundler
  33. // SfM system. This is also the camera model/residual for the bundle
  34. // adjustment problems in the BAL dataset. It is templated so that we
  35. // can use Ceres's automatic differentiation to compute analytic
  36. // jacobians.
  37. //
  38. // For details see: http://phototour.cs.washington.edu/bundler/
  39. // and http://grail.cs.washington.edu/projects/bal/
  40. #ifndef CERES_EXAMPLES_SNAVELY_REPROJECTION_ERROR_H_
  41. #define CERES_EXAMPLES_SNAVELY_REPROJECTION_ERROR_H_
  42. #include "ceres/rotation.h"
  43. namespace ceres {
  44. namespace examples {
  45. // Templated pinhole camera model for used with Ceres. The camera is
  46. // parameterized using 9 parameters: 3 for rotation, 3 for translation, 1 for
  47. // focal length and 2 for radial distortion. The principal point is not modeled
  48. // (i.e. it is assumed be located at the image center).
  49. struct SnavelyReprojectionError {
  50. SnavelyReprojectionError(double observed_x, double observed_y)
  51. : observed_x(observed_x), observed_y(observed_y) {}
  52. template <typename T>
  53. bool operator()(const T* const camera,
  54. const T* const point,
  55. T* residuals) const {
  56. // camera[0,1,2] are the angle-axis rotation.
  57. T p[3];
  58. AngleAxisRotatePoint(camera, point, p);
  59. // camera[3,4,5] are the translation.
  60. p[0] += camera[3];
  61. p[1] += camera[4];
  62. p[2] += camera[5];
  63. // Compute the center of distortion. The sign change comes from
  64. // the camera model that Noah Snavely's Bundler assumes, whereby
  65. // the camera coordinate system has a negative z axis.
  66. const T xp = -p[0] / p[2];
  67. const T yp = -p[1] / p[2];
  68. // Apply second and fourth order radial distortion.
  69. const T& l1 = camera[7];
  70. const T& l2 = camera[8];
  71. const T r2 = xp * xp + yp * yp;
  72. const T distortion = 1.0 + r2 * (l1 + l2 * r2);
  73. // Compute final projected point position.
  74. const T& focal = camera[6];
  75. const T predicted_x = focal * distortion * xp;
  76. const T predicted_y = focal * distortion * yp;
  77. // The error is the difference between the predicted and observed position.
  78. residuals[0] = predicted_x - observed_x;
  79. residuals[1] = predicted_y - observed_y;
  80. return true;
  81. }
  82. // Factory to hide the construction of the CostFunction object from
  83. // the client code.
  84. static ceres::CostFunction* Create(const double observed_x,
  85. const double observed_y) {
  86. return (new ceres::AutoDiffCostFunction<SnavelyReprojectionError, 2, 9, 3>(
  87. new SnavelyReprojectionError(observed_x, observed_y)));
  88. }
  89. double observed_x;
  90. double observed_y;
  91. };
  92. // Templated pinhole camera model for used with Ceres. The camera is
  93. // parameterized using 10 parameters. 4 for rotation, 3 for
  94. // translation, 1 for focal length and 2 for radial distortion. The
  95. // principal point is not modeled (i.e. it is assumed be located at
  96. // the image center).
  97. struct SnavelyReprojectionErrorWithQuaternions {
  98. // (u, v): the position of the observation with respect to the image
  99. // center point.
  100. SnavelyReprojectionErrorWithQuaternions(double observed_x, double observed_y)
  101. : observed_x(observed_x), observed_y(observed_y) {}
  102. template <typename T>
  103. bool operator()(const T* const camera,
  104. const T* const point,
  105. T* residuals) const {
  106. // camera[0,1,2,3] is are the rotation of the camera as a quaternion.
  107. //
  108. // We use QuaternionRotatePoint as it does not assume that the
  109. // quaternion is normalized, since one of the ways to run the
  110. // bundle adjuster is to let Ceres optimize all 4 quaternion
  111. // parameters without using a Quaternion manifold.
  112. T p[3];
  113. QuaternionRotatePoint(camera, point, p);
  114. p[0] += camera[4];
  115. p[1] += camera[5];
  116. p[2] += camera[6];
  117. // Compute the center of distortion. The sign change comes from
  118. // the camera model that Noah Snavely's Bundler assumes, whereby
  119. // the camera coordinate system has a negative z axis.
  120. const T xp = -p[0] / p[2];
  121. const T yp = -p[1] / p[2];
  122. // Apply second and fourth order radial distortion.
  123. const T& l1 = camera[8];
  124. const T& l2 = camera[9];
  125. const T r2 = xp * xp + yp * yp;
  126. const T distortion = 1.0 + r2 * (l1 + l2 * r2);
  127. // Compute final projected point position.
  128. const T& focal = camera[7];
  129. const T predicted_x = focal * distortion * xp;
  130. const T predicted_y = focal * distortion * yp;
  131. // The error is the difference between the predicted and observed position.
  132. residuals[0] = predicted_x - observed_x;
  133. residuals[1] = predicted_y - observed_y;
  134. return true;
  135. }
  136. // Factory to hide the construction of the CostFunction object from
  137. // the client code.
  138. static ceres::CostFunction* Create(const double observed_x,
  139. const double observed_y) {
  140. return (
  141. new ceres::AutoDiffCostFunction<SnavelyReprojectionErrorWithQuaternions,
  142. 2,
  143. 10,
  144. 3>(
  145. new SnavelyReprojectionErrorWithQuaternions(observed_x,
  146. observed_y)));
  147. }
  148. double observed_x;
  149. double observed_y;
  150. };
  151. } // namespace examples
  152. } // namespace ceres
  153. #endif // CERES_EXAMPLES_SNAVELY_REPROJECTION_ERROR_H_