contrib/rpl/rgrl/rgrl_initializer_ran_sam.h
Go to the documentation of this file.
00001 #ifndef rgrl_initializer_ran_sam_h_
00002 #define rgrl_initializer_ran_sam_h_
00003 //:
00004 // \file
00005 // \brief Generate the initial estimate using random sampling
00006 // \author Charlene Tsai
00007 // \date Sep 2002
00008 
00009 #include "rgrl_initializer.h"
00010 #include "rgrl_match_set_sptr.h"
00011 #include "rgrl_scale_sptr.h"
00012 #include "rgrl_estimator_sptr.h"
00013 #include "rgrl_scale_estimator_sptr.h"
00014 #include "rgrl_mask.h"
00015 
00016 #include <vcl_vector.h>
00017 
00018 class vnl_random;
00019 
00020 //: Generate the initial estimate using random sampling.
00021 //  This is the re-implementation of rrel_ran_sam_search.
00022 //  Modifications include using unweighted scale estimator
00023 //  to perform selection of the best transformation.
00024 //
00025 class rgrl_initializer_ran_sam
00026   : public rgrl_initializer
00027 {
00028  public:
00029   //: Constructor using a non-deterministic random-sampling seed.
00030   rgrl_initializer_ran_sam( );
00031 
00032   //: Constructor using a given random-sampling seed.
00033   rgrl_initializer_ran_sam( int seed );
00034 
00035   virtual
00036   ~rgrl_initializer_ran_sam();
00037 
00038   //  Parameters to control the search technique.  The default set
00039   //  when the constructor is called is to sample as in generate
00040   //  samples as specified in least-median of squares.
00041 
00042   //: Indicate that all possible minimal subset samples should be tried.
00043   void set_gen_all_samples();
00044 
00045   //: Set the parameters for random sampling.
00046   void set_sampling_params( double max_outlier_frac = 0.5,
00047                             double desired_prob_good = 0.99,
00048                             unsigned int max_populations_expected = 1,
00049                             unsigned int min_samples = 0 );
00050 
00051   //: Initialize the data with a view, which contains the regions and the transformation estimator.
00052   //
00053   // If \a should_estimate_global_region is true, the \a
00054   // from_image_roi will be re-estimated/updated based on the
00055   // transform estimate computed by the random sampling process.
00056   //
00057   void set_data(rgrl_match_set_sptr                init_match_set,
00058                 rgrl_scale_estimator_unwgted_sptr  scale_est,
00059                 rgrl_view_sptr                     prior_view,
00060                 bool should_estimate_global_region = true);
00061 
00062   //: Initialize with a set of information without packing everything into a vie
00063   void set_data(rgrl_match_set_sptr                init_match_set,
00064                 rgrl_scale_estimator_unwgted_sptr  scale_est,
00065                 rgrl_mask_sptr      const&         from_image_roi,
00066                 rgrl_mask_sptr      const&         to_image_roi,
00067                 rgrl_mask_box       const&         initial_from_image_roi,
00068                 rgrl_estimator_sptr                xform_estimator,
00069                 unsigned                           initial_resolution = 0,
00070                 bool should_estimate_global_region = true);
00071 
00072   //: Initialize with a set of information, assuming that registration applies to \a from_image_roi always.
00073   void set_data(rgrl_match_set_sptr                init_match_set,
00074                 rgrl_scale_estimator_unwgted_sptr  scale_est,
00075                 rgrl_mask_sptr      const&         from_image_roi,
00076                 rgrl_mask_sptr      const&         to_image_roi,
00077                 rgrl_estimator_sptr                xform_estimator,
00078                 unsigned                           initial_resolution = 0);
00079 
00080   //: Initialize with a set of information, assuming that registration applies to \a from_image_roi always;
00081   //  And \a from_image_roi and \a to_image_roi are the same
00082   void set_data(rgrl_match_set_sptr                init_match_set,
00083                 rgrl_scale_estimator_unwgted_sptr  scale_est,
00084                 rgrl_mask_sptr      const&         from_image_roi,
00085                 rgrl_estimator_sptr                xform_estimator,
00086                 unsigned                           initial_resolution = 0);
00087 
00088 
00089   //: Get next initial estimate when first called, but return false thereafter.
00090   bool next_initial( rgrl_view_sptr           & view,
00091                      rgrl_scale_sptr          & prior_scale);
00092 
00093   //:  Get the scale estimate.
00094   rgrl_scale_sptr scale() const { return scale_; }
00095 
00096   //:  Get the parameter estimate.
00097   rgrl_transformation_sptr transformation() const { return xform_; }
00098 
00099   //:  Get the number of samples tested in during estimation.
00100   int samples_tested() const { return samples_to_take_; }
00101 
00102   // Defines type-related functions
00103   rgrl_type_macro( rgrl_initializer_ran_sam, rgrl_initializer );
00104 
00105  private:
00106   //: Estimate the best transform.
00107   bool estimate();
00108 
00109   //
00110   //: Calculate number of samples --- non-unique matching estimation problems
00111   void calc_num_samples( unsigned int num_matches );
00112 
00113   //: Determine the next random sample, filling in the "sample" vector.
00114   void next_sample( unsigned int taken, unsigned int num_points,
00115                     vcl_vector<int>& sample,
00116                     unsigned int points_per_sample );
00117 
00118   //: Extract the matches indexed by the point_indices
00119   rgrl_match_set_sptr get_matches(const vcl_vector<int>&  point_indices, unsigned int total_num_matches);
00120 
00121   //: For debugging
00122   void trace_sample( const vcl_vector<int>& point_indices ) const;
00123 
00124   //: return number of initializations
00125   //  -1 stands for unknown
00126   virtual int size() const
00127   { return -1;}
00128 
00129  protected:
00130   rgrl_match_set_sptr      match_set_;
00131   rgrl_estimator_sptr      transform_estiamtor_;
00132   rgrl_scale_estimator_unwgted_sptr  scale_estimator_;
00133   rgrl_view_sptr           init_view_;
00134   bool                     called_before_;    //  has next_initial been called yet?
00135   bool                     data_set_;
00136   //unsigned int num_unique_matches_;
00137 
00138  private:
00139   // Parameters
00140   //
00141   double max_outlier_frac_;
00142   double desired_prob_good_;
00143   unsigned int max_populations_expected_;
00144   unsigned int min_samples_;
00145   bool generate_all_;
00146   bool should_estimate_global_region_;
00147 
00148   //: Random number generator.
00149   // Normally, this will point to the "global" generator, but a could
00150   // point to a local one if the user wants to specify a seed.
00151   vnl_random* generator_;
00152   bool own_generator_;
00153 
00154   // The estimate
00155   //
00156   rgrl_transformation_sptr xform_;
00157   rgrl_scale_sptr scale_;
00158 
00159   // Sampling variables
00160   //
00161   unsigned int samples_to_take_;
00162 };
00163 
00164 #endif