contrib/rpl/rgrl/rgrl_est_proj_rad_func.h
Go to the documentation of this file.
00001 #ifndef rgrl_est_proj_rad_func_h_
00002 #define rgrl_est_proj_rad_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 with radial distortion parameter(s)  using L-M
00009 
00010 #include <vnl/vnl_vector_fixed.h>
00011 #include <rgrl/rgrl_fwd.h>
00012 #include <rgrl/rgrl_match_set_sptr.h>
00013 #include <rgrl/rgrl_est_proj_func.h>
00014 #include <vcl_vector.h>
00015 
00016 template <unsigned int Tdim, unsigned int Fdim>
00017 class rgrl_est_proj_rad_func
00018 : public rgrl_est_proj_func<Tdim, Fdim>
00019 {
00020  public:
00021   typedef rgrl_est_proj_func<Tdim, Fdim> base_type;
00022  public:
00023   //: ctor
00024   rgrl_est_proj_rad_func( rgrl_set_of<rgrl_match_set_sptr> const& matches,
00025                           unsigned int camera_dof,
00026                           bool with_grad = true );
00027 
00028   //: ctor without matches
00029   rgrl_est_proj_rad_func( unsigned int camera_dof,
00030                           bool with_grad = true );
00031 
00032   //: estimate the projective transformation and the associated covariance
00033   bool
00034   projective_estimate( vnl_matrix_fixed<double, Tdim+1, Fdim+1>& proj,
00035                        vcl_vector<double>& rad_dist,
00036                        vnl_matrix<double>& full_covar,
00037                        vnl_vector_fixed<double, Fdim>& from_centre,
00038                        vnl_vector_fixed<double, Tdim>& to_centre,
00039                        vnl_vector_fixed<double, Tdim> const& camera_centre);
00040 
00041   //: obj func value
00042   void f(vnl_vector<double> const& x, vnl_vector<double>& fx);
00043 
00044   //: Jacobian
00045   void gradf(vnl_vector<double> const& x, vnl_matrix<double>& jacobian);
00046 
00047   //: set image centre
00048   void
00049   set_image_centre( vnl_vector_fixed<double, Tdim> const& image_centre )
00050   {
00051     centre_mag_norm_const_
00052       = image_centre.squared_magnitude() / 400;
00053     if( centre_mag_norm_const_ < 1.0 )
00054       centre_mag_norm_const_ = 1.0;
00055    image_centre_ = image_centre-this->to_centre_;
00056   }
00057 
00058   //: set from, to, and image centres
00059   void
00060   set_centres( vnl_vector_fixed<double, Fdim> const& from_centre,
00061                vnl_vector_fixed<double, Tdim> const& to_centre,
00062                vnl_vector_fixed<double, Tdim> const& image_centre )
00063   {
00064     rgrl_est_proj_func<Tdim, Fdim>::set_centres( from_centre, to_centre );
00065     centre_mag_norm_const_
00066       = image_centre.squared_magnitude() / 400;
00067     if( centre_mag_norm_const_ < 1.0 )
00068       centre_mag_norm_const_ = 1.0;
00069     image_centre_ = image_centre-this->to_centre_;
00070   }
00071 
00072   //: get centred coordinate normalization constant
00073   double
00074   centre_mag_norm_const () const
00075   { return centre_mag_norm_const_; }
00076 
00077   //: map a location
00078   inline
00079   void
00080   map_loc( vnl_vector_fixed<double, Tdim>& mapped,
00081            vnl_matrix_fixed<double, Tdim+1, Fdim+1> const& proj,
00082            vcl_vector<double> const& rad_k,
00083            vnl_vector_fixed<double, Fdim> const& from  ) const
00084   {
00085 
00086     vnl_vector_fixed<double, Tdim> proj_mapped;
00087     rgrl_est_proj_map_inhomo_point<Tdim, Fdim>( proj_mapped, proj, from-this->from_centre_ );
00088 
00089     // apply radial distortion
00090     apply_radial_distortion( mapped, proj_mapped, rad_k );
00091 
00092     mapped += this->to_centre_;
00093   }
00094 
00095 protected:
00096 
00097   //: compute jacobian
00098   void
00099   reduced_proj_rad_jacobian( vnl_matrix<double>                            & base_jac,
00100                              vnl_matrix_fixed<double, Tdim+1, Fdim+1> const& proj,
00101                              vcl_vector<double>                       const& rad_k,
00102                              vnl_vector_fixed<double, Fdim>           const& from ) const;
00103 
00104   //: compute the full jacobian
00105   void
00106   full_proj_rad_jacobian( vnl_matrix<double>                            & base_jac,
00107                           vnl_matrix_fixed<double, Tdim+1, Fdim+1> const& proj,
00108                           vcl_vector<double>                       const& rad_k,
00109                           vnl_vector_fixed<double, Fdim>           const& from ) const;
00110 
00111   //: compute jacobian w.r.t. location
00112   void
00113   proj_jac_wrt_loc( vnl_matrix_fixed<double, Tdim, Fdim>          & jac_loc,
00114                     vnl_matrix_fixed<double, Tdim+1, Fdim+1> const& proj,
00115                     vcl_vector<double>                       const& rad_k,
00116                     vnl_vector_fixed<double, Fdim>           const& from ) const;
00117 
00118   //: convert parameters
00119   void
00120   convert_parameters( vnl_vector<double>& params,
00121                       vnl_matrix_fixed<double, Tdim+1, Fdim+1> const& proj_matrix,
00122                       vcl_vector<double>             const& rad_dist,
00123                       vnl_vector_fixed<double, Fdim> const& fc,
00124                       vnl_vector_fixed<double, Tdim> const& tc,
00125                       vnl_vector_fixed<double, Tdim> const& camera_centre );
00126 
00127   void
00128   apply_radial_distortion( vnl_vector_fixed<double, Tdim>      & mapped,
00129                            vnl_vector_fixed<double, Tdim> const& p,
00130                            vcl_vector<double> const& rad_k ) const;
00131 
00132   //: transfer parameters into the temp vector
00133   void
00134   transfer_radial_params_into_temp_storage( vnl_vector<double> const& params ) const;
00135 
00136 protected:
00137   unsigned int                    camera_dof_;
00138   vnl_vector_fixed<double, Tdim>  image_centre_;
00139   double                          centre_mag_norm_const_;
00140   mutable vcl_vector<double>  temp_rad_k_;
00141 };
00142 
00143 #endif //rgrl_est_proj_rad_func_h_