contrib/brl/bmvl/bmvv/bmvv_f_manager.cxx
Go to the documentation of this file.
00001 // This is brl/bmvl/bmvv/bmvv_f_manager.cxx
00002 #include "bmvv_f_manager.h"
00003 //:
00004 // \file
00005 // \author J.L. Mundy
00006 
00007 #if 1 //JLM
00008 #include <vpgl/file_formats/vpgl_nitf_rational_camera.h>
00009 #endif
00010 // include for project points menu option
00011 #include <vpgl/vpgl_rational_camera.h>
00012 #include <vpgl/algo/vpgl_fm_compute_ransac.h>
00013 #include <vpgl/algo/vpgl_fm_compute_8_point.h>
00014 #include <vpgl/algo/vpgl_fm_compute_2_point.h>
00015 #include <vcl_iostream.h>
00016 #include <vcl_cstdio.h> // sprintf
00017 #include <vcl_fstream.h>
00018 #include <vgl/vgl_point_2d.h>
00019 #include <vgl/vgl_homg_point_2d.h>
00020 #include <vgl/vgl_homg_line_2d.h>
00021 #include <vul/vul_file.h>
00022 #include <vil/vil_image_view.h>
00023 #include <vil/vil_blocked_image_resource.h>
00024 #include <vil/vil_pyramid_image_resource.h>
00025 #include <vil/vil_load.h>
00026 #include <vil/vil_save.h>
00027 #include <vil/vil_new.h>
00028 #include <vil/vil_property.h>
00029 #include <bgui/bgui_vsol_soview2D.h>
00030 #include <sdet/sdet_detector_params.h>
00031 #include <sdet/sdet_detector.h>
00032 #include <sdet/sdet_harris_detector_params.h>
00033 #include <sdet/sdet_harris_detector.h>
00034 #include <sdet/sdet_nonmax_suppression_params.h>
00035 #include <sdet/sdet_nonmax_suppression.h>
00036 #include <sdet/sdet_fit_lines_params.h>
00037 #include <sdet/sdet_fit_lines.h>
00038 #include <sdet/sdet_fit_conics.h>
00039 #include <sdet/sdet_grid_finder_params.h>
00040 #include <sdet/sdet_grid_finder.h>
00041 #include <vgui/vgui.h>
00042 #include <vgui/vgui_find.h>
00043 #include <vgui/vgui_tableau.h>
00044 #include <vgui/vgui_dialog.h>
00045 #include <vgui/vgui_style_sptr.h>
00046 #include <vgui/vgui_style.h>
00047 #include <vgui/vgui_viewer2D_tableau.h>
00048 #include <vgui/vgui_shell_tableau.h>
00049 #include <vgui/vgui_grid_tableau.h>
00050 #include <vgui/vgui_range_map_params.h>
00051 #include <bgui/bgui_image_tableau.h>
00052 #include <bgui/bgui_vtol2D_tableau.h>
00053 #include <bgui/bgui_picker_tableau.h>
00054 #include <bgui/bgui_range_adjuster_tableau.h>
00055 #include <bgui/bgui_image_utils.h>
00056 #include <vsol/vsol_point_2d.h>
00057 #include <vsol/vsol_point_2d_sptr.h>
00058 #include <vsol/vsol_conic_2d.h>
00059 #include <vsol/vsol_polyline_2d.h>
00060 #include <vtol/vtol_vertex_2d.h>
00061 #include <vtol/vtol_vertex.h>
00062 #include <vtol/vtol_edge_2d.h>
00063 #include <vtol/vtol_intensity_face.h>
00064 #include <brip/brip_vil1_float_ops.h>
00065 #include <brip/brip_vil_float_ops.h>
00066 #include <brip/brip_watershed_params.h>
00067 #include <sdet/sdet_watershed_region_proc_params.h>
00068 #include <sdet/sdet_watershed_region_proc.h>
00069 #include <sdet/sdet_region_proc_params.h>
00070 #include <sdet/sdet_region_proc.h>
00071 #include <strk/strk_region_info_params.h>
00072 #include <strk/strk_region_info.h>
00073 #include <strk/strk_io.h>
00074 #include <brct/brct_algos.h>
00075 
00076 ;
00077 bmvv_f_manager *bmvv_f_manager::instance_ = 0;
00078 
00079 bmvv_f_manager *bmvv_f_manager::instance()
00080 {
00081   if (!instance_)
00082   {
00083     instance_ = new bmvv_f_manager();
00084     instance_->init();
00085   }
00086   return bmvv_f_manager::instance_;
00087 }
00088 
00089 //-----------------------------------------------------------
00090 // constructors/destructor
00091 //
00092 bmvv_f_manager::bmvv_f_manager():vgui_wrapper_tableau()
00093 {
00094   first_ = true;
00095 }
00096 
00097 bmvv_f_manager::~bmvv_f_manager()
00098 {
00099   for (unsigned v = 0; v<vtabs_.size(); ++v)
00100   {
00101     bgui_vtol2D_tableau_sptr t = vtabs_[v];
00102     vpgl_proj_camera<double>* cam = cam_map_[v];
00103     cam_map_[v] = 0;
00104     delete cam;
00105   }
00106 }
00107 
00108 //: Set up the tableaux
00109 void bmvv_f_manager::init()
00110 {
00111   bgui_image_tableau_sptr itab = bgui_image_tableau_new();
00112   bgui_vtol2D_tableau_sptr t2D = bgui_vtol2D_tableau_new(itab);
00113   vtabs_.push_back(t2D);
00114   bgui_picker_tableau_sptr picktab = bgui_picker_tableau_new(t2D);
00115   vgui_viewer2D_tableau_sptr v2D = vgui_viewer2D_tableau_new(picktab);
00116   grid_ = vgui_grid_tableau_new(1,1);
00117   grid_->set_grid_size_changeable(true);
00118   grid_->add_at(v2D, 0, 0);
00119   vgui_shell_tableau_sptr shell = vgui_shell_tableau_new(grid_);
00120   this->add_child(shell);
00121   first_ = true;
00122 }
00123 
00124 //: Calculate the range parameters for the input image
00125 vgui_range_map_params_sptr bmvv_f_manager::
00126 range_params(vil_image_resource_sptr const& image)
00127 {
00128   float gamma = 1.0;
00129   bool invert = false;
00130   bool gl_map = false;
00131   bool cache = true;
00132 
00133   //Check if the image is blocked
00134   vil_blocked_image_resource_sptr bir = blocked_image_resource(image);
00135   if (bir)
00136   { gl_map = true; cache = false; }
00137 
00138   //Check if the image is a pyramid
00139   bool pyr = image->get_property(vil_property_pyramid, 0);
00140   if (pyr)
00141   { gl_map = true; cache = false; }
00142   //Get max min parameters
00143 
00144   double min=0, max=0;
00145   unsigned n_components = image->nplanes();
00146   vgui_range_map_params_sptr rmps;
00147   if (n_components == 1)
00148   {
00149     bgui_image_utils iu(image);
00150     iu.range(min, max);
00151     rmps= new vgui_range_map_params(min, max, gamma, invert,
00152                                     gl_map, cache);
00153   }
00154   else if (n_components == 3)
00155   {
00156     min = 0; max = 255;//for now - ultimately need to compute color histogram
00157     rmps = new vgui_range_map_params(min, max, min, max, min, max,
00158                                      gamma, gamma, gamma, invert,
00159                                      gl_map, cache);
00160   }
00161   return rmps;
00162 }
00163 
00164 //: set the image at the currently selected grid cell
00165 void bmvv_f_manager::
00166 set_selected_grid_image(vil_image_resource_sptr const& image,
00167                         vgui_range_map_params_sptr const& rmps)
00168 {
00169   bgui_image_tableau_sptr itab = this->selected_image_tab();
00170   if (!itab)
00171     this->add_image(image, rmps);
00172   else
00173   {
00174     itab->set_image_resource(image);
00175     itab->set_mapping(rmps);
00176   }
00177   itab->post_redraw();
00178 }
00179 
00180 //: Add an image at the specified grid cell
00181 void bmvv_f_manager::
00182 add_image_at(vil_image_resource_sptr const& image,
00183              const unsigned col, const unsigned row,
00184              vgui_range_map_params_sptr const& rmps)
00185 {
00186   vgui_range_map_params_sptr rmap = rmps;
00187   if (!rmps)
00188     rmap = range_params(image);
00189   bgui_image_tableau_sptr itab = bgui_image_tableau_new(image);
00190   itab->set_mapping(rmap);
00191   bgui_vtol2D_tableau_sptr t2D = bgui_vtol2D_tableau_new(itab);
00192   vtabs_.push_back(t2D);
00193   bgui_picker_tableau_sptr picktab = bgui_picker_tableau_new(t2D);
00194   vgui_viewer2D_tableau_sptr v2D = vgui_viewer2D_tableau_new(picktab);
00195   grid_->add_at(v2D, col, row);
00196   itab->post_redraw();
00197 }
00198 
00199 //: Add an image to the currently selected grid cell
00200 void bmvv_f_manager::
00201 add_image(vil_image_resource_sptr const& image,
00202           vgui_range_map_params_sptr const& rmps)
00203 {
00204   unsigned row=0, col=0;
00205   grid_->get_last_selected_position(&col, &row);
00206   this->add_image_at(image, col, row, rmps);
00207 }
00208 
00209 #if 0
00210 //-----------------------------------------------------------------------------
00211 //: remove the selected image
00212 //-----------------------------------------------------------------------------
00213 void bmvv_f_manager::remove_image()
00214 {
00215   unsigned row=0, col=0;
00216   grid_->get_last_selected_position(&col, &row);
00217   grid_->remove_at(col, row);
00218 }
00219 
00220 void bmvv_f_manager::convert_to_grey()
00221 {
00222   vil_image_resource_sptr img = this->selected_image();
00223   if (!img)
00224     return;
00225   vil_image_view<unsigned char> grey =
00226     brip_vil_float_ops::convert_to_byte(img);
00227   vil_image_resource_sptr gimg = vil_new_image_resource_of_view(grey);
00228   this->add_image(gimg);
00229 }
00230 #endif
00231 
00232 //: Get the image tableau for the currently selected grid cell
00233 bgui_image_tableau_sptr bmvv_f_manager::selected_image_tab()
00234 {
00235   unsigned row=0, col=0;
00236   grid_->get_last_selected_position(&col, &row);
00237   vgui_tableau_sptr top_tab = grid_->get_tableau_at(col, row);
00238   if (top_tab)
00239   {
00240     bgui_image_tableau_sptr itab;
00241     itab.vertical_cast(vgui_find_below_by_type_name(top_tab,
00242                                                     vcl_string("vgui_image_tableau")));
00243     if (itab)
00244       return itab;
00245   }
00246   vcl_cout << "Unable to get bgui_image_tableau at (" << col
00247            << ", " << row << ")\n";
00248   return bgui_image_tableau_sptr();
00249 }
00250 
00251 //: Get the vtol2D tableau at the specified grid cell
00252 bgui_vtol2D_tableau_sptr
00253 bmvv_f_manager::vtol2D_tab_at(const unsigned col,
00254                               const unsigned row)
00255 {
00256   vgui_tableau_sptr top_tab = grid_->get_tableau_at(col, row);
00257   if (top_tab)
00258   {
00259     bgui_vtol2D_tableau_sptr v2D;
00260     v2D.vertical_cast(vgui_find_below_by_type_name(top_tab,
00261                                                    vcl_string("bgui_vtol2D_tableau")));
00262     if (v2D)
00263       return v2D;
00264   }
00265   vcl_cout << "Unable to get bgui_vtol2D_tableau at (" << col
00266            << ", " << row << ")\n";
00267   return bgui_vtol2D_tableau_sptr();
00268 }
00269 
00270 //: Get the vtol2D tableau for the currently selected grid cell
00271 bgui_vtol2D_tableau_sptr bmvv_f_manager::selected_vtol2D_tab()
00272 {
00273   unsigned row=0, col=0;
00274   grid_->get_last_selected_position(&col, &row);
00275   return this->vtol2D_tab_at(col, row);
00276 }
00277 
00278 //: Get the picker tableau for the currently selected grid cell
00279 bgui_picker_tableau_sptr bmvv_f_manager::selected_picker_tab()
00280 {
00281   unsigned row=0, col=0;
00282   grid_->get_last_selected_position(&col, &row);
00283   vgui_tableau_sptr top_tab = grid_->get_tableau_at(col, row);
00284   if (top_tab)
00285   {
00286     bgui_picker_tableau_sptr pick;
00287     pick.vertical_cast(vgui_find_below_by_type_name(top_tab,
00288                                                     vcl_string("bgui_picker_tableau")));
00289     if (pick)
00290       return pick;
00291   }
00292   vcl_cout << "Unable to get bgui_picker_tableau at (" << col
00293            << ", " << row << ")\n";
00294   return bgui_picker_tableau_sptr();
00295 }
00296 
00297 
00298 vil_image_resource_sptr bmvv_f_manager::selected_image()
00299 {
00300   bgui_image_tableau_sptr itab = this->selected_image_tab();
00301   if (!itab)
00302     return 0;
00303   return itab->get_image_resource();
00304 }
00305 
00306 vil_image_resource_sptr bmvv_f_manager::image_at(const unsigned col,
00307                                                  const unsigned row)
00308 {
00309   vgui_tableau_sptr top_tab = grid_->get_tableau_at(col, row);
00310   if (!top_tab)
00311     return 0;
00312 
00313   bgui_image_tableau_sptr itab;
00314   itab.vertical_cast(vgui_find_below_by_type_name(top_tab,
00315                                                   vcl_string("vgui_image_tableau")));
00316   if (!itab)
00317   {
00318     vcl_cout << "Unable to get bgui_image_tableau at (" << col
00319              << ", " << row << ")\n";
00320     return 0;
00321   }
00322   return itab->get_image_resource();
00323 }
00324 
00325 //-----------------------------------------------------------------------------
00326 //: Clear spatial objects from the selected display
00327 //-----------------------------------------------------------------------------
00328 void bmvv_f_manager::clear_display()
00329 {
00330   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00331   if (!t2D)
00332     return;
00333   t2D->clear_all();
00334 }
00335 
00336 //-----------------------------------------------------------------------------
00337 //: Clear spatial objects from all spatial panes
00338 //-----------------------------------------------------------------------------
00339 void bmvv_f_manager::clear_all()
00340 {
00341   unsigned ncols = grid_->cols(), nrows = grid_->rows();
00342   for (unsigned r=0; r<nrows; ++r)
00343     for (unsigned c=0; c<ncols; ++c)
00344     {
00345       bgui_vtol2D_tableau_sptr t = this->vtol2D_tab_at(c, r);
00346       if (t)
00347         t->clear_all();
00348     }
00349 }
00350 
00351 //-----------------------------------------------------------------------------
00352 //: Draw edges onto the tableau
00353 //-----------------------------------------------------------------------------
00354 void
00355 bmvv_f_manager::draw_edges(vcl_vector<vtol_edge_2d_sptr>& edges,
00356                            bool verts)
00357 {
00358   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00359   if (!t2D)
00360     return;
00361   this->clear_display();
00362 #if 0
00363   vgui_image_tableau_sptr itab = t2D->get_image_tableau();
00364   if (!itab)
00365   {
00366     vcl_cout << "In bmvv_f_manager::draw_edges - null image tab\n";
00367     return;
00368   }
00369 #endif
00370   for (vcl_vector<vtol_edge_2d_sptr>::iterator eit = edges.begin();
00371        eit != edges.end(); eit++)
00372   {
00373     t2D->add_edge(*eit);
00374     //optionally display the edge vertices
00375     if (verts)
00376     {
00377       if ((*eit)->v1())
00378       {
00379         vtol_vertex_2d_sptr v1 = (*eit)->v1()->cast_to_vertex_2d();
00380         t2D->add_vertex(v1);
00381       }
00382       if ((*eit)->v2())
00383       {
00384         vtol_vertex_2d_sptr v2 = (*eit)->v2()->cast_to_vertex_2d();
00385         t2D->add_vertex(v2);
00386       }
00387     }
00388   }
00389   t2D->post_redraw();
00390 }
00391 
00392 
00393 //-----------------------------------------------------------------------------
00394 //: Draw polylines on the tableau
00395 //-----------------------------------------------------------------------------
00396 void bmvv_f_manager::
00397 draw_polylines(vcl_vector<vsol_polyline_2d_sptr > const& polys,
00398                vgui_style_sptr style)
00399 {
00400   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00401   if (!t2D)
00402     return;
00403   //this->clear_display();
00404 #if 0
00405   vgui_image_tableau_sptr itab = t2D->get_image_tableau();
00406   if (!itab)
00407   {
00408     vcl_cout << "In bmvv_f_manager::draw_polylines - null image tab\n";
00409     return;
00410   }
00411 #endif
00412   for (vcl_vector<vsol_polyline_2d_sptr>::const_iterator pit = polys.begin();
00413        pit != polys.end(); pit++)
00414   {
00415     t2D->add_vsol_polyline_2d(*pit, style);
00416   }
00417 
00418   t2D->post_redraw();
00419 }
00420 
00421 //-----------------------------------------------------------------------------
00422 //: Draw line segments on the tableau
00423 //-----------------------------------------------------------------------------
00424 void bmvv_f_manager::
00425 draw_lines(vcl_vector<vsol_line_2d_sptr > const& lines,
00426            const vgui_style_sptr& style)
00427 {
00428   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00429   if (!t2D)
00430     return;
00431   //this->clear_display();
00432 #if 0
00433   vgui_image_tableau_sptr itab = t2D->get_image_tableau();
00434   if (!itab)
00435   {
00436     vcl_cout << "In bmvv_f_manager::draw_edges - null image tab\n";
00437     return;
00438   }
00439 #endif
00440   for (vcl_vector<vsol_line_2d_sptr>::const_iterator lit = lines.begin();
00441        lit != lines.end(); lit++)
00442   {
00443     t2D->add_vsol_line_2d(*lit,style);
00444   }
00445 
00446   t2D->post_redraw();
00447 }
00448 
00449 //-----------------------------------------------------------------------------
00450 //: Draw conic segments on the tableau
00451 //-----------------------------------------------------------------------------
00452 void bmvv_f_manager::
00453 draw_conics(vcl_vector<vsol_conic_2d_sptr > const& conics,
00454             const vgui_style_sptr& style)
00455 {
00456   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00457   if (!t2D)
00458     return;
00459   //this->clear_display();
00460 #if 0
00461   vgui_image_tableau_sptr itab = t2D->get_image_tableau();
00462   if (!itab)
00463   {
00464     vcl_cout << "In bmvv_f_manager::draw_edges - null image tab\n";
00465     return;
00466   }
00467 #endif
00468   for (vcl_vector<vsol_conic_2d_sptr>::const_iterator lit = conics.begin();
00469        lit != conics.end(); lit++)
00470   {
00471     t2D->add_vsol_conic_2d(*lit,style);
00472   }
00473   t2D->post_redraw();
00474 }
00475 
00476 //-----------------------------------------------------------------------------
00477 //: Draw points on the tableau
00478 //-----------------------------------------------------------------------------
00479 void bmvv_f_manager::
00480 draw_points(vcl_vector<vsol_point_2d_sptr> const& points, const vgui_style_sptr& style)
00481 {
00482   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00483   if (!t2D)
00484     return;
00485   //this->clear_display();
00486 #if 0
00487   vgui_image_tableau_sptr itab = t2D->get_image_tableau();
00488   if (!itab)
00489   {
00490     vcl_cout << "In bmvv_f_manager::draw_edges - null image tab\n";
00491     return;
00492   }
00493 #endif
00494   for (vcl_vector<vsol_point_2d_sptr>::const_iterator pit = points.begin();
00495        pit != points.end(); pit++)
00496   {
00497     t2D->add_vsol_point_2d(*pit,style);
00498   }
00499 
00500   t2D->post_redraw();
00501 }
00502 
00503 void bmvv_f_manager::draw_regions(vcl_vector<vtol_intensity_face_sptr>& regions,
00504                                   bool verts)
00505 {
00506   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00507   if (!t2D)
00508     return;
00509   for (vcl_vector<vtol_intensity_face_sptr>::iterator rit = regions.begin();
00510        rit != regions.end(); rit++)
00511   {
00512     vtol_face_2d_sptr f = (*rit)->cast_to_face_2d();
00513     t2D->add_face(f);
00514     if (verts)
00515     {
00516       vcl_vector<vtol_vertex_sptr> vts;
00517       f->vertices(vts);
00518       for (vcl_vector<vtol_vertex_sptr>::iterator vit = vts.begin();
00519            vit != vts.end(); vit++)
00520       {
00521         vtol_vertex_2d_sptr v = (*vit)->cast_to_vertex_2d();
00522         t2D->add_vertex(v);
00523       }
00524     }
00525   }
00526   t2D->post_redraw();
00527 }
00528 
00529 void bmvv_f_manager::quit()
00530 {
00531   this->clear_all();
00532   vgui::quit();
00533 }
00534 
00535 void bmvv_f_manager::load_image()
00536 {
00537   static bool greyscale = false;
00538   static bool sblock = false;
00539   vgui_dialog load_image_dlg("Load image file");
00540   static vcl_string image_filename = "/home/dec/images/cal_image1.tif";
00541   static vcl_string ext = "*.*";
00542   load_image_dlg.file("Image Filename:", ext, image_filename);
00543   load_image_dlg.checkbox("greyscale ", greyscale);
00544   load_image_dlg.checkbox("blocked?:", sblock);
00545   if (!load_image_dlg.ask())
00546     return;
00547   //first check to see if the filename is a directory
00548   //if so, then assume a pyramid image
00549   bool pyrm = false;
00550   vil_image_resource_sptr image;
00551   if (vul_file::is_directory(image_filename.c_str()))
00552   {
00553     vil_pyramid_image_resource_sptr pyr =
00554       vil_load_pyramid_resource(image_filename.c_str());
00555     if (pyr)
00556     {
00557       image = pyr.ptr();
00558       pyrm = true;
00559     }
00560   }
00561   if (!image)
00562     image = vil_load_image_resource(image_filename.c_str());
00563 
00564   if (!image)
00565     return;
00566 
00567   if (greyscale&&!pyrm)
00568   {
00569     vil_image_view<unsigned char> grey_view =
00570       brip_vil_float_ops::convert_to_grey(*image);
00571     image = vil_new_image_resource_of_view(grey_view);
00572   }
00573 
00574   if (sblock&&!pyrm)
00575   {
00576     vil_blocked_image_resource_sptr bimage = vil_new_blocked_image_facade(image);
00577     image = (vil_image_resource*)(vil_new_cached_image_resource(bimage)).ptr();
00578   }
00579 
00580   vgui_range_map_params_sptr rmps = range_params(image);
00581 
00582   if (first_)
00583   {
00584     this->set_selected_grid_image(image, rmps);
00585     first_ = false;
00586   }
00587   else
00588     this->add_image(image, rmps);
00589 }
00590 
00591 void bmvv_f_manager::save_image()
00592 {
00593   vgui_dialog file_dialog("Save Image");
00594   static vcl_string image_file;
00595   static vcl_string ext = "tif";
00596   static vcl_string type = "tiff";
00597   static unsigned size_block = 0;
00598   static bool byte = false;
00599   file_dialog.file("Image Filename:", ext, image_file);
00600   file_dialog.field("Image Format: ", type);
00601   file_dialog.field("BlockSize", size_block);
00602   file_dialog.checkbox("Convert to byte image", byte);
00603   if (!file_dialog.ask())
00604     return;
00605   vil_image_resource_sptr img = this->selected_image();
00606   if (!img)
00607   {
00608     vcl_cerr << "Null image in bmvv_f_manager::save_image\n";
00609     return;
00610   }
00611   vil_image_resource_sptr save_image = img;
00612   if (byte)
00613   {
00614     vil_image_view<unsigned char> byte_view = brip_vil_float_ops::convert_to_byte(img);
00615     save_image = vil_new_image_resource_of_view(byte_view);
00616   }
00617   if (size_block>0)
00618   {
00619     vil_blocked_image_resource_sptr bim =
00620       vil_new_blocked_image_resource(image_file.c_str(),
00621                                      save_image->ni(), save_image->nj(),
00622                                      save_image->nplanes(),
00623                                      save_image->pixel_format(),
00624                                      size_block, size_block,
00625                                      "tiff");
00626     vil_image_view_base_sptr view = save_image->get_view();
00627     if (view)
00628       bim->vil_image_resource::put_view(*view);
00629     return;
00630   }
00631 
00632   if (!vil_save_image_resource(save_image, image_file.c_str(), type.c_str()))
00633     vcl_cerr << "bmvv_f_manager::save_image operation failed\n";
00634 }
00635 
00636 void bmvv_f_manager::set_range_params()
00637 {
00638   bgui_image_tableau_sptr itab = this->selected_image_tab();
00639   if (!itab)
00640     return;
00641   vgui_range_map_params_sptr rmps = itab->map_params();
00642   if (!rmps)
00643   {
00644     vil_image_resource_sptr img = itab->get_image_resource();
00645     if (!img)
00646       return;
00647     rmps = range_params(img);
00648     if (!rmps)
00649       return;
00650   }
00651   unsigned nc = rmps->n_components_;
00652   static double min = static_cast<double>(rmps->min_L_),
00653                 max = static_cast<double>(rmps->max_L_);
00654   static float gamma = rmps->gamma_L_;
00655   static bool invert = rmps->invert_;
00656   static bool gl_map = rmps->use_glPixelMap_;
00657   static bool cache = rmps->cache_mapped_pix_;
00658   if (nc==3)
00659   {
00660     min = static_cast<double>(rmps->min_R_);
00661     max = static_cast<double>(rmps->max_R_);
00662     gamma = rmps->gamma_R_;
00663   }
00664   vgui_dialog range_dlg("Set Range Map Params");
00665   range_dlg.field("Range min:", min);
00666   range_dlg.field("Range max:", max);
00667   range_dlg.field("Gamma:", gamma);
00668   range_dlg.checkbox("Invert:", invert);
00669   range_dlg.checkbox("Use GL Mapping", gl_map);
00670   range_dlg.checkbox("Cache Pixels", cache);
00671   if (!range_dlg.ask())
00672     return;
00673   if (nc==1)
00674     rmps= new vgui_range_map_params(min, max, gamma, invert,
00675                                     gl_map, cache);
00676   else if (nc == 3)
00677     rmps = new vgui_range_map_params(min, max, min, max, min, max,
00678                                      gamma, gamma, gamma, invert,
00679                                      gl_map, cache);
00680   else
00681     rmps = 0;
00682   itab->set_mapping(rmps);
00683 }
00684 
00685 void bmvv_f_manager::read_corrs()
00686 {
00687   vgui_dialog corr_dlg("Read Correspondences");
00688   static vcl_string corr_file = "";
00689   static vcl_string corr_ext = "*.corr";
00690   corr_dlg.file("Corr File", corr_ext, corr_file);
00691   if (!corr_dlg.ask())
00692     return;
00693   vcl_ifstream corr_istr(corr_file.c_str());
00694   corrs_.clear();
00695   if (!brct_algos::read_brct_corrs(corr_istr, corrs_))
00696   {
00697     vcl_cout << "Read Failed\n";
00698     return;
00699   }
00700   for (unsigned i = 0; i< corrs_.size(); ++i)
00701     vcl_cout << "c[" << i << "]:" << *(corrs_[i]) << '\n';
00702 }
00703 
00704 void bmvv_f_manager::save_corrs()
00705 {
00706   vcl_cout <<"Saving these corrs\n";
00707   for (unsigned i = 0; i< corrs_.size(); ++i)
00708     vcl_cout << "c[" << i << "]:" << *(corrs_[i]) << '\n';
00709   vgui_dialog corr_dlg("Save Correspondences");
00710   static vcl_string corr_file = "";
00711   static vcl_string corr_ext = "*.corr";
00712   corr_dlg.file("Corr File", corr_ext, corr_file);
00713   if (!corr_dlg.ask())
00714     return;
00715   vcl_ofstream corr_ostr(corr_file.c_str());
00716   if (!brct_algos::write_brct_corrs(corr_ostr, corrs_))
00717   {
00718     vcl_cout << "Read Failed\n";
00719     return;
00720   }
00721 }
00722 
00723 void bmvv_f_manager::read_f_matrix()
00724 {
00725   vgui_dialog f_dlg("Read F Matrix");
00726   static vcl_string f_file = "";
00727   static vcl_string f_ext = "*.fm";
00728   f_dlg.file("F Matrix File", f_ext, f_file);
00729   if (!f_dlg.ask())
00730     return;
00731   vcl_ifstream f_istr(f_file.c_str());
00732   if (!f_istr.is_open()){
00733     vcl_cout << "Read Failed\n";
00734     return;
00735   }
00736   vnl_matrix_fixed<double, 3,3> m;
00737   f_istr >> m;
00738   fm_.set_matrix(m);
00739 }
00740 
00741 void bmvv_f_manager::save_f_matrix()
00742 {
00743   vgui_dialog f_dlg("Save F Matrix");
00744   static vcl_string f_file = "";
00745   static vcl_string f_ext = "*.fm";
00746   f_dlg.file("F Matrix File", f_ext, f_file);
00747   if (!f_dlg.ask())
00748     return;
00749   vcl_ofstream f_ostr(f_file.c_str());
00750   if (!f_ostr.is_open()){
00751     vcl_cout << "Write Failed\n";
00752     return;
00753   }
00754   vnl_matrix_fixed<double, 3,3> m = fm_.get_matrix();
00755   f_ostr << m;
00756 }
00757 
00758 void bmvv_f_manager::display_corrs()
00759 {
00760   unsigned n = corrs_.size();
00761   if (!n)
00762     return;
00763   brct_corr_sptr bc = corrs_[0];
00764   unsigned int ncams = bc->n_cams();
00765   unsigned cols = grid_->cols();
00766   if (cols!=ncams)
00767     return;
00768   //assume left image is in col 0 and right image is in col 1
00769   bgui_vtol2D_tableau_sptr t0 = this->vtol2D_tab_at(0, 0);
00770   if (!t0)
00771     return;
00772   bgui_vtol2D_tableau_sptr t1 = this->vtol2D_tab_at(1, 0);
00773   if (!t1)
00774     return;
00775   vgui_style_sptr st = vgui_style::new_style(0,1,0,3,1);
00776   //clear the map
00777   corr_map_.clear();
00778   for (unsigned i=0; i<n; ++i)
00779   {
00780     bc = corrs_[i];
00781     vgl_point_2d<double> m0(bc->match(0)), m1(bc->match(1));
00782     vsol_point_2d_sptr p0 = new vsol_point_2d(m0);
00783     vsol_point_2d_sptr p1 = new vsol_point_2d(m1);
00784     bgui_vsol_soview2D_point* sov = t0->add_vsol_point_2d(p0,st);
00785     int id = sov->get_id();
00786     int n = corrs_.size();
00787     corr_map_[id]=n-1;
00788     t1->add_vsol_point_2d(p1,st);
00789   }
00790   t0->post_redraw();
00791   t1->post_redraw();
00792 }
00793 
00794 void bmvv_f_manager::display_right_epi_lines()
00795 {
00796   unsigned n = corrs_.size();
00797   if (!n)
00798     return;
00799   brct_corr_sptr bc = corrs_[0];
00800   unsigned int ncams = bc->n_cams();
00801   unsigned cols = grid_->cols();
00802   if (cols!=ncams)
00803     return;
00804   //assume left image is in col 0 and right image is in col 1
00805   bgui_vtol2D_tableau_sptr t1 = this->vtol2D_tab_at(1, 0);
00806   if (!t1)
00807     return;
00808   vgui_style_sptr st =vgui_style::new_style(0,1,0,1,2);
00809   for (unsigned i=0; i<n; ++i)
00810   {
00811     bc = corrs_[i];
00812     vgl_homg_point_2d<double> hpl=bc->match(0);
00813     vgl_homg_line_2d<double> hl = fm_.r_epipolar_line(hpl);
00814     t1->add_infinite_line((float)hl.a(), (float)hl.b(), (float)hl.c());
00815   }
00816   t1->post_redraw();
00817 }
00818 
00819 void bmvv_f_manager::display_picked_epi_line()
00820 {
00821   //determine if left or right image
00822   unsigned row=0, col=0;
00823   grid_->get_last_selected_position(&col, &row);
00824   bgui_picker_tableau_sptr picktab = this->selected_picker_tab();
00825   if (!picktab)
00826     return;
00827   //get a point
00828   float x=0, y=0;
00829   picktab->pick_point(&x, &y);
00830   vgl_homg_point_2d<double> hp(x,y);
00831   vgl_homg_line_2d<double> hl;
00832   if (col==0)
00833   {
00834     bgui_vtol2D_tableau_sptr t1 = this->vtol2D_tab_at(1, 0);
00835     hl = fm_.r_epipolar_line(hp);
00836     t1->add_infinite_line((float)hl.a(), (float)hl.b(), (float)hl.c());
00837     t1->post_redraw();
00838     return;
00839   }
00840   if (col==1)
00841   {
00842     bgui_vtol2D_tableau_sptr t0 = this->vtol2D_tab_at(0, 0);
00843     hl = fm_.l_epipolar_line(hp);
00844     t0->add_infinite_line((float)hl.a(), (float)hl.b(), (float)hl.c());
00845     t0->post_redraw();
00846     return;
00847   }
00848   vcl_cout << "Draw failed\n";
00849 }
00850 
00851 brct_corr_sptr  bmvv_f_manager::get_selected_corr()
00852 {
00853   bgui_vtol2D_tableau_sptr t0 = this->vtol2D_tab_at(0, 0);
00854   vcl_vector<unsigned> ids = t0->get_selected();
00855   //take the last selected
00856   int n = ids.size();
00857   if (!n)
00858   {
00859     vcl_cout << "Nothing selected\n";
00860     return 0;
00861   }
00862   unsigned int i = corr_map_[ids[n-1]];
00863   if (i<corrs_.size())
00864   {
00865     return corrs_[i];
00866   }
00867   vcl_cout << "Bogus correspondence\n";
00868   return 0;
00869 }
00870 
00871 void bmvv_f_manager::create_correspondence()
00872 {
00873   //determine if left or right image
00874   unsigned row=0, col=0;
00875   grid_->get_last_selected_position(&col, &row);
00876   if (col!=0)
00877   {
00878     vcl_cout << "Select left pane and retry\n";
00879     return;
00880   }
00881   bgui_picker_tableau_sptr picktab = this->selected_picker_tab();
00882   if (!picktab)
00883     return;
00884   //get a point
00885   float x=0, y=0;
00886   picktab->pick_point(&x, &y);
00887   brct_corr_sptr bc = new brct_corr(2);
00888   bc->set_match(col, x, y);
00889   corrs_.push_back(bc);
00890   bgui_vtol2D_tableau_sptr t0 = this->vtol2D_tab_at(0, 0);
00891   vsol_point_2d_sptr p = new vsol_point_2d(x, y);
00892   vgui_style_sptr st = vgui_style::new_style(1,1,0,5,1);
00893   bgui_vsol_soview2D_point* sov = t0->add_vsol_point_2d(p,st);
00894   int id = sov->get_id();
00895   int n = corrs_.size();
00896   corr_map_[id]=n-1;
00897 }
00898 
00899 void bmvv_f_manager::pick_correspondence()
00900 {
00901   brct_corr_sptr bc = this->get_selected_corr();
00902   if (!bc)
00903   {
00904     vcl_cout << "No correspondence selected\n";
00905     return;
00906   }
00907   //determine if left or right image
00908   unsigned row=0, col=0;
00909   grid_->get_last_selected_position(&col, &row);
00910   if (col!=1)
00911   {
00912     vcl_cout << "Select right pane and retry\n";
00913     return;
00914   }
00915   bgui_picker_tableau_sptr picktab = this->selected_picker_tab();
00916   if (!picktab)
00917     return;
00918   //get a point
00919   float x=0, y=0;
00920   picktab->pick_point(&x, &y);
00921   bc->set_match(col,x,y);
00922   bgui_vtol2D_tableau_sptr t1 = this->vtol2D_tab_at(1, 0);
00923   vsol_point_2d_sptr p = new vsol_point_2d(x, y);
00924   vgui_style_sptr st = vgui_style::new_style(1,1,0,5,1);
00925   t1->add_vsol_point_2d(p,st);
00926 }
00927 
00928 bool bmvv_f_manager::point_lists(vcl_vector<vgl_point_2d<double> >& lpts,
00929                                  vcl_vector<vgl_point_2d<double> >& rpts)
00930 {
00931   unsigned n = corrs_.size();
00932   if (!n)
00933     return false;
00934   lpts.clear(); rpts.clear();
00935   for (unsigned i = 0; i<n; ++i)
00936   {
00937     brct_corr_sptr bc = corrs_[i];
00938     if (!bc->valid(0)||!bc->valid(1))
00939       continue;
00940     vgl_homg_point_2d<double> m0 = bc->match(0), m1 = bc->match(1);
00941     lpts.push_back(vgl_point_2d<double>(m0));
00942     rpts.push_back(vgl_point_2d<double>(m1));
00943   }
00944   return true;
00945 }
00946 
00947 void bmvv_f_manager::compute_f_matrix()
00948 {
00949   vgui_dialog f_dlg("Compute F Matrix");
00950   static bool refine = false;
00951   static bool trans = false;
00952   f_dlg.checkbox("Refine F Matrix? ", refine);
00953   f_dlg.checkbox("Translation F Matrix? ", trans);
00954   if (!f_dlg.ask())
00955     return;
00956   vcl_vector<vgl_point_2d<double> > lpts, rpts;
00957   vcl_vector< vgl_homg_point_2d<double> > pir, pil;
00958   if (!this->point_lists(lpts, rpts))
00959   {
00960     vcl_cout << "No corresponding points to compute f matrix\n";
00961     return;
00962   }
00963   if (trans)
00964   {
00965     for (unsigned i = 0; i<lpts.size(); ++i)
00966     {
00967       pir.push_back(vgl_homg_point_2d<double>(rpts[i]));
00968       pil.push_back(vgl_homg_point_2d<double>(lpts[i]));
00969     }
00970     vpgl_fm_compute_2_point cl2;
00971     cl2.compute( pir, pil, fm_);
00972   }
00973   else
00974   {
00975     vpgl_fm_compute_ransac fmcr;
00976     fmcr.set_generate_all(true);
00977     fmcr.set_outlier_threshold(2);
00978     fmcr.compute( rpts, lpts, fm_);
00979     vnl_matrix_fixed<double,3,3> fm_vnl = fm_.get_matrix();
00980     vcl_cout << "\nRansac estimated fundamental matrix:\n" << fm_vnl << '\n';
00981     vcl_vector<bool> outliers = fmcr.outliers;
00982     vcl_vector<double> res = fmcr.residuals;
00983     vcl_cout << "\noutliers\n";
00984     for (unsigned i = 0; i<outliers.size(); ++i)
00985       vcl_cout << "O[" << i << "]= " << outliers[i]
00986                << "  e "<< res[i] <<  '\n';
00987     //Form new point sets throwing out outliers
00988     for (unsigned i = 0; i<lpts.size(); ++i)
00989     {
00990       if (outliers[i])
00991         continue;
00992       pir.push_back(vgl_homg_point_2d<double>(rpts[i]));
00993       pil.push_back(vgl_homg_point_2d<double>(lpts[i]));
00994     }
00995     if (refine){
00996       vpgl_fm_compute_8_point cl;
00997       cl.compute( pir, pil, fm_);
00998       vnl_matrix_fixed<double,3,3> fl_vnl = fm_.get_matrix();
00999       vcl_cerr << "\nLinear refined fundamental matrix:\n" << fl_vnl << '\n';
01000     }
01001   }
01002   vgl_homg_point_2d<double> er,  el;
01003   fm_.get_epipoles(er, el);
01004   vcl_cout << "Right Epipole " << er << '\n';
01005 }
01006 
01007 void bmvv_f_manager::intensity_profile()
01008 {
01009   bgui_picker_tableau_sptr ptab = selected_picker_tab();
01010   float start_col=0, end_col=0, start_row=0, end_row=0;
01011   ptab->pick_line(&start_col, &start_row, &end_col, &end_row);
01012   bgui_image_tableau_sptr itab = selected_image_tab();
01013   vcl_vector<double> pos, vals;
01014   itab->image_line(start_col, start_row, end_col, end_row, pos, vals);
01015   bgui_graph_tableau_sptr g = bgui_graph_tableau_new(512, 512);
01016   g->update(pos, vals);
01017   //popup a profile graph
01018   char location[100];
01019   vcl_sprintf(location, "scan:(%d, %d)<->(%d, %d)",
01020               static_cast<unsigned>(start_col),
01021               static_cast<unsigned>(start_row),
01022               static_cast<unsigned>(end_col),
01023               static_cast<unsigned>(end_row));
01024   vgui_dialog* ip_dialog = g->popup_graph(location);
01025   if (!ip_dialog->ask())
01026   {
01027     delete ip_dialog;
01028     return;
01029   }
01030   delete ip_dialog;
01031 }
01032 
01033 void bmvv_f_manager::intensity_histogram()
01034 {
01035   vil_image_resource_sptr img = selected_image();
01036   if (!img||!img->ni()||!img->nj())
01037   {
01038     vcl_cout << "In bmvv_f_manager::intensity_histogram() - no image\n";
01039     return;
01040   }
01041   bgui_image_utils iu(img);
01042   bgui_graph_tableau_sptr g = iu.hist_graph();
01043 
01044   if (!g)
01045   { vcl_cout << "In bmvv_f_manager::intensity_histogram()- color images not supported\n";
01046     return;
01047   }
01048 
01049   //popup a profile graph
01050   char location[100];
01051   vcl_sprintf(location, "Intensity Histogram");
01052   vgui_dialog* ip_dialog = g->popup_graph(location);
01053   if (!ip_dialog->ask())
01054   {
01055     delete ip_dialog;
01056     return;
01057   }
01058   delete ip_dialog;
01059 }
01060 
01061 void bmvv_f_manager::load_image_and_cam()
01062 {
01063   static bool greyscale = false;
01064   static vcl_string cam_file = "";
01065   static vcl_string cam_ext = "*.cam";
01066   static vcl_string image_file = "";
01067   static vcl_string ext = "*.*";
01068   vgui_dialog image_cam_dlg("Read Image and Cam");
01069   image_cam_dlg.file("Image Filename:", ext, image_file);
01070   image_cam_dlg.file("Camera File", cam_ext, cam_file);
01071   image_cam_dlg.checkbox("greyscale ", greyscale);
01072   if (!image_cam_dlg.ask())
01073     return;
01074   //first check to see if the filename is a directory
01075   //if so, then assume a pyramid image
01076   bool pyrm = false;
01077   vil_image_resource_sptr image;
01078   if (vul_file::is_directory(image_file.c_str()))
01079   {
01080     vil_pyramid_image_resource_sptr pyr =
01081       vil_load_pyramid_resource(image_file.c_str());
01082     if (pyr)
01083     {
01084       image = pyr.ptr();
01085       pyrm = true;
01086     }
01087   }
01088   if (!image)
01089     image = vil_load_image_resource(image_file.c_str());
01090 
01091   if (!image)
01092     return;
01093 
01094   if (greyscale&&!pyrm)
01095   {
01096     vil_image_view<unsigned char> grey_view =
01097       brip_vil_float_ops::convert_to_grey(*image);
01098     image = vil_new_image_resource_of_view(grey_view);
01099   }
01100 
01101   vgui_range_map_params_sptr rmps = range_params(image);
01102 
01103   if (first_)
01104   {
01105     this->set_selected_grid_image(image, rmps);
01106     first_ = false;
01107   }
01108   else
01109     this->add_image(image, rmps);
01110 
01111   vcl_ifstream f_istr(cam_file.c_str());
01112   if (!f_istr.is_open()) {
01113     vcl_cout << "Camera read failed\n";
01114     return;
01115   }
01116 
01117   vpgl_proj_camera<double>* cam = new vpgl_proj_camera<double>;
01118   f_istr >> *cam;
01119   cam_map_[vtabs_.size()-1]=cam;
01120 }
01121 
01122 void bmvv_f_manager::load_world()
01123 {
01124   vgui_dialog world_dlg("Load World Points3D");
01125   static vcl_string world_file = "";
01126   static vcl_string world_ext = "*.wd";
01127   world_dlg.file("World Point File", world_ext, world_file);
01128   if (!world_dlg.ask())
01129     return;
01130   vcl_ifstream world_istr(world_file.c_str());
01131   if (!brct_algos::read_world_points(world_istr, world_))
01132   {
01133     vcl_cout << "Failed to read world\n";
01134     return ;
01135   }
01136   this->project_world();
01137 }
01138 
01139 void bmvv_f_manager::save_world()
01140 {
01141   vgui_dialog world_dlg("Save World Points3D");
01142   static vcl_string world_file = "";
01143   static vcl_string world_ext = "*.wd";
01144   world_dlg.file("World Point File", world_ext, world_file);
01145   if (!world_dlg.ask())
01146     return;
01147   vcl_ofstream world_ostr(world_file.c_str());
01148   brct_algos::write_world_points(world_ostr, world_);
01149 }
01150 
01151 void bmvv_f_manager::project_world()
01152 {
01153   vgui_style_sptr st = vgui_style::new_style(1,1,0,5,1);
01154   for (unsigned v = 0; v<vtabs_.size(); ++v)
01155   {
01156     bgui_vtol2D_tableau_sptr t = vtabs_[v];
01157     vpgl_proj_camera<double>* cam = cam_map_[v];
01158     t->clear_all();
01159     for (unsigned i = 0; i<world_.size(); ++i)
01160     {
01161       vgl_point_2d<double> pg = cam->project(world_[i]);
01162       vsol_point_2d_sptr ps = new vsol_point_2d(pg);
01163       t->add_vsol_point_2d(ps,st);
01164     }
01165   }
01166 }
01167 
01168 void bmvv_f_manager::reconstruct_world()
01169 {
01170   if (cam_map_.size()!=2)
01171   {
01172     vcl_cout << "Need exactly two cameras to reconstruct world\n";
01173     return;
01174   }
01175   brct_algos::reconstruct_corrs(corrs_, *(cam_map_[0]), *(cam_map_[1]), world_);
01176 }