contrib/gel/mrc/vpgl/vpgl_fundamental_matrix.h
Go to the documentation of this file.
00001 // This is gel/mrc/vpgl/vpgl_fundamental_matrix.h
00002 #ifndef vpgl_fundamental_matrix_h_
00003 #define vpgl_fundamental_matrix_h_
00004 //:
00005 // \file
00006 // \brief A class for the fundamental matrix between two projective cameras.
00007 // \author Thomas Pollard
00008 // \date January 28, 2005
00009 // \author Joseph Mundy, Matt Leotta, Vishal Jain
00010 //
00011 //  A class which holds the fundamental matrix and performs basic computations with it.
00012 //  More advanced functions using the fundamental matrix can be found in
00013 //  "vpgl_fundamental_matrix_functions.h".
00014 //
00015 //  This implementation forces the rank of the fundamental matrix to be rank 2, and if
00016 //  the matrix is set with a rank 3 matrix, it will be reduced in rank using SVD
00017 //  decomposition.
00018 //
00019 //  The notation "left" and "right" refers to camera producing points used on
00020 //  the left side of the F matrix and vice versa.
00021 //
00022 // \verbatim
00023 //  Modifications
00024 //   May 06, 2009  Ricardo Fabbri   Overloaded {l,r}_epipolar_line to take line as input
00025 // \endverbatim
00026 
00027 #include <vnl/vnl_fwd.h>
00028 #include <vgl/vgl_fwd.h>
00029 #include <vcl_iosfwd.h>
00030 
00031 #include "vpgl_proj_camera.h"
00032 
00033 
00034 template <class T>
00035 class vpgl_fundamental_matrix
00036 {
00037  public:
00038   // Constructors:----------------------
00039 
00040   //: Default constructor creates dummy rank 2 matrix.
00041   vpgl_fundamental_matrix();
00042 
00043   //: Main constructor takes two projective cameras.
00044   //  The RHS of the fundamental matrix will correspond to cr and the LHS to cl.
00045   vpgl_fundamental_matrix( const vpgl_proj_camera<T>& cr,
00046                            const vpgl_proj_camera<T>& cl ) : cached_svd_(NULL)
00047   { set_matrix( cr, cl ); }
00048 
00049   //: Construct from a fundamental matrix in vnl form.
00050   vpgl_fundamental_matrix( const vnl_matrix_fixed<T,3,3>& F ) : cached_svd_(NULL)
00051   { set_matrix( F ); }
00052 
00053   //: Copy Constructor
00054   vpgl_fundamental_matrix(const vpgl_fundamental_matrix<T>& other);
00055 
00056   //: Assignment
00057   const vpgl_fundamental_matrix<T>& operator=( const vpgl_fundamental_matrix<T>& fm );
00058 
00059   //: Destructor
00060   virtual ~vpgl_fundamental_matrix();
00061 
00062   // Basic Operations:-------------------
00063 
00064   //: Put the coordinates of the epipoles in er, el.
00065   void get_epipoles( vgl_homg_point_2d<T>& er, vgl_homg_point_2d<T>& el ) const;
00066 
00067   //: Given a point in one image, find the corresponding epipolar line in the other image.
00068   vgl_homg_line_2d<T> r_epipolar_line( const vgl_homg_point_2d<T>& pl ) const;
00069   vgl_homg_line_2d<T> l_epipolar_line( const vgl_homg_point_2d<T>& pr ) const;
00070 
00071   //: Given an epipolar line in one image, find the corresponding epipolar line in the other image.
00072   // H&Z 2nd ed p. 247
00073   vgl_homg_line_2d<T> r_epipolar_line(const vgl_homg_line_2d<T> &epiline_l) const;
00074   vgl_homg_line_2d<T> l_epipolar_line(const vgl_homg_line_2d<T> &epiline_r) const;
00075 
00076   //: Gives the left camera matrix corresponding to the fundamental matrix
00077   // The right camera matrix is assumed to be identity.
00078   // The variables v, lambda are free parameters as described in H&Z 2nd ed p. 256.
00079   vpgl_proj_camera<T> extract_left_camera(
00080     const vnl_vector_fixed<T,3>& v, T lambda ) const;
00081 
00082   //: Alternative left camera extractor.
00083   // Takes corresponding lists of image points with their world locations
00084   // to determine the correct camera.  Must give at least 2 pairs of correspondences.
00085   // This is not a robust algorithm but this shouldn't be a problem
00086   // as these correspondences will usually be picked by hand.
00087   vpgl_proj_camera<T> extract_left_camera(
00088     const vcl_vector< vgl_point_3d<T> >& world_points,
00089     const vcl_vector< vgl_point_2d<T> >& image_points ) const;
00090 
00091   // Getters and Setters:----------------
00092 
00093   //: Get a copy of the FM in vnl form.
00094   const vnl_matrix_fixed<T,3,3>& get_matrix() const { return F_; }
00095 
00096   //: Get a copy of the svd of the fundamental matrix.
00097   // The svd is computed when the matrix is first set, so this just accesses a cached version.
00098   const vnl_svd<T>& svd() const{ return *cached_svd_; }
00099 
00100   void set_matrix( const vpgl_proj_camera<T>& cr,
00101                    const vpgl_proj_camera<T>& cl );
00102 
00103   void set_matrix( const vnl_matrix_fixed<T,3,3>& F );
00104 
00105  protected:
00106   //: Internal representation of the fundamental matrix.
00107   vnl_matrix_fixed<T,3,3> F_;
00108 
00109   //: Cached copy of the svd.
00110   mutable vnl_svd<T>* cached_svd_;
00111 };
00112 
00113 //: Write vpgl_fundamental_matrix to stream
00114 template <class T>
00115 vcl_ostream&  operator<<(vcl_ostream& s, vpgl_fundamental_matrix<T> const& p);
00116 
00117 //: Read vpgl_fundamental_matrix from stream
00118 template <class T>
00119 vcl_istream&  operator>>(vcl_istream& s, vpgl_fundamental_matrix<T>& p);
00120 
00121 #endif // vpgl_fundamental_matrix_h_