contrib/rpl/rgrl/rgrl_debug_util.cxx
Go to the documentation of this file.
00001 #include "rgrl_debug_util.h"
00002 //:
00003 // \file
00004 // \brief Utility functions/classes to help debug registration
00005 //
00006 // Current implementations only consider featured-based registration.
00007 // For the view-based registration, it should be fairly easy to add
00008 // the support for views.
00009 // \author Gehua Yang
00010 // \date Aug 2004
00011 
00012 #include <rgrl/rgrl_feature_based_registration.h>
00013 #include <rgrl/rgrl_transformation_sptr.h>
00014 #include <rgrl/rgrl_transformation.h>
00015 #include <rgrl/rgrl_match_set.h>
00016 
00017 #include <vcl_cstdio.h>
00018 #include <vcl_fstream.h>
00019 
00020 //: observer to view transformations at each iteration of feature-based registration engine
00021 void
00022 rgrl_debug_feature_iteration_print::
00023 execute(const rgrl_object* caller, const rgrl_event & event )
00024 {
00025   const rgrl_feature_based_registration* reg_engine =
00026     dynamic_cast<const rgrl_feature_based_registration*>(caller);
00027   if ( !reg_engine ) {
00028     vcl_cerr << "WARNING: " << __FILE__ << "(line " << __LINE__ << ")\n"
00029              << "         This iteration observer only works with rgrl_feature_based_registrion engine.\n";
00030     return;
00031   }
00032   vcl_cerr << "WARNING: rgrl_debug_feature_iteration_print::execute(): event "
00033            << event.name() << " is not used\n";
00034 
00035   // current stage
00036   vcl_cout <<"Current stage = " << reg_engine->current_stage() << vcl_endl;
00037   // current ieration of this stage
00038   vcl_cout <<"Current iteration = " << reg_engine->iterations_at_current_stage() << vcl_endl;
00039   // current transformation
00040   const rgrl_transformation_sptr& trans_sptr = reg_engine->current_transformation();
00041   if ( !trans_sptr ) {
00042     vcl_cout << "Current transformation is invalid!\n";
00043     return;
00044   } else {
00045     vcl_cout << "Current transformation =\n";
00046     trans_sptr->write( vcl_cout );
00047     vcl_cout << vcl_endl;
00048   }
00049 }
00050 
00051 //: constructor
00052 rgrl_debug_feature_iteration_save_matches::
00053 rgrl_debug_feature_iteration_save_matches( const vcl_string& path,
00054                                            const vcl_string& prefix,
00055                                            const rgrl_mask_sptr& from_roi )
00056   : path_(path), file_prefix_(prefix), from_roi_sptr_(from_roi)
00057 {
00058 }
00059 
00060 
00061 void
00062 rgrl_debug_feature_iteration_save_matches::
00063 execute(const rgrl_object* caller, const rgrl_event & event )
00064 {
00065   static char stage_buffer[31], iter_buffer[31];
00066   const rgrl_feature_based_registration* reg_engine =
00067     dynamic_cast<const rgrl_feature_based_registration*>(caller);
00068   if ( !reg_engine ) {
00069     vcl_cerr << "WARNING: " << __FILE__ << "(line " << __LINE__ << ")\n"
00070              << "         This iteration observer only works with rgrl_feature_based_registrion engine.\n";
00071     return;
00072   }
00073   vcl_cerr << "WARNING: rgrl_debug_feature_iteration_save_matches::execute(): event "
00074            << event.name() << " is not used\n";
00075 
00076   // current stage
00077   const int stage = reg_engine->current_stage();
00078   vcl_sprintf( stage_buffer, "%02d", stage );
00079   vcl_cout <<"Current stage = " << stage << vcl_endl;
00080   // current ieration of this stage
00081   const int iteration = reg_engine->iterations_at_current_stage();
00082   vcl_sprintf( iter_buffer, "%02d", iteration );
00083   vcl_cout <<"Current iteration = " << iteration << vcl_endl;
00084 
00085   // current transformation
00086   // compose file name for storing transformation
00087   vcl_string xform_name = path_ + '/' + file_prefix_ + '_' + stage_buffer + '_' + iter_buffer + ".xform";
00088   const rgrl_transformation_sptr& trans_sptr = reg_engine->current_transformation();
00089   if ( !trans_sptr )  {
00090     vcl_cout << "Current transformation is invalid!\n";
00091     return;
00092   } else {
00093     vcl_cout << "Trying to store transformation to " << xform_name << " ........\n";
00094     vcl_ofstream ofs( xform_name.c_str() );
00095     if ( !ofs ) {
00096       vcl_cerr << "ERROR: Cannot open file to write: " << xform_name << vcl_endl;
00097       return;
00098     }
00099     trans_sptr->write( ofs );
00100     ofs.close();
00101   }
00102 
00103   // Output current match sets
00104   //
00105   typedef rgrl_match_set::from_iterator  from_iter;
00106   typedef from_iter::to_iterator         to_iter;
00107 
00108   // compose file name for storing matches
00109   vcl_string matches_name = path_ + '/' + file_prefix_ + '_' + stage_buffer + '_' + iter_buffer + ".matches";
00110   vcl_cout << "Trying to store matches to " << matches_name << " ........\n";
00111   vcl_ofstream mofs( matches_name.c_str() );
00112   if ( !mofs ) {
00113     vcl_cerr << "ERROR: Cannot open file to write: " << matches_name << vcl_endl;
00114     return;
00115   }
00116 
00117   const rgrl_set_of<rgrl_match_set_sptr>& match_sets = reg_engine->current_match_sets();
00118   // output dim
00119   const unsigned dim = match_sets[0]->from_begin().from_feature()->location().size();
00120   mofs << dim << vcl_endl;
00121 
00122   for ( unsigned ms=0; ms < match_sets.size(); ++ms ) {
00123     const rgrl_match_set_sptr& match_set = match_sets[ms];
00124     //  for each from image feature being matched
00125     for ( from_iter fitr = match_set->from_begin();
00126          fitr != match_set->from_end(); ++fitr ){
00127       // skip empty ones
00128       if ( fitr.size() == 0 )  continue;
00129 
00130       // check roi
00131       if ( from_roi_sptr_ && !from_roi_sptr_->inside( fitr.from_feature()->location() ) )
00132         continue;
00133 
00134       const rgrl_feature_sptr& from_feature = fitr.from_feature();
00135       const rgrl_feature_sptr& mapped_from = fitr.mapped_from_feature();
00136       for ( to_iter titr = fitr.begin(); titr != fitr.end(); ++titr ) {
00137         //  for each match with a "to" image feature
00138         const rgrl_feature_sptr& to_feature = titr.to_feature();
00139         double error = titr.to_feature()->geometric_error( *mapped_from );
00140         double cum_wgt = titr.cumulative_weight();
00141         double sig_wgt = titr.signature_weight();
00142 
00143         // now output in the format of:
00144         // [from loc] [to loc] wgt error [mapped_to loc]
00145         mofs << from_feature->location() << " \t" << to_feature->location() << " \t"
00146              << sig_wgt << ' ' << cum_wgt << " \t" << error << " \t\t" << mapped_from->location() << vcl_endl;
00147       }
00148     }
00149   }
00150 }