iSAM
stereo.cpp
Go to the documentation of this file.
00001 
00008 #include <iostream>
00009 #include <iomanip>
00010 
00011 #include <isam/isam.h>
00012 #include <isam/slam_stereo.h>
00013 #include <isam/robust.h>
00014 
00015 using namespace std;
00016 using namespace isam;
00017 using namespace Eigen;
00018 
00019 // stereo camera parameters
00020 const double f = 360;  // focal length in pixels
00021 const double u0 = 240; // principal point in pixels
00022 const double v0 = 120;
00023 const double b = 0.12; // baseline in meters
00024 
00025 double robust_cost_function(double d) {
00026   return cost_pseudo_huber(d, .5);
00027 }
00028 
00029 void simple_stereo() {
00030 
00031   Vector2d pp(u0, v0);
00032   StereoCamera camera(f, pp, b);
00033 
00034   Pose3d origin;
00035   Point3d p0(5.,1.,2.);
00036 
00037   Slam slam;
00038 
00039   // first stereo camera
00040   Pose3d_Node* pose0 = new Pose3d_Node();
00041   slam.add_node(pose0);
00042 
00043   // create a prior on the camera position
00044   Noise noise6 = Information(100. * eye(6));
00045   Pose3d_Factor* prior = new Pose3d_Factor(pose0, origin, noise6);
00046   slam.add_factor(prior);
00047 
00048   // second stereo camera
00049   Pose3d_Node* pose1 = new Pose3d_Node();
00050   slam.add_node(pose1);
00051 
00052   // add odometry measurement
00053   Pose3d delta(1.,0,0, 0,0,0); // move one meter forward (noise-free measurement...)
00054   Pose3d_Pose3d_Factor* odo = new Pose3d_Pose3d_Factor(pose0, pose1, delta, noise6);
00055   slam.add_factor(odo);
00056 
00057   // add some stereo measurements
00058   Point3d_Node* point = new Point3d_Node();
00059   slam.add_node(point);
00060   Noise noise3 = Information(eye(3));
00061   // first stereo camera projection
00062   StereoMeasurement measurement0 = camera.project(origin, p0);
00063   cout << "Projection in first camera:" << endl;
00064   cout << measurement0.u << " " << measurement0.v << " " << measurement0.u2 << endl;
00065   measurement0.u += 0.5; // add some "noise"
00066   measurement0.v -= 0.2;
00067   Stereo_Factor* factor1 = new Stereo_Factor(pose0, point, &camera, measurement0, noise3);
00068   slam.add_factor(factor1);
00069   // second stereo camera projection
00070   StereoMeasurement measurement1 = camera.project(delta, p0);
00071   measurement1.u -= 0.3; // add some "noise"
00072   measurement1.v += 0.7;
00073   Stereo_Factor* factor2 = new Stereo_Factor(pose1, point, &camera, measurement1, noise3);
00074   slam.add_factor(factor2);
00075 
00076   cout << "Before optimization:" << endl;
00077   cout << setprecision(3) << setiosflags(ios::fixed);
00078   cout << point->value() << endl;
00079   cout << pose0->value() << endl;
00080   cout << pose1->value() << endl;
00081 
00082   // not needed here, but for more complex examples use Powell's dog leg and
00083   // optionally a robust estimator (pseudo Huber) to deal with occasional outliers
00084   Properties prop = slam.properties();
00085   prop.method = DOG_LEG;
00086   slam.set_properties(prop);
00087 //  slam.set_cost_function(robust_cost_function);
00088 
00089   // optimize
00090   slam.batch_optimization();
00091 
00092   cout << "After optimization:" << endl;
00093   cout << point->value() << endl;
00094   cout << pose0->value() << endl;
00095   cout << pose1->value() << endl;
00096 }
00097 
00098 int main() {
00099 
00100   simple_stereo();
00101 
00102   return 0;
00103 }
 All Classes Files Functions Variables