contrib/brl/bseg/vpro/vpro_grid_finder_process.cxx
Go to the documentation of this file.
00001 // This is brl/bseg/vpro/vpro_grid_finder_process.cxx
00002 #include "vpro_grid_finder_process.h"
00003 //:
00004 // \file
00005 #include <vcl_string.h>
00006 #include <vcl_fstream.h>
00007 #include <vcl_iostream.h>
00008 #include <vil1/vil1_image.h>
00009 #include <vsol/vsol_line_2d.h>
00010 #include <sdet/sdet_fit_lines.h>
00011 #include <sdet/sdet_grid_finder.h>
00012 #include "vpro_edge_process.h"
00013 #include "vpro_line_fit_process.h"
00014 
00015 vpro_grid_finder_process::vpro_grid_finder_process(sdet_detector_params& dp,
00016                                                    sdet_fit_lines_params& flp,
00017                                                    sdet_grid_finder_params& gfp)
00018   : sdet_detector_params(dp), sdet_fit_lines_params(flp),
00019     sdet_grid_finder_params(gfp)
00020 {
00021   output_filename_ = "grid_points.txt";
00022 }
00023 
00024 vpro_grid_finder_process::~vpro_grid_finder_process()
00025 {
00026 }
00027 
00028 //: set the output filename
00029 void vpro_grid_finder_process::set_output_file(vcl_string filename)
00030 {
00031   output_filename_ = filename;
00032 }
00033 
00034 //------------------------------------------------------------------
00035 //: setup a pipeline for edges and line segments
00036 //
00037 bool vpro_grid_finder_process::execute()
00038 {
00039   this->clear_output();
00040   //my_spat_objs_.clear();
00041   int nimages = -1;
00042   if ((nimages = this->get_N_input_images())!=1)
00043   {
00044     vcl_cout << "In vpro_grid_finder_process::execute() - not exactly one input image ("<<nimages<<")\n";
00045     frame_scores_.push_back(0.0);
00046     return false;
00047   }
00048   vil1_image img = this->get_input_image(0);
00049   vpro_edge_process ep(*((sdet_detector_params*)this));
00050   ep.add_input_image(img);
00051   if (!ep.execute())
00052   {
00053     this->clear_input();
00054     frame_scores_.push_back(0.0);
00055     return false;
00056   }
00057   vpro_line_fit_process lfp(*((sdet_fit_lines_params*)this));
00058   lfp.add_input_topology(ep.get_output_topology());
00059   if (!lfp.execute())
00060   {
00061     this->clear_input();
00062     frame_scores_.push_back(0.0);
00063     return false;
00064   }
00065   //convert spatial objects to lines, should be in bsol ops
00066   vcl_vector<vsol_line_2d_sptr> lines;
00067   vcl_vector<vsol_spatial_object_2d_sptr> sos = lfp.get_output_spatial_objects();
00068   for (vcl_vector<vsol_spatial_object_2d_sptr>::iterator sit = sos.begin();
00069        sit!=sos.end(); sit++)
00070   {
00071     vsol_line_2d_sptr l = (*sit)->cast_to_curve()->cast_to_line();
00072     if (!l)
00073       continue;
00074     lines.push_back(l);
00075   }
00076   sdet_grid_finder gf(*((sdet_grid_finder_params*)this));
00077   gf.unset_verbose();
00078 
00079   if (!gf.set_lines(float(img.width()), float(img.height()), lines))
00080   {
00081     this->clear_input();
00082     frame_scores_.push_back(0.0);
00083     return false;
00084   }
00085   //Get the backprojected grid
00086   vcl_vector<vsol_line_2d_sptr> mapped_lines;
00087   if (!gf.compute_homography())
00088   {
00089     vcl_cout << "compute_homography() failed\n";
00090     this->clear_input();
00091     frame_scores_.push_back(0.0);
00092     return false;
00093   }
00094   if (!gf.get_backprojected_grid(mapped_lines))
00095   {
00096     this->clear_input();
00097     frame_scores_.push_back(0.0);
00098     return false;
00099   }
00100   for (vcl_vector<vsol_line_2d_sptr>::iterator lit = mapped_lines.begin();
00101        lit != mapped_lines.end(); lit++)
00102     output_spat_objs_.push_back((*lit)->cast_to_spatial_object());
00103   // double check backprojected grid with image intensities
00104   if (!gf.check_grid_match(img))
00105   {
00106     vcl_cout << "check_grid_match() failed - disregarding homography\n";
00107     this->clear_input();
00108     frame_scores_.push_back(0.0);
00109     return true;
00110   }
00111   // TEMP - Write grid points to file
00112   if (output_filename_ != "")
00113   {
00114     static int view_count = 0;
00115     static vcl_ofstream outstream(output_filename_.c_str());
00116 
00117     if (view_count == 0)
00118       gf.init_output_file(outstream);
00119 
00120     gf.write_image_points(outstream);
00121     ++view_count;
00122 
00123     vcl_cout << "total of "<<view_count<<" views written.\n";
00124   }
00125   this->clear_input();
00126   frame_scores_.push_back(100.0);
00127   return true;
00128 }