contrib/rpl/rgrl/rgrl_est_homo2d_proj.cxx
Go to the documentation of this file.
00001 #include "rgrl_est_homo2d_proj.h"
00002 //:
00003 // \file
00004 #include <rgrl/rgrl_est_homography2d.h>
00005 #include <rgrl/rgrl_est_proj_func.h>
00006 #include <rgrl/rgrl_trans_homography2d.h>
00007 #include <rgrl/rgrl_match_set.h>
00008 #include <rgrl/rgrl_internal_util.h>
00009 
00010 #include <vnl/vnl_double_2.h>
00011 
00012 // --------------------------------------------------------------------
00013 
00014 rgrl_est_homo2d_proj::
00015 rgrl_est_homo2d_proj( bool with_grad )
00016   : with_grad_( with_grad )
00017 {
00018    rgrl_estimator::set_param_dof( 8 );
00019 
00020   // default value
00021   rgrl_nonlinear_estimator::set_max_num_iter( 50 );
00022   rgrl_nonlinear_estimator::set_rel_thres( 1e-5 );
00023 }
00024 
00025 rgrl_transformation_sptr
00026 rgrl_est_homo2d_proj::
00027 estimate( rgrl_set_of<rgrl_match_set_sptr> const& matches,
00028           rgrl_transformation const& cur_transform ) const
00029 {
00030   // get initialization
00031   vnl_matrix_fixed<double, 3, 3> init_H;
00032 
00033   if ( !rgrl_internal_util_upgrade_to_homography2D( init_H, cur_transform ) )
00034   {
00035     // use normalized DLT to initialize
00036     DebugMacro( 0, "Use normalized DLT to initialize" );
00037     rgrl_est_homography2d est_homo;
00038     rgrl_transformation_sptr tmp_trans= est_homo.estimate( matches, cur_transform );
00039     if ( !tmp_trans )
00040       return 0;
00041     rgrl_trans_homography2d const& trans = static_cast<rgrl_trans_homography2d const&>( *tmp_trans );
00042     init_H = trans.H();
00043   }
00044 
00045   // construct least square cost function
00046   rgrl_est_proj_func<2,2> homo_func( matches, with_grad_ );
00047   homo_func.set_max_num_iter( max_num_iterations_ );
00048   homo_func.set_rel_thres( relative_threshold_ );
00049 
00050 
00051   // apply estimation
00052   vnl_double_2 from_centre, to_centre;
00053   vnl_matrix<double> covar;
00054   if ( !homo_func.projective_estimate( init_H, covar, from_centre, to_centre ) ) {
00055     WarningMacro( "L-M estimation failed." << vcl_endl );
00056     return 0;
00057   }
00058 
00059   return new rgrl_trans_homography2d( init_H.as_ref(), covar, from_centre.as_ref(), to_centre.as_ref() );
00060 }
00061 
00062 
00063 const vcl_type_info&
00064 rgrl_est_homo2d_proj::
00065 transformation_type() const
00066 {
00067   return rgrl_trans_homography2d::type_id();
00068 }