Blame examples/snavely_reprojection_error.h

Packit ea1746
// Ceres Solver - A fast non-linear least squares minimizer
Packit ea1746
// Copyright 2015 Google Inc. All rights reserved.
Packit ea1746
// http://ceres-solver.org/
Packit ea1746
//
Packit ea1746
// Redistribution and use in source and binary forms, with or without
Packit ea1746
// modification, are permitted provided that the following conditions are met:
Packit ea1746
//
Packit ea1746
// * Redistributions of source code must retain the above copyright notice,
Packit ea1746
//   this list of conditions and the following disclaimer.
Packit ea1746
// * Redistributions in binary form must reproduce the above copyright notice,
Packit ea1746
//   this list of conditions and the following disclaimer in the documentation
Packit ea1746
//   and/or other materials provided with the distribution.
Packit ea1746
// * Neither the name of Google Inc. nor the names of its contributors may be
Packit ea1746
//   used to endorse or promote products derived from this software without
Packit ea1746
//   specific prior written permission.
Packit ea1746
//
Packit ea1746
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit ea1746
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit ea1746
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit ea1746
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Packit ea1746
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Packit ea1746
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Packit ea1746
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit ea1746
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Packit ea1746
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit ea1746
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Packit ea1746
// POSSIBILITY OF SUCH DAMAGE.
Packit ea1746
//
Packit ea1746
// Author: sameeragarwal@google.com (Sameer Agarwal)
Packit ea1746
//
Packit ea1746
// Templated struct implementing the camera model and residual
Packit ea1746
// computation for bundle adjustment used by Noah Snavely's Bundler
Packit ea1746
// SfM system. This is also the camera model/residual for the bundle
Packit ea1746
// adjustment problems in the BAL dataset. It is templated so that we
Packit ea1746
// can use Ceres's automatic differentiation to compute analytic
Packit ea1746
// jacobians.
Packit ea1746
//
Packit ea1746
// For details see: http://phototour.cs.washington.edu/bundler/
Packit ea1746
// and http://grail.cs.washington.edu/projects/bal/
Packit ea1746
Packit ea1746
#ifndef CERES_EXAMPLES_SNAVELY_REPROJECTION_ERROR_H_
Packit ea1746
#define CERES_EXAMPLES_SNAVELY_REPROJECTION_ERROR_H_
Packit ea1746
Packit ea1746
#include "ceres/rotation.h"
Packit ea1746
Packit ea1746
namespace ceres {
Packit ea1746
namespace examples {
Packit ea1746
Packit ea1746
// Templated pinhole camera model for used with Ceres.  The camera is
Packit ea1746
// parameterized using 9 parameters: 3 for rotation, 3 for translation, 1 for
Packit ea1746
// focal length and 2 for radial distortion. The principal point is not modeled
Packit ea1746
// (i.e. it is assumed be located at the image center).
Packit ea1746
struct SnavelyReprojectionError {
Packit ea1746
  SnavelyReprojectionError(double observed_x, double observed_y)
Packit ea1746
      : observed_x(observed_x), observed_y(observed_y) {}
Packit ea1746
Packit ea1746
  template <typename T>
Packit ea1746
  bool operator()(const T* const camera,
Packit ea1746
                  const T* const point,
Packit ea1746
                  T* residuals) const {
Packit ea1746
    // camera[0,1,2] are the angle-axis rotation.
Packit ea1746
    T p[3];
Packit ea1746
    AngleAxisRotatePoint(camera, point, p);
Packit ea1746
Packit ea1746
    // camera[3,4,5] are the translation.
Packit ea1746
    p[0] += camera[3];
Packit ea1746
    p[1] += camera[4];
Packit ea1746
    p[2] += camera[5];
Packit ea1746
Packit ea1746
    // Compute the center of distortion. The sign change comes from
Packit ea1746
    // the camera model that Noah Snavely's Bundler assumes, whereby
Packit ea1746
    // the camera coordinate system has a negative z axis.
Packit ea1746
    const T xp = - p[0] / p[2];
Packit ea1746
    const T yp = - p[1] / p[2];
Packit ea1746
Packit ea1746
    // Apply second and fourth order radial distortion.
Packit ea1746
    const T& l1 = camera[7];
Packit ea1746
    const T& l2 = camera[8];
Packit ea1746
    const T r2 = xp*xp + yp*yp;
Packit ea1746
    const T distortion = 1.0 + r2  * (l1 + l2  * r2);
Packit ea1746
Packit ea1746
Packit ea1746
    // Compute final projected point position.
Packit ea1746
    const T& focal = camera[6];
Packit ea1746
    const T predicted_x = focal * distortion * xp;
Packit ea1746
    const T predicted_y = focal * distortion * yp;
Packit ea1746
Packit ea1746
    // The error is the difference between the predicted and observed position.
Packit ea1746
    residuals[0] = predicted_x - observed_x;
Packit ea1746
    residuals[1] = predicted_y - observed_y;
Packit ea1746
Packit ea1746
    return true;
Packit ea1746
  }
Packit ea1746
Packit ea1746
  // Factory to hide the construction of the CostFunction object from
Packit ea1746
  // the client code.
Packit ea1746
  static ceres::CostFunction* Create(const double observed_x,
Packit ea1746
                                     const double observed_y) {
Packit ea1746
    return (new ceres::AutoDiffCostFunction<SnavelyReprojectionError, 2, 9, 3>(
Packit ea1746
                new SnavelyReprojectionError(observed_x, observed_y)));
Packit ea1746
  }
Packit ea1746
Packit ea1746
  double observed_x;
Packit ea1746
  double observed_y;
Packit ea1746
};
Packit ea1746
Packit ea1746
// Templated pinhole camera model for used with Ceres.  The camera is
Packit ea1746
// parameterized using 10 parameters. 4 for rotation, 3 for
Packit ea1746
// translation, 1 for focal length and 2 for radial distortion. The
Packit ea1746
// principal point is not modeled (i.e. it is assumed be located at
Packit ea1746
// the image center).
Packit ea1746
struct SnavelyReprojectionErrorWithQuaternions {
Packit ea1746
  // (u, v): the position of the observation with respect to the image
Packit ea1746
  // center point.
Packit ea1746
  SnavelyReprojectionErrorWithQuaternions(double observed_x, double observed_y)
Packit ea1746
      : observed_x(observed_x), observed_y(observed_y) {}
Packit ea1746
Packit ea1746
  template <typename T>
Packit ea1746
  bool operator()(const T* const camera,
Packit ea1746
                  const T* const point,
Packit ea1746
                  T* residuals) const {
Packit ea1746
    // camera[0,1,2,3] is are the rotation of the camera as a quaternion.
Packit ea1746
    //
Packit ea1746
    // We use QuaternionRotatePoint as it does not assume that the
Packit ea1746
    // quaternion is normalized, since one of the ways to run the
Packit ea1746
    // bundle adjuster is to let Ceres optimize all 4 quaternion
Packit ea1746
    // parameters without a local parameterization.
Packit ea1746
    T p[3];
Packit ea1746
    QuaternionRotatePoint(camera, point, p);
Packit ea1746
Packit ea1746
    p[0] += camera[4];
Packit ea1746
    p[1] += camera[5];
Packit ea1746
    p[2] += camera[6];
Packit ea1746
Packit ea1746
    // Compute the center of distortion. The sign change comes from
Packit ea1746
    // the camera model that Noah Snavely's Bundler assumes, whereby
Packit ea1746
    // the camera coordinate system has a negative z axis.
Packit ea1746
    const T xp = - p[0] / p[2];
Packit ea1746
    const T yp = - p[1] / p[2];
Packit ea1746
Packit ea1746
    // Apply second and fourth order radial distortion.
Packit ea1746
    const T& l1 = camera[8];
Packit ea1746
    const T& l2 = camera[9];
Packit ea1746
Packit ea1746
    const T r2 = xp*xp + yp*yp;
Packit ea1746
    const T distortion = 1.0 + r2  * (l1 + l2  * r2);
Packit ea1746
Packit ea1746
    // Compute final projected point position.
Packit ea1746
    const T& focal = camera[7];
Packit ea1746
    const T predicted_x = focal * distortion * xp;
Packit ea1746
    const T predicted_y = focal * distortion * yp;
Packit ea1746
Packit ea1746
    // The error is the difference between the predicted and observed position.
Packit ea1746
    residuals[0] = predicted_x - observed_x;
Packit ea1746
    residuals[1] = predicted_y - observed_y;
Packit ea1746
Packit ea1746
    return true;
Packit ea1746
  }
Packit ea1746
Packit ea1746
  // Factory to hide the construction of the CostFunction object from
Packit ea1746
  // the client code.
Packit ea1746
  static ceres::CostFunction* Create(const double observed_x,
Packit ea1746
                                     const double observed_y) {
Packit ea1746
    return (new ceres::AutoDiffCostFunction<
Packit ea1746
            SnavelyReprojectionErrorWithQuaternions, 2, 10, 3>(
Packit ea1746
                new SnavelyReprojectionErrorWithQuaternions(observed_x,
Packit ea1746
                                                            observed_y)));
Packit ea1746
  }
Packit ea1746
Packit ea1746
  double observed_x;
Packit ea1746
  double observed_y;
Packit ea1746
};
Packit ea1746
Packit ea1746
}  // namespace examples
Packit ea1746
}  // namespace ceres
Packit ea1746
Packit ea1746
#endif  // CERES_EXAMPLES_SNAVELY_REPROJECTION_ERROR_H_