contrib/brl/bmvl/brct/brct_epi_reconstructor.h

Go to the documentation of this file.
00001 #ifndef brct_epi_reconstructor_h_
00002 #define brct_epi_reconstructor_h_
00003 
00004 #if defined(_MSC_VER) && ( _MSC_VER > 1000 )
00005 #pragma once
00006 #endif // _MSC_VER > 1000
00007 //:
00008 // \file
00009 // \brief A class to attack structure from motion problem
00010 //
00011 //  Input:
00012 //  -  A set of tracked vdgl_digital_curve(s). Currently these
00013 //     curves are read in from a track file, which is generated by
00014 //     a 2-d tracking algorithm. The tracks are stored in tracks_.
00015 //  -  A timestamp file that gives the observation time for each frame
00016 //     in milliseconds.
00017 //  -  The calibration matrix K_ for the camera.  Currently this
00018 //     matrix is hard-coded in ::init_cam_intrinsic()
00019 //  -  Initial epipole image location e_, a 2-d point.
00020 //
00021 //  Internals:
00022 //   The camera translation is computed from the matched curves and an
00023 //   assumed epipolar geometry.  This class is a heavily modified version
00024 //   of the original Kalman filter class written by Kongbin Kang.
00025 //
00026 // \author J.L. Mundy
00027 // \verbatim
00028 //  Modifications
00029 //   Initial version Dec. 2003
00030 //   The class doe
00031 // \endverbatim
00032 //
00033 //////////////////////////////////////////////////////////////////////
00034 
00035 #include <vcl_vector.h>
00036 #include <vnl/vnl_double_2.h>
00037 #include <vnl/vnl_double_3.h>
00038 #include <vnl/vnl_double_3x3.h>
00039 #include <vnl/vnl_double_3x4.h>
00040 #include <vnl/vnl_vector_fixed.h>
00041 #include <vnl/vnl_matrix_fixed.h>
00042 #include <vnl/vnl_matrix.h>
00043 #include <vdgl/vdgl_digital_curve_sptr.h>
00044 #include <bugl/bugl_gaussian_point_2d.h>
00045 #include <vgl/vgl_point_2d.h>
00046 #include <vgl/vgl_point_3d.h>
00047 #include <bugl/bugl_curve_3d.h>
00048 
00049 class brct_epi_reconstructor
00050 {
00051  public:
00052 
00053   //: write result of a frame into a file
00054   void write_results(const char* fname);
00055   vgl_point_2d<double> get_cur_epipole() const;
00056   //: get backprojection for debugging
00057   vcl_vector<vnl_matrix<double> > get_back_projection() const;
00058 
00059   //: predict next curve.
00060   vnl_matrix<double> get_predicted_curve();
00061   vnl_double_3 get_next_motion(vnl_double_3 v);
00062   vcl_vector<vgl_point_2d<double> > get_pre_observes();
00063   vcl_vector<vgl_point_2d<double> > get_cur_observes();
00064   vcl_vector<vgl_point_2d<double> > get_next_observes();
00065   //joe version
00066   vcl_vector<vgl_point_2d<double> > get_joe_pre_observes();
00067   vcl_vector<vgl_point_2d<double> > get_joe_cur_observes();
00068   vcl_vector<vgl_point_2d<double> > get_joe_next_observes();
00069 
00070 
00071   vcl_vector<vgl_point_3d<double> > get_local_pts(); // will be superseded
00072   bugl_curve_3d get_curve_3d();
00073 
00074   //: read all the data including time stamps and  tracks.
00075   void read_data(const char* fname);
00076 
00077   //: read vishual tracker result out of a file
00078   vcl_vector<vdgl_digital_curve_sptr> read_track_file(char* fname);
00079 
00080 
00081   //: initialize the kalman filter states
00082   void init();
00083   void init_epipole(double x, double y);
00084   void inc();
00085   vnl_double_2 projection(const vnl_double_3x4 &P, const vnl_double_3 &X);
00086 
00087   //: constructors
00088   //
00089   brct_epi_reconstructor();
00090   brct_epi_reconstructor(const char* fname);
00091   virtual ~brct_epi_reconstructor();
00092 
00093   //: direct access
00094   void add_track(vcl_vector<vdgl_digital_curve_sptr> const& track);
00095 
00096   //: print track data
00097   void print_track(const int track_index, const int frame);
00098 
00099   //: print motion grouping histogram
00100   void print_motion_array();
00101  protected:
00102   //: read time stamp
00103   vcl_vector<double> read_timestamp_file(char* fname);
00104 
00105   //: if the zero probability returned, the matched point is a outlier
00106   double matched_point_prob(vnl_double_2& z, vnl_double_2& z_pred);
00107 
00108   //: get time interval from ith frame to j-th frame
00109   vnl_matrix_fixed<double, 6, 6> get_transit_matrix(int i, int j);
00110 
00111   //: update the confidence for each 3d point
00112   void update_confidence();
00113 
00114   //: update the matched points in the next frame using closest neighbour.
00115   void update_observes(const vnl_double_3x4 &P, int iframe);
00116   void update_observes_joe(int iframe);
00117   void init_velocity();
00118 
00119   //: set linearized observation matrix
00120   vnl_matrix_fixed<double, 2, 6> get_H_matrix(vnl_double_3x4 &P, vnl_double_3 &Y);
00121 
00122   //: compute projective matrix from predicted position
00123   vnl_double_3x4 get_projective_matrix(const vnl_double_3 &v) const;
00124 
00125   void init_covariant_matrix();
00126   void init_cam_intrinsic();
00127   void init_state_3d_estimation();
00128   void init_transit_matrix();
00129 
00130   //: utility functions
00131   bool match_point(vdgl_digital_curve_sptr const& dc,
00132                    bugl_gaussian_point_2d<double>& p0,
00133                    double grad_angle,
00134                    bugl_gaussian_point_2d<double>& p);
00135 
00136   vcl_vector<bugl_gaussian_point_2d<double> >
00137     get_cur_joe_observes(int frame);
00138  private:
00139    //: position and confidence of feature samples
00140   bugl_curve_3d curve_3d_;
00141 
00142   vcl_vector<double> prob_;
00143 
00144   //: the set of 2-d points in each frame, used for matching
00145   vcl_vector<vcl_vector<bugl_gaussian_point_2d<double> > > observes_;
00146 
00147   //: the set of 2-d points in each frame grouped by tracked curves
00148   vcl_vector<vcl_vector<vcl_vector<bugl_gaussian_point_2d<double> > > > joe_observes_;
00149 
00150   //: the gradient angles corresponding to frame 0 matched tracked curves
00151   vcl_vector<vcl_vector<double> > grad_angles_;
00152 
00153   //: each element represents image capture time for each frame.
00154   vcl_vector<double> time_tick_;
00155 
00156   //: each element of the vector represents a projection of the same 3D curves.
00157   vcl_vector<vcl_vector<vdgl_digital_curve_sptr> > tracks_;
00158 
00159   //: the sequence of translations
00160   vcl_vector<vnl_double_3> motions_;
00161 
00162   //: current frame position in history pool
00163   int cur_pos_;
00164   int queue_size_;
00165   int num_points_;
00166 
00167   //: how much the queue has been used
00168   int memory_size_;
00169 
00170   //: state vector
00171   vnl_vector_fixed<double, 6> X_;
00172 
00173   //: covariant matrix of state vector
00174   vnl_matrix_fixed<double, 6, 6> Q_;
00175 
00176   //: constraint kalman gain matrix
00177   vnl_matrix_fixed<double, 6, 2> G_;
00178 
00179   //: initial covariant matrix of state vector
00180   vnl_matrix_fixed<double, 2, 2> R_;
00181 
00182   //: covariant matrix of 2D projection
00183   vnl_matrix_fixed<double, 6, 6> Q0_;
00184 
00185   //: initial epipole
00186   vnl_double_2* e_;
00187 
00188   //: camera intrinsic parameters
00189   vnl_double_3x3 K_;
00190 
00191   //: used to denote outlier point in image
00192   static const double large_num_;
00193   //: debug flag
00194   bool debug_;
00195 };
00196 
00197 #endif // brct_epi_reconstructor_h_

Generated on Mon Mar 8 05:35:53 2010 for contrib/brl/bmvl/brct by  doxygen 1.5.1