contrib/rpl/rgrl/rgrl_est_proj_func.h
Go to the documentation of this file.
00001 #ifndef rgrl_est_proj_func_h_
00002 #define rgrl_est_proj_func_h_
00003 
00004 //:
00005 // \file
00006 // \author Gehua Yang
00007 // \date   March 2007
00008 // \brief  a generic class to estimate a homogeneous projection matrix using L-M
00009 
00010 #include <vnl/vnl_least_squares_function.h>
00011 #include <vnl/vnl_vector_fixed.h>
00012 #include <rgrl/rgrl_fwd.h>
00013 #include <rgrl/rgrl_match_set_sptr.h>
00014 
00015 //: mapping of homogeneous points
00016 template <unsigned int Tdim, unsigned int Fdim>
00017 inline
00018 void
00019 rgrl_est_proj_map_homo_point( vnl_vector_fixed<double, Tdim+1>& mapped,
00020                 vnl_matrix_fixed<double, Tdim+1, Fdim+1> const& proj,
00021                 vnl_vector_fixed<double, Fdim> const& loc )
00022 {
00023   for ( unsigned i=0; i<Tdim+1; ++i ) {
00024     // shift term
00025     mapped[i] = proj(i, Fdim);
00026 
00027     for ( unsigned j=0; j<Fdim; ++j )
00028       mapped[i] += loc[j] * proj(i,j);
00029   }
00030 }
00031 
00032 //: mapping of inhomogeneous points
00033 template <unsigned int Tdim, unsigned int Fdim>
00034 inline
00035 void
00036 rgrl_est_proj_map_inhomo_point( vnl_vector_fixed<double, Tdim>& mapped,
00037                   vnl_matrix_fixed<double, Tdim+1, Fdim+1> const& proj,
00038                   vnl_vector_fixed<double, Fdim> const& loc )
00039 {
00040   vnl_vector_fixed<double, Tdim+1> tmp;
00041 
00042   // map homo point
00043   for ( unsigned i=0; i<Tdim+1; ++i ) {
00044     // shift term
00045     tmp[i] = proj(i, Fdim);
00046 
00047     for ( unsigned j=0; j<Fdim; ++j )
00048       tmp[i] += loc[j] * proj(i,j);
00049   }
00050 
00051   // get inhomo point
00052   for ( unsigned i=0; i<Tdim; ++i )
00053     mapped[i] = tmp[i]/tmp[Tdim];
00054 }
00055 
00056 //:a generic class to estimate a homogeneous projection matrix using L-M
00057 template <unsigned int Tdim, unsigned int Fdim>
00058 class rgrl_est_proj_func
00059 : public vnl_least_squares_function
00060 {
00061  public:
00062   //: ctor
00063   rgrl_est_proj_func( rgrl_set_of<rgrl_match_set_sptr> const& matches,
00064                       bool with_grad = true );
00065 
00066   //: ctor without matches
00067   rgrl_est_proj_func( bool with_grad = true );
00068 
00069    void set_centres( vnl_vector_fixed<double, Fdim> const& fc,
00070                      vnl_vector_fixed<double, Tdim> const& tc )
00071    {
00072      from_centre_ = fc;
00073      to_centre_ = tc;
00074    }
00075 
00076   //: set max number of iterations
00077   void set_max_num_iter( int max )
00078   { max_num_iterations_ = max; }
00079 
00080   //: return max number of iterations
00081   int max_num_iter() const
00082   { return max_num_iterations_; }
00083 
00084   //: set relative threshold for parameters change
00085   void set_rel_thres( double thres )
00086   { relative_threshold_ = thres; }
00087 
00088   //: relative threshold
00089   double rel_thres() const
00090   { return relative_threshold_; }
00091 
00092   //: set zero singular value threshold for SVD
00093   void set_zero_svd_thres( double thres )
00094   { zero_svd_thres_ = thres; }
00095 
00096   //: zero singular value threshold
00097   double zero_svd_thres() const
00098   { return zero_svd_thres_; }
00099 
00100   //: estimate the projective transformation and the associated covariance
00101   bool
00102   projective_estimate( vnl_matrix_fixed<double, Tdim+1, Fdim+1>& proj,
00103                        vnl_matrix<double>& full_covar,
00104                        vnl_vector_fixed<double, Fdim>& from_centre,
00105                        vnl_vector_fixed<double, Tdim>& to_centre );
00106 
00107   //: obj func value
00108   void f(vnl_vector<double> const& x, vnl_vector<double>& fx);
00109 
00110   //: Jacobian
00111   void gradf(vnl_vector<double> const& x, vnl_matrix<double>& jacobian);
00112 
00113   //: uncentre projection matrix
00114   vnl_matrix_fixed<double, Tdim+1, Fdim+1>
00115   uncentre_proj( vnl_matrix_fixed<double, Tdim+1, Fdim+1> const& proj ) const;
00116 
00117   //: map a location
00118   inline
00119   void
00120   map_loc( vnl_vector_fixed<double, Tdim>& mapped,
00121            vnl_matrix_fixed<double, Tdim+1, Fdim+1> const& proj,
00122            vnl_vector_fixed<double, Fdim> const& from  ) const
00123   {
00124     rgrl_est_proj_map_inhomo_point<Tdim, Fdim>( mapped, proj, from-from_centre_ );
00125     mapped += to_centre_;
00126   }
00127 
00128  protected:
00129   //: compute jacobian
00130   void
00131   reduced_proj_jacobian( vnl_matrix_fixed<double, Tdim, (Fdim+1)*(Tdim+1)-1>& base_jac,
00132                          vnl_matrix_fixed<double, Tdim+1, Fdim+1> const& proj,
00133                          vnl_vector_fixed<double, Fdim>           const& from ) const;
00134 
00135   //: compute jacobian
00136   void
00137   full_proj_jacobian( vnl_matrix_fixed<double, Tdim, (Fdim+1)*(Tdim+1)>& base_jac,
00138                       vnl_matrix_fixed<double, Tdim+1, Fdim+1> const& proj,
00139                       vnl_vector_fixed<double, Fdim>           const& from ) const;
00140 
00141   //: compute jacobian w.r.t. location
00142   void
00143   proj_jac_wrt_loc( vnl_matrix_fixed<double, Tdim, Fdim>          & jac_loc,
00144                     vnl_matrix_fixed<double, Tdim+1, Fdim+1> const& proj,
00145                     vnl_vector_fixed<double, Fdim>           const& from ) const;
00146 
00147   //: convert parameters
00148   void convert_parameters( vnl_vector<double>& params,
00149                            vnl_matrix_fixed<double, Tdim+1, Fdim+1> proj_matrix,
00150                            vnl_vector_fixed<double, Fdim> const& fc,
00151                            vnl_vector_fixed<double, Tdim> const& tc );
00152   //: convert parameters
00153   void restored_centered_proj( vnl_matrix_fixed<double, Tdim+1, Fdim+1>& proj_matrix,
00154                                vnl_vector<double> const& params ) const;
00155 
00156  protected:
00157   static const unsigned int proj_size_ = (Tdim+1)*(Fdim+1);
00158 
00159   rgrl_set_of<rgrl_match_set_sptr> const* matches_ptr_;
00160   vnl_vector_fixed<double, Fdim>          from_centre_;
00161   vnl_vector_fixed<double, Tdim>          to_centre_;
00162 
00163   unsigned int index_row_;
00164   unsigned int index_col_;
00165   //: specify the maximum number of iterations for this estimator
00166   int max_num_iterations_;
00167 
00168   //: The threshold for relative parameter change before termination
00169   double relative_threshold_;
00170 
00171   //: zero singular value threshold
00172   double zero_svd_thres_;
00173 };
00174 
00175 #if 0
00176 //: a generic class to estimate projection matrices using L-M
00177 //  Includes 2D homography, 3D homography, camera matrix
00178 
00179 template <unsigned int Tdim, unsigned int Fdim>
00180 class rgrl_est_proj_lm
00181   : public rgrl_nonlinear_estimator
00182 {
00183  public:
00184   //: Default constructor
00185   rgrl_est_proj_lm( bool with_grad = true );
00186 
00187 
00188   // Defines type-related functions
00189   rgrl_type_macro( rgrl_est_proj_lm, rgrl_nonlinear_estimator );
00190 
00191  protected:
00192 
00193   bool with_grad_;
00194   //vnl_vector_fixed<double, Fdim> from_camera_centre_;
00195   //vnl_vector_fixed<double, Tdim> to_camera_centre_;
00196 };
00197 #endif // 0
00198 
00199 #endif //rgrl_est_proj_func_h_