Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

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 #if 0
00031 #ifdef HAS_XERCES
00032 #include <bxml/bxml_vtol_io.h>
00033 #endif
00034 #endif
00035 #include <sdet/sdet_detector_params.h>
00036 #include <sdet/sdet_detector.h>
00037 #include <sdet/sdet_harris_detector_params.h>
00038 #include <sdet/sdet_harris_detector.h>
00039 #include <sdet/sdet_nonmax_suppression_params.h>
00040 #include <sdet/sdet_nonmax_suppression.h>
00041 #include <sdet/sdet_fit_lines_params.h>
00042 #include <sdet/sdet_fit_lines.h>
00043 #include <sdet/sdet_fit_conics.h>
00044 #include <sdet/sdet_grid_finder_params.h>
00045 #include <sdet/sdet_grid_finder.h>
00046 #include <vgui/vgui.h>
00047 #include <vgui/vgui_find.h>
00048 #include <vgui/vgui_tableau.h>
00049 #include <vgui/vgui_dialog.h>
00050 #include <vgui/vgui_style_sptr.h>
00051 #include <vgui/vgui_style.h>
00052 #include <vgui/vgui_viewer2D_tableau.h>
00053 #include <vgui/vgui_shell_tableau.h>
00054 #include <vgui/vgui_grid_tableau.h>
00055 #include <vgui/vgui_range_map_params.h>
00056 #include <bgui/bgui_image_tableau.h>
00057 #include <bgui/bgui_vtol2D_tableau.h>
00058 #include <bgui/bgui_picker_tableau.h>
00059 #include <bgui/bgui_range_adjuster_tableau.h>
00060 #include <bgui/bgui_image_utils.h>
00061 #include <vsol/vsol_point_2d.h>
00062 #include <vsol/vsol_point_2d_sptr.h>
00063 #include <vsol/vsol_conic_2d.h>
00064 #include <vsol/vsol_polyline_2d.h>
00065 #include <vtol/vtol_vertex_2d.h>
00066 #include <vtol/vtol_vertex.h>
00067 #include <vtol/vtol_edge_2d.h>
00068 #include <vtol/vtol_intensity_face.h>
00069 #include <brip/brip_vil1_float_ops.h>
00070 #include <brip/brip_vil_float_ops.h>
00071 #include <brip/brip_para_cvrg_params.h>
00072 #include <brip/brip_para_cvrg.h>
00073 #include <brip/brip_watershed_params.h>
00074 #include <sdet/sdet_watershed_region_proc_params.h>
00075 #include <sdet/sdet_watershed_region_proc.h>
00076 #include <sdet/sdet_vehicle_finder_params.h>
00077 #include <sdet/sdet_vehicle_finder.h>
00078 #include <sdet/sdet_region_proc_params.h>
00079 #include <sdet/sdet_region_proc.h>
00080 #include <strk/strk_region_info_params.h>
00081 #include <strk/strk_region_info.h>
00082 #include <strk/strk_io.h>
00083 #include <brct/brct_algos.h>
00084 
00085 ;
00086 bmvv_f_manager *bmvv_f_manager::instance_ = 0;
00087 
00088 bmvv_f_manager *bmvv_f_manager::instance()
00089 {
00090   if (!instance_)
00091   {
00092     instance_ = new bmvv_f_manager();
00093     instance_->init();
00094   }
00095   return bmvv_f_manager::instance_;
00096 }
00097 
00098 //-----------------------------------------------------------
00099 // constructors/destructor
00100 //
00101 bmvv_f_manager::bmvv_f_manager():vgui_wrapper_tableau()
00102 {
00103   first_ = true;
00104 }
00105 
00106 bmvv_f_manager::~bmvv_f_manager()
00107 {
00108   for (unsigned v = 0; v<vtabs_.size(); ++v)
00109   {
00110     bgui_vtol2D_tableau_sptr t = vtabs_[v];
00111     vpgl_proj_camera<double>* cam = cam_map_[v];
00112     cam_map_[v] = 0;
00113     delete cam;
00114   }
00115 }
00116 
00117 //: Set up the tableaux
00118 void bmvv_f_manager::init()
00119 {
00120   bgui_image_tableau_sptr itab = bgui_image_tableau_new();
00121   bgui_vtol2D_tableau_sptr t2D = bgui_vtol2D_tableau_new(itab);
00122   vtabs_.push_back(t2D);
00123   bgui_picker_tableau_sptr picktab = bgui_picker_tableau_new(t2D);
00124   vgui_viewer2D_tableau_sptr v2D = vgui_viewer2D_tableau_new(picktab);
00125   grid_ = vgui_grid_tableau_new(1,1);
00126   grid_->set_grid_size_changeable(true);
00127   grid_->add_at(v2D, 0, 0);
00128   vgui_shell_tableau_sptr shell = vgui_shell_tableau_new(grid_);
00129   this->add_child(shell);
00130   first_ = true;
00131 }
00132 
00133 //: Calculate the range parameters for the input image
00134 vgui_range_map_params_sptr bmvv_f_manager::
00135 range_params(vil_image_resource_sptr const& image)
00136 {
00137   float gamma = 1.0;
00138   bool invert = false;
00139   bool gl_map = false;
00140   bool cache = true;
00141 
00142   //Check if the image is blocked
00143   vil_blocked_image_resource_sptr bir = blocked_image_resource(image);
00144   if (bir)
00145   { gl_map = true; cache = false; }
00146 
00147   //Check if the image is a pyramid
00148   bool pyr = image->get_property(vil_property_pyramid, 0);
00149   if (pyr)
00150   { gl_map = true; cache = false; }
00151   //Get max min parameters
00152 
00153   double min=0, max=0;
00154   unsigned n_components = image->nplanes();
00155   vgui_range_map_params_sptr rmps;
00156   if (n_components == 1)
00157   {
00158     bgui_image_utils iu(image);
00159     iu.range(min, max);
00160     rmps= new vgui_range_map_params(min, max, gamma, invert,
00161                                     gl_map, cache);
00162   }
00163   else if (n_components == 3)
00164   {
00165     min = 0; max = 255;//for now - ultimately need to compute color histogram
00166     rmps = new vgui_range_map_params(min, max, min, max, min, max,
00167                                      gamma, gamma, gamma, invert,
00168                                      gl_map, cache);
00169   }
00170   return rmps;
00171 }
00172 
00173 //: set the image at the currently selected grid cell
00174 void bmvv_f_manager::
00175 set_selected_grid_image(vil_image_resource_sptr const& image,
00176                         vgui_range_map_params_sptr const& rmps)
00177 {
00178   bgui_image_tableau_sptr itab = this->selected_image_tab();
00179   if (!itab)
00180     this->add_image(image, rmps);
00181   else
00182   {
00183     itab->set_image_resource(image);
00184     itab->set_mapping(rmps);
00185   }
00186   itab->post_redraw();
00187 }
00188 
00189 //: Add an image at the specified grid cell
00190 void bmvv_f_manager::
00191 add_image_at(vil_image_resource_sptr const& image,
00192              const unsigned col, const unsigned row,
00193              vgui_range_map_params_sptr const& rmps)
00194 {
00195   vgui_range_map_params_sptr rmap = rmps;
00196   if (!rmps)
00197     rmap = range_params(image);
00198   bgui_image_tableau_sptr itab = bgui_image_tableau_new(image);
00199   itab->set_mapping(rmap);
00200   bgui_vtol2D_tableau_sptr t2D = bgui_vtol2D_tableau_new(itab);
00201   vtabs_.push_back(t2D);
00202   bgui_picker_tableau_sptr picktab = bgui_picker_tableau_new(t2D);
00203   vgui_viewer2D_tableau_sptr v2D = vgui_viewer2D_tableau_new(picktab);
00204   grid_->add_at(v2D, col, row);
00205   itab->post_redraw();
00206 }
00207 
00208 //: Add an image to the currently selected grid cell
00209 void bmvv_f_manager::
00210 add_image(vil_image_resource_sptr const& image,
00211           vgui_range_map_params_sptr const& rmps)
00212 {
00213   unsigned row=0, col=0;
00214   grid_->get_last_selected_position(&col, &row);
00215   this->add_image_at(image, col, row, rmps);
00216 }
00217 
00218 #if 0
00219 //-----------------------------------------------------------------------------
00220 //: remove the selected image
00221 //-----------------------------------------------------------------------------
00222 void bmvv_f_manager::remove_image()
00223 {
00224   unsigned row=0, col=0;
00225   grid_->get_last_selected_position(&col, &row);
00226   grid_->remove_at(col, row);
00227 }
00228 
00229 void bmvv_f_manager::convert_to_grey()
00230 {
00231   vil_image_resource_sptr img = this->selected_image();
00232   if (!img)
00233     return;
00234   vil_image_view<unsigned char> grey =
00235     brip_vil_float_ops::convert_to_byte(img);
00236   vil_image_resource_sptr gimg = vil_new_image_resource_of_view(grey);
00237   this->add_image(gimg);
00238 }
00239 #endif
00240 
00241 //: Get the image tableau for the currently selected grid cell
00242 bgui_image_tableau_sptr bmvv_f_manager::selected_image_tab()
00243 {
00244   unsigned row=0, col=0;
00245   grid_->get_last_selected_position(&col, &row);
00246   vgui_tableau_sptr top_tab = grid_->get_tableau_at(col, row);
00247   if (top_tab)
00248   {
00249     bgui_image_tableau_sptr itab;
00250     itab.vertical_cast(vgui_find_below_by_type_name(top_tab,
00251                                                     vcl_string("vgui_image_tableau")));
00252     if (itab)
00253       return itab;
00254   }
00255   vcl_cout << "Unable to get bgui_image_tableau at (" << col
00256            << ", " << row << ")\n";
00257   return bgui_image_tableau_sptr();
00258 }
00259 
00260 //: Get the vtol2D tableau at the specified grid cell
00261 bgui_vtol2D_tableau_sptr
00262 bmvv_f_manager::vtol2D_tab_at(const unsigned col,
00263                               const unsigned row)
00264 {
00265   vgui_tableau_sptr top_tab = grid_->get_tableau_at(col, row);
00266   if (top_tab)
00267   {
00268     bgui_vtol2D_tableau_sptr v2D;
00269     v2D.vertical_cast(vgui_find_below_by_type_name(top_tab,
00270                                                    vcl_string("bgui_vtol2D_tableau")));
00271     if (v2D)
00272       return v2D;
00273   }
00274   vcl_cout << "Unable to get bgui_vtol2D_tableau at (" << col
00275            << ", " << row << ")\n";
00276   return bgui_vtol2D_tableau_sptr();
00277 }
00278 
00279 //: Get the vtol2D tableau for the currently selected grid cell
00280 bgui_vtol2D_tableau_sptr bmvv_f_manager::selected_vtol2D_tab()
00281 {
00282   unsigned row=0, col=0;
00283   grid_->get_last_selected_position(&col, &row);
00284   return this->vtol2D_tab_at(col, row);
00285 }
00286 
00287 //: Get the picker tableau for the currently selected grid cell
00288 bgui_picker_tableau_sptr bmvv_f_manager::selected_picker_tab()
00289 {
00290   unsigned row=0, col=0;
00291   grid_->get_last_selected_position(&col, &row);
00292   vgui_tableau_sptr top_tab = grid_->get_tableau_at(col, row);
00293   if (top_tab)
00294   {
00295     bgui_picker_tableau_sptr pick;
00296     pick.vertical_cast(vgui_find_below_by_type_name(top_tab,
00297                                                     vcl_string("bgui_picker_tableau")));
00298     if (pick)
00299       return pick;
00300   }
00301   vcl_cout << "Unable to get bgui_picker_tableau at (" << col
00302            << ", " << row << ")\n";
00303   return bgui_picker_tableau_sptr();
00304 }
00305 
00306 
00307 vil_image_resource_sptr bmvv_f_manager::selected_image()
00308 {
00309   bgui_image_tableau_sptr itab = this->selected_image_tab();
00310   if (!itab)
00311     return 0;
00312   return itab->get_image_resource();
00313 }
00314 
00315 vil_image_resource_sptr bmvv_f_manager::image_at(const unsigned col,
00316                                                  const unsigned row)
00317 {
00318   vgui_tableau_sptr top_tab = grid_->get_tableau_at(col, row);
00319   if (!top_tab)
00320     return 0;
00321 
00322   bgui_image_tableau_sptr itab;
00323   itab.vertical_cast(vgui_find_below_by_type_name(top_tab,
00324                                                   vcl_string("vgui_image_tableau")));
00325   if (!itab)
00326   {
00327     vcl_cout << "Unable to get bgui_image_tableau at (" << col
00328              << ", " << row << ")\n";
00329     return 0;
00330   }
00331   return itab->get_image_resource();
00332 }
00333 
00334 //-----------------------------------------------------------------------------
00335 //: Clear spatial objects from the selected display
00336 //-----------------------------------------------------------------------------
00337 void bmvv_f_manager::clear_display()
00338 {
00339   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00340   if (!t2D)
00341     return;
00342   t2D->clear_all();
00343 }
00344 
00345 //-----------------------------------------------------------------------------
00346 //: Clear spatial objects from all spatial panes
00347 //-----------------------------------------------------------------------------
00348 void bmvv_f_manager::clear_all()
00349 {
00350   unsigned ncols = grid_->cols(), nrows = grid_->rows();
00351   for (unsigned r=0; r<nrows; ++r)
00352     for (unsigned c=0; c<ncols; ++c)
00353     {
00354       bgui_vtol2D_tableau_sptr t = this->vtol2D_tab_at(c, r);
00355       if (t)
00356         t->clear_all();
00357     }
00358 }
00359 
00360 //-----------------------------------------------------------------------------
00361 //: Draw edges onto the tableau
00362 //-----------------------------------------------------------------------------
00363 void
00364 bmvv_f_manager::draw_edges(vcl_vector<vtol_edge_2d_sptr>& edges,
00365                            bool verts)
00366 {
00367   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00368   if (!t2D)
00369     return;
00370   this->clear_display();
00371 #if 0
00372   vgui_image_tableau_sptr itab = t2D->get_image_tableau();
00373   if (!itab)
00374   {
00375     vcl_cout << "In bmvv_f_manager::draw_edges - null image tab\n";
00376     return;
00377   }
00378 #endif
00379   for (vcl_vector<vtol_edge_2d_sptr>::iterator eit = edges.begin();
00380        eit != edges.end(); eit++)
00381   {
00382     t2D->add_edge(*eit);
00383     //optionally display the edge vertices
00384     if (verts)
00385     {
00386       if ((*eit)->v1())
00387       {
00388         vtol_vertex_2d_sptr v1 = (*eit)->v1()->cast_to_vertex_2d();
00389         t2D->add_vertex(v1);
00390       }
00391       if ((*eit)->v2())
00392       {
00393         vtol_vertex_2d_sptr v2 = (*eit)->v2()->cast_to_vertex_2d();
00394         t2D->add_vertex(v2);
00395       }
00396     }
00397   }
00398   t2D->post_redraw();
00399 }
00400 
00401 
00402 //-----------------------------------------------------------------------------
00403 //: Draw polylines on the tableau
00404 //-----------------------------------------------------------------------------
00405 void bmvv_f_manager::
00406 draw_polylines(vcl_vector<vsol_polyline_2d_sptr > const& polys,
00407                vgui_style_sptr style)
00408 {
00409   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00410   if (!t2D)
00411     return;
00412   //this->clear_display();
00413 #if 0
00414   vgui_image_tableau_sptr itab = t2D->get_image_tableau();
00415   if (!itab)
00416   {
00417     vcl_cout << "In bmvv_f_manager::draw_polylines - null image tab\n";
00418     return;
00419   }
00420 #endif
00421   for (vcl_vector<vsol_polyline_2d_sptr>::const_iterator pit = polys.begin();
00422        pit != polys.end(); pit++)
00423   {
00424     t2D->add_vsol_polyline_2d(*pit, style);
00425   }
00426 
00427   t2D->post_redraw();
00428 }
00429 
00430 //-----------------------------------------------------------------------------
00431 //: Draw line segments on the tableau
00432 //-----------------------------------------------------------------------------
00433 void bmvv_f_manager::
00434 draw_lines(vcl_vector<vsol_line_2d_sptr > const& lines,
00435            const vgui_style_sptr& style)
00436 {
00437   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00438   if (!t2D)
00439     return;
00440   //this->clear_display();
00441 #if 0
00442   vgui_image_tableau_sptr itab = t2D->get_image_tableau();
00443   if (!itab)
00444   {
00445     vcl_cout << "In bmvv_f_manager::draw_edges - null image tab\n";
00446     return;
00447   }
00448 #endif
00449   for (vcl_vector<vsol_line_2d_sptr>::const_iterator lit = lines.begin();
00450        lit != lines.end(); lit++)
00451   {
00452     t2D->add_vsol_line_2d(*lit,style);
00453   }
00454 
00455   t2D->post_redraw();
00456 }
00457 
00458 //-----------------------------------------------------------------------------
00459 //: Draw conic segments on the tableau
00460 //-----------------------------------------------------------------------------
00461 void bmvv_f_manager::
00462 draw_conics(vcl_vector<vsol_conic_2d_sptr > const& conics,
00463             const vgui_style_sptr& style)
00464 {
00465   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00466   if (!t2D)
00467     return;
00468   //this->clear_display();
00469 #if 0
00470   vgui_image_tableau_sptr itab = t2D->get_image_tableau();
00471   if (!itab)
00472   {
00473     vcl_cout << "In bmvv_f_manager::draw_edges - null image tab\n";
00474     return;
00475   }
00476 #endif
00477   for (vcl_vector<vsol_conic_2d_sptr>::const_iterator lit = conics.begin();
00478        lit != conics.end(); lit++)
00479   {
00480     t2D->add_vsol_conic_2d(*lit,style);
00481   }
00482   t2D->post_redraw();
00483 }
00484 
00485 //-----------------------------------------------------------------------------
00486 //: Draw points on the tableau
00487 //-----------------------------------------------------------------------------
00488 void bmvv_f_manager::
00489 draw_points(vcl_vector<vsol_point_2d_sptr> const& points, const vgui_style_sptr& style)
00490 {
00491   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00492   if (!t2D)
00493     return;
00494   //this->clear_display();
00495 #if 0
00496   vgui_image_tableau_sptr itab = t2D->get_image_tableau();
00497   if (!itab)
00498   {
00499     vcl_cout << "In bmvv_f_manager::draw_edges - null image tab\n";
00500     return;
00501   }
00502 #endif
00503   for (vcl_vector<vsol_point_2d_sptr>::const_iterator pit = points.begin();
00504        pit != points.end(); pit++)
00505   {
00506     t2D->add_vsol_point_2d(*pit,style);
00507   }
00508 
00509   t2D->post_redraw();
00510 }
00511 
00512 void bmvv_f_manager::draw_regions(vcl_vector<vtol_intensity_face_sptr>& regions,
00513                                   bool verts)
00514 {
00515   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00516   if (!t2D)
00517     return;
00518   for (vcl_vector<vtol_intensity_face_sptr>::iterator rit = regions.begin();
00519        rit != regions.end(); rit++)
00520   {
00521     vtol_face_2d_sptr f = (*rit)->cast_to_face_2d();
00522     t2D->add_face(f);
00523     if (verts)
00524     {
00525       vcl_vector<vtol_vertex_sptr> vts;
00526       f->vertices(vts);
00527       for (vcl_vector<vtol_vertex_sptr>::iterator vit = vts.begin();
00528            vit != vts.end(); vit++)
00529       {
00530         vtol_vertex_2d_sptr v = (*vit)->cast_to_vertex_2d();
00531         t2D->add_vertex(v);
00532       }
00533     }
00534   }
00535   t2D->post_redraw();
00536 }
00537 
00538 void bmvv_f_manager::quit()
00539 {
00540   this->clear_all();
00541   vgui::quit();
00542 }
00543 
00544 void bmvv_f_manager::load_image()
00545 {
00546   static bool greyscale = false;
00547   static bool sblock = false;
00548   vgui_dialog load_image_dlg("Load image file");
00549   static vcl_string image_filename = "/home/dec/images/cal_image1.tif";
00550   static vcl_string ext = "*.*";
00551   load_image_dlg.file("Image Filename:", ext, image_filename);
00552   load_image_dlg.checkbox("greyscale ", greyscale);
00553   load_image_dlg.checkbox("blocked?:", sblock);
00554   if (!load_image_dlg.ask())
00555     return;
00556   //first check to see if the filename is a directory
00557   //if so, then assume a pyramid image
00558   bool pyrm = false;
00559   vil_image_resource_sptr image;
00560   if (vul_file::is_directory(image_filename.c_str()))
00561   {
00562     vil_pyramid_image_resource_sptr pyr =
00563       vil_load_pyramid_resource(image_filename.c_str());
00564     if (pyr)
00565     {
00566       image = pyr.ptr();
00567       pyrm = true;
00568     }
00569   }
00570   if (!image)
00571     image = vil_load_image_resource(image_filename.c_str());
00572 
00573   if (!image)
00574     return;
00575 
00576   if (greyscale&&!pyrm)
00577   {
00578     vil_image_view<unsigned char> grey_view =
00579       brip_vil_float_ops::convert_to_grey(*image);
00580     image = vil_new_image_resource_of_view(grey_view);
00581   }
00582 
00583   if (sblock&&!pyrm)
00584   {
00585     vil_blocked_image_resource_sptr bimage = vil_new_blocked_image_facade(image);
00586     image = (vil_image_resource*)(vil_new_cached_image_resource(bimage)).ptr();
00587   }
00588 
00589   vgui_range_map_params_sptr rmps = range_params(image);
00590 
00591   if (first_)
00592   {
00593     this->set_selected_grid_image(image, rmps);
00594     first_ = false;
00595   }
00596   else
00597     this->add_image(image, rmps);
00598 }
00599 
00600 void bmvv_f_manager::save_image()
00601 {
00602   vgui_dialog file_dialog("Save Image");
00603   static vcl_string image_file;
00604   static vcl_string ext = "tif";
00605   static vcl_string type = "tiff";
00606   static unsigned size_block = 0;
00607   static bool byte = false;
00608   file_dialog.file("Image Filename:", ext, image_file);
00609   file_dialog.field("Image Format: ", type);
00610   file_dialog.field("BlockSize", size_block);
00611   file_dialog.checkbox("Convert to byte image", byte);
00612   if (!file_dialog.ask())
00613     return;
00614   vil_image_resource_sptr img = this->selected_image();
00615   if (!img)
00616   {
00617     vcl_cerr << "Null image in bmvv_f_manager::save_image\n";
00618     return;
00619   }
00620   vil_image_resource_sptr save_image = img;
00621   if (byte)
00622   {
00623     vil_image_view<unsigned char> byte_view = brip_vil_float_ops::convert_to_byte(img);
00624     save_image = vil_new_image_resource_of_view(byte_view);
00625   }
00626   if (size_block>0)
00627   {
00628     vil_blocked_image_resource_sptr bim =
00629       vil_new_blocked_image_resource(image_file.c_str(),
00630                                      save_image->ni(), save_image->nj(),
00631                                      save_image->nplanes(),
00632                                      save_image->pixel_format(),
00633                                      size_block, size_block,
00634                                      "tiff");
00635     vil_image_view_base_sptr view = save_image->get_view();
00636     if (view)
00637       bim->vil_image_resource::put_view(*view);
00638     return;
00639   }
00640 
00641   if (!vil_save_image_resource(save_image, image_file.c_str(), type.c_str()))
00642     vcl_cerr << "bmvv_f_manager::save_image operation failed\n";
00643 }
00644 
00645 void bmvv_f_manager::set_range_params()
00646 {
00647   bgui_image_tableau_sptr itab = this->selected_image_tab();
00648   if (!itab)
00649     return;
00650   vgui_range_map_params_sptr rmps = itab->map_params();
00651   if (!rmps)
00652   {
00653     vil_image_resource_sptr img = itab->get_image_resource();
00654     if (!img)
00655       return;
00656     rmps = range_params(img);
00657     if (!rmps)
00658       return;
00659   }
00660   unsigned nc = rmps->n_components_;
00661   static double min = static_cast<double>(rmps->min_L_),
00662                 max = static_cast<double>(rmps->max_L_);
00663   static float gamma = rmps->gamma_L_;
00664   static bool invert = rmps->invert_;
00665   static bool gl_map = rmps->use_glPixelMap_;
00666   static bool cache = rmps->cache_mapped_pix_;
00667   if (nc==3)
00668   {
00669     min = static_cast<double>(rmps->min_R_);
00670     max = static_cast<double>(rmps->max_R_);
00671     gamma = rmps->gamma_R_;
00672   }
00673   vgui_dialog range_dlg("Set Range Map Params");
00674   range_dlg.field("Range min:", min);
00675   range_dlg.field("Range max:", max);
00676   range_dlg.field("Gamma:", gamma);
00677   range_dlg.checkbox("Invert:", invert);
00678   range_dlg.checkbox("Use GL Mapping", gl_map);
00679   range_dlg.checkbox("Cache Pixels", cache);
00680   if (!range_dlg.ask())
00681     return;
00682   if (nc==1)
00683     rmps= new vgui_range_map_params(min, max, gamma, invert,
00684                                     gl_map, cache);
00685   else if (nc == 3)
00686     rmps = new vgui_range_map_params(min, max, min, max, min, max,
00687                                      gamma, gamma, gamma, invert,
00688                                      gl_map, cache);
00689   else
00690     rmps = 0;
00691   itab->set_mapping(rmps);
00692 }
00693 
00694 void bmvv_f_manager::read_corrs()
00695 {
00696   vgui_dialog corr_dlg("Read Correspondences");
00697   static vcl_string corr_file = "";
00698   static vcl_string corr_ext = "*.corr";
00699   corr_dlg.file("Corr File", corr_ext, corr_file);
00700   if (!corr_dlg.ask())
00701     return;
00702   vcl_ifstream corr_istr(corr_file.c_str());
00703   corrs_.clear();
00704   if (!brct_algos::read_brct_corrs(corr_istr, corrs_))
00705   {
00706     vcl_cout << "Read Failed\n";
00707     return;
00708   }
00709   for (unsigned i = 0; i< corrs_.size(); ++i)
00710     vcl_cout << "c[" << i << "]:" << *(corrs_[i]) << '\n';
00711 }
00712 
00713 void bmvv_f_manager::save_corrs()
00714 {
00715   vcl_cout <<"Saving these corrs\n";
00716   for (unsigned i = 0; i< corrs_.size(); ++i)
00717     vcl_cout << "c[" << i << "]:" << *(corrs_[i]) << '\n';
00718   vgui_dialog corr_dlg("Save Correspondences");
00719   static vcl_string corr_file = "";
00720   static vcl_string corr_ext = "*.corr";
00721   corr_dlg.file("Corr File", corr_ext, corr_file);
00722   if (!corr_dlg.ask())
00723     return;
00724   vcl_ofstream corr_ostr(corr_file.c_str());
00725   if (!brct_algos::write_brct_corrs(corr_ostr, corrs_))
00726   {
00727     vcl_cout << "Read Failed\n";
00728     return;
00729   }
00730 }
00731 
00732 void bmvv_f_manager::read_f_matrix()
00733 {
00734   vgui_dialog f_dlg("Read F Matrix");
00735   static vcl_string f_file = "";
00736   static vcl_string f_ext = "*.fm";
00737   f_dlg.file("F Matrix File", f_ext, f_file);
00738   if (!f_dlg.ask())
00739     return;
00740   vcl_ifstream f_istr(f_file.c_str());
00741   if (!f_istr.is_open()){
00742     vcl_cout << "Read Failed\n";
00743     return;
00744   }
00745   vnl_matrix_fixed<double, 3,3> m;
00746   f_istr >> m;
00747   fm_.set_matrix(m);
00748 }
00749 
00750 void bmvv_f_manager::save_f_matrix()
00751 {
00752   vgui_dialog f_dlg("Save F Matrix");
00753   static vcl_string f_file = "";
00754   static vcl_string f_ext = "*.fm";
00755   f_dlg.file("F Matrix File", f_ext, f_file);
00756   if (!f_dlg.ask())
00757     return;
00758   vcl_ofstream f_ostr(f_file.c_str());
00759   if (!f_ostr.is_open()){
00760     vcl_cout << "Write Failed\n";
00761     return;
00762   }
00763   vnl_matrix_fixed<double, 3,3> m = fm_.get_matrix();
00764   f_ostr << m;
00765 }
00766 
00767 void bmvv_f_manager::display_corrs()
00768 {
00769   unsigned n = corrs_.size();
00770   if (!n)
00771     return;
00772   brct_corr_sptr bc = corrs_[0];
00773   unsigned int ncams = bc->n_cams();
00774   unsigned cols = grid_->cols();
00775   if (cols!=ncams)
00776     return;
00777   //assume left image is in col 0 and right image is in col 1
00778   bgui_vtol2D_tableau_sptr t0 = this->vtol2D_tab_at(0, 0);
00779   if (!t0)
00780     return;
00781   bgui_vtol2D_tableau_sptr t1 = this->vtol2D_tab_at(1, 0);
00782   if (!t1)
00783     return;
00784   vgui_style_sptr st = vgui_style::new_style(0,1,0,3,1);
00785   //clear the map
00786   corr_map_.clear();
00787   for (unsigned i=0; i<n; ++i)
00788   {
00789     bc = corrs_[i];
00790     vgl_point_2d<double> m0(bc->match(0)), m1(bc->match(1));
00791     vsol_point_2d_sptr p0 = new vsol_point_2d(m0);
00792     vsol_point_2d_sptr p1 = new vsol_point_2d(m1);
00793     bgui_vsol_soview2D_point* sov = t0->add_vsol_point_2d(p0,st);
00794     int id = sov->get_id();
00795     int n = corrs_.size();
00796     corr_map_[id]=n-1;
00797     t1->add_vsol_point_2d(p1,st);
00798   }
00799   t0->post_redraw();
00800   t1->post_redraw();
00801 }
00802 
00803 void bmvv_f_manager::display_right_epi_lines()
00804 {
00805   unsigned n = corrs_.size();
00806   if (!n)
00807     return;
00808   brct_corr_sptr bc = corrs_[0];
00809   unsigned int ncams = bc->n_cams();
00810   unsigned cols = grid_->cols();
00811   if (cols!=ncams)
00812     return;
00813   //assume left image is in col 0 and right image is in col 1
00814   bgui_vtol2D_tableau_sptr t1 = this->vtol2D_tab_at(1, 0);
00815   if (!t1)
00816     return;
00817   vgui_style_sptr st =vgui_style::new_style(0,1,0,1,2);
00818   for (unsigned i=0; i<n; ++i)
00819   {
00820     bc = corrs_[i];
00821     vgl_homg_point_2d<double> hpl=bc->match(0);
00822     vgl_homg_line_2d<double> hl = fm_.r_epipolar_line(hpl);
00823     t1->add_infinite_line(hl.a(), hl.b(), hl.c());
00824   }
00825   t1->post_redraw();
00826 }
00827 
00828 void bmvv_f_manager::display_picked_epi_line()
00829 {
00830   //determine if left or right image
00831   unsigned row=0, col=0;
00832   grid_->get_last_selected_position(&col, &row);
00833   bgui_picker_tableau_sptr picktab = this->selected_picker_tab();
00834   if (!picktab)
00835     return;
00836   //get a point
00837   float x=0, y=0;
00838   picktab->pick_point(&x, &y);
00839   vgl_homg_point_2d<double> hp(x,y);
00840   vgl_homg_line_2d<double> hl;
00841   if (col==0)
00842   {
00843     bgui_vtol2D_tableau_sptr t1 = this->vtol2D_tab_at(1, 0);
00844     hl = fm_.r_epipolar_line(hp);
00845     t1->add_infinite_line(hl.a(), hl.b(), hl.c());
00846     t1->post_redraw();
00847     return;
00848   }
00849   if (col==1)
00850   {
00851     bgui_vtol2D_tableau_sptr t0 = this->vtol2D_tab_at(0, 0);
00852     hl = fm_.l_epipolar_line(hp);
00853     t0->add_infinite_line(hl.a(), hl.b(), hl.c());
00854     t0->post_redraw();
00855     return;
00856   }
00857   vcl_cout << "Draw failed\n";
00858 }
00859 
00860 brct_corr_sptr  bmvv_f_manager::get_selected_corr()
00861 {
00862   bgui_vtol2D_tableau_sptr t0 = this->vtol2D_tab_at(0, 0);
00863   vcl_vector<unsigned> ids = t0->get_selected();
00864   //take the last selected
00865   int n = ids.size();
00866   if (!n)
00867   {
00868     vcl_cout << "Nothing selected\n";
00869     return 0;
00870   }
00871   unsigned int i = corr_map_[ids[n-1]];
00872   if (i<corrs_.size())
00873   {
00874     return corrs_[i];
00875   }
00876   vcl_cout << "Bogus correspondence\n";
00877   return 0;
00878 }
00879 
00880 void bmvv_f_manager::create_correspondence()
00881 {
00882   //determine if left or right image
00883   unsigned row=0, col=0;
00884   grid_->get_last_selected_position(&col, &row);
00885   if (col!=0)
00886   {
00887     vcl_cout << "Select left pane and retry\n";
00888     return;
00889   }
00890   bgui_picker_tableau_sptr picktab = this->selected_picker_tab();
00891   if (!picktab)
00892     return;
00893   //get a point
00894   float x=0, y=0;
00895   picktab->pick_point(&x, &y);
00896   brct_corr_sptr bc = new brct_corr(2);
00897   bc->set_match(col, x, y);
00898   corrs_.push_back(bc);
00899   bgui_vtol2D_tableau_sptr t0 = this->vtol2D_tab_at(0, 0);
00900   vsol_point_2d_sptr p = new vsol_point_2d(x, y);
00901   vgui_style_sptr st = vgui_style::new_style(1,1,0,5,1);
00902   bgui_vsol_soview2D_point* sov = t0->add_vsol_point_2d(p,st);
00903   int id = sov->get_id();
00904   int n = corrs_.size();
00905   corr_map_[id]=n-1;
00906 }
00907 
00908 void bmvv_f_manager::pick_correspondence()
00909 {
00910   brct_corr_sptr bc = this->get_selected_corr();
00911   if (!bc)
00912   {
00913     vcl_cout << "No correspondence selected\n";
00914     return;
00915   }
00916   //determine if left or right image
00917   unsigned row=0, col=0;
00918   grid_->get_last_selected_position(&col, &row);
00919   if (col!=1)
00920   {
00921     vcl_cout << "Select right pane and retry\n";
00922     return;
00923   }
00924   bgui_picker_tableau_sptr picktab = this->selected_picker_tab();
00925   if (!picktab)
00926     return;
00927   //get a point
00928   float x=0, y=0;
00929   picktab->pick_point(&x, &y);
00930   bc->set_match(col,x,y);
00931   bgui_vtol2D_tableau_sptr t1 = this->vtol2D_tab_at(1, 0);
00932   vsol_point_2d_sptr p = new vsol_point_2d(x, y);
00933   vgui_style_sptr st = vgui_style::new_style(1,1,0,5,1);
00934   t1->add_vsol_point_2d(p,st);
00935 }
00936 
00937 bool bmvv_f_manager::point_lists(vcl_vector<vgl_point_2d<double> >& lpts,
00938                                  vcl_vector<vgl_point_2d<double> >& rpts)
00939 {
00940   unsigned n = corrs_.size();
00941   if (!n)
00942     return false;
00943   lpts.clear(); rpts.clear();
00944   for (unsigned i = 0; i<n; ++i)
00945   {
00946     brct_corr_sptr bc = corrs_[i];
00947     if (!bc->valid(0)||!bc->valid(1))
00948       continue;
00949     vgl_homg_point_2d<double> m0 = bc->match(0), m1 = bc->match(1);
00950     lpts.push_back(vgl_point_2d<double>(m0));
00951     rpts.push_back(vgl_point_2d<double>(m1));
00952   }
00953   return true;
00954 }
00955 
00956 void bmvv_f_manager::compute_f_matrix()
00957 {
00958   vgui_dialog f_dlg("Compute F Matrix");
00959   static bool refine = false;
00960   static bool trans = false;
00961   f_dlg.checkbox("Refine F Matrix? ", refine);
00962   f_dlg.checkbox("Translation F Matrix? ", trans);
00963   if (!f_dlg.ask())
00964     return;
00965   vcl_vector<vgl_point_2d<double> > lpts, rpts;
00966   vcl_vector< vgl_homg_point_2d<double> > pir, pil;
00967   if (!this->point_lists(lpts, rpts))
00968   {
00969     vcl_cout << "No corresponding points to compute f matrix\n";
00970     return;
00971   }
00972   if (trans)
00973   {
00974     for (unsigned i = 0; i<lpts.size(); ++i)
00975     {
00976       pir.push_back(