contrib/brl/bmvl/bmvv/bmvv_recon_manager.cxx

Go to the documentation of this file.
00001 // This is brl/bmvl/bmvv/bmvv_recon_manager.cxx
00002 #include "bmvv_recon_manager.h"
00003 //:
00004 // \file
00005 // \author J.L. Mundy
00006 
00007 #include <vcl_cstdlib.h> // for vcl_exit()
00008 #include <vcl_iostream.h>
00009 #include <vcl_fstream.h>
00010 #include <vil1/vil1_load.h>
00011 #include <vil1/vil1_memory_image_of.h>
00012 #include <vnl/vnl_numeric_traits.h>
00013 #include <vnl/vnl_math.h>
00014 #include <vgl/vgl_point_2d.h>
00015 #include <vgl/vgl_point_3d.h>
00016 #include <vgl/vgl_homg_point_2d.h>
00017 #include <vgl/vgl_homg_line_2d.h>
00018 #include <vgl/algo/vgl_h_matrix_2d.h>
00019 #include <vgl/algo/vgl_p_matrix.h>
00020 #include <vgui/vgui.h>
00021 #include <vgui/vgui_find.h>
00022 #include <vgui/vgui_style.h>
00023 #include <vgui/vgui_macro.h>
00024 #include <vgui/vgui_dialog.h>
00025 #include <vgui/vgui_soview2D.h>
00026 #include <vgui/vgui_image_tableau.h>
00027 #include <vgui/vgui_viewer2D_tableau.h>
00028 #include <vgui/vgui_grid_tableau.h>
00029 #include <vgui/vgui_shell_tableau.h>
00030 #include <bgui/bgui_image_tableau.h>
00031 #include <bgui/bgui_vtol2D_tableau.h>
00032 #include <bgui/bgui_picker_tableau.h>
00033 #include <brip/brip_vil1_float_ops.h>
00034 #include <brct/brct_plane_sweeper_params.h>
00035 #include <brct/brct_volume_processor_params.h>
00036 #include <brct/brct_dense_reconstructor.h>
00037 #include <brct/brct_algos.h>
00038 #include <vsol/vsol_point_2d.h>
00039 #include <vsol/vsol_point_3d.h>
00040 //static live_video_manager instance
00041 bmvv_recon_manager *bmvv_recon_manager::instance_ = 0;
00042 //===============================================================
00043 //: The singleton pattern - only one instance of the manager can occur
00044 //==============================================================
00045 bmvv_recon_manager *bmvv_recon_manager::instance()
00046 {
00047   if (!instance_)
00048   {
00049     instance_ = new bmvv_recon_manager();
00050     instance_->init();
00051   }
00052   return bmvv_recon_manager::instance_;
00053 }
00054 
00055 //==================================================================
00056 //: constructors/destructor
00057 //==================================================================
00058 bmvv_recon_manager::
00059 bmvv_recon_manager() : vgui_wrapper_tableau(),
00060                        sweep_(brct_plane_sweeper_params()),
00061                        vproc_(brct_volume_processor_params())
00062 {
00063   images_set_ = false;
00064   harris_set_ = false;
00065   plane_ = 0;
00066 }
00067 
00068 bmvv_recon_manager::~bmvv_recon_manager()
00069 {
00070 }
00071 
00072 //======================================================================
00073 //: set up the tableaux at each grid cell
00074 //  the vtol2D_tableaux have been initialized in the constructor
00075 //======================================================================
00076 void bmvv_recon_manager::init()
00077 {
00078   grid_ = vgui_grid_tableau_new(2,1);
00079   grid_->set_grid_size_changeable(true);
00080   for (unsigned int col=0, row=0; col<2; ++col)
00081   {
00082     vgui_image_tableau_sptr itab = bgui_image_tableau_new();
00083     bgui_vtol2D_tableau_sptr btab = bgui_vtol2D_tableau_new(itab);
00084     vtol_tabs_.push_back(btab);
00085     bgui_picker_tableau_new pcktab(btab);
00086     vgui_viewer2D_tableau_sptr v2d = vgui_viewer2D_tableau_new(pcktab);
00087     grid_->add_at(v2d, col, row);
00088   }
00089   vgui_shell_tableau_sptr shell = vgui_shell_tableau_new(grid_);
00090   this->add_child(shell);
00091 }
00092 
00093 //=========================================================================
00094 //: make an event handler
00095 // note that we have to get an adaptor and set the tableau to receive events
00096 // this handler does nothing but is a place holder for future event processing
00097 // For now, just pass the events down to the child tableaux
00098 //=========================================================================
00099 bool bmvv_recon_manager::handle(const vgui_event &e)
00100 {
00101   return this->child.handle(e);
00102 }
00103 
00104 //====================================================================
00105 //: Gets the picker tableau at the given position.
00106 //  Returns a null tableau if it fails
00107 //====================================================================
00108 bgui_picker_tableau_sptr
00109 bmvv_recon_manager::get_picker_tableau_at(unsigned col, unsigned row)
00110 {
00111   vgui_tableau_sptr top_tab = grid_->get_tableau_at(col, row);
00112   if (top_tab)
00113   {
00114     bgui_picker_tableau_sptr tt;
00115     tt.vertical_cast(vgui_find_below_by_type_name(top_tab,
00116                                                   vcl_string("bgui_picker_tableau")));
00117     if (tt)
00118       return tt;
00119   }
00120   vgui_macro_warning << "Unable to get bgui_picker_tableau at (" << col << ", "
00121                      << row << ")\n";
00122   return 0;
00123 }
00124 
00125 //====================================================================
00126 //: Gets the picker tableau at the currently selected grid position
00127 //  Returns a null tableau if it fails
00128 //====================================================================
00129 bgui_picker_tableau_sptr bmvv_recon_manager::get_selected_picker_tableau()
00130 {
00131   unsigned int row=0, col=0;
00132   grid_->get_last_selected_position(&col, &row);
00133   return this->get_picker_tableau_at(col, row);
00134 }
00135 
00136 //====================================================================
00137 //: Gets the picker tableau at the given position.
00138 //  if col, row are out of bounds then null is returned
00139 //  row is currently not used but may be when we have more than 2 cameras
00140 //====================================================================
00141 bgui_vtol2D_tableau_sptr bmvv_recon_manager::get_vtol2D_tableau_at(unsigned col, unsigned row)
00142 {
00143   if (row!=0)
00144     return 0;
00145   bgui_vtol2D_tableau_sptr btab = 0;
00146   if (col==0||col==1)
00147     btab = vtol_tabs_[col];
00148   return btab;
00149 }
00150 
00151 
00152 //=================================================================
00153 //: Get the vtol_2D_tableau at the currently selected grid cell.
00154 //=================================================================
00155 bgui_vtol2D_tableau_sptr bmvv_recon_manager::get_selected_vtol2D_tableau()
00156 {
00157   unsigned int row =0, col=0;
00158   grid_->get_last_selected_position(&col, &row);
00159   return this->get_vtol2D_tableau_at(col, row);
00160 }
00161 
00162 int bmvv_recon_manager::get_cam()
00163 {
00164   unsigned int row =0, col=0;
00165   grid_->get_last_selected_position(&col, &row);
00166   if (!row&&!col)
00167     return 0;
00168   return 1;
00169 }
00170 
00171 void bmvv_recon_manager::quit()
00172 {
00173   vcl_exit(1);
00174 }
00175 
00176 //=========================================================================
00177 //: load an image an put it in the currently selected grid cell
00178 //=========================================================================
00179 void bmvv_recon_manager::load_image_file(vcl_string image_filename, bool /* greyscale */, unsigned col, unsigned row)
00180 {
00181   img_ = vil1_load(image_filename.c_str());
00182   bgui_vtol2D_tableau_sptr btab = this->get_vtol2D_tableau_at(col, row);
00183   if (btab)
00184   {
00185     vgui_image_tableau_sptr itab = btab->get_image_tableau();
00186     itab->set_image(img_);
00187     return;
00188   }
00189   vcl_cout << "In bmvv_recon_manager::load_image_file() - null tableau\n";
00190 }
00191 
00192 //=========================================================================
00193 //: load an image an put it in the currently selected grid cell
00194 //=========================================================================
00195 void bmvv_recon_manager::load_image()
00196 {
00197   static bool greyscale = false;
00198   vgui_dialog load_image_dlg("Load Image");
00199   static vcl_string image_filename = "";
00200   static vcl_string ext = "*.*";
00201   load_image_dlg.file("Image Filename:", ext, image_filename);
00202   load_image_dlg.checkbox("greyscale ", greyscale);
00203   if (!load_image_dlg.ask())
00204     return;
00205   vil1_image temp = vil1_load(image_filename.c_str());
00206   if (greyscale)
00207   {
00208     vil1_memory_image_of<unsigned char> temp1 =
00209     brip_vil1_float_ops::convert_to_grey(temp);
00210     img_ = temp1;
00211   }
00212   else
00213     img_ = temp;
00214 
00215   bgui_vtol2D_tableau_sptr btab = this->get_selected_vtol2D_tableau();
00216   if (btab)
00217   {
00218     vgui_image_tableau_sptr itab = btab->get_image_tableau();
00219     itab->set_image(img_);
00220     itab->post_redraw();
00221     images_set_ = false;
00222     harris_set_ = false;
00223     return;
00224   }
00225   vcl_cout << "In bmvv_recon_manager::load_image() - null tableau\n";
00226 }
00227 
00228 //===================================================================
00229 //: Clear the display
00230 //===================================================================
00231 void bmvv_recon_manager::clear_display()
00232 {
00233   bgui_vtol2D_tableau_sptr btab = this->get_selected_vtol2D_tableau();
00234   if (btab)
00235     btab->clear_all();
00236   else
00237     vcl_cout << "In bmvv_recon_manager::clear_display() - null tableau\n";
00238 }
00239 
00240 //===================================================================
00241 //: clear all selections in both panes
00242 //===================================================================
00243 void bmvv_recon_manager::clear_selected()
00244 {
00245   for (vcl_vector<bgui_vtol2D_tableau_sptr>::iterator bit = vtol_tabs_.begin();
00246        bit != vtol_tabs_.end(); bit++)
00247     if (*bit)
00248       (*bit)->deselect_all();
00249 }
00250 
00251 
00252 //====================================================================
00253 //: Gets the image at the given position.
00254 //  if col, row are out of bounds then null is returned
00255 //  row is currently not used but may be when we have more than 2 cameras
00256 //====================================================================
00257 vil1_image bmvv_recon_manager::get_image_at(unsigned col, unsigned row)
00258 {
00259   bgui_vtol2D_tableau_sptr btab = this->get_vtol2D_tableau_at(col, row);
00260   if (btab)
00261   {
00262     vgui_image_tableau_sptr itab = btab->get_image_tableau();
00263     return itab->get_image();
00264   }
00265   vcl_cout << "In bmvv_recon_manager::get_image_at() - null tableau\n";
00266   return 0;
00267 }
00268 
00269 void bmvv_recon_manager::read_3d_points()
00270 {
00271   vgui_dialog load_3d_points_dlg("Load 3d Points");
00272   static vcl_string points_filename = "";
00273   static vcl_string ext = "*.*";
00274   load_3d_points_dlg.file("Planar 3-d Points File:", ext, points_filename);
00275   if (!load_3d_points_dlg.ask())
00276     return;
00277   cal_.read_data(points_filename);
00278 }
00279 
00280 void bmvv_recon_manager::initial_model_projection()
00281 {
00282   bgui_vtol2D_tableau_sptr btab = this->get_selected_vtol2D_tableau();
00283   if (!btab)
00284     return;
00285   vgui_dialog initial_project_dlg("Project Model (Inital)");
00286   initial_project_dlg.field("World Plane", plane_);
00287   if (!initial_project_dlg.ask())
00288     return;
00289   vgui_image_tableau_sptr itab = btab->get_image_tableau();
00290   vil1_image image = itab->get_image();
00291   if (!image)
00292     return;
00293   int width = image.width(), height = image.height();
00294   cal_.set_image_size(brct_plane_calibrator::LEFT, width, height);
00295   cal_.set_image_size(brct_plane_calibrator::RIGHT, width, height);
00296   if (!cal_.compute_initial_homographies())
00297   {
00298     vcl_cout << "In initial_model_projection() - problem computing initial homographies\n";
00299     return;
00300   }
00301   vcl_vector<vgl_point_2d<double> > pts_2d =
00302     cal_.projected_3d_points_initial(plane_, this->get_cam());
00303   btab->set_point_radius(5.0f);
00304   vcl_vector<vgl_point_2d<double> >::iterator pit = pts_2d.begin();
00305   for (int i=0; pit != pts_2d.end(); ++pit, ++i)
00306   {
00307     vgui_soview2D_point* sov = btab->add_point(static_cast<float>((*pit).x()), static_cast<float>((*pit).y()));
00308     int id = sov->get_id();
00309     point_3d_map_[id]=i;
00310   }
00311   btab->post_redraw();
00312 }
00313 
00314 void bmvv_recon_manager::model_projection()
00315 {
00316  bgui_vtol2D_tableau_sptr btab = this->get_selected_vtol2D_tableau();
00317   if (!btab)
00318     return;
00319  vgui_dialog initial_project_dlg("Project Model");
00320   initial_project_dlg.field("World Plane", plane_);
00321   if (!initial_project_dlg.ask())
00322     return;
00323   vcl_vector<vgl_point_2d<double> > pts_2d =
00324     cal_.projected_3d_points(plane_, this->get_cam());
00325   btab->set_point_radius(5.0f);
00326   vcl_vector<vgl_point_2d<double> >::iterator pit = pts_2d.begin();
00327   for (int i=0; pit != pts_2d.end(); ++pit, ++i)
00328   {
00329     vgui_soview2D_point* sov = btab->add_point(static_cast<float>((*pit).x()), static_cast<float>((*pit).y()));
00330     int id = sov->get_id();
00331     point_3d_map_[id]=i;
00332   }
00333   btab->post_redraw();
00334 }
00335 
00336 brct_plane_corr_sptr bmvv_recon_manager::get_selected_corr()
00337 {
00338   bgui_vtol2D_tableau_sptr btab = this->get_selected_vtol2D_tableau();
00339   if (!btab)
00340     return (brct_plane_corr*)0;
00341   vcl_vector<unsigned> ids = btab->get_selected();
00342   //take the last selected
00343   int n = ids.size();
00344   if (!n)
00345   {
00346     vcl_cout << "Nothing selected\n";
00347     return (brct_plane_corr*)0;
00348   }
00349   int i = point_3d_map_[ids[n-1]];
00350   return cal_.corr(plane_, i);
00351 }
00352 
00353 void bmvv_recon_manager::print_selected_corr()
00354 {
00355   brct_plane_corr_sptr corr = this->get_selected_corr();
00356   vcl_cout << *corr << vcl_endl;
00357 }
00358 
00359 void bmvv_recon_manager::draw_corr_point(const float x, const float y)
00360 {
00361  bgui_vtol2D_tableau_sptr btab = this->get_selected_vtol2D_tableau();
00362  if (!btab)
00363     return;
00364  btab->set_point_radius(3.0f);
00365  btab->set_foreground(0.0f, 1.0f, 0.0f);
00366  btab->add_point(x, y);
00367  btab->post_redraw();
00368 }
00369 
00370 void bmvv_recon_manager::pick_corr()
00371 {
00372   brct_plane_corr_sptr corr = this->get_selected_corr();
00373   if (!corr)
00374     return;
00375   bgui_picker_tableau_sptr ptab = this->get_selected_picker_tableau();
00376   if (!ptab)
00377     return;
00378   float x=0, y=0;
00379   ptab->pick_point(&x,&y);
00380   corr->set_match(this->get_cam(), x, y);
00381   this->draw_corr_point(x, y);
00382 }
00383 
00384 void bmvv_recon_manager::write_corrs()
00385 {
00386   vgui_dialog corr_dlg("Write Correspondences");
00387   static vcl_string corr_file = "";
00388   static vcl_string ext = "*.*";
00389   corr_dlg.file("Correspondence File", ext, corr_file);
00390   if (!corr_dlg.ask())
00391     return;
00392   if (!cal_.write_corrs(corr_file))
00393     vcl_cout << "In bmvv_recon_manager::write_corrs() - failed write\n";
00394 }
00395 
00396 void bmvv_recon_manager::read_corrs()
00397 {
00398   vgui_dialog corr_dlg("Read Correspondences");
00399   static vcl_string corr_file = "";
00400   static vcl_string ext = "*.*";
00401   corr_dlg.file("Correspondence File", ext, corr_file);
00402   if (!corr_dlg.ask())
00403     return;
00404   if (!cal_.read_corrs(corr_file))
00405     vcl_cout << "In bmvv_recon_manager::read_corrs() - failed read\n";
00406 }
00407 
00408 void bmvv_recon_manager::compute_homographies()
00409 {
00410   if (!cal_.compute_homographies())
00411     vcl_cout << "In bmvv_recon_manager::compute_homographies() - failed\n";
00412 }
00413 
00414 void bmvv_recon_manager::write_homographies()
00415 {
00416   vgui_dialog homg_dlg("Write Homographies");
00417   static vcl_string homg_file = "";
00418   static vcl_string ext = "*.*";
00419   homg_dlg.file("Homgraphy File", ext, homg_file);
00420   if (!homg_dlg.ask())
00421     return;
00422   if (!cal_.write_homographies(homg_file))
00423     vcl_cout << "In bmvv_recon_manager::write_homgraphies() - failed\n";
00424 }
00425 
00426 void bmvv_recon_manager::read_homographies()
00427 {
00428   vgui_dialog homg_dlg("Read Homographies");
00429   static vcl_string homg_file = "";
00430   static vcl_string ext = "*.*";
00431   homg_dlg.file("Homgraphy File", ext, homg_file);
00432   if (!homg_dlg.ask())
00433     return;
00434   if (!sweep_.read_homographies(homg_file))
00435     vcl_cout << "In bmvv_recon_manager::read_homgraphies() - failed\n";
00436 }
00437 
00438 void bmvv_recon_manager::project_image()
00439 {
00440  bgui_vtol2D_tableau_sptr btab = this->get_selected_vtol2D_tableau();
00441  if (!btab)
00442    return;
00443  vgui_image_tableau_sptr itab = btab->get_image_tableau();
00444  vil1_image image = itab->get_image();
00445  if (!image)
00446  {
00447    vcl_cout << "In bmvv_recon_manager::project_image() -"
00448             << " no image loaded in selected pane\n";
00449    return;
00450  }
00451  vgui_dialog image_project_dlg("Project Image");
00452   image_project_dlg.field("World Plane", plane_);
00453   if (!image_project_dlg.ask())
00454     return;
00455   int cam = this->get_cam();
00456   sweep_.set_image(cam, image);
00457   vil1_memory_image_of<unsigned char> pimg =
00458     sweep_.project_image_to_plane(plane_, cam);
00459   itab->set_image(pimg);
00460   itab->post_redraw();
00461 }
00462 
00463 void bmvv_recon_manager::set_images()
00464 {
00465   for (int cam = 0; cam<2; cam++)
00466   {
00467     bgui_vtol2D_tableau_sptr btab = vtol_tabs_[cam];
00468     vgui_image_tableau_sptr itab = btab->get_image_tableau();
00469     vil1_image image = itab->get_image();
00470     if (!image)
00471     {
00472       vcl_cout << "In bmvv_recon_manager::set_images()-"
00473                << " not enought images\n";
00474       return;
00475     }
00476     if (!sweep_.set_image(cam, image))
00477     {
00478       vcl_cout << "In bmvv_recon_manager::set_images()-"
00479                << " can't set image "<< cam << vcl_endl;
00480       return;
00481     }
00482   }
00483   images_set_ = true;
00484   harris_set_ = false;
00485 }
00486 
00487 void bmvv_recon_manager::overlapping_projections()
00488 {
00489   if (!images_set_)
00490   {
00491     vcl_cout << "In bmvv_recon_manager::overlapping_projections() -"
00492              << " images not set\n";
00493     return;
00494   }
00495   vgui_dialog image_project_dlg("Overlapping Projections");
00496   image_project_dlg.field("World Plane", plane_);
00497   if (!image_project_dlg.ask())
00498     return;
00499   vcl_vector<vil1_memory_image_of<float> > imgs;
00500   if (!sweep_.overlapping_projections(plane_, imgs))
00501   {
00502     vcl_cout << "In bmvv_recon_manager::overlapping_projections()-"
00503              << " overlap failed\n";
00504         return;
00505   }
00506   for (int cam = 0; cam<2; cam++)
00507   {
00508     bgui_vtol2D_tableau_sptr btab = vtol_tabs_[cam];
00509     vgui_image_tableau_sptr itab = btab->get_image_tableau();
00510     vil1_memory_image_of<unsigned char> temp =
00511       brip_vil1_float_ops::convert_to_byte(imgs[cam], 0, 255);
00512     itab->set_image(temp);
00513     itab->post_redraw();
00514   }
00515 }
00516 
00517 void bmvv_recon_manager::overlapping_projections_z()
00518 {
00519   if (!images_set_)
00520   {
00521     vcl_cout << "In bmvv_recon_manager::overlapping_projections_z() -"
00522              << " images not set\n";
00523     return;
00524   }
00525   static double z;
00526   vgui_dialog image_project_dlg("Overlapping Projections at Z");
00527   image_project_dlg.field("Depth z value", z);
00528   if (!image_project_dlg.ask())
00529     return;
00530   vcl_vector<vil1_memory_image_of<float> > imgs;
00531   if (!sweep_.overlapping_projections(z, imgs))
00532   {
00533     vcl_cout << "In bmvv_recon_manager::overlapping_projections()-"
00534              << " overlap failed\n";
00535         return;
00536   }
00537   for (int cam = 0; cam<2; cam++)
00538   {
00539     bgui_vtol2D_tableau_sptr btab = vtol_tabs_[cam];
00540     vgui_image_tableau_sptr itab = btab->get_image_tableau();
00541     vil1_memory_image_of<unsigned char> temp =
00542       brip_vil1_float_ops::convert_to_byte(imgs[cam], 0, 255);
00543     itab->set_image(temp);
00544     itab->post_redraw();
00545   }
00546 }
00547 
00548 void bmvv_recon_manager::
00549 draw_vsol_points(const int cam, vcl_vector<vsol_point_2d_sptr> const & points,
00550                  bool clear, const float r, const float g, const float b)
00551 {
00552   bgui_vtol2D_tableau_sptr btab = vtol_tabs_[cam];
00553   if (!btab)
00554   {
00555     vcl_cout << "In bmvv_recon_manager::draw_vsol_points(..) -"
00556              << " null btol tableau for pane " << cam << vcl_endl;
00557     return;
00558   }
00559   if (clear)
00560     btab->clear_all();
00561   vgui_style_sptr style = vgui_style::new_style(r, g, b, 3, 0);
00562   for (vcl_vector<vsol_point_2d_sptr>::const_iterator pit = points.begin();
00563        pit != points.end(); pit++)
00564     btab->add_vsol_point_2d(*pit, style);
00565 }
00566 
00567 void bmvv_recon_manager::
00568 draw_vsol_point(const int cam, vsol_point_2d_sptr const & point,
00569                 bool clear, const float r, const float g, const float b)
00570 {
00571   bgui_vtol2D_tableau_sptr btab = vtol_tabs_[cam];
00572   if (!btab)
00573   {
00574     vcl_cout << "In bmvv_recon_manager::draw_vsol_point(..) -"
00575              << " null btol tableau for pane " << cam << vcl_endl;
00576     return;
00577   }
00578   if (clear)
00579     btab->clear_all();
00580   vgui_style_sptr style = vgui_style::new_style(r, g, b, 3, 0);
00581   btab->add_vsol_point_2d(point,style);
00582 }
00583 
00584 //: color is determined by depth
00585 void bmvv_recon_manager::
00586 draw_vsol_3d_points(const int cam, vcl_vector<vsol_point_3d_sptr> const& pts3d,
00587                     bool clear)
00588 {
00589   bgui_vtol2D_tableau_sptr btab = vtol_tabs_[cam];
00590   if (!btab)
00591   {
00592     vcl_cout << "In bmvv_recon_manager::draw_vsol_3d_points(..) -"
00593              << " null btol tableau for pane " << cam << vcl_endl;
00594     return;
00595   }
00596   if (clear)
00597     btab->clear_all();
00598   double zmin = vnl_numeric_traits<double>::maxval, zmax = -zmin;
00599   for (vcl_vector<vsol_point_3d_sptr>::const_iterator pit = pts3d.begin();
00600        pit != pts3d.end(); pit++)
00601   {
00602     zmin = vnl_math_min(zmin, (*pit)->z());
00603     zmax = vnl_math_max(zmax, (*pit)->z());
00604   }
00605   double d = zmax-zmin, s = 1;
00606   if (d)
00607     s = 1/d;
00608   for (vcl_vector<vsol_point_3d_sptr>::const_iterator pit = pts3d.begin();
00609        pit != pts3d.end(); pit++)
00610   {
00611     float f = (float)(((*pit)->z()-zmin)*s);
00612     vcl_cout << "f(" << (*pit)->z() << ")=" << f << vcl_endl;
00613     vsol_point_2d_sptr p = new vsol_point_2d((*pit)->x(), (*pit)->y());
00614     vgui_style_sptr style = vgui_style::new_style(f, 0, 1-f, 3, 0);
00615     btab->add_vsol_point_2d(p,style);
00616   }
00617 }
00618 
00619 void bmvv_recon_manager::compute_harris_corners()
00620 {
00621   if (!images_set_)
00622   {
00623     vcl_cout << "In bmvv_recon_manager::compute_harris_corners() -"
00624              << " images not set\n";
00625     return;
00626   }
00627   vgui_dialog harris_dialog("Compute Harris Corners");
00628   harris_dialog.field("sigma", sweep_.hdp_.sigma_);
00629   harris_dialog.field("thresh", sweep_.hdp_.thresh_);
00630   harris_dialog.field("N = 2n+1, (n)", sweep_.hdp_.n_);
00631   harris_dialog.field("Max No Corners(percent)", sweep_.hdp_.percent_corners_);
00632   harris_dialog.field("scale_factor", sweep_.hdp_.scale_factor_);
00633   if (!harris_dialog.ask())
00634     return;
00635   if (!sweep_.compute_harris())
00636     return;
00637   for (int cam = 0; cam<2; cam++)
00638   {
00639     vcl_vector<vsol_point_2d_sptr> points = sweep_.harris_corners(cam);
00640     this->draw_vsol_points(cam, points);
00641   }
00642   harris_set_ = true;
00643 }
00644 
00645 void bmvv_recon_manager::overlapping_harris_proj_z()
00646 {
00647   if (!images_set_||!harris_set_)
00648   {
00649     vcl_cout << "In bmvv_recon_manager::overlapping_projections_z() -"
00650              << " images not set or harris not ready\n";
00651     return;
00652   }
00653   static double z;
00654   vgui_dialog image_project_dlg("Overlapping Harris Projections at Z");
00655   image_project_dlg.field("Depth z value", z);
00656   if (!image_project_dlg.ask())
00657     return;
00658   vcl_vector<vil1_memory_image_of<float> > imgs;
00659   vcl_vector<vcl_vector<vsol_point_2d_sptr> > harris_corners;
00660   if (!sweep_.overlapping_projections(z, imgs, harris_corners))
00661   {
00662     vcl_cout << "In bmvv_recon_manager::overlapping_projections()-"
00663              << " overlap failed\n";
00664         return;
00665   }
00666   for (int cam = 0; cam<2; cam++)
00667   {
00668     bgui_vtol2D_tableau_sptr btab = vtol_tabs_[cam];
00669     vgui_image_tableau_sptr itab = btab->get_image_tableau();
00670     vil1_memory_image_of<unsigned char> temp =
00671       brip_vil1_float_ops::convert_to_byte(imgs[cam], 0, 255);
00672     itab->set_image(temp);
00673     itab->post_redraw();
00674     this->draw_vsol_points(cam, harris_corners[cam]);
00675   }
00676 }
00677 
00678 void bmvv_recon_manager::cross_correlate_plane()
00679 {
00680   if (!images_set_)
00681   {
00682     vcl_cout << "In bmvv_recon_manager::overlapping_projections() -"
00683              << " images not set\n";
00684     return;
00685   }
00686  vgui_dialog cc_plane_dlg("Cross Correlate Plane");
00687   cc_plane_dlg.field("World Plane", plane_);
00688   cc_plane_dlg.field("Corr Display Range (min)", sweep_.corr_min_);
00689   cc_plane_dlg.field("Corr Display Range (max)", sweep_.corr_max_);
00690   cc_plane_dlg.field("Correlation Sigma", sweep_.corr_sigma_);
00691 
00692   if (!cc_plane_dlg.ask())
00693     return;
00694   vil1_memory_image_of<unsigned char> cc =
00695     sweep_.cross_correlate_projections(plane_);
00696   if (!cc)
00697   {
00698     vcl_cout << "In bmvv_recon_manager::cross_correlate_plane()-"
00699              << " correlation failed\n";
00700     return;
00701   }
00702   bgui_vtol2D_tableau_sptr btab = vtol_tabs_[0];
00703   vgui_image_tableau_sptr itab = btab->get_image_tableau();
00704   itab->set_image(cc);
00705   itab->post_redraw();
00706 }
00707 
00708 void bmvv_recon_manager::cross_correlate_z()
00709 {
00710   if (!images_set_)
00711   {
00712     vcl_cout << "In bmvv_recon_manager::overlapping_projections() -"
00713              << " images not set\n";
00714     return;
00715   }
00716   static double z = 0;
00717   vgui_dialog cc_z_dlg("Cross Correlate Z");
00718   cc_z_dlg.field("World Plane", z);
00719   cc_z_dlg.field("Corr Display Range (min)", sweep_.corr_min_);
00720   cc_z_dlg.field("Corr Display Range (max)", sweep_.corr_max_);
00721   cc_z_dlg.field("Corr Sigma", sweep_.corr_sigma_);
00722   if (!cc_z_dlg.ask())
00723     return;
00724   vil1_memory_image_of<unsigned char> cc =
00725     sweep_.cross_correlate_projections(z);
00726   if (!cc)
00727   {
00728     vcl_cout << "In bmvv_recon_manager::cross_correlate_z()-"
00729              << " correlation failed\n";
00730     return;
00731   }
00732   bgui_vtol2D_tableau_sptr btab = vtol_tabs_[0];
00733   vgui_image_tableau_sptr itab = btab->get_image_tableau();
00734   itab->set_image(cc);
00735   itab->post_redraw();
00736 }
00737 
00738 void bmvv_recon_manager::cross_correlate_harris_z()
00739 {
00740   if (!images_set_||!harris_set_)
00741   {
00742     vcl_cout << "In bmvv_recon_manager::overlapping_projections() -"
00743              << " images or harris corners not set\n";
00744     return;
00745   }
00746   static double z = 0;
00747   vgui_dialog cc_z_harris_dlg("Cross Correlate Harris Corners at Z");
00748   cc_z_harris_dlg.field("World Plane", z);
00749   cc_z_harris_dlg.field("Match radius", sweep_.point_radius_);
00750   cc_z_harris_dlg.field("Corr radius", sweep_.corr_radius_);
00751   cc_z_harris_dlg.field("Correlation Threshold", sweep_.corr_thresh_);
00752   if (!cc_z_harris_dlg.ask())
00753     return;
00754   vil1_image img;
00755   vcl_vector<vsol_point_2d_sptr> matched_corners, back_proj_cnrs, orig_cnrs0;
00756 
00757   if (!sweep_.cross_correlate_proj_corners(z, img,
00758                                           matched_corners,
00759                                           back_proj_cnrs,
00760                                           orig_cnrs0))
00761     {
00762       vcl_cout << "In bmvv_recon_manager::cross_correlate_harris_z()-"
00763                << " correlation failed\n";
00764       return;
00765     }
00766   bgui_vtol2D_tableau_sptr btab = vtol_tabs_[1];
00767   vgui_image_tableau_sptr itab = btab->get_image_tableau();
00768   itab->set_image(img);
00769   itab->post_redraw();
00770   this->draw_vsol_points(0, orig_cnrs0);
00771   this->draw_vsol_points(0, back_proj_cnrs, false, 1, 0, 0);
00772   this->draw_vsol_points(1, matched_corners);
00773 }
00774 
00775 void bmvv_recon_manager::depth_image()
00776 {
00777   if (!images_set_)
00778   {
00779     vcl_cout << "In bmvv_recon_manager::overlapping_projections() -"
00780              << " images not set\n";
00781     return;
00782   }
00783   vgui_dialog depth_dlg("Compute Depth Image");
00784   depth_dlg.field("Min Z", sweep_.zmin_);
00785   depth_dlg.field("Max Z", sweep_.zmax_);
00786   depth_dlg.field("N Depth Planes", sweep_.nz_);
00787   depth_dlg.field("Corr Display Range (min)", sweep_.corr_min_);
00788   depth_dlg.field("Corr Display Range (max)", sweep_.corr_max_);
00789   depth_dlg.field("Corr Sigma", sweep_.corr_sigma_);
00790   depth_dlg.field("Correlation Threshold", sweep_.corr_thresh_);
00791   depth_dlg.field("Mean Intensity Threshold", sweep_.intensity_thresh_);
00792   if (!depth_dlg.ask())
00793     return;
00794   vil1_memory_image_of<unsigned char> depth, max_corr;
00795   if (!sweep_.depth_image(depth, max_corr))
00796   {
00797     vcl_cout << "In bmvv_recon_manager::depth_image()-"
00798              << " depth image failed\n";
00799     return;
00800   }
00801   bgui_vtol2D_tableau_sptr btab = vtol_tabs_[0];
00802   vgui_image_tableau_sptr itab = btab->get_image_tableau();
00803   itab->set_image(depth);
00804   itab->post_redraw();
00805   btab = vtol_tabs_[1];
00806   itab = btab->get_image_tableau();
00807   itab->set_image(max_corr);
00808   itab->post_redraw();
00809 }
00810 
00811 void bmvv_recon_manager::z_corr_image()
00812 {
00813  bgui_vtol2D_tableau_sptr btab = this->get_selected_vtol2D_tableau();
00814  if (!btab)
00815    return;
00816  vgui_image_tableau_sptr itab = btab->get_image_tableau();
00817  static int z = 0;
00818  vgui_dialog z_corr_dlg("Z-Correlation Image");
00819   z_corr_dlg.field("Z index", z);
00820   z_corr_dlg.field("Corr Display Range (min)", sweep_.corr_min_);
00821   z_corr_dlg.field("Corr Display Range (max)", sweep_.corr_max_);
00822   if (!z_corr_dlg.ask())
00823     return;
00824 
00825   vil1_memory_image_of<unsigned char> z_image = sweep_.z_corr_image(z);
00826   if (!z_image)
00827   {
00828     vcl_cout << "No Z_corr_images available\n";
00829     return;
00830   }
00831   itab->set_image(z_image);
00832   itab->post_redraw();
00833 }
00834 
00835 void bmvv_recon_manager::harris_depth_match()
00836 {
00837   if (!images_set_||!harris_set_)
00838   {
00839     vcl_cout << "In bmvv_recon_manager::harris_depth_match() -"
00840              << " images or harris corners not set\n";
00841     return;
00842   }
00843   vgui_dialog depth_dlg("Compute Harris Depth Match");
00844   depth_dlg.field("Min Z", sweep_.zmin_);
00845   depth_dlg.field("Max Z", sweep_.zmax_);
00846   depth_dlg.field("N Depth Planes", sweep_.nz_);
00847   depth_dlg.field("Corr Display Range (min)", sweep_.corr_min_);
00848   depth_dlg.field("Corr Display Range (max)", sweep_.corr_max_);
00849   depth_dlg.field("Point Match Radius", sweep_.point_radius_);
00850   depth_dlg.field("Correlation Window Radius", sweep_.corr_radius_);
00851   depth_dlg.field("CorrelationSigma", sweep_.corr_sigma_);
00852   depth_dlg.field("Correlation Threshold", sweep_.corr_thresh_);
00853   depth_dlg.field("Mean Intensity Threshold", sweep_.intensity_thresh_);
00854   if (!depth_dlg.ask())
00855     return;
00856   vcl_vector<vsol_point_3d_sptr> points_3d;
00857   vcl_vector<vsol_point_2d_sptr> proj_points;
00858   if (!sweep_.harris_depth_match(points_3d, proj_points))
00859   {
00860     vcl_cout << "In bmvv_recon_manager::depth_image()-"
00861              << " depth image failed\n";
00862     return;
00863   }
00864   this->draw_vsol_points(1, proj_points);
00865 }
00866 
00867 void bmvv_recon_manager::corr_plot()
00868 {
00869   bgui_picker_tableau_sptr ptab = this->get_selected_picker_tableau();
00870   if (!ptab)
00871     return;
00872   float x=0, y=0;
00873   ptab->pick_point(&x,&y);
00874   vcl_vector<float> z, corr;
00875   int ix = (int)x, iy = (int)y;
00876   sweep_.corr_vals(ix, iy, z, corr);
00877   int n = z.size();
00878   if (!n)
00879     return;
00880   vcl_cout << "C(z)[" << x << "][" << y << "]\n";
00881   for (int i = 0; i<n; i++)
00882     vcl_cout << z[i] << '\t' << corr[i] << '\n';
00883   vcl_cout << vcl_endl;
00884 }
00885 
00886 //: create a point in the pane corresponding to cam
00887 void bmvv_recon_manager::create_point(int& cam, vsol_point_2d_sptr& p)
00888 {
00889   cam = this->get_cam();
00890   bgui_picker_tableau_sptr ptab = this->get_selected_picker_tableau();
00891   if (!ptab)
00892     return;
00893   float x=0, y=0;
00894   ptab->pick_point(&x,&y);
00895   this->draw_corr_point(x, y);
00896   p = new vsol_point_2d(x, y);
00897 }
00898 
00899 //: map a point from cam a to cam b at depth z
00900 void bmvv_recon_manager::map_point()
00901 {
00902   static double z = 0;
00903   vgui_dialog z_map_dlg("Map Point at Z");
00904   z_map_dlg.field("Depth value (z)", z);
00905   if (!z_map_dlg.ask())
00906     return;
00907   vcl_cout << "Waiting to pick a point\n";
00908   int cam = 0;
00909   vsol_point_2d_sptr p, q;
00910   this->create_point(cam, p);
00911   if (!p)
00912   {
00913     vcl_cout << "Pick Failed\n";
00914     return;
00915   }
00916   vcl_cout << "got point at " << p->x() << ' ' << p->y() << vcl_endl;
00917   q = sweep_.map_point(p, cam, z);
00918   if (!q)
00919   {
00920     vcl_cout << "Map Failed\n";
00921     return;
00922   }
00923   int camb = 1-cam;
00924   this->draw_vsol_point(camb, q);
00925 }
00926 
00927 void bmvv_recon_manager::map_image()
00928 {
00929   if (!images_set_)
00930   {
00931     vcl_cout << "Images not set\n";
00932     return;
00933   }
00934  static double z = 0;
00935  vgui_dialog z_map_dlg("Image to image Mapping");
00936  z_map_dlg.field("Depth value (z)", z);
00937  if (!z_map_dlg.ask())
00938    return;
00939  int from_cam = this->get_cam();
00940  vil1_memory_image_of<unsigned char> mapped_to_image, orig_to_image;
00941  if (!sweep_.map_image_to_image(from_cam, z, mapped_to_image, orig_to_image))
00942    return;
00943  bgui_vtol2D_tableau_sptr btab = vtol_tabs_[0];
00944  vgui_image_tableau_sptr itab = btab->get_image_tableau();
00945  itab->set_image(orig_to_image);
00946  itab->post_redraw();
00947  btab = vtol_tabs_[1];
00948  itab = btab->get_image_tableau();
00949  itab->set_image(mapped_to_image);
00950  itab->post_redraw();
00951 }
00952 
00953 void bmvv_recon_manager::map_harris_corners()
00954 {
00955   if (!harris_set_)
00956   {
00957     vcl_cout << "No Harris corners\n";
00958     return;
00959   }
00960   static double z = 0;
00961   vgui_dialog z_map_dlg("Harris corner Mapping");
00962   z_map_dlg.field("Depth value (z)", z);
00963   if (!z_map_dlg.ask())
00964     return;
00965   int from_cam = this->get_cam();
00966   vcl_vector<vsol_point_2d_sptr> mapped_to_points, orig_to_points;
00967   if (!sweep_.map_harris_corners(from_cam, z, mapped_to_points, orig_to_points))
00968     return;
00969   this->draw_vsol_points(0, orig_to_points, true, 0, 1, 0);
00970   this->draw_vsol_points(0, mapped_to_points, false, 1, 0, 0);
00971 }
00972 
00973 void bmvv_recon_manager::match_harris_corners()
00974 {
00975   if (!harris_set_)
00976   {
00977     vcl_cout << "No Harris corners\n";
00978     return;
00979   }
00980   static double z = 0;
00981   vgui_dialog z_match_dlg("Harris corner Matching");
00982   z_match_dlg.field("Depth value (z)", z);
00983   z_match_dlg.field("Point Radiuus", sweep_.point_radius_);
00984   if (!z_match_dlg.ask())
00985     return;
00986   int from_cam = this->get_cam();
00987   vcl_vector<vsol_point_2d_sptr> matched_to_points, orig_to_points;
00988   sweep_.init_harris_match(from_cam);
00989   if (!sweep_.match_harris_corners(from_cam, z, matched_to_points,
00990                                    orig_to_points))
00991     return;
00992   this->draw_vsol_points(0, orig_to_points, true, 0, 1, 0);
00993   this->draw_vsol_points(0, matched_to_points, false, 1, 0, 0);
00994 }
00995 
00996 void bmvv_recon_manager::harris_sweep()
00997 {
00998   static int from_cam=1;
00999   if (!images_set_||!harris_set_)
01000   {
01001     vcl_cout << "In bmvv_recon_manager::harris_sweep() -"
01002              << " images or harris corners not set\n";
01003     return;
01004   }
01005   vgui_dialog harris_sweep_dlg("Harris Sweep");
01006   harris_sweep_dlg.field("From CAM", from_cam);
01007   harris_sweep_dlg.field("Min Z", sweep_.zmin_);
01008   harris_sweep_dlg.field("Max Z", sweep_.zmax_);
01009   harris_sweep_dlg.field("N Depth Planes", sweep_.nz_);
01010   harris_sweep_dlg.field("Point Match Radius", sweep_.point_radius_);
01011   harris_sweep_dlg.field("Correlation Thresh", sweep_.corr_thresh_);
01012   harris_sweep_dlg.field("Correlation Radius", sweep_.corr_radius_);
01013   if (!harris_sweep_dlg.ask())
01014     return;
01015   if (!sweep_.harris_sweep(from_cam))
01016   {
01017     vcl_cout << "Sweep failed\n"<< vcl_flush;
01018     return;
01019   }
01020 }
01021 
01022 void bmvv_recon_manager::display_matched_corners()
01023 {
01024   static int z_index;
01025   vgui_dialog hmatch_dlg("Harris Sweep");
01026   hmatch_dlg.field("Zindex", z_index);
01027   if (!hmatch_dlg.ask())
01028     return;
01029   vcl_vector<vsol_point_2d_sptr> matched_points =
01030     sweep_.matched_points_at_z_index(z_index);
01031   int n = matched_points.size();
01032   if (!n)
01033     return;
01034   //for now assume we display on pane 0
01035   this->draw_vsol_points(0, matched_points, true, 0, 1, 0);
01036 }
01037 
01038 void bmvv_recon_manager::display_harris_3d()
01039 {
01040   if (!harris_set_)
01041     return;
01042   vcl_vector<vsol_point_3d_sptr> points = sweep_.proj_points_3d();
01043   int n = points.size();
01044   if (!n)
01045     return;
01046   //for now assume we display on pane 0
01047   this->draw_vsol_3d_points(0, points, true);
01048 }
01049 
01050 void bmvv_recon_manager::write_points_vrml()
01051 {
01052   vgui_dialog save_vrml_dlg("Save VRML File");
01053   static vcl_string filename = "";
01054   static vcl_string ext = "*.*";
01055   save_vrml_dlg.file("VRML file name", ext, filename);
01056   if (!save_vrml_dlg.ask())
01057     return;
01058   if (filename == "")
01059   {
01060     vcl_cout << "Need a file name\n";
01061     return;
01062   }
01063   if (!sweep_.save_world_points(filename))
01064   {
01065     vcl_cout << "VRML Save Failed\n";
01066     return;
01067   }
01068 }
01069 
01070 void bmvv_recon_manager::read_points_vrml()
01071 {
01072   vgui_dialog read_vrml_dlg("Read VRML points File");
01073   static vcl_string filename = "";
01074   static vcl_string ext = "*.*";
01075   read_vrml_dlg.file("VRML file name", ext, filename);
01076   if (!read_vrml_dlg.ask())
01077     return;
01078   if (filename == "")
01079   {
01080     vcl_cout << "Need a file name\n";
01081     return;
01082   }
01083   if (!vproc_.read_points_3d_vrml(filename))
01084   {
01085     vcl_cout << "VRML read failed\n";
01086     return;
01087   }
01088 }
01089 
01090 void bmvv_recon_manager::write_volumes_vrml()
01091 {
01092   vgui_dialog write_volumes_dlg("Write VRML Volume File");
01093   static vcl_string filename = "";
01094   static vcl_string ext = "*.*";
01095   write_volumes_dlg.file("VRML file name", ext, filename);
01096   if (!write_volumes_dlg.ask())
01097     return;
01098   if (filename == "")
01099   {
01100     vcl_cout << "Need a file name\n";
01101     return;
01102   }
01103   if (!vproc_.write_prob_volumes_vrml(filename))
01104   {
01105     vcl_cout << "VRML volume write failed\n";
01106     return;
01107   }
01108 }
01109 
01110 void bmvv_recon_manager::read_change_data()
01111 {
01112   static vcl_string filename = "";
01113   static vcl_string ext = "*.*";
01114   vgui_dialog change_vrml_dlg("Change data File(VRML)");
01115   change_vrml_dlg.file("Change data file name (VRML)", ext, filename);
01116   if (!change_vrml_dlg.ask())
01117     return;
01118   if (filename == "")
01119   {
01120     vcl_cout << "Need a file name\n";
01121     return;
01122   }
01123   if (!vproc_.read_change_data_vrml(filename))
01124   {
01125     vcl_cout << "VRML read failed\n";
01126     return;
01127   }
01128 }
01129 
01130 void bmvv_recon_manager::write_change_volumes_vrml()
01131 {
01132   vgui_dialog write_change_volumes_dlg("Write Change Volumes(VRML)");
01133   static vcl_string filename = "";
01134   static vcl_string ext = "*.*";
01135   write_change_volumes_dlg.file("VRML file name", ext, filename);
01136   if (!write_change_volumes_dlg.ask())
01137     return;
01138   if (filename == "")
01139   {
01140     vcl_cout << "Need a file name\n";
01141     return;
01142   }
01143   if (!vproc_.write_changed_volumes_vrml(filename))
01144   {
01145     vcl_cout << "VRML volume write failed\n";
01146     return;
01147   }
01148 }
01149 
01150 void bmvv_recon_manager::compute_change()
01151 {
01152   vgui_dialog change_dlg("Change Detection");
01153   change_dlg.field("Cell Thresh", vproc_.cell_thresh_);
01154   if (!change_dlg.ask())
01155     return;
01156   vproc_.compute_change();
01157 }
01158 #if 0
01159 //: display a line of the dense match by drawing tokens
01160 void bmvv_recon_manager::display_dense_match()
01161 {
01162   vil1_image img0 = this->get_image_at(0,0);
01163   vil1_image img1 = this->get_image_at(1,0);
01164   if (!img0||!img1)
01165   {
01166     vcl_cout << "In bmvv_recon_manager::display_dense_match()"
01167              << " - need two images\n";
01168     return;
01169   }
01170   vtol_tabs_[0]->clear_all();
01171   vtol_tabs_[1]->clear_all();
01172   static int raster = 200;
01173   static int range = 10;
01174   static int radius = 5;
01175   static double inner = 1.0;
01176   static double outer = 0.5;
01177   static double continuity = 0.1;
01178   static bool print_corr = false;
01179   vgui_dialog dense_recon_dlg("Dense Reconstruction");
01180   dense_recon_dlg.field("Raster No.", raster);
01181   dense_recon_dlg.field("Search Range.", range);
01182   dense_recon_dlg.field("Corr. Window Radius", radius);
01183   dense_recon_dlg.field("Inner Cost", inner);
01184   dense_recon_dlg.field("Outer Cost", outer);
01185   dense_recon_dlg.field("Continuity", continuity);
01186   dense_recon_dlg.checkbox("Print Corr", print_corr);
01187   if (!dense_recon_dlg.ask())
01188     return;
01189 
01190   brct_dense_reconstructor dr(img0, img1);
01191   dr.set_search_range(range);
01192   dr.set_correlation_window_radius(radius);
01193   dr.set_inner_cost(inner);
01194   dr.set_outer_cost(outer);
01195   dr.set_continuity_cost(continuity);
01196   dr.print_params();
01197   dr.initial_calculations();
01198   dr.evaluate_raster(raster);
01199   vcl_vector<vsol_point_2d_sptr> points0 = dr.points0(raster);
01200   vcl_vector<vsol_point_2d_sptr> points1 = dr.points1(raster);
01201   int k = 0, ip = 0;
01202   for (vcl_vector<vsol_point_2d_sptr>::iterator pit = points0.begin();
01203        pit != points0.end(); ++pit, ++ip, ++k)
01204   {
01205     if (k==3)
01206       k = 0;
01207     if (points1[ip]->x()<0)
01208     {
01209       this->draw_vsol_point(0, *pit, false, 0, 0, 0);
01210       this->draw_vsol_point(1, points1[ip], false, 1, 1, 1);
01211       continue;
01212     }
01213     if (!k)
01214     {
01215       this->draw_vsol_point(0, *pit, false, 1, 0, 0);
01216       this->draw_vsol_point(1, points1[ip], false, 1, 0, 0);
01217     }
01218     if (k==1)
01219     {
01220       this->draw_vsol_point(0, *pit, false, 0, 1, 0);
01221       this->draw_vsol_point(1, points1[ip], false, 0, 1, 0);
01222     }
01223     if (k==2)
01224     {
01225       this->draw_vsol_point(0, *pit, false, 0, 0, 1);
01226       this->draw_vsol_point(1, points1[ip], false, 0, 0, 1);
01227     }
01228   }
01229   vtol_tabs_[0]->post_redraw();
01230   vtol_tabs_[1]->post_redraw();
01231   if (print_corr)
01232   {
01233     static double x0 = 100;
01234     vgui_dialog corr_dlg("Print Corr");
01235     corr_dlg.field("Xlocation", x0);
01236     if (!corr_dlg.ask())
01237       return;
01238     vcl_vector<int> xpos;
01239     vcl_vector<double> corr;
01240     dr.get_correlation(int(x0), raster, xpos, corr);
01241     vcl_cout << "Correlation\n";
01242     unsigned int n = xpos.size();
01243     for (unsigned int i = 0; i<n; ++i)
01244       vcl_cout << xpos[i] << '\t' << corr[i] << '\n';
01245     vcl_cout << vcl_flush;
01246   }
01247 }
01248 #endif
01249 //===================================================================
01250 //: read the fundamental matrix
01251 //
01252 //===================================================================
01253 void bmvv_recon_manager::read_f_matrix()
01254 {
01255   vgui_dialog read_F_dlg("Read F Matrix");
01256   static vcl_string filename = "";
01257   static vcl_string ext = "*.*";
01258   read_F_dlg.file("F Matrix file name", ext, filename);
01259   if (!read_F_dlg.ask())
01260     return;
01261   if (filename == "")
01262   {
01263     vcl_cout << "Need a file name\n";
01264     return;
01265   }
01266   vcl_ifstream istr(filename.c_str());
01267   vnl_matrix<double> fm;
01268   istr >> fm;
01269   FMatrix F(fm);
01270   vcl_cout << "F =\n" << F << vcl_endl;
01271   f_matrix_ = F;
01272 }
01273 
01274 //===================================================================
01275 //: pick a point in the left image and show the corresponding epipolar
01276 //  line in the right image
01277 //===================================================================
01278 void bmvv_recon_manager::show_epipolar_line()
01279 {
01280   this->clear_display();
01281   vgui::out << "pick point in left image\n";
01282   unsigned int col=0, row=0;//left image
01283   bgui_picker_tableau_sptr pkt = this->get_picker_tableau_at(col, row);
01284   if (!pkt)
01285   {
01286     vcl_cout << "In bmvv_multiview_manager::show_epipolar_line() - null tableau\n";
01287     return;
01288   }
01289   float x = 0, y=0;
01290   pkt->pick_point(&x, &y);
01291   vgui::out << "p(" << x << ' ' << y << ")\n";
01292   vcl_cout << "p(" << x << ' ' << y << ")\n";
01293 
01294   col = 1;//right image
01295   bgui_vtol2D_tableau_sptr v2D = this->get_vtol2D_tableau_at(col,row);
01296 
01297   //temporary test for FMatrix
01298   vgl_homg_point_2d<double> pl(x,y);
01299   vgl_homg_line_2d<double> lr = f_matrix_.image2_epipolar_line(pl);
01300   //end test
01301   if (v2D)
01302   {
01303     v2D->add_infinite_line((float)lr.a(), (float)lr.b(), (float)lr.c());
01304     v2D->post_redraw();
01305   }
01306 }
01307 
01308 void bmvv_recon_manager::show_world_homography()
01309 {
01310   vgui_dialog read_homg_dlg("Read Correspondence File");
01311   static double mag = 10.0;
01312   static vcl_string corr_filename = "";
01313   static vcl_string corr_ext = "*.cm";
01314   static vcl_string cam_filename = "";
01315   static vcl_string cam_ext = "*.cam";
01316   read_homg_dlg.file("correspondence file", corr_ext, corr_filename);
01317   read_homg_dlg.file("camera file", cam_ext, cam_filename);
01318   read_homg_dlg.field("Magnification",mag);
01319   if (!read_homg_dlg.ask())
01320     return;
01321   if (corr_filename == ""||cam_filename == "")
01322   {
01323     vcl_cout << "Need a file name\n";
01324     return;
01325   }
01326   vcl_ifstream istr(corr_filename.c_str());
01327   vcl_ofstream ostr(cam_filename.c_str());
01328   vcl_vector<vgl_point_2d<double> > image_points;
01329   vcl_vector<vgl_point_3d<double> > world_points;
01330   vcl_vector<bool> valid(image_points.size(), true);
01331   if (!brct_algos::read_target_corrs(istr, valid, image_points, world_points))
01332   {
01333     vcl_cout << "Failed to read correspondences\n";
01334     return;
01335   }
01336   vgl_h_matrix_2d<double> H, Hs, Hm;
01337   brct_algos::homography(world_points, image_points, H);
01338   vcl_cout << "H\n" << H << vcl_endl;
01339   brct_algos::scale_and_translate_world(-120.4,-8.29, mag, Hs);
01340   vnl_double_3x3 M, Ms, Mm;
01341   M = H.get_inverse().get_matrix();
01342   Ms = Hs.get_matrix();
01343   vcl_cout << "Ms\n" << Ms << vcl_endl;
01344   Mm = Ms*M;
01345   Hm = vgl_h_matrix_2d<double>(Mm);
01346   vcl_cout << "Back-projected and scaled world points\n";
01347   for (unsigned int i = 0; i<world_points.size(); i++)
01348   {
01349     vgl_point_2d<double>pi = image_points[i];
01350     vgl_homg_point_2d<double> hpi(pi.x(), pi.y()), hpw;
01351     hpw = Hm(hpi);
01352     vgl_point_2d<double> pw(hpw);
01353     vcl_cout << pw << vcl_endl;
01354   }
01355   bgui_vtol2D_tableau_sptr v2d = this->get_selected_vtol2D_tableau();
01356   if (!v2d)
01357   {
01358     vcl_cout << "Null Tableau\n";
01359     return;
01360   }
01361   int cam = this->get_cam();
01362   bgui_vtol2D_tableau_sptr other_tab = vtol_tabs_[1-cam];
01363   if (!other_tab)
01364   {
01365     vcl_cout << "Null Tableau\n";
01366     return;
01367   }
01368   vil1_image img = v2d->get_image_tableau()->get_image();
01369   vil1_memory_image_of<float> fimg = brip_vil1_float_ops::convert_to_float(img);
01370   vil1_memory_image_of<float> warped(1600, 450);
01371   if (brip_vil1_float_ops::homography(fimg, Hm, warped, true))
01372   {
01373     vil1_memory_image_of<unsigned char> cimg =
01374       brip_vil1_float_ops::convert_to_byte(warped, 0, 255);
01375     vgui_image_tableau_sptr itab = other_tab->get_image_tableau();
01376     itab->set_image(cimg);
01377   }
01378   else
01379     vcl_cout << "Warping failed\n";
01380   vcl_vector<vgl_point_2d<double> > proj_image_points;
01381   vgl_p_matrix<double> P =  brct_algos::p_from_h(H);
01382   brct_algos::project(world_points, P, proj_image_points);
01383 
01384   vgui_style_sptr r_style = vgui_style::new_style(1.0, 0.0, 0.0, 5, 0);
01385   vgui_style_sptr g_style = vgui_style::new_style(0.0, 1.0, 0.0, 5, 0);
01386   for (unsigned int i = 0; i<world_points.size(); i++)
01387   {
01388     vgl_point_2d<double> ip = image_points[i];
01389     vgl_point_2d<double> pip = proj_image_points[i];
01390     vsol_point_2d_sptr vip = new vsol_point_2d(ip.x(),ip.y());
01391     vsol_point_2d_sptr vpip = new vsol_point_2d(pip.x(),pip.y());
01392     vcl_cout << ip << " == " << pip << vcl_endl;
01393     v2d->add_vsol_point_2d(vip,r_style);
01394     v2d->add_vsol_point_2d(vpip,g_style);
01395   }
01396   v2d->post_redraw();
01397   //write out the camera
01398   ostr << P;
01399   //Also as a target camera for interest sake
01400   vcl_string target_file = cam_filename + "-target";
01401   vcl_ofstream ostr_tar(target_file.c_str());
01402   brct_algos::write_target_camera(ostr_tar, P.get_matrix());
01403 }

Generated on Mon Mar 8 05:36:26 2010 for contrib/brl/bmvl/bmvv by  doxygen 1.5.1