Cal3Fisheye.cpp
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
19 #include <gtsam/base/Matrix.h>
20 #include <gtsam/base/Vector.h>
22 #include <gtsam/geometry/Point2.h>
23 #include <gtsam/geometry/Point3.h>
24 
25 namespace gtsam {
26 
27 /* ************************************************************************* */
28 Vector9 Cal3Fisheye::vector() const {
29  Vector9 v;
30  v << fx_, fy_, s_, u0_, v0_, k1_, k2_, k3_, k4_;
31  return v;
32 }
33 
34 /* ************************************************************************* */
35 double Cal3Fisheye::Scaling(double r) {
36  static constexpr double threshold = 1e-8;
37  if (r > threshold || r < -threshold) {
38  return atan(r) / r;
39  } else {
40  // Taylor expansion close to 0
41  double r2 = r * r, r4 = r2 * r2;
42  return 1.0 - r2 / 3 + r4 / 5;
43  }
44 }
45 
46 /* ************************************************************************* */
48  OptionalJacobian<2, 2> H2) const {
49  const double xi = p.x(), yi = p.y();
50  const double r2 = xi * xi + yi * yi, r = sqrt(r2);
51  const double t = atan(r);
52  const double t2 = t * t, t4 = t2 * t2, t6 = t2 * t4, t8 = t4 * t4;
53  Vector5 K, T;
54  K << 1, k1_, k2_, k3_, k4_;
55  T << 1, t2, t4, t6, t8;
56  const double scaling = Scaling(r);
57  const double s = scaling * K.dot(T);
58  const double xd = s * xi, yd = s * yi;
59  Point2 uv(fx_ * xd + s_ * yd + u0_, fy_ * yd + v0_);
60 
61  Matrix2 DK;
62  if (H1 || H2) DK << fx_, s_, 0.0, fy_;
63 
64  // Derivative for calibration parameters (2 by 9)
65  if (H1) {
66  Matrix25 DR1;
67  // order: fx, fy, s, u0, v0
68  DR1 << xd, 0.0, yd, 1.0, 0.0, 0.0, yd, 0.0, 0.0, 1.0;
69 
70  // order: k1, k2, k3, k4
71  Matrix24 DR2;
72  auto T4 = T.tail<4>().transpose();
73  DR2 << xi * T4, yi * T4;
74  *H1 << DR1, DK * scaling * DR2;
75  }
76 
77  // Derivative for points in intrinsic coords (2 by 2)
78  if (H2) {
79  const double dtd_dt =
80  1 + 3 * k1_ * t2 + 5 * k2_ * t4 + 7 * k3_ * t6 + 9 * k4_ * t8;
81  const double dt_dr = 1 / (1 + r2);
82  const double rinv = 1 / r;
83  const double dr_dxi = xi * rinv;
84  const double dr_dyi = yi * rinv;
85  const double dtd_dxi = dtd_dt * dt_dr * dr_dxi;
86  const double dtd_dyi = dtd_dt * dt_dr * dr_dyi;
87 
88  const double td = t * K.dot(T);
89  const double rrinv = 1 / r2;
90  const double dxd_dxi =
91  dtd_dxi * dr_dxi + td * rinv - td * xi * rrinv * dr_dxi;
92  const double dxd_dyi = dtd_dyi * dr_dxi - td * xi * rrinv * dr_dyi;
93  const double dyd_dxi = dtd_dxi * dr_dyi - td * yi * rrinv * dr_dxi;
94  const double dyd_dyi =
95  dtd_dyi * dr_dyi + td * rinv - td * yi * rrinv * dr_dyi;
96 
97  Matrix2 DR;
98  DR << dxd_dxi, dxd_dyi, dyd_dxi, dyd_dyi;
99 
100  *H2 = DK * DR;
101  }
102 
103  return uv;
104 }
105 
106 /* ************************************************************************* */
108  OptionalJacobian<2, 2> Dp) const {
109  // initial gues just inverts the pinhole model
110  const double u = uv.x(), v = uv.y();
111  const double yd = (v - v0_) / fy_;
112  const double xd = (u - s_ * yd - u0_) / fx_;
113  Point2 pi(xd, yd);
114 
115  // Perform newtons method, break when solution converges past tol_,
116  // throw exception if max iterations are reached
117  const int maxIterations = 10;
118  int iteration;
119  for (iteration = 0; iteration < maxIterations; ++iteration) {
120  Matrix2 jac;
121 
122  // Calculate the current estimate (uv_hat) and the jacobian
123  const Point2 uv_hat = uncalibrate(pi, boost::none, jac);
124 
125  // Test convergence
126  if ((uv_hat - uv).norm() < tol_) break;
127 
128  // Newton's method update step
129  pi = pi - jac.inverse() * (uv_hat - uv);
130  }
131 
132  if (iteration >= maxIterations)
133  throw std::runtime_error(
134  "Cal3Fisheye::calibrate fails to converge. need a better "
135  "initialization");
136 
137  calibrateJacobians<Cal3Fisheye, dimension>(*this, pi, Dcal, Dp);
138 
139  return pi;
140 }
141 
142 /* ************************************************************************* */
143 std::ostream& operator<<(std::ostream& os, const Cal3Fisheye& cal) {
144  os << (Cal3&)cal;
145  os << ", k1: " << cal.k1() << ", k2: " << cal.k2() << ", k3: " << cal.k3()
146  << ", k4: " << cal.k4();
147  return os;
148 }
149 
150 /* ************************************************************************* */
151 void Cal3Fisheye::print(const std::string& s_) const {
152  gtsam::print((Matrix)K(), s_ + ".K");
153  gtsam::print(Vector(k()), s_ + ".k");
154 }
155 
156 /* ************************************************************************* */
157 bool Cal3Fisheye::equals(const Cal3Fisheye& K, double tol) const {
158  const Cal3* base = dynamic_cast<const Cal3*>(&K);
159  return Cal3::equals(*base, tol) && std::fabs(k1_ - K.k1_) < tol &&
160  std::fabs(k2_ - K.k2_) < tol && std::fabs(k3_ - K.k3_) < tol &&
161  std::fabs(k4_ - K.k4_) < tol;
162 }
163 
164 /* ************************************************************************* */
165 
166 } // \ namespace gtsam
void print(const Matrix &A, const string &s, ostream &stream)
Definition: Matrix.cpp:155
static double Scaling(double r)
Helper function that calculates atan(r)/r.
Definition: Cal3Fisheye.cpp:35
double k4_
fisheye distortion coefficients
Definition: Cal3Fisheye.h:52
void print(const std::string &s="") const override
print with optional string
double k2_
fisheye distortion coefficients
Definition: Cal3Fisheye.h:51
double k2() const
Second distortion coefficient.
Definition: Cal3Fisheye.h:97
double tol_
tolerance value when calibrating
Definition: Cal3Fisheye.h:53
Vector2 Point2
Definition: Point2.h:27
ArrayXcf v
Definition: Cwise_arg.cpp:1
Eigen::MatrixXd Matrix
Definition: base/Matrix.h:43
GTSAM_EXPORT friend std::ostream & operator<<(std::ostream &os, const Cal3Fisheye &cal)
Output stream operator.
EIGEN_DEVICE_FUNC const SqrtReturnType sqrt() const
double u0_
Definition: Cal3.h:73
double k4() const
Second tangential distortion coefficient.
Definition: Cal3Fisheye.h:103
double k1() const
First distortion coefficient.
Definition: Cal3Fisheye.h:94
Real fabs(const Real &a)
virtual Matrix3 K() const
return calibration matrix K
Definition: Cal3.h:167
static const Similarity3 T4(R, P, s)
Point2 calibrate(const Point2 &p, OptionalJacobian< 2, 9 > Dcal=boost::none, OptionalJacobian< 2, 2 > Dp=boost::none) const
Eigen::VectorXd Vector
Definition: Vector.h:38
double fy_
focal length
Definition: Cal3.h:71
Eigen::Triplet< double > T
EIGEN_DEVICE_FUNC const AtanReturnType atan() const
double s_
skew
Definition: Cal3.h:72
Array< double, 1, 3 > e(1./3., 0.5, 2.)
RealScalar s
static const double r2
bool equals(const Cal3 &K, double tol=10e-9) const
Check if equal up to specified tolerance.
Definition: Cal3.cpp:59
Expression< Point2 > uv_hat(uncalibrate< Cal3_S2 >, K, projection)
double v0_
principal point
Definition: Cal3.h:73
Vector xi
Definition: testPose2.cpp:150
double k3() const
First tangential distortion coefficient.
Definition: Cal3Fisheye.h:100
Vector9 vector() const
Return all parameters as a vector.
Definition: Cal3Fisheye.cpp:28
traits
Definition: chartTesting.h:28
typedef and functions to augment Eigen&#39;s VectorXd
ofstream os("timeSchurFactors.csv")
bool equals(const Cal3Fisheye &K, double tol=10e-9) const
assert equality up to a tolerance
3D Point
float * p
Point2 uncalibrate(const Point2 &p, OptionalJacobian< 2, 9 > Dcal=boost::none, OptionalJacobian< 2, 2 > Dp=boost::none) const
convert intrinsic coordinates [x_i; y_i] to (distorted) image coordinates [u; v]
Definition: Cal3Fisheye.cpp:47
Calibration of a fisheye camera.
Annotation indicating that a class derives from another given type.
Definition: attr.h:42
const G double tol
Definition: Group.h:83
double fx_
Definition: Cal3.h:71
2D Point
Vector4 k() const
return distortion parameter vector
Definition: Cal3Fisheye.h:106
Point2 t(10, 10)


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:41:44