vpgl_em_compute_5_point.h
Go to the documentation of this file.
1 // This is core/vpgl/algo/vpgl_em_compute_5_point.h
2 #ifndef vpgl_em_compute_5_point_h_
3 #define vpgl_em_compute_5_point_h_
4 //:
5 // \file
6 // \brief The 5 point algorithm as described by David Nister for computing an essential matrix from point correspondences.
7 // \author Noah Snavely, ported to VXL by Andrew Hoelscher
8 // \date April 24, 2011
9 //
10 // \verbatim
11 // Modifications
12 // August 31, 2011 Andrew Hoelscher Added a ransac routine
13 // \endverbatim
14 
15 #include <vector>
16 #ifdef _MSC_VER
17 # include <vcl_msvc_warnings.h>
18 #endif
19 
20 #include <vnl/vnl_matrix.h>
21 #include <vnl/vnl_vector_fixed.h>
23 
24 #include <vgl/vgl_point_2d.h>
25 
28 
29 
30 template <class T>
32 {
33  public:
34 
35  vpgl_em_compute_5_point(): verbose(false), tolerance(0.0001) { }
36  vpgl_em_compute_5_point(bool v): verbose(v), tolerance(0.0001) { }
37  vpgl_em_compute_5_point(bool v, double t): verbose(v), tolerance(t) { }
38 
39  //: Compute from two sets of corresponding points.
40  // Puts the resulting matrix into em, returns true if successful.
41  // Each of right_points and left_points must contain exactly 5 points!
42  // This function returns a set of potential solutions, generally 10.
43  // Each of these solutions is appropriate to use as RANSAC hypthosis.
44  //
45  // The points must be normalized!! Use the function below to avoid
46  // normalizing the points yourself.
47  bool compute( const std::vector<vgl_point_2d<T> > &normed_right_points,
48  const std::vector<vgl_point_2d<T> > &normed_left_points,
49  std::vector<vpgl_essential_matrix<T> > &ems) const;
50 
51  //Same as above, but performs the normalization using the two
52  // calibration matrices.
53  bool compute( const std::vector<vgl_point_2d<T> > &right_points,
54  const vpgl_calibration_matrix<T> &k_right,
55  const std::vector<vgl_point_2d<T> > &left_points,
56  const vpgl_calibration_matrix<T> &k_left,
57  std::vector<vpgl_essential_matrix<T> > &ems) const;
58 
59  protected:
60  const bool verbose;
61  const double tolerance;
62 
63  void normalize(
64  const std::vector<vgl_point_2d<T> > &points,
66  std::vector<vgl_point_2d<T> > &normed_points) const;
67 
69  const std::vector<vgl_point_2d<T> > &right_points,
70  const std::vector<vgl_point_2d<T> > &left_points,
71  std::vector<vnl_vector_fixed<T, 9> > &basis) const;
72 
74  const std::vector<vnl_vector_fixed<T,9> > &basis,
75  std::vector<vnl_real_npolynomial> &constraints) const;
76 
78  const std::vector<vnl_real_npolynomial> &constraints,
79  vnl_matrix<double> &groebner_basis) const;
80 
82  const vnl_matrix<double> &groebner_basis,
83  vnl_matrix<double> &action_matrix) const;
84 
85  void compute_e_matrices(
86  const std::vector<vnl_vector_fixed<T, 9> > &basis,
87  const vnl_matrix<double> &action_matrix,
88  std::vector<vpgl_essential_matrix<T> > &ems) const;
89 
90  double get_coeff(
91  const vnl_real_npolynomial &p,
92  unsigned int x_p, unsigned int y_p, unsigned int z_p) const;
93 };
94 
95 
96 template <class T>
98 {
99  public:
101  num_rounds(512u), inlier_threshold(2.25), verbose(false) { }
102 
103  vpgl_em_compute_5_point_ransac(unsigned nr, double trsh, bool v) :
104  num_rounds(nr), inlier_threshold(trsh), verbose(v) { }
105 
106  bool compute(
107  std::vector<vgl_point_2d<T> > const& right_points,
108  vpgl_calibration_matrix<T> const& right_k,
109  std::vector<vgl_point_2d<T> > const& left_points,
110  vpgl_calibration_matrix<T> const& left_k,
111 
112  vpgl_essential_matrix<T> &best_em) const;
113 
114 
115  private:
116  const unsigned num_rounds;
117  const double inlier_threshold;
118  const bool verbose;
119 };
120 
121 #endif // vpgl_em_compute_5_point_h_
A class for the essential matrix between two projective cameras.
void compute_nullspace_basis(const std::vector< vgl_point_2d< T > > &right_points, const std::vector< vgl_point_2d< T > > &left_points, std::vector< vnl_vector_fixed< T, 9 > > &basis) const
Constructs the 5x9 epipolar constraint matrix based off the constraint that q1' * E * q2 = 0,...
void compute_groebner_basis(const std::vector< vnl_real_npolynomial > &constraints, vnl_matrix< double > &groebner_basis) const
A class representing the "K" matrix of a perspective camera matrix as described in.
#define v
double get_coeff(const vnl_real_npolynomial &p, unsigned int x_p, unsigned int y_p, unsigned int z_p) const
Returns the coefficient of a term of a vnl_real_npolynomial in three variables with an x power of x_p...
bool compute(std::vector< vgl_point_2d< T > > const &right_points, vpgl_calibration_matrix< T > const &right_k, std::vector< vgl_point_2d< T > > const &left_points, vpgl_calibration_matrix< T > const &left_k, vpgl_essential_matrix< T > &best_em) const
vpgl_em_compute_5_point(bool v, double t)
void compute_constraint_polynomials(const std::vector< vnl_vector_fixed< T, 9 > > &basis, std::vector< vnl_real_npolynomial > &constraints) const
Finds 10 constraint polynomials, based on the following criteria: if X, Y, Z and W are the basis vect...
void normalize(const std::vector< vgl_point_2d< T > > &points, const vpgl_calibration_matrix< T > &k, std::vector< vgl_point_2d< T > > &normed_points) const
Normalization is the process of left-multiplying by the inverse of the calibration matrix.
void compute_action_matrix(const vnl_matrix< double > &groebner_basis, vnl_matrix< double > &action_matrix) const
void compute_e_matrices(const std::vector< vnl_vector_fixed< T, 9 > > &basis, const vnl_matrix< double > &action_matrix, std::vector< vpgl_essential_matrix< T > > &ems) const
bool compute(const std::vector< vgl_point_2d< T > > &normed_right_points, const std::vector< vgl_point_2d< T > > &normed_left_points, std::vector< vpgl_essential_matrix< T > > &ems) const
Compute from two sets of corresponding points.
vpgl_em_compute_5_point_ransac(unsigned nr, double trsh, bool v)
A class for the calibration matrix component of a perspective camera matrix.