StereoCamera.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 
20 using namespace std;
21 using namespace gtsam;
22 
23 namespace gtsam {
24 
25  /* ************************************************************************* */
26  StereoCamera::StereoCamera(const Pose3& leftCamPose,
28  leftCamPose_(leftCamPose), K_(K) {
29  }
30 
31  /* ************************************************************************* */
33  return project2(point);
34  }
35 
36  /* ************************************************************************* */
39 
40  const Point3 q = leftCamPose_.transformTo(point);
41 
42 #ifdef GTSAM_THROW_CHEIRALITY_EXCEPTION
43  if (q.z() <= 0)
45 #endif
46 
47  // get calibration
48  const Cal3_S2Stereo& K = *K_;
49  const double fx = K.fx(), fy = K.fy(), b = K.baseline();
50 
51  // calculate scaled but not translated image coordinates
52  const double d = 1.0 / q.z();
53  const double x = q.x(), y = q.y();
54  const double dfx = d*fx, dfy = d*fy;
55  const double uL = dfx*x;
56  const double uR = dfx*(x - b);
57  const double v = dfy*y;
58 
59  // check if derivatives need to be computed
60  if (H1 || H2) {
61  // optimized version, see StereoCamera.nb
62  if (H1) {
63  const double v1 = v/fy, v2 = fx*v1, dx=d*x;
64  *H1 << uL*v1, -fx-dx*uL, v2, -dfx, 0.0, d*uL,
65  uR*v1, -fx-dx*uR, v2, -dfx, 0.0, d*uR,
66  fy + v*v1, -dx*v , -x*dfy, 0.0, -dfy, d*v;
67  }
68  if (H2) {
69  const Matrix3 R(leftCamPose_.rotation().matrix());
70  *H2 << fx*R(0, 0) - R(0, 2)*uL, fx*R(1, 0) - R(1, 2)*uL, fx*R(2, 0) - R(2, 2)*uL,
71  fx*R(0, 0) - R(0, 2)*uR, fx*R(1, 0) - R(1, 2)*uR, fx*R(2, 0) - R(2, 2)*uR,
72  fy*R(0, 1) - R(0, 2)*v , fy*R(1, 1) - R(1, 2)*v , fy*R(2, 1) - R(2, 2)*v;
73  *H2 << d * (*H2);
74  }
75  }
76 
77  // finally translate
78  return StereoPoint2(K.px() + uL, K.px() + uR, K.py() + v);
79  }
80 
81  /* ************************************************************************* */
84  OptionalJacobian<3,0> H3) const {
85  if (H3)
86  throw std::runtime_error(
87  "StereoCamera::project does not support third derivative - BTW use project2");
88  return project2(point,H1,H2);
89  }
90 
91  /* ************************************************************************* */
93  Vector measured = z.vector();
94  double Z = K_->baseline() * K_->fx() / (measured[0] - measured[1]);
95  double X = Z * (measured[0] - K_->px()) / K_->fx();
96  double Y = Z * (measured[2] - K_->py()) / K_->fy();
98  return point;
99  }
100 
101  /* ************************************************************************* */
103  OptionalJacobian<3, 3> H2) const {
104  const Cal3_S2Stereo& K = *K_;
105  const double fx = K.fx(), fy = K.fy(), cx = K.px(), cy = K.py(), b = K.baseline();
106 
107  double uL = z.uL(), uR = z.uR(), v = z.v();
108  double disparity = uL - uR;
109 
110  double local_z = b * fx / disparity;
111  const Point3 local(local_z * (uL - cx)/ fx, local_z * (v - cy) / fy, local_z);
112 
113  if(H1 || H2) {
114  double z_partial_uR = local_z/disparity;
115  double x_partial_uR = local.x()/disparity;
116  double y_partial_uR = local.y()/disparity;
117  Matrix3 D_local_z;
118  D_local_z << -x_partial_uR + local.z()/fx, x_partial_uR, 0,
119  -y_partial_uR, y_partial_uR, local.z() / fy,
120  -z_partial_uR, z_partial_uR, 0;
121 
122  Matrix3 D_point_local;
123  const Point3 point = leftCamPose_.transformFrom(local, H1, D_point_local);
124 
125  if(H2) {
126  *H2 = D_point_local * D_local_z;
127  }
128 
129  return point;
130  }
131 
132  return leftCamPose_.transformFrom(local);
133  }
134 
135 }
double fy() const
focal length y
Definition: Cal3.h:142
double uR() const
get uR
Definition: StereoPoint2.h:109
double uL() const
get uL
Definition: StereoPoint2.h:106
Scalar * y
Vector v2
Vector v1
ArrayXcf v
Definition: Cwise_arg.cpp:1
double baseline() const
return baseline
Rot2 R(Rot2::fromAngle(0.1))
const double cx
static Cal3_S2 K(500, 500, 0.1, 640/2, 480/2)
Definition: Half.h:150
Cal3_S2Stereo::shared_ptr K_
Definition: StereoCamera.h:60
Matrix3 matrix() const
Definition: Rot3M.cpp:219
Point3 point(10, 0,-5)
#define Z
Definition: icosphere.cpp:21
double v() const
get v
Definition: StereoPoint2.h:112
Point3 transformFrom(const Point3 &point, OptionalJacobian< 3, 6 > Hself=boost::none, OptionalJacobian< 3, 3 > Hpoint=boost::none) const
takes point in Pose coordinates and transforms it to world coordinates
Definition: Pose3.cpp:298
Point3 transformTo(const Point3 &point, OptionalJacobian< 3, 6 > Hself=boost::none, OptionalJacobian< 3, 3 > Hpoint=boost::none) const
takes point in world coordinates and transforms it to Pose coordinates
Definition: Pose3.cpp:314
const double fy
boost::shared_ptr< Cal3_S2Stereo > shared_ptr
Definition: Cal3_S2Stereo.h:38
Eigen::VectorXd Vector
Definition: Vector.h:38
double py() const
image center in y
Definition: Cal3.h:154
EIGEN_DEVICE_FUNC const Scalar & q
StereoPoint2 project2(const Point3 &point, OptionalJacobian< 3, 6 > H1=boost::none, OptionalJacobian< 3, 3 > H2=boost::none) const
const G & b
Definition: Group.h:83
traits
Definition: chartTesting.h:28
A Stereo Camera based on two Simple Cameras.
Vector3 vector() const
Definition: StereoPoint2.h:115
const double cy
double px() const
image center in x
Definition: Cal3.h:151
double fx() const
focal length x
Definition: Cal3.h:139
Vector3 Point3
Definition: Point3.h:35
StereoPoint2 project(const Point3 &point) const
Project 3D point to StereoPoint2 (uL,uR,v)
const double fx
Point3 measured
#define X
Definition: icosphere.cpp:20
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
Point3 backproject2(const StereoPoint2 &z, OptionalJacobian< 3, 6 > H1=boost::none, OptionalJacobian< 3, 3 > H2=boost::none) const
const Rot3 & rotation(OptionalJacobian< 3, 6 > Hself=boost::none) const
get rotation
Definition: Pose3.cpp:266
Point3 backproject(const StereoPoint2 &z) const
back-project a measurement


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