contrib/rpl/rrel/rrel_homography2d_est.h
Go to the documentation of this file.
00001 #ifndef rrel_homography2d_est_h_
00002 #define rrel_homography2d_est_h_
00003 
00004 //:
00005 // \file
00006 // \author Chuck Stewart
00007 // \date March 2001
00008 // \brief Class to maintain data and optimization model for 2d homography estimation
00009 
00010 #include <vcl_vector.h>
00011 #include <vgl/vgl_fwd.h>
00012 #include <vnl/vnl_fwd.h>
00013 
00014 #include <rrel/rrel_estimation_problem.h>
00015 
00016 //: Class to maintain data and optimization model for 2d homography estimation.
00017 //  This class assumes each point has a unique correspondence, even
00018 //  though it may be incorrect.   This is the usual assumption used in
00019 //  2d homography estimation.  It probably isn't the best thing to do
00020 //  in practice, though, because correspondences are hard to find
00021 //  without knowing the transformation and robust estimation can pick
00022 //  out the correct correspondences even when they aren't unique.
00023 //
00024 //  The corresponding data points are provided as a vectors of
00025 //  vgl_homg_point_2d.  Corresponding points are assumed to share the
00026 //  same index in the two vectors.
00027 //
00028 //  Several aspects of this class aren't quite up with the "best"
00029 //  techniques in the literature, although the practical significance
00030 //  of this is known to be quite limited.  First, the symmetric
00031 //  transfer error is used in computing residuals.  Second, the
00032 //  weighted least-squares fit is just a robust version of Hartley's
00033 //  normalized 8-point algorithm.  More sophisticated versions could
00034 //  be developed, but this class was written mostly for demonstration
00035 //  purposes.
00036 
00037 
00038 class rrel_homography2d_est : public rrel_estimation_problem
00039 {
00040  public:
00041 
00042   //: Constructor from vgl_homg_point_2d's
00043   //  By default, we want a full 8-DOF homography
00044   rrel_homography2d_est( const vcl_vector< vgl_homg_point_2d<double> > & from_pts,
00045                          const vcl_vector< vgl_homg_point_2d<double> > & to_pts,
00046                          unsigned int homog_dof = 8 );
00047 
00048   //: Constructor from vnl_vectors
00049   //  By default, we want a full 8-DOF homography
00050   rrel_homography2d_est( const vcl_vector< vnl_vector<double> > & from_pts,
00051                          const vcl_vector< vnl_vector<double> > & to_pts,
00052                          unsigned int homog_dof = 8 );
00053 
00054   //: Destructor.
00055   virtual ~rrel_homography2d_est();
00056 
00057   //: Total number of correspondences.
00058   unsigned int num_samples( ) const;
00059 
00060   //: The degrees of freedom in the residual.
00061   // Each coordinate of the correspondence pair has Gaussian error, so
00062   // the Euclidean distance residual has 4 degrees of freedom.
00063   unsigned int residual_dof() const { return 4; }
00064 
00065   //: Generate a parameter estimate from a minimal sample.
00066   bool fit_from_minimal_set( const vcl_vector<int>& point_indices,
00067                              vnl_vector<double>& params ) const;
00068 
00069   //: Compute unsigned fit residuals relative to the parameter estimate.
00070   void compute_residuals( const vnl_vector<double>& params,
00071                           vcl_vector<double>& residuals ) const;
00072 
00073   //: Weighted least squares parameter estimate.  The normalized covariance is not yet filled in.
00074   bool weighted_least_squares_fit( vnl_vector<double>& params,
00075                                    vnl_matrix<double>& norm_covar,
00076                                    const vcl_vector<double>* weights=0 ) const;
00077 
00078   //: Convert a homography to a linear parameter list (for estimation).
00079   //  Overloaded for specialized reduced-DOF homographies (i.e. affine)
00080   virtual void  homog_to_params(const vnl_matrix<double>&  m,
00081                                 vnl_vector<double>&        p) const;
00082 
00083   //: Convert a linear parameter list (from estimation) to a homography.
00084   //  Overloaded for specialized reduced-DOF homographies (i.e. affine)
00085   virtual void  params_to_homog(const vnl_vector<double>&  p,
00086                                 vnl_matrix<double>&        m) const;
00087 
00088  public:  // testing / debugging utility
00089   //: \brief Print information as a test utility.
00090   void print_points() const;
00091 
00092  protected:
00093   void normalize( const vcl_vector< vnl_vector<double> >& pts,
00094                   const vcl_vector< double >& wgts,
00095                   vcl_vector< vnl_vector<double> > & norm_pts,
00096                   vnl_matrix< double > & norm_matrix ) const;
00097 
00098  protected:
00099   vcl_vector< vnl_vector<double> > from_pts_;
00100   vcl_vector< vnl_vector<double> > to_pts_;
00101   unsigned int homog_dof_;
00102   unsigned int min_num_pts_;
00103 };
00104 
00105 #endif // rrel_homography2d_est_h_