GSoC2011SfM  0.1
Google Summer of Code 2011: Structure from motion
D:/Travail/These/Determination caracteristiques camera/GSoC/SfM/src/TracksOfPoints.h
00001 
00002 #ifndef _GSOC_SFM_TRACKS_OF_POINTS_H
00003 #define _GSOC_SFM_TRACKS_OF_POINTS_H 1
00004 
00005 #include "macro.h" //SFM_EXPORTS
00006 
00007 #include <numeric>
00008 #include "opencv2/calib3d/calib3d.hpp"
00009 
00010 namespace OpencvSfM{
00011   class SFM_EXPORTS PointsToTrack;
00012   class SFM_EXPORTS PointOfView;
00013 
00023   class SFM_EXPORTS TrackOfPoints
00024   {
00025     friend class SequenceAnalyzer;
00026 
00027   protected:
00028     cv::Ptr<cv::Vec3d> point3D;
00029     std::vector<unsigned int> images_indexes_;
00030     std::vector<unsigned int> point_indexes_;
00031     unsigned int color;
00032 
00036     std::vector<bool> good_values;
00041     int track_consistance;
00042   public:
00046     template<typename Type, int size>
00047     operator cv::Vec<Type,size>&( ) {
00048       return *point3D;
00049     }
00050     TrackOfPoints( ):track_consistance( 0 ),color(0){};
00051     ~TrackOfPoints();
00059     bool addMatch( const int image_src, const int point_idx );
00060 
00066     inline bool containImage( const int image_wanted ) const
00067     {
00068       std::vector<unsigned int>::const_iterator idx = std::find(
00069         images_indexes_.begin( ),images_indexes_.end( ),
00070         image_wanted );
00071       return (idx != images_indexes_.end( ) &&
00072         good_values[idx - images_indexes_.begin( ) ]==true );
00073     }
00080     bool containPoint( const int image_src, const int point_idx1 ) const;
00085     inline unsigned int getNbTrack( ) const
00086     {
00087       return track_consistance<0 ? 0 : std::accumulate(
00088     good_values.begin( ), good_values.end( ), 0 );
00089     };
00096     cv::DMatch toDMatch( const int img1,const int img2 ) const;
00103     void getMatch( const unsigned int index,
00104       int &idImage, int &idPoint ) const;
00110     inline int getPointIndex( const unsigned int image ) const
00111     {
00112       std::vector<unsigned int>::const_iterator result =
00113         std::find( images_indexes_.begin( ),images_indexes_.end( ),image );
00114       if( result == images_indexes_.end( ) )
00115         return -1;
00116       return point_indexes_[ result - images_indexes_.begin( ) ];
00117     };
00124     inline int getImageIndex( const unsigned int idx ) const
00125     {
00126       return images_indexes_[ idx ];
00127     };
00128     
00136     double triangulateLinear( std::vector<PointOfView>& cameras,
00137       const std::vector< cv::Ptr<PointsToTrack> > &points_to_track,
00138       cv::Vec3d& points3D,
00139       const std::vector<bool> &masks = std::vector<bool>( ) );
00149     double triangulateRobust( std::vector<PointOfView>& cameras,
00150       const std::vector< cv::Ptr< PointsToTrack > > &points_to_track,
00151       cv::Vec3d& points3D,
00152       double reproj_error = 4,
00153       const std::vector<bool> &masks = std::vector<bool>( ) );
00154     
00163     void removeOutliers( std::vector<PointOfView>& cameras,
00164       const std::vector< cv::Ptr< PointsToTrack > > &points_to_track,
00165       double reproj_error = 4,
00166       std::vector<bool> *masks = NULL );
00167 
00172     static void fusionDuplicates( std::vector<TrackOfPoints>& tracks );
00173     
00178     inline void set3DPosition( cv::Vec3d newPoint )
00179     {
00180       if( !point3D )
00181         point3D = new cv::Vec3d( newPoint );
00182       else
00183         *point3D = newPoint;
00184     }
00189     inline cv::Ptr<cv::Vec3d> get3DPosition(){ return point3D; };
00190     
00195     inline unsigned int getColor() const {return color;};
00200     inline void setColor(unsigned int c) {color = c;};
00201     
00207     static void keepTrackHavingImage( unsigned int idx_image,
00208       std::vector<TrackOfPoints>& tracks );
00214     static void keepTrackWithImages( const
00215       std::vector<int>& imgList,
00216       std::vector<TrackOfPoints>& tracks );
00217 
00225     static void mixTracks( const std::vector<TrackOfPoints>& list_tracks,
00226       std::vector<TrackOfPoints>* mixed_tracks);
00227 
00228   protected:
00237     double errorEstimate( std::vector< PointOfView >& cameras,
00238       const std::vector< cv::Ptr< PointsToTrack > > &points_to_track,
00239       cv::Vec3d& points3D,
00240       const std::vector<bool> &masks = std::vector<bool>( ) ) const;
00241   };
00242 
00243 
00247   typedef struct
00248   {
00249     int imgSrc;
00250     int imgDest;
00251   } ImageLink;
00256   class SFM_EXPORTS ImagesGraphConnection
00257   {
00258   protected:
00264     cv::SparseMat images_graph_;
00265 
00272     inline void orderedIdx( int i1,int i2,int idx[ 2 ] )
00273     {
00274       if( i1<i2 )
00275       {
00276         idx[ 0 ] = i1;
00277         idx[ 1 ] = i2;
00278       }
00279       else
00280       {
00281         idx[ 0 ] = i2;
00282         idx[ 1 ] = i1;
00283       }
00284     }
00285   public:
00289     ImagesGraphConnection( ){};
00290 
00296     inline bool isGraphCreated( int nbImages )
00297     {
00298       return images_graph_.size( )!=0 && images_graph_.nzcount( )!=0 &&
00299         images_graph_.size( )[ 0 ] == nbImages &&
00300         images_graph_.size( )[ 1 ] == nbImages;
00301     }
00302 
00307     inline void initStructure( int nb_images )
00308     {
00309       const int dims = 2;
00310       int size[ ] = {nb_images,nb_images};
00311       images_graph_.create( dims,size,CV_32S );
00312     }
00318     inline void addLink( int first_image,int second_image )
00319     {
00320       int idx[ 2 ];
00321       orderedIdx( first_image,second_image,idx );
00322       images_graph_.ref<int>( idx ) += 1;
00323     }
00330     inline int getNumbersOfLinks( int first_image,int second_image )
00331     {
00332       int idx[ 2 ];
00333       orderedIdx( first_image,second_image,idx );
00334       const int *value=images_graph_.find<int>( idx );
00335       if( value!=NULL )
00336         return *value;
00337       return 0;
00338     }
00346     int getHighestLink( int &first_image,int &second_image,
00347       int max_number=1e9 );
00354     void getOrderedLinks( std::vector<ImageLink>& outList,
00355       int min_number=0, int max_number=1e9 );
00363     void getImagesRelatedTo( int first_image, std::vector<ImageLink>& outList,
00364       int min_number=0, int max_number=1e9 );
00365   };
00366 }
00367 #endif
 All Classes Functions Variables