Cal3DS2_Base.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 Cal3DS2_Base::vector() const {
29  Vector9 v;
30  v << fx_, fy_, s_, u0_, v0_, k1_, k2_, p1_, p2_;
31  return v;
32 }
33 
34 /* ************************************************************************* */
35 std::ostream& operator<<(std::ostream& os, const Cal3DS2_Base& cal) {
36  os << (Cal3&)cal;
37  os << ", k1: " << cal.k1() << ", k2: " << cal.k2() << ", p1: " << cal.p1()
38  << ", p2: " << cal.p2();
39  return os;
40 }
41 
42 /* ************************************************************************* */
43 void Cal3DS2_Base::print(const std::string& s) const {
44  gtsam::print((Matrix)K(), s + ".K");
45  gtsam::print(Vector(k()), s + ".k");
46 }
47 
48 /* ************************************************************************* */
49 bool Cal3DS2_Base::equals(const Cal3DS2_Base& K, double tol) const {
50  const Cal3* base = dynamic_cast<const Cal3*>(&K);
51  return Cal3::equals(*base, tol) && std::fabs(k1_ - K.k1_) < tol &&
52  std::fabs(k2_ - K.k2_) < tol && std::fabs(p1_ - K.p1_) < tol &&
53  std::fabs(p2_ - K.p2_) < tol;
54 }
55 
56 /* ************************************************************************* */
57 static Matrix29 D2dcalibration(double x, double y, double xx, double yy,
58  double xy, double rr, double r4, double pnx,
59  double pny, const Matrix2& DK) {
60  Matrix25 DR1;
61  DR1 << pnx, 0.0, pny, 1.0, 0.0, 0.0, pny, 0.0, 0.0, 1.0;
62  Matrix24 DR2;
63  DR2 << x * rr, x * r4, 2 * xy, rr + 2 * xx, //
64  y * rr, y * r4, rr + 2 * yy, 2 * xy;
65  Matrix29 D;
66  D << DR1, DK * DR2;
67  return D;
68 }
69 
70 /* ************************************************************************* */
71 static Matrix2 D2dintrinsic(double x, double y, double rr, double g, double k1,
72  double k2, double p1, double p2,
73  const Matrix2& DK) {
74  const double drdx = 2. * x;
75  const double drdy = 2. * y;
76  const double dgdx = k1 * drdx + k2 * 2. * rr * drdx;
77  const double dgdy = k1 * drdy + k2 * 2. * rr * drdy;
78 
79  // Dx = 2*p1*xy + p2*(rr+2*xx);
80  // Dy = 2*p2*xy + p1*(rr+2*yy);
81  const double dDxdx = 2. * p1 * y + p2 * (drdx + 4. * x);
82  const double dDxdy = 2. * p1 * x + p2 * drdy;
83  const double dDydx = 2. * p2 * y + p1 * drdx;
84  const double dDydy = 2. * p2 * x + p1 * (drdy + 4. * y);
85 
86  Matrix2 DR;
87  DR << g + x * dgdx + dDxdx, x * dgdy + dDxdy, //
88  y * dgdx + dDydx, g + y * dgdy + dDydy;
89 
90  return DK * DR;
91 }
92 
93 /* ************************************************************************* */
95  OptionalJacobian<2, 2> Dp) const {
96  // r² = x² + y²;
97  // g = (1 + k(1)*r² + k(2)*r⁴);
98  // dp = [2*k(3)*x*y + k(4)*(r² + 2*x²); 2*k(4)*x*y + k(3)*(r² + 2*y²)];
99  // pi(:,i) = g * pn(:,i) + dp;
100  const double x = p.x(), y = p.y(), xy = x * y, xx = x * x, yy = y * y;
101  const double rr = xx + yy;
102  const double r4 = rr * rr;
103  const double g = 1. + k1_ * rr + k2_ * r4; // scaling factor
104 
105  // tangential component
106  const double dx = 2. * p1_ * xy + p2_ * (rr + 2. * xx);
107  const double dy = 2. * p2_ * xy + p1_ * (rr + 2. * yy);
108 
109  // Radial and tangential distortion applied
110  const double pnx = g * x + dx;
111  const double pny = g * y + dy;
112 
113  Matrix2 DK;
114  if (Dcal || Dp) {
115  DK << fx_, s_, 0.0, fy_;
116  }
117 
118  // Derivative for calibration
119  if (Dcal) {
120  *Dcal = D2dcalibration(x, y, xx, yy, xy, rr, r4, pnx, pny, DK);
121  }
122 
123  // Derivative for points
124  if (Dp) {
125  *Dp = D2dintrinsic(x, y, rr, g, k1_, k2_, p1_, p2_, DK);
126  }
127 
128  // Regular uncalibrate after distortion
129  return Point2(fx_ * pnx + s_ * pny + u0_, fy_ * pny + v0_);
130 }
131 
132 /* ************************************************************************* */
134  OptionalJacobian<2, 2> Dp) const {
135  // Use the following fixed point iteration to invert the radial distortion.
136  // pn_{t+1} = (inv(K)*pi - dp(pn_{t})) / g(pn_{t})
137 
138  const Point2 invKPi((1 / fx_) * (pi.x() - u0_ - (s_ / fy_) * (pi.y() - v0_)),
139  (1 / fy_) * (pi.y() - v0_));
140 
141  // initialize by ignoring the distortion at all, might be problematic for
142  // pixels around boundary
143  Point2 pn = invKPi;
144 
145  // iterate until the uncalibrate is close to the actual pixel coordinate
146  const int maxIterations = 10;
147  int iteration;
148  for (iteration = 0; iteration < maxIterations; ++iteration) {
149  if (distance2(uncalibrate(pn), pi) <= tol_) break;
150  const double px = pn.x(), py = pn.y(), xy = px * py, xx = px * px,
151  yy = py * py;
152  const double rr = xx + yy;
153  const double g = (1 + k1_ * rr + k2_ * rr * rr);
154  const double dx = 2 * p1_ * xy + p2_ * (rr + 2 * xx);
155  const double dy = 2 * p2_ * xy + p1_ * (rr + 2 * yy);
156  pn = (invKPi - Point2(dx, dy)) / g;
157  }
158 
159  if (iteration >= maxIterations)
160  throw std::runtime_error(
161  "Cal3DS2::calibrate fails to converge. need a better initialization");
162 
163  calibrateJacobians<Cal3DS2_Base, dimension>(*this, pn, Dcal, Dp);
164 
165  return pn;
166 }
167 
168 /* ************************************************************************* */
169 Matrix2 Cal3DS2_Base::D2d_intrinsic(const Point2& p) const {
170  const double x = p.x(), y = p.y(), xx = x * x, yy = y * y;
171  const double rr = xx + yy;
172  const double r4 = rr * rr;
173  const double g = (1 + k1_ * rr + k2_ * r4);
174  Matrix2 DK;
175  DK << fx_, s_, 0.0, fy_;
176  return D2dintrinsic(x, y, rr, g, k1_, k2_, p1_, p2_, DK);
177 }
178 
179 /* ************************************************************************* */
180 Matrix29 Cal3DS2_Base::D2d_calibration(const Point2& p) const {
181  const double x = p.x(), y = p.y(), xx = x * x, yy = y * y, xy = x * y;
182  const double rr = xx + yy;
183  const double r4 = rr * rr;
184  const double g = (1 + k1_ * rr + k2_ * r4);
185  const double dx = 2 * p1_ * xy + p2_ * (rr + 2 * xx);
186  const double dy = 2 * p2_ * xy + p1_ * (rr + 2 * yy);
187  const double pnx = g * x + dx;
188  const double pny = g * y + dy;
189  Matrix2 DK;
190  DK << fx_, s_, 0.0, fy_;
191  return D2dcalibration(x, y, xx, yy, xy, rr, r4, pnx, pny, DK);
192 }
193 }
194 /* ************************************************************************* */
void print(const Matrix &A, const string &s, ostream &stream)
Definition: Matrix.cpp:155
Vector4 k() const
return distortion parameter vector
Definition: Cal3DS2_Base.h:113
Scalar * y
GTSAM_EXPORT friend std::ostream & operator<<(std::ostream &os, const Cal3DS2_Base &cal)
Output stream operator.
Vector2 Point2
Definition: Point2.h:32
double p2() const
Second tangential distortion coefficient.
Definition: Cal3DS2_Base.h:110
double p2_
tangential distortion
Definition: Cal3DS2_Base.h:45
Eigen::MatrixXd Matrix
Definition: base/Matrix.h:39
static Matrix2 D2dintrinsic(double x, double y, double rr, double g, double k1, double k2, double p1, double p2, const Matrix2 &DK)
Calibration of a camera with radial distortion.
Definition: Cal3DS2_Base.h:42
double u0_
Definition: Cal3.h:73
double py() const
image center in y
Definition: Cal3.h:154
Matrix< float, 2, 1 > xy
Definition: LLT_solve.cpp:7
Real fabs(const Real &a)
void g(const string &key, int i)
Definition: testBTree.cpp:41
Common base class for all calibration models.
Definition: Cal3.h:69
static Matrix29 D2dcalibration(double x, double y, double xx, double yy, double xy, double rr, double r4, double pnx, double pny, const Matrix2 &DK)
Matrix2 D2d_intrinsic(const Point2 &p) const
Derivative of uncalibrate wrpt intrinsic coordinates.
Eigen::VectorXd Vector
Definition: Vector.h:38
double k2_
radial 2nd-order and 4th-order
Definition: Cal3DS2_Base.h:44
double fy_
focal length
Definition: Cal3.h:71
bool equals(const Cal3 &K, double tol=10e-9) const
Check if equal up to specified tolerance.
Definition: Cal3.cpp:59
Array< int, Dynamic, 1 > v
double s_
skew
Definition: Cal3.h:72
RealScalar s
Vector9 vector() const
Return all parameters as a vector.
void print(const std::string &s="") const override
print with optional string
double v0_
principal point
Definition: Cal3.h:73
Point2 calibrate(const Point2 &p, OptionalJacobian< 2, 9 > Dcal={}, OptionalJacobian< 2, 2 > Dp={}) const
Convert (distorted) image coordinates uv to intrinsic coordinates xy.
virtual Matrix3 K() const
return calibration matrix K
Definition: Cal3.h:167
traits
Definition: chartTesting.h:28
typedef and functions to augment Eigen&#39;s VectorXd
Point2 uncalibrate(const Point2 &p, OptionalJacobian< 2, 9 > Dcal={}, OptionalJacobian< 2, 2 > Dp={}) const
ofstream os("timeSchurFactors.csv")
double distance2(const Point2 &p, const Point2 &q, OptionalJacobian< 1, 2 > H1, OptionalJacobian< 1, 2 > H2)
distance between two points
Definition: Point2.cpp:39
bool equals(const Cal3DS2_Base &K, double tol=1e-8) const
assert equality up to a tolerance
3D Point
float * p
double tol_
tolerance value when calibrating
Definition: Cal3DS2_Base.h:46
double p1() const
First tangential distortion coefficient.
Definition: Cal3DS2_Base.h:107
Matrix29 D2d_calibration(const Point2 &p) const
Derivative of uncalibrate wrpt the calibration parameters.
Annotation indicating that a class derives from another given type.
Definition: attr.h:61
const G double tol
Definition: Group.h:86
double fx_
Definition: Cal3.h:71
double k1() const
First distortion coefficient.
Definition: Cal3DS2_Base.h:101
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
2D Point
double k2() const
Second distortion coefficient.
Definition: Cal3DS2_Base.h:104
double px() const
image center in x
Definition: Cal3.h:151


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:34:00