testCalibratedCamera.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/geometry/Pose2.h>
20 #include <gtsam/base/Testable.h>
22 
24 
25 #include <iostream>
26 
27 using namespace std;
28 using namespace gtsam;
29 
31 
32 // Camera situated at 0.5 meters high, looking down
33 static const Pose3 kDefaultPose(Rot3(Vector3(1, -1, -1).asDiagonal()),
34  Point3(0, 0, 0.5));
35 
37 
38 static const Point3 point1(-0.08,-0.08, 0.0);
39 static const Point3 point2(-0.08, 0.08, 0.0);
40 static const Point3 point3( 0.08, 0.08, 0.0);
41 static const Point3 point4( 0.08,-0.08, 0.0);
42 
43 /* ************************************************************************* */
45 {
47 }
48 
49 //******************************************************************************
51  Matrix actualH;
52  EXPECT(assert_equal(camera, CalibratedCamera::Create(kDefaultPose, actualH)));
53 
54  // Check derivative
55  boost::function<CalibratedCamera(Pose3)> f = //
56  boost::bind(CalibratedCamera::Create, _1, boost::none);
57  Matrix numericalH = numericalDerivative11<CalibratedCamera, Pose3>(f, kDefaultPose);
58  EXPECT(assert_equal(numericalH, actualH, 1e-9));
59 }
60 
61 /* ************************************************************************* */
63 {
64  // Create a level camera, looking in X-direction
65  Pose2 pose2(0.1,0.2,0);
66  CalibratedCamera camera = CalibratedCamera::Level(pose2, 0.3);
67 
68  // expected
69  Point3 x(0,-1,0),y(0,0,-1),z(1,0,0);
70  Rot3 wRc(x,y,z);
71  Pose3 expected(wRc,Point3(0.1,0.2,0.3));
72  CHECK(assert_equal( camera.pose(), expected));
73 }
74 
75 /* ************************************************************************* */
77 {
78  // Create a level camera, looking in Y-direction
79  Pose2 pose2(0.4,0.3,M_PI/2.0);
80  CalibratedCamera camera = CalibratedCamera::Level(pose2, 0.1);
81 
82  // expected
83  Point3 x(1,0,0),y(0,0,-1),z(0,1,0);
84  Rot3 wRc(x,y,z);
85  Pose3 expected(wRc,Point3(0.4,0.3,0.1));
86  CHECK(assert_equal( camera.pose(), expected));
87 }
88 
89 /* ************************************************************************* */
91 {
92  CHECK(assert_equal( Point2(-.16, .16), camera.project(point1) ));
93  CHECK(assert_equal( Point2(-.16, -.16), camera.project(point2) ));
94  CHECK(assert_equal( Point2( .16, -.16), camera.project(point3) ));
95  CHECK(assert_equal( Point2( .16, .16), camera.project(point4) ));
96 }
97 
98 /* ************************************************************************* */
99 static Point2 Project1(const Point3& point) {
100  return PinholeBase::Project(point);
101 }
102 
103 TEST( CalibratedCamera, DProject1) {
104  Point3 pp(155, 233, 131);
105  Matrix test1;
106  CalibratedCamera::Project(pp, test1);
107  Matrix test2 = numericalDerivative11<Point2, Point3>(Project1, pp);
108  CHECK(assert_equal(test1, test2));
109 }
110 
111 /* ************************************************************************* */
112 static Point2 Project2(const Unit3& point) {
113  return PinholeBase::Project(point);
114 }
115 
116 Unit3 pointAtInfinity(0, 0, -1000);
117 TEST( CalibratedCamera, DProjectInfinity) {
118  Matrix test1;
119  CalibratedCamera::Project(pointAtInfinity, test1);
120  Matrix test2 = numericalDerivative11<Point2, Unit3>(Project2,
122  CHECK(assert_equal(test1, test2));
123 }
124 
125 /* ************************************************************************* */
127  return camera.project(point);
128 }
129 
130 
131 TEST( CalibratedCamera, Dproject_point_pose)
132 {
133  Matrix Dpose, Dpoint;
134  Point2 result = camera.project(point1, Dpose, Dpoint);
135  Matrix numerical_pose = numericalDerivative21(project2, camera, point1);
136  Matrix numerical_point = numericalDerivative22(project2, camera, point1);
137  CHECK(assert_equal(Point3(-0.08, 0.08, 0.5), camera.pose().transformTo(point1)));
138  CHECK(assert_equal(Point2(-.16, .16), result));
139  CHECK(assert_equal(numerical_pose, Dpose, 1e-7));
140  CHECK(assert_equal(numerical_point, Dpoint, 1e-7));
141 }
142 
143 /* ************************************************************************* */
144 // Add a test with more arbitrary rotation
145 TEST( CalibratedCamera, Dproject_point_pose2)
146 {
147  static const Pose3 kDefaultPose(Rot3::Ypr(0.1, -0.1, 0.4), Point3(0, 0, -10));
148  static const CalibratedCamera camera(kDefaultPose);
149  Matrix Dpose, Dpoint;
150  camera.project(point1, Dpose, Dpoint);
151  Matrix numerical_pose = numericalDerivative21(project2, camera, point1);
152  Matrix numerical_point = numericalDerivative22(project2, camera, point1);
153  CHECK(assert_equal(numerical_pose, Dpose, 1e-7));
154  CHECK(assert_equal(numerical_point, Dpoint, 1e-7));
155 }
156 
157 /* ************************************************************************* */
159  return camera.project2(point);
160 }
161 
162 TEST( CalibratedCamera, Dproject_point_pose_infinity)
163 {
164  Matrix Dpose, Dpoint;
165  Point2 result = camera.project2(pointAtInfinity, Dpose, Dpoint);
166  Matrix numerical_pose = numericalDerivative21(projectAtInfinity, camera, pointAtInfinity);
167  Matrix numerical_point = numericalDerivative22(projectAtInfinity, camera, pointAtInfinity);
168  CHECK(assert_equal(Point2(0,0), result));
169  CHECK(assert_equal(numerical_pose, Dpose, 1e-7));
170  CHECK(assert_equal(numerical_point, Dpoint, 1e-7));
171 }
172 
173 /* ************************************************************************* */
174 // Add a test with more arbitrary rotation
175 TEST( CalibratedCamera, Dproject_point_pose2_infinity)
176 {
177  static const Pose3 pose(Rot3::Ypr(-M_PI/4.0, M_PI + M_PI/9.5, M_PI/8.0), Point3(0, 0, -10));
178  static const CalibratedCamera camera(pose);
179  Matrix Dpose, Dpoint;
180  camera.project2(pointAtInfinity, Dpose, Dpoint);
181  Matrix numerical_pose = numericalDerivative21(projectAtInfinity, camera, pointAtInfinity);
182  Matrix numerical_point = numericalDerivative22(projectAtInfinity, camera, pointAtInfinity);
183  CHECK(assert_equal(numerical_pose, Dpose, 1e-7));
184  CHECK(assert_equal(numerical_point, Dpoint, 1e-7));
185 }
186 
188  const double& depth) {
189  return camera.BackprojectFromCamera(point, depth);
190 }
191 TEST( CalibratedCamera, DBackprojectFromCamera)
192 {
193  static const Pose3 pose(Rot3::Ypr(-M_PI/4.0, M_PI + M_PI/9.5, M_PI/8.0), Point3(0, 0, -10));
194  static const CalibratedCamera camera(pose);
195  static const double depth = 5.4;
196  static const Point2 point(10.1, 50.3);
197  Matrix Dpoint, Ddepth;
198  camera.BackprojectFromCamera(point, depth, Dpoint, Ddepth);
199  Matrix numerical_point = numericalDerivative32(BackprojectFromCamera, camera, point, depth);
200  Matrix numerical_depth = numericalDerivative33(BackprojectFromCamera, camera, point, depth);
201  CHECK(assert_equal(numerical_point, Dpoint, 1e-7));
202  CHECK(assert_equal(numerical_depth, Ddepth, 1e-7));
203 }
204 
205 
206 static Point3 backproject(const Pose3& pose, const Point2& point, const double& depth) {
207  return CalibratedCamera(pose).backproject(point, depth);
208 }
209 TEST( PinholePose, DbackprojectCalibCamera)
210 {
211  Matrix36 Dpose;
212  Matrix31 Ddepth;
213  Matrix32 Dpoint;
214  const Point2 point(-100, 100);
215  const double depth(10);
216  static const Pose3 pose(Rot3::Ypr(-M_PI/4.0, M_PI + M_PI/9.5, M_PI/8.0), Point3(0, 0, -10));
217  static const CalibratedCamera camera(pose);
218  camera.backproject(point, depth, Dpose, Dpoint, Ddepth);
219  Matrix expectedDpose = numericalDerivative31(backproject, pose, point, depth);
220  Matrix expectedDpoint = numericalDerivative32(backproject, pose, point, depth);
221  Matrix expectedDdepth = numericalDerivative33(backproject, pose,point, depth);
222 
223  EXPECT(assert_equal(expectedDpose, Dpose, 1e-7));
224  EXPECT(assert_equal(expectedDpoint, Dpoint, 1e-7));
225  EXPECT(assert_equal(expectedDdepth, Ddepth, 1e-7));
226 }
227 
228 
229 /* ************************************************************************* */
230 int main() { TestResult tr; return TestRegistry::runAllTests(tr); }
231 /* ************************************************************************* */
static Point2 project2(const CalibratedCamera &camera, const Point3 &point)
#define CHECK(condition)
Definition: Test.h:109
static Point2 project(const Pose3 &pose, const Unit3 &pointAtInfinity, const Cal3_S2::shared_ptr &cal)
Scalar * y
static Point3 BackprojectFromCamera(const CalibratedCamera &camera, const Point2 &point, const double &depth)
Concept check for values that can be used in unit tests.
static int runAllTests(TestResult &result)
Eigen::Vector3d Vector3
Definition: Vector.h:43
Matrix expected
Definition: testMatrix.cpp:974
Point2 project2(const Point3 &point, OptionalJacobian< 2, 6 > Dpose=boost::none, OptionalJacobian< 2, 3 > Dpoint=boost::none) const
Vector2 Point2
Definition: Point2.h:27
Eigen::MatrixXd Matrix
Definition: base/Matrix.h:43
#define M_PI
Definition: main.h:78
Definition: Half.h:150
internal::FixedSizeMatrix< Y, X3 >::type numericalDerivative33(boost::function< Y(const X1 &, const X2 &, const X3 &)> h, const X1 &x1, const X2 &x2, const X3 &x3, double delta=1e-5)
Some functions to compute numerical derivatives.
internal::FixedSizeMatrix< Y, X1 >::type numericalDerivative31(boost::function< Y(const X1 &, const X2 &, const X3 &)> h, const X1 &x1, const X2 &x2, const X3 &x3, double delta=1e-5)
internal::FixedSizeMatrix< Y, X1 >::type numericalDerivative21(const boost::function< Y(const X1 &, const X2 &)> &h, const X1 &x1, const X2 &x2, double delta=1e-5)
const Pose3 & pose() const
return pose, constant version
static Point3 backproject(const Pose3 &pose, const Point2 &point, const double &depth)
Unit3 pointAtInfinity(0, 0,-1000)
Point3 point(10, 0,-5)
int main()
Represents a 3D point on a unit sphere.
Definition: Unit3.h:42
internal::FixedSizeMatrix< Y, X2 >::type numericalDerivative32(boost::function< Y(const X1 &, const X2 &, const X3 &)> h, const X1 &x1, const X2 &x2, const X3 &x3, double delta=1e-5)
Point3 backproject(const Point2 &pn, double depth, OptionalJacobian< 3, 6 > Dresult_dpose=boost::none, OptionalJacobian< 3, 2 > Dresult_dp=boost::none, OptionalJacobian< 3, 1 > Dresult_ddepth=boost::none) const
backproject a 2-dimensional point to a 3-dimensional point at given depth
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
static Point2 Project2(const Unit3 &point)
internal::FixedSizeMatrix< Y, X2 >::type numericalDerivative22(boost::function< Y(const X1 &, const X2 &)> h, const X1 &x1, const X2 &x2, double delta=1e-5)
Values result
static const Pose3 pose(Rot3(Vector3(1,-1,-1).asDiagonal()), Point3(0, 0, 0.5))
TEST(CalibratedCamera, constructor)
#define EXPECT(condition)
Definition: Test.h:151
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
static Point3 BackprojectFromCamera(const Point2 &p, const double depth, OptionalJacobian< 3, 2 > Dpoint=boost::none, OptionalJacobian< 3, 1 > Ddepth=boost::none)
backproject a 2-dimensional point to a 3-dimensional point at given depth
Array< double, 1, 3 > e(1./3., 0.5, 2.)
static const Point3 point3(0.08, 0.08, 0.0)
static const Point3 point2(-0.08, 0.08, 0.0)
Point2 project(const Point3 &point, OptionalJacobian< 2, 6 > Dcamera=boost::none, OptionalJacobian< 2, 3 > Dpoint=boost::none) const
Calibrated camera for which only pose is unknown.
traits
Definition: chartTesting.h:28
static Point2 projectAtInfinity(const CalibratedCamera &camera, const Unit3 &point)
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:42
static Point2 Project1(const Point3 &point)
#define GTSAM_CONCEPT_MANIFOLD_INST(T)
Definition: Manifold.h:180
static const Point3 point1(-0.08,-0.08, 0.0)
Point2(* Project)(const Point3 &, OptionalJacobian< 2, 3 >)
static const Pose3 kDefaultPose(Rot3(Vector3(1,-1,-1).asDiagonal()), Point3(0, 0, 0.5))
static const Point3 point4(0.08,-0.08, 0.0)
static const Pose3 pose2
Vector3 Point3
Definition: Point3.h:35
2D Pose
static const CalibratedCamera camera(kDefaultPose)
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
void test2(OptionalJacobian<-1,-1 > H=boost::none)


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:46:21