vpgl_fundamental_matrix.h
Go to the documentation of this file.
1 // This is core/vpgl/vpgl_fundamental_matrix.h
2 #ifndef vpgl_fundamental_matrix_h_
3 #define vpgl_fundamental_matrix_h_
4 //:
5 // \file
6 // \brief A class for the fundamental matrix between two projective cameras.
7 // \author Thomas Pollard
8 // \date January 28, 2005
9 // \author Joseph Mundy, Matt Leotta, Vishal Jain
10 //
11 // A class which holds the fundamental matrix and performs basic computations with it.
12 // More advanced functions using the fundamental matrix can be found in
13 // "vpgl_fundamental_matrix_functions.h".
14 //
15 // This implementation forces the rank of the fundamental matrix to be rank 2, and if
16 // the matrix is set with a rank 3 matrix, it will be reduced in rank using SVD
17 // decomposition.
18 //
19 // The notation "left" and "right" refers to camera producing points used on
20 // the left side of the F matrix and vice versa.
21 //
22 // \verbatim
23 // Modifications
24 // May 06, 2009 Ricardo Fabbri Overloaded {l,r}_epipolar_line to take line as input
25 // May 10, 2010 Andrew Hoelscher - Added a constructor based off two
26 // calibration matrices and an essential matrix.
27 // \endverbatim
28 
29 #include <iosfwd>
30 #include <vnl/vnl_fwd.h>
31 #include <vgl/vgl_fwd.h>
32 #ifdef _MSC_VER
33 # include <vcl_msvc_warnings.h>
34 #endif
35 
36 #include "vpgl_proj_camera.h"
37 
38 template <class T> class vpgl_essential_matrix;
39 template <class T> class vpgl_calibration_matrix;
40 
41 template <class T>
43 {
44  public:
45  // Constructors:----------------------
46 
47  //: Default constructor creates dummy rank 2 matrix.
49 
50  //: Main constructor takes two projective cameras.
51  // The RHS of the fundamental matrix will correspond to cr and the LHS to cl.
53  const vpgl_proj_camera<T>& cl ) : cached_svd_(nullptr)
54  { set_matrix( cr, cl ); }
55 
56  //: Construct from a fundamental matrix in vnl form.
58  { set_matrix( F ); }
59 
60  //: Copy Constructor
62 
63  //: Construct from an essential matrix and two calibration matrices.
64  // Since E = Kl^T * F * Kr, then F = Kl^-T * E * Kr^-1.
65  // WARNING! This constructor uses two 3x3 inverse calculations, so it is expensive.
68  const vpgl_essential_matrix<T> &em);
69  //: Assignment
71 
72  //: Destructor
73  virtual ~vpgl_fundamental_matrix();
74 
75  // Basic Operations:-------------------
76 
77  //: Put the coordinates of the epipoles in er, el.
79 
80  //: Given a point in one image, find the corresponding epipolar line in the other image.
83 
84  //: Given an epipolar line in one image, find the corresponding epipolar line in the other image.
85  // H&Z 2nd ed p. 247
88 
89  //: Gives the left camera matrix corresponding to the fundamental matrix
90  // The right camera matrix is assumed to be identity.
91  // The variables v, lambda are free parameters as described in H&Z 2nd ed p. 256.
93  const vnl_vector_fixed<T,3>& v, T lambda ) const;
94 
95  //: Alternative left camera extractor.
96  // Takes corresponding lists of image points with their world locations
97  // to determine the correct camera. Must give at least 2 pairs of correspondences.
98  // This is not a robust algorithm but this shouldn't be a problem
99  // as these correspondences will usually be picked by hand.
101  const std::vector< vgl_point_3d<T> >& world_points,
102  const std::vector< vgl_point_2d<T> >& image_points ) const;
103 
104  // Getters and Setters:----------------
105 
106  //: Get a copy of the FM in vnl form.
107  const vnl_matrix_fixed<T,3,3>& get_matrix() const { return F_; }
108 
109  //: Get a copy of the svd of the fundamental matrix.
110  // The svd is computed when the matrix is first set, so this just accesses a cached version.
111  const vnl_svd<T>& svd() const{ return *cached_svd_; }
112 
113  void set_matrix( const vpgl_proj_camera<T>& cr,
114  const vpgl_proj_camera<T>& cl );
115 
116  void set_matrix( const vnl_matrix_fixed<T,3,3>& F );
117 
118  protected:
119  //: Internal representation of the fundamental matrix.
121 
122  //: Cached copy of the svd.
124 };
125 
126 //: Write vpgl_fundamental_matrix to stream
127 template <class T>
128 std::ostream& operator<<(std::ostream& s, vpgl_fundamental_matrix<T> const& p);
129 
130 //: Read vpgl_fundamental_matrix from stream
131 template <class T>
132 std::istream& operator>>(std::istream& s, vpgl_fundamental_matrix<T>& p);
133 
134 #endif // vpgl_fundamental_matrix_h_
const vnl_svd< T > & svd() const
Get a copy of the svd of the fundamental matrix.
vgl_homg_line_2d< T > l_epipolar_line(const vgl_homg_point_2d< T > &pr) const
std::istream & operator>>(std::istream &is, vpgl_local_rational_camera< T > &p)
Read from stream.
void get_epipoles(vgl_homg_point_2d< T > &er, vgl_homg_point_2d< T > &el) const
Put the coordinates of the epipoles in er, el.
A class representing the "K" matrix of a perspective camera matrix as described in.
const vpgl_fundamental_matrix< T > & operator=(const vpgl_fundamental_matrix< T > &fm)
Assignment.
vnl_matrix_fixed< T, 3, 3 > F_
Internal representation of the fundamental matrix.
vnl_svd< T > * cached_svd_
Cached copy of the svd.
vpgl_fundamental_matrix(const vpgl_proj_camera< T > &cr, const vpgl_proj_camera< T > &cl)
Main constructor takes two projective cameras.
vpgl_fundamental_matrix()
Default constructor creates dummy rank 2 matrix.
std::ostream & operator<<(std::ostream &s, const vpgl_local_rational_camera< T > &p)
Write to stream.
A camera model using the standard 3x4 matrix representation.
vpgl_fundamental_matrix(const vnl_matrix_fixed< T, 3, 3 > &F)
Construct from a fundamental matrix in vnl form.
vpgl_proj_camera< T > extract_left_camera(const vnl_vector_fixed< T, 3 > &v, T lambda) const
Gives the left camera matrix corresponding to the fundamental matrix.
virtual ~vpgl_fundamental_matrix()
Destructor.
vgl_homg_line_2d< T > r_epipolar_line(const vgl_homg_point_2d< T > &pl) const
Given a point in one image, find the corresponding epipolar line in the other image.
const vnl_matrix_fixed< T, 3, 3 > & get_matrix() const
Get a copy of the FM in vnl form.
void set_matrix(const vpgl_proj_camera< T > &cr, const vpgl_proj_camera< T > &cl)