|
| 1 | +#include <vector> |
| 2 | +#include <set> |
| 3 | + |
| 4 | +#include <TooN/TooN.h> |
| 5 | +#include <TooN/SVD.h> |
| 6 | +#include <TooN/SymEigen.h> |
| 7 | + |
| 8 | +#include <cvd/random.h> |
| 9 | +#include <cvd/gl_helpers.h> |
| 10 | + |
| 11 | +#include "gui_view.h" |
| 12 | +#include "transformations.h" |
| 13 | +#include "stopwatch.h" |
| 14 | +#include "bundle_adjuster.h" |
| 15 | +#include "graph_optimizer.h" |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +using namespace std; |
| 20 | +using namespace CVD; |
| 21 | +using namespace TooN; |
| 22 | +using namespace RobotVision; |
| 23 | + |
| 24 | +int main(int argc, char *argv[]) |
| 25 | +{ |
| 26 | + const int WIDTH = 640; |
| 27 | + const int HEIGHT = 480; |
| 28 | + |
| 29 | + double image_noise = 0.5; |
| 30 | + double spurious_matches_prob = 0.05; |
| 31 | + bool robust_kernel = true; |
| 32 | + |
| 33 | + if (argc>3) |
| 34 | + { |
| 35 | + image_noise = atof(argv[1]); |
| 36 | + spurious_matches_prob = atof(argv[2]); |
| 37 | + robust_kernel = atof(argv[3]); |
| 38 | + } |
| 39 | + else |
| 40 | + { |
| 41 | + cout << endl; |
| 42 | + cout << "Type: ./ba_demo X Y Z" << endl; |
| 43 | + cout << endl; |
| 44 | + cout << "X := noise in the image e.g., X=0.5" << endl; |
| 45 | + cout << "Y := probability of a spurious match, e.g. Y=0.05 or Y=0.0" |
| 46 | + << endl; |
| 47 | + cout << "Z := use robust kernel?. Z=0 (false) or Z=1 (true)" |
| 48 | + << endl; |
| 49 | + cout << endl; |
| 50 | + exit(0); |
| 51 | + } |
| 52 | + |
| 53 | + cout << endl; |
| 54 | + cout << "BUNDLE ADJUSTMENT EXAMPLE" << endl; |
| 55 | + cout << "Image noise: " << image_noise <<endl; |
| 56 | + cout << "Probability of a spurious match: " |
| 57 | + << spurious_matches_prob <<endl; |
| 58 | + cout << "use robust kernel: " << robust_kernel<<endl<<endl<<endl; |
| 59 | + |
| 60 | + ImageRef win_size(WIDTH,HEIGHT); |
| 61 | + GuiWindow glwin(win_size); |
| 62 | + |
| 63 | + LinearCamera cam(makeVector(484.69093,-485.57926), |
| 64 | + makeVector(312.22988,247.83456), |
| 65 | + ImageRef(640,480)); |
| 66 | + |
| 67 | + LinearCamera guiview_cam(makeVector(1000,-1000), |
| 68 | + makeVector(WIDTH/2,HEIGHT/2), |
| 69 | + ImageRef(WIDTH,HEIGHT)); |
| 70 | + |
| 71 | + SE3XYZ se3xyz(cam); |
| 72 | + |
| 73 | + GuiView view3d(ImageRef(WIDTH,HEIGHT), |
| 74 | + guiview_cam, |
| 75 | + makeVector(0,0,0), |
| 76 | + makeVector(-0.5,0,3)); |
| 77 | + |
| 78 | + view3d.init(&glwin,Rectangle(0,0,1,1)); |
| 79 | + view3d.set_handler(new DoomViewHandler(&view3d)); |
| 80 | + |
| 81 | + vector<Vector<3> > true_point_list; |
| 82 | + vector<SE3<double> > true_frame_list; |
| 83 | + |
| 84 | + vector<Vector<3> > noisy_point_list; |
| 85 | + |
| 86 | + vector<Vector<3> > visible_point_list; |
| 87 | + vector<SE3<double> > noisy_frame_list; |
| 88 | + |
| 89 | + vector<Vector<3> > cor_point_list; |
| 90 | + vector<SE3<double> > cor_frame_list; |
| 91 | + |
| 92 | + // create list of true points |
| 93 | + for (int i=0; i<500; ++i) |
| 94 | + { |
| 95 | + true_point_list.push_back(makeVector(rand_u()*0.9, |
| 96 | + (rand_u()-0.5)*0.9,rand_u())); |
| 97 | + } |
| 98 | + |
| 99 | + // create list of noisy points |
| 100 | + for (uint i=0; i<true_point_list.size(); ++i) |
| 101 | + { |
| 102 | + noisy_point_list.push_back(true_point_list[i] |
| 103 | + + makeVector(rand_g(),rand_g(),rand_g()) * 0.01); |
| 104 | + } |
| 105 | + |
| 106 | + vector<IdObs<2> > obs_vec; |
| 107 | + |
| 108 | + //create list of true and noisy poses/frames |
| 109 | + for (int i=0; i<10; ++i) |
| 110 | + { |
| 111 | + SO3<double> rot; |
| 112 | + true_frame_list.push_back(SE3<double>(rot,makeVector(-i*0.1,0,1))); |
| 113 | + if (i<1) |
| 114 | + { |
| 115 | + noisy_frame_list.push_back(SE3<double>(rot,makeVector(-i*0.1,0,1))); |
| 116 | + } |
| 117 | + else |
| 118 | + { |
| 119 | + noisy_frame_list.push_back( |
| 120 | + SE3<double>(rot,makeVector(-i*0.1,0,1) |
| 121 | + + makeVector(rand_g(),rand_g(),rand_g()) * 0.1 )); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + //create list of observations and visible points |
| 126 | + int j_p = 0; |
| 127 | + for (uint j=0; j<true_point_list.size(); ++j) |
| 128 | + { |
| 129 | + bool matched = false; |
| 130 | + vector<IdObs<2> > tmp_vec; |
| 131 | + |
| 132 | + Vector<3> & p3d = true_point_list[j]; |
| 133 | + for (uint i=0; i<true_frame_list.size(); ++i) |
| 134 | + { |
| 135 | + Vector<2> obs; |
| 136 | + |
| 137 | + obs = se3xyz.map(true_frame_list[i],p3d) |
| 138 | + + image_noise*makeVector(rand_g(),rand_g()); |
| 139 | + |
| 140 | + if(obs[0]>=0 && obs[1]>=0 && obs[0]<=639 && obs[1] <= 479) |
| 141 | + { |
| 142 | + if (rand_u()<spurious_matches_prob) |
| 143 | + { |
| 144 | + obs = makeVector(rand_u()*640,rand_u()*480); |
| 145 | + } |
| 146 | + matched = true; |
| 147 | + tmp_vec.push_back(IdObs<2>(j_p,i,obs)); |
| 148 | + } |
| 149 | + } |
| 150 | + if (matched){ |
| 151 | + ++j_p; |
| 152 | + obs_vec.insert(obs_vec.end(),tmp_vec.begin(),tmp_vec.end()); |
| 153 | + visible_point_list.push_back(noisy_point_list[j]); |
| 154 | + |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + BundleAdjuster<SE3<> ,6,3,3,IdObs<2>,2> ba; |
| 159 | + |
| 160 | + StopWatch sw; |
| 161 | + cor_frame_list = noisy_frame_list; |
| 162 | + cor_point_list = visible_point_list; |
| 163 | + |
| 164 | + uint num_iters = 50; |
| 165 | + BundleAdjusterParams opt_params(robust_kernel,1,num_iters); |
| 166 | + |
| 167 | + cout << "Do " << num_iters << " iterations of bundle adjustment!" << endl; |
| 168 | + |
| 169 | + sw.start(); |
| 170 | + ba.calcFull(cor_frame_list, |
| 171 | + cor_point_list, |
| 172 | + se3xyz, |
| 173 | + obs_vec, |
| 174 | + 1, |
| 175 | + 0, |
| 176 | + opt_params, |
| 177 | + false); |
| 178 | + sw.stop(); |
| 179 | + cout << "time in s: " << sw.getStoppedTime() << endl <<endl; |
| 180 | + |
| 181 | + SE3CompareModScale t; |
| 182 | + double s=1; |
| 183 | + double rmse = t.optimize(true_frame_list,cor_frame_list,s,10); |
| 184 | + cout << "RMS error: " << rmse << endl<<endl; |
| 185 | + cout << "True data is shown in blue." << endl; |
| 186 | + cout << "Noisy data before optimisation is shown in grey." << endl; |
| 187 | + cout << "Optimised data is shown in red." << endl; |
| 188 | + cout << endl <<endl; |
| 189 | + |
| 190 | + while (true) |
| 191 | + { |
| 192 | + glClearColor(1,1,1,1); |
| 193 | + glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 194 | + |
| 195 | + view3d.activate3D(); |
| 196 | + |
| 197 | + glColor3f(0.8,0.8,0.8); |
| 198 | + for (uint i=0; i<true_point_list.size(); ++i) |
| 199 | + { |
| 200 | + view3d.drawBall3D(noisy_point_list[i],0.005); |
| 201 | + } |
| 202 | + for (uint i=0; i<noisy_frame_list.size(); ++i) |
| 203 | + { |
| 204 | + view3d.drawPose3D(noisy_frame_list[i]); |
| 205 | + } |
| 206 | + |
| 207 | + glColor3f(0,0,0.75); |
| 208 | + for (uint i=0; i<true_frame_list.size(); ++i){ |
| 209 | + view3d.drawPose3D(true_frame_list[i]); |
| 210 | + } |
| 211 | + for (uint i=0; i<true_point_list.size(); ++i) |
| 212 | + { |
| 213 | + view3d.drawBall3D(true_point_list[i],0.005); |
| 214 | + } |
| 215 | + |
| 216 | + glColor3f(0.75,0,0); |
| 217 | + for (uint i=0; i<cor_frame_list.size(); ++i){ |
| 218 | + view3d.drawPose3D(cor_frame_list[i]); |
| 219 | + } |
| 220 | + for (uint i=0; i<cor_point_list.size(); ++i) |
| 221 | + { |
| 222 | + view3d.drawLine3D(true_point_list[i],cor_point_list[i]); |
| 223 | + view3d.drawBall3D(cor_point_list[i],0.005); |
| 224 | + } |
| 225 | + |
| 226 | + glColor3f(0.3,0.3,0.3); |
| 227 | + view3d.drawLine3D(makeVector(0,0,0),makeVector(10000,0,0)); |
| 228 | + view3d.drawLine3D(makeVector(0,0,0),makeVector(0,10000,0)); |
| 229 | + view3d.drawLine3D(makeVector(0,0,0),makeVector(0,0,10000)); |
| 230 | + |
| 231 | + glFlush(); |
| 232 | + glwin.swap_buffers(); |
| 233 | + |
| 234 | + glwin.handle_events_default(); |
| 235 | + |
| 236 | + usleep(1000); |
| 237 | + } |
| 238 | +} |
| 239 | + |
| 240 | + |
| 241 | + |
| 242 | + |
0 commit comments