contrib/brl/bseg/segv/segv_vil_segmentation_manager.cxx

Go to the documentation of this file.
00001 // This is brl/bseg/segv/segv_vil_segmentation_manager.cxx
00002 #include "segv_vil_segmentation_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 <vcl_iostream.h>
00013 #include <vcl_cstdio.h> // sprintf
00014 // not used? #include <vcl_fstream.h>
00015 #include <vbl/vbl_array_2d.h>
00016 #include <vil/vil_image_view.h>
00017 #include <vil/vil_blocked_image_resource.h>
00018 #include <vil/vil_pyramid_image_resource.h>
00019 #include <vil/vil_load.h>
00020 #include <vil/vil_save.h>
00021 #include <vil/vil_new.h>
00022 #include <vil/vil_copy.h>
00023 #include <vil/vil_property.h>
00024 #include <vil/vil_flip.h>
00025 #include <vil/vil_resample_bicub.h>
00026 #include <vil/vil_convert.h>
00027 #include <vil/algo/vil_sobel_1x3.h>
00028 #include <vgl/vgl_vector_2d.h>
00029 #include <sdet/sdet_detector_params.h>
00030 #include <sdet/sdet_detector.h>
00031 #include <sdet/sdet_harris_detector_params.h>
00032 #include <sdet/sdet_harris_detector.h>
00033 #include <sdet/sdet_nonmax_suppression_params.h>
00034 #include <sdet/sdet_nonmax_suppression.h>
00035 #include <sdet/sdet_fit_lines_params.h>
00036 #include <sdet/sdet_fit_lines.h>
00037 #include <sdet/sdet_fit_conics.h>
00038 #include <sdet/sdet_grid_finder_params.h>
00039 #include <sdet/sdet_grid_finder.h>
00040 #include <vgui/vgui.h>
00041 #include <vgui/vgui_find.h>
00042 #include <vgui/vgui_tableau.h>
00043 #include <vgui/vgui_dialog.h>
00044 #include <vgui/vgui_style_sptr.h>
00045 #include <vgui/vgui_style.h>
00046 #include <vgui/vgui_viewer2D_tableau.h>
00047 #include <vgui/vgui_shell_tableau.h>
00048 #include <vgui/vgui_grid_tableau.h>
00049 #include <vgui/vgui_range_map_params.h>
00050 #include <bgui/bgui_image_tableau.h>
00051 #include <bgui/bgui_vtol2D_tableau.h>
00052 #include <bgui/bgui_picker_tableau.h>
00053 #include <bgui/bgui_range_adjuster_tableau.h>
00054 #include <bgui/bgui_image_utils.h>
00055 #include <vsol/vsol_point_2d.h>
00056 #include <vsol/vsol_point_2d_sptr.h>
00057 #include <vsol/vsol_conic_2d.h>
00058 #include <vsol/vsol_polyline_2d_sptr.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_para_cvrg_params.h>
00067 #include <brip/brip_para_cvrg.h>
00068 #include <brip/brip_watershed_params.h>
00069 #include <sdet/sdet_watershed_region_proc_params.h>
00070 #include <sdet/sdet_watershed_region_proc.h>
00071 #include <sdet/sdet_vehicle_finder_params.h>
00072 #include <sdet/sdet_vehicle_finder.h>
00073 #include <sdet/sdet_region_proc_params.h>
00074 #include <sdet/sdet_region_proc.h>
00075 #include <strk/strk_region_info_params.h>
00076 #include <strk/strk_region_info.h>
00077 #include <strk/strk_io.h>
00078 
00079 ;
00080 segv_vil_segmentation_manager *segv_vil_segmentation_manager::instance_ = 0;
00081 
00082 segv_vil_segmentation_manager *segv_vil_segmentation_manager::instance()
00083 {
00084   if (!instance_)
00085   {
00086     instance_ = new segv_vil_segmentation_manager();
00087     instance_->init();
00088   }
00089   return segv_vil_segmentation_manager::instance_;
00090 }
00091 
00092 //-----------------------------------------------------------
00093 // constructors/destructor
00094 //
00095 segv_vil_segmentation_manager::segv_vil_segmentation_manager():vgui_wrapper_tableau()
00096 {
00097   first_ = true;
00098 }
00099 
00100 segv_vil_segmentation_manager::~segv_vil_segmentation_manager()
00101 {
00102 }
00103 
00104 //: Set up the tableaux
00105 void segv_vil_segmentation_manager::init()
00106 {
00107   bgui_image_tableau_sptr itab = bgui_image_tableau_new();
00108   bgui_vtol2D_tableau_sptr t2D = bgui_vtol2D_tableau_new(itab);
00109   bgui_picker_tableau_sptr picktab = bgui_picker_tableau_new(t2D);
00110   vgui_viewer2D_tableau_sptr v2D = vgui_viewer2D_tableau_new(picktab);
00111   grid_ = vgui_grid_tableau_new(1,1);
00112   grid_->set_grid_size_changeable(true);
00113   grid_->add_at(v2D, 0, 0);
00114   vgui_shell_tableau_sptr shell = vgui_shell_tableau_new(grid_);
00115   this->add_child(shell);
00116   first_ = true;
00117 }
00118 
00119 //: Calculate the range parameters for the input image
00120 vgui_range_map_params_sptr segv_vil_segmentation_manager::
00121 range_params(vil_image_resource_sptr const& image)
00122 {
00123   float gamma = 1.0;
00124   bool invert = false;
00125   bool gl_map = false;
00126   bool cache = true;
00127 
00128   //Check if the image is blocked
00129   vil_blocked_image_resource_sptr bir = blocked_image_resource(image);
00130   if (bir)
00131   { gl_map = true; cache = true;}
00132 
00133   //Check if the image is a pyramid
00134   bool pyr = image->get_property(vil_property_pyramid, 0);
00135   if (pyr)
00136   { gl_map = true; cache = true;}
00137 
00138   bgui_image_utils iu(image);
00139   iu.set_percent_limit(0.005);
00140 
00141   vgui_range_map_params_sptr rmps;
00142   if (iu.range_map_from_hist(gamma, invert, gl_map, cache, rmps))
00143     return rmps;
00144 
00145   return 0;
00146 }
00147 
00148 //: set the image at the currently selected grid cell
00149 void segv_vil_segmentation_manager::
00150 set_selected_grid_image(vil_image_resource_sptr const& image,
00151                         vgui_range_map_params_sptr const& rmps)
00152 {
00153   bgui_image_tableau_sptr itab = this->selected_image_tab();
00154   if (!itab)
00155     this->add_image(image, rmps);
00156   else
00157   {
00158     itab->set_image_resource(image);
00159     itab->set_mapping(rmps);
00160   }
00161   itab->post_redraw();
00162 }
00163 
00164 //: Add an image at the specified grid cell
00165 void segv_vil_segmentation_manager::
00166 add_image_at(vil_image_resource_sptr const& image,
00167              const unsigned col, const unsigned row,
00168              vgui_range_map_params_sptr const& rmps)
00169 {
00170   vgui_range_map_params_sptr rmap = rmps;
00171   if (!rmps)
00172     rmap = range_params(image);
00173   bgui_image_tableau_sptr itab = bgui_image_tableau_new(image,rmps);
00174   //  itab->set_mapping(rmap);
00175   bgui_vtol2D_tableau_sptr t2D = bgui_vtol2D_tableau_new(itab);
00176   bgui_picker_tableau_sptr picktab = bgui_picker_tableau_new(t2D);
00177   vgui_viewer2D_tableau_sptr v2D = vgui_viewer2D_tableau_new(picktab);
00178   grid_->add_at(v2D, col, row);
00179   itab->post_redraw();
00180 }
00181 
00182 //: Add an image to the currently selected grid cell
00183 void segv_vil_segmentation_manager::
00184 add_image(vil_image_resource_sptr const& image,
00185           vgui_range_map_params_sptr const& rmps)
00186 {
00187   unsigned row=0, col=0;
00188   grid_->get_last_selected_position(&col, &row);
00189   this->add_image_at(image, col, row, rmps);
00190 }
00191 
00192 //-----------------------------------------------------------------------------
00193 //: remove the selected image
00194 //-----------------------------------------------------------------------------
00195 void segv_vil_segmentation_manager::remove_image()
00196 {
00197   unsigned row=0, col=0;
00198   grid_->get_last_selected_position(&col, &row);
00199   grid_->remove_at(col, row);
00200 }
00201 
00202 void segv_vil_segmentation_manager::convert_to_grey()
00203 {
00204   vil_image_resource_sptr img = this->selected_image();
00205   if (!img)
00206     return;
00207   vil_image_view<unsigned char> grey =
00208     brip_vil_float_ops::convert_to_byte(img);
00209   vil_image_resource_sptr gimg = vil_new_image_resource_of_view(grey);
00210   this->add_image(gimg);
00211 }
00212 
00213 //: Get the image tableau for the currently selected grid cell
00214 bgui_image_tableau_sptr segv_vil_segmentation_manager::selected_image_tab()
00215 {
00216   unsigned row=0, col=0;
00217   grid_->get_last_selected_position(&col, &row);
00218   vgui_tableau_sptr top_tab = grid_->get_tableau_at(col, row);
00219   if (top_tab)
00220   {
00221     bgui_image_tableau_sptr itab;
00222     itab.vertical_cast(vgui_find_below_by_type_name(top_tab,
00223                                                     vcl_string("vgui_image_tableau")));
00224     if (itab)
00225       return itab;
00226   }
00227   vcl_cout << "Unable to get bgui_image_tableau at (" << col
00228            << ", " << row << ")\n";
00229   return bgui_image_tableau_sptr();
00230 }
00231 
00232 //: Get the vtol2D tableau at the specified grid cell
00233 bgui_vtol2D_tableau_sptr
00234 segv_vil_segmentation_manager::vtol2D_tab_at(const unsigned col,
00235                                              const unsigned row)
00236 {
00237   vgui_tableau_sptr top_tab = grid_->get_tableau_at(col, row);
00238   if (top_tab)
00239   {
00240     bgui_vtol2D_tableau_sptr v2D;
00241     v2D.vertical_cast(vgui_find_below_by_type_name(top_tab,
00242                                                    vcl_string("bgui_vtol2D_tableau")));
00243     if (v2D)
00244       return v2D;
00245   }
00246   vcl_cout << "Unable to get bgui_vtol2D_tableau at (" << col
00247            << ", " << row << ")\n";
00248   return bgui_vtol2D_tableau_sptr();
00249 }
00250 
00251 //: Get the vtol2D tableau for the currently selected grid cell
00252 bgui_vtol2D_tableau_sptr segv_vil_segmentation_manager::selected_vtol2D_tab()
00253 {
00254   unsigned row=0, col=0;
00255   grid_->get_last_selected_position(&col, &row);
00256   return this->vtol2D_tab_at(col, row);
00257 }
00258 
00259 //: Get the picker tableau for the currently selected grid cell
00260 bgui_picker_tableau_sptr segv_vil_segmentation_manager::selected_picker_tab()
00261 {
00262   unsigned row=0, col=0;
00263   grid_->get_last_selected_position(&col, &row);
00264   vgui_tableau_sptr top_tab = grid_->get_tableau_at(col, row);
00265   if (top_tab)
00266   {
00267     bgui_picker_tableau_sptr pick;
00268     pick.vertical_cast(vgui_find_below_by_type_name(top_tab,
00269                                                     vcl_string("bgui_picker_tableau")));
00270     if (pick)
00271       return pick;
00272   }
00273   vcl_cout << "Unable to get bgui_picker_tableau at (" << col
00274            << ", " << row << ")\n";
00275   return bgui_picker_tableau_sptr();
00276 }
00277 
00278 
00279 vil_image_resource_sptr segv_vil_segmentation_manager::selected_image()
00280 {
00281   bgui_image_tableau_sptr itab = this->selected_image_tab();
00282   if (!itab)
00283     return 0;
00284   return itab->get_image_resource();
00285 }
00286 
00287 vil_image_resource_sptr segv_vil_segmentation_manager::image_at(const unsigned col,
00288                                                                 const unsigned row)
00289 {
00290   vgui_tableau_sptr top_tab = grid_->get_tableau_at(col, row);
00291   if (!top_tab)
00292     return 0;
00293 
00294   bgui_image_tableau_sptr itab;
00295   itab.vertical_cast(vgui_find_below_by_type_name(top_tab,
00296                                                   vcl_string("vgui_image_tableau")));
00297   if (!itab)
00298   {
00299     vcl_cout << "Unable to get bgui_image_tableau at (" << col
00300              << ", " << row << ")\n";
00301     return 0;
00302   }
00303   return itab->get_image_resource();
00304 }
00305 
00306 //-----------------------------------------------------------------------------
00307 //: Clear spatial objects from the selected display
00308 //-----------------------------------------------------------------------------
00309 void segv_vil_segmentation_manager::clear_display()
00310 {
00311   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00312   if (!t2D)
00313     return;
00314   t2D->clear_all();
00315 }
00316 
00317 //-----------------------------------------------------------------------------
00318 //: Clear spatial objects from all spatial panes
00319 //-----------------------------------------------------------------------------
00320 void segv_vil_segmentation_manager::clear_all()
00321 {
00322   unsigned ncols = grid_->cols(), nrows = grid_->rows();
00323   for (unsigned r=0; r<nrows; ++r)
00324     for (unsigned c=0; c<ncols; ++c)
00325     {
00326       bgui_vtol2D_tableau_sptr t = this->vtol2D_tab_at(c, r);
00327       if (t)
00328         t->clear_all();
00329     }
00330 }
00331 
00332 //-----------------------------------------------------------------------------
00333 //: Draw edges onto the tableau
00334 //-----------------------------------------------------------------------------
00335 void
00336 segv_vil_segmentation_manager::draw_edges(vcl_vector<vtol_edge_2d_sptr>& edges,
00337                                           bool verts)
00338 {
00339   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00340   if (!t2D)
00341     return;
00342   this->clear_display();
00343 #if 0
00344   vgui_image_tableau_sptr itab = t2D->get_image_tableau();
00345   if (!itab)
00346   {
00347     vcl_cout << "In segv_vil_segmentation_manager::draw_edges - null image tab\n";
00348     return;
00349   }
00350 #endif
00351   for (vcl_vector<vtol_edge_2d_sptr>::iterator eit = edges.begin();
00352        eit != edges.end(); eit++)
00353   {
00354     t2D->add_edge(*eit);
00355     //optionally display the edge vertices
00356     if (verts)
00357     {
00358       if ((*eit)->v1())
00359       {
00360         vtol_vertex_2d_sptr v1 = (*eit)->v1()->cast_to_vertex_2d();
00361         t2D->add_vertex(v1);
00362       }
00363       if ((*eit)->v2())
00364       {
00365         vtol_vertex_2d_sptr v2 = (*eit)->v2()->cast_to_vertex_2d();
00366         t2D->add_vertex(v2);
00367       }
00368     }
00369   }
00370   t2D->post_redraw();
00371 }
00372 
00373 
00374 //-----------------------------------------------------------------------------
00375 //: Draw polylines on the tableau
00376 //-----------------------------------------------------------------------------
00377 void segv_vil_segmentation_manager::
00378 draw_polylines(vcl_vector<vsol_polyline_2d_sptr > const& polys)
00379 {
00380   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00381   if (!t2D)
00382     return;
00383   //this->clear_display();
00384   vgui_image_tableau_sptr itab = t2D->get_image_tableau();
00385   if (!itab)
00386   {
00387     vcl_cout << "In segv_vil_segmentation_manager::draw_edges - null image tab\n";
00388     return;
00389   }
00390   for (vcl_vector<vsol_polyline_2d_sptr>::const_iterator pit = polys.begin();
00391        pit != polys.end(); pit++)
00392   {
00393     t2D->add_vsol_polyline_2d(*pit);
00394   }
00395 
00396   t2D->post_redraw();
00397 }
00398 
00399 //-----------------------------------------------------------------------------
00400 //: Draw line segments on the tableau
00401 //-----------------------------------------------------------------------------
00402 void segv_vil_segmentation_manager::
00403 draw_lines(vcl_vector<vsol_line_2d_sptr > const& lines,
00404            const vgui_style_sptr& style)
00405 {
00406   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00407   if (!t2D)
00408     return;
00409   //this->clear_display();
00410 #if 0
00411   vgui_image_tableau_sptr itab = t2D->get_image_tableau();
00412   if (!itab)
00413   {
00414     vcl_cout << "In segv_vil_segmentation_manager::draw_edges - null image tab\n";
00415     return;
00416   }
00417 #endif
00418   for (vcl_vector<vsol_line_2d_sptr>::const_iterator lit = lines.begin();
00419        lit != lines.end(); lit++)
00420   {
00421     t2D->add_vsol_line_2d(*lit,style);
00422   }
00423 
00424   t2D->post_redraw();
00425 }
00426 
00427 //-----------------------------------------------------------------------------
00428 //: Draw conic segments on the tableau
00429 //-----------------------------------------------------------------------------
00430 void segv_vil_segmentation_manager::
00431 draw_conics(vcl_vector<vsol_conic_2d_sptr > const& conics,
00432             const vgui_style_sptr& style)
00433 {
00434   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00435   if (!t2D)
00436     return;
00437   //this->clear_display();
00438 #if 0
00439   vgui_image_tableau_sptr itab = t2D->get_image_tableau();
00440   if (!itab)
00441   {
00442     vcl_cout << "In segv_vil_segmentation_manager::draw_edges - null image tab\n";
00443     return;
00444   }
00445 #endif
00446   for (vcl_vector<vsol_conic_2d_sptr>::const_iterator lit = conics.begin();
00447        lit != conics.end(); lit++)
00448   {
00449     t2D->add_vsol_conic_2d(*lit,style);
00450   }
00451   t2D->post_redraw();
00452 }
00453 
00454 //-----------------------------------------------------------------------------
00455 //: Draw points on the tableau
00456 //-----------------------------------------------------------------------------
00457 void segv_vil_segmentation_manager::
00458 draw_points(vcl_vector<vsol_point_2d_sptr> const& points, const vgui_style_sptr& style)
00459 {
00460   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00461   if (!t2D)
00462     return;
00463   //this->clear_display();
00464 #if 0
00465   vgui_image_tableau_sptr itab = t2D->get_image_tableau();
00466   if (!itab)
00467   {
00468     vcl_cout << "In segv_vil_segmentation_manager::draw_edges - null image tab\n";
00469     return;
00470   }
00471 #endif
00472   for (vcl_vector<vsol_point_2d_sptr>::const_iterator pit = points.begin();
00473        pit != points.end(); pit++)
00474   {
00475     t2D->add_vsol_point_2d(*pit,style);
00476   }
00477 
00478   t2D->post_redraw();
00479 }
00480 
00481 void segv_vil_segmentation_manager::draw_regions(vcl_vector<vtol_intensity_face_sptr>& regions,
00482                                                  bool verts)
00483 {
00484   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00485   if (!t2D)
00486     return;
00487   for (vcl_vector<vtol_intensity_face_sptr>::iterator rit = regions.begin();
00488        rit != regions.end(); rit++)
00489   {
00490     vtol_face_2d_sptr f = (*rit)->cast_to_face_2d();
00491     t2D->add_face(f);
00492     if (verts)
00493     {
00494       vcl_vector<vtol_vertex_sptr> vts;
00495       f->vertices(vts);
00496       for (vcl_vector<vtol_vertex_sptr>::iterator vit = vts.begin();
00497            vit != vts.end(); vit++)
00498       {
00499         vtol_vertex_2d_sptr v = (*vit)->cast_to_vertex_2d();
00500         t2D->add_vertex(v);
00501       }
00502     }
00503   }
00504   t2D->post_redraw();
00505 }
00506 
00507 void segv_vil_segmentation_manager::quit()
00508 {
00509   this->clear_all();
00510   vgui::quit();
00511 }
00512 
00513 void segv_vil_segmentation_manager::load_image()
00514 {
00515   static bool greyscale = false;
00516   static bool sblock = false;
00517   vgui_dialog load_image_dlg("Load image file");
00518   static vcl_string image_filename = "/home/dec/images/cal_image1.tif";
00519   static vcl_string ext = "*.*";
00520   load_image_dlg.file("Image Filename:", ext, image_filename);
00521   load_image_dlg.checkbox("greyscale ", greyscale);
00522   load_image_dlg.checkbox("blocked?:", sblock);
00523   if (!load_image_dlg.ask())
00524     return;
00525   //first check to see if the image is a pyramid
00526   bool pyrm = false;
00527   vil_image_resource_sptr image;
00528   vil_pyramid_image_resource_sptr pyr =
00529       vil_load_pyramid_resource(image_filename.c_str());
00530   if (pyr)
00531   {
00532     image = pyr.ptr();
00533     pyrm = true;
00534   }
00535 
00536   if (!image)
00537     image = vil_load_image_resource(image_filename.c_str());
00538 
00539   if (!image)
00540     return;
00541 
00542   if (greyscale&&!pyrm)
00543   {
00544     vil_image_view<unsigned char> grey_view =
00545       brip_vil_float_ops::convert_to_grey(*image);
00546     image = vil_new_image_resource_of_view(grey_view);
00547   }
00548 
00549   if (sblock&&!pyrm)
00550   {
00551     vil_blocked_image_resource_sptr bimage = vil_new_blocked_image_facade(image);
00552     image = (vil_image_resource*)(vil_new_cached_image_resource(bimage)).ptr();
00553   }
00554 
00555   vgui_range_map_params_sptr rmps = range_params(image);
00556 
00557   if (first_)
00558   {
00559     this->set_selected_grid_image(image, rmps);
00560     first_ = false;
00561   }
00562   else
00563     this->add_image(image, rmps);
00564 }
00565 
00566 void segv_vil_segmentation_manager::save_image()
00567 {
00568   vgui_dialog file_dialog("Save Image");
00569   static vcl_string image_file;
00570   static vcl_string ext = "tif";
00571   static vcl_string type = "tiff";
00572   static unsigned size_block = 0;
00573   static bool byte = false;
00574   file_dialog.file("Image Filename:", ext, image_file);
00575   file_dialog.field("Image Format: ", type);
00576   file_dialog.field("BlockSize", size_block);
00577   file_dialog.checkbox("Convert to byte image", byte);
00578   if (!file_dialog.ask())
00579     return;
00580   vil_image_resource_sptr img = this->selected_image();
00581   if (!img)
00582   {
00583     vcl_cerr << "Null image in segv_vil_segmentation_manager::save_image\n";
00584     return;
00585   }
00586   vil_image_resource_sptr save_image = img;
00587   if (byte)
00588   {
00589     vil_image_view<unsigned char> byte_view = brip_vil_float_ops::convert_to_byte(img);
00590     save_image = vil_new_image_resource_of_view(byte_view);
00591   }
00592   if (size_block>0)
00593   {
00594     vil_blocked_image_resource_sptr bim =
00595       vil_new_blocked_image_resource(image_file.c_str(),
00596                                      save_image->ni(), save_image->nj(),
00597                                      save_image->nplanes(),
00598                                      save_image->pixel_format(),
00599                                      size_block, size_block,
00600                                      "tiff");
00601     vil_image_view_base_sptr view = save_image->get_view();
00602     if (view)
00603       bim->vil_image_resource::put_view(*view);
00604     return;
00605   }
00606 
00607   if (!vil_save_image_resource(save_image, image_file.c_str(), type.c_str()))
00608     vcl_cerr << "segv_vil_segmentation_manager::save_image operation failed\n";
00609 }
00610 
00611 void segv_vil_segmentation_manager::save_nitf_camera()
00612 {
00613   vil_image_resource_sptr img = this->selected_image();
00614   if (!img)
00615   {
00616     vcl_cerr << "Null image in segv_vil_segmentation_manager::save_camera\n";
00617     return;
00618   }
00619 
00620   vil_nitf2_image* nitf = 0;
00621   vcl_string format = img->file_format();
00622   vcl_string prefix = format.substr(0,4);
00623   if (prefix == "nitf") {
00624     nitf = (vil_nitf2_image*)img.ptr();
00625     vgui_dialog file_dialog("Save NITF Camera");
00626     static vcl_string image_file;
00627     static vcl_string ext = "rpc";
00628     file_dialog.file("Image Filename:", ext, image_file);
00629     if (!file_dialog.ask())
00630       return;
00631     vpgl_nitf_rational_camera rpcam(nitf, true);
00632     rpcam.save(image_file);
00633   }
00634 }
00635 
00636 void segv_vil_segmentation_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 segv_vil_segmentation_manager::threshold_image()
00686 {
00687   vil_image_resource_sptr img = selected_image();
00688   if (!img)
00689   {
00690     vcl_cout << "In segv_segmentation_manager::threshold_image - no image\n";
00691     return;
00692   }
00693   static float thresh = 128.0f;
00694   vgui_dialog thresh_dlg("Threshold Image");
00695   thresh_dlg.field("Threshold", thresh);
00696   if (!thresh_dlg.ask())
00697     return;
00698   vil_image_view<float> fimage = brip_vil_float_ops::convert_to_float(*img);
00699   vil_image_view<float> timage =
00700     brip_vil_float_ops::threshold(fimage, thresh, 255);
00701   vil_image_view<unsigned char> cimage = brip_vil_float_ops::convert_to_byte(timage, 0, 255);
00702   this->add_image(vil_new_image_resource_of_view(cimage));
00703 }
00704 
00705 void segv_vil_segmentation_manager::harris_corners()
00706 {
00707   vil_image_resource_sptr img = selected_image();
00708   if (!img)
00709   {
00710     vcl_cout << "In segv_segmentation_manager::harris_measure) - no image\n";
00711     return;
00712   }
00713   static sdet_harris_detector_params hdp;
00714   vgui_dialog harris_dialog("harris");
00715   harris_dialog.field("sigma", hdp.sigma_);
00716   harris_dialog.field("thresh", hdp.thresh_);
00717   harris_dialog.field("N = 2n+1, (n)", hdp.n_);
00718   harris_dialog.field("Max No Corners(percent)", hdp.percent_corners_);
00719   harris_dialog.field("scale_factor", hdp.scale_factor_);
00720   harris_dialog.checkbox("Use vil harris\n corner strength ", hdp.use_vil_harris_);
00721   if (!harris_dialog.ask())
00722     return;
00723   sdet_harris_detector hd(hdp);
00724   hd.set_image_resource(img);
00725   hd.extract_corners();
00726   vcl_vector<vsol_point_2d_sptr>& points = hd.get_points();
00727   int N = points.size();
00728   if (!N)
00729     return;
00730   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00731   if (!t2D)
00732     return;
00733   t2D->clear_all();
00734   for (int i=0; i<N; i++)
00735     t2D->add_vsol_point_2d(points[i]);
00736   t2D->post_redraw();
00737 }
00738 
00739 void segv_vil_segmentation_manager::nonmaximal_suppression()
00740 {
00741   bool show_lines = false;
00742   vil_image_resource_sptr img = selected_image();
00743   if (!img)
00744   {
00745     vcl_cout << "In segv_segmentation_manager::nonmaximal_suppression) - no image\n";
00746     return;
00747   }
00748   static sdet_nonmax_suppression_params nsp;
00749   vgui_dialog nonmax_dialog("non-maximal suppression");
00750   nonmax_dialog.field("Gradient Magnitude Percentage Threshold ", nsp.thresh_);
00751   nonmax_dialog.choice("Number of Points Used for Parabola Fit ", "3", "9", nsp.pfit_type_);
00752   nonmax_dialog.checkbox("Show lines at the edge points? ", show_lines);
00753   if (!nonmax_dialog.ask())
00754     return;
00755 
00756   // prepare input for the nonmax suppression
00757   vil_image_view<vxl_byte> input, input_grey;
00758   vil_image_view<double> grad_i, grad_j, grad_mag_img;
00759   vbl_array_2d<double> grad_x, grad_y, grad_mag;
00760   vbl_array_2d<vgl_vector_2d <double> > input_directions;
00761 
00762   // vil_convert_cast(img->get_view(), input);
00763   switch ( img->get_view()->pixel_format() )
00764   {
00765 #define macro(F , T) \
00766     case F: vil_convert_cast( vil_image_view<T >(img->get_view()), input ); break;
00767     macro( VIL_PIXEL_FORMAT_UINT_32, vxl_uint_32 )
00768     macro( VIL_PIXEL_FORMAT_INT_32, vxl_int_32 )
00769     macro( VIL_PIXEL_FORMAT_UINT_16, vxl_uint_16 )
00770     macro( VIL_PIXEL_FORMAT_INT_16, vxl_int_16 )
00771     macro( VIL_PIXEL_FORMAT_BYTE, vxl_byte )
00772     macro( VIL_PIXEL_FORMAT_SBYTE, vxl_sbyte )
00773     macro( VIL_PIXEL_FORMAT_FLOAT, float )
00774     macro( VIL_PIXEL_FORMAT_DOUBLE, double )
00775     macro( VIL_PIXEL_FORMAT_BOOL, bool )
00776     default: img->get_view() = 0;
00777 #undef macro
00778   }
00779 
00780   int ni = input.ni();
00781   int nj = input.nj();
00782 
00783   grad_i.set_size(ni,nj);
00784   grad_j.set_size(ni,nj);
00785   grad_mag_img.set_size(ni,nj);
00786   input_grey.set_size(ni,nj);
00787   grad_x.resize(ni,nj);
00788   grad_y.resize(ni,nj);
00789   grad_mag.resize(ni,nj);
00790   input_directions.resize(ni,nj);
00791 
00792   if (input.nplanes() > 1)
00793     vil_convert_planes_to_grey(input, input_grey);
00794   else
00795     input_grey = input;
00796 
00797   vil_sobel_1x3 <vxl_byte, double> (input_grey, grad_i, grad_j);
00798   for (int j=0;j<nj; j++)
00799   {
00800     for (int i=0;i<ni; i++)
00801     {
00802       double xval = grad_i(i,j);
00803       double yval = grad_j(i,j);
00804       grad_x(i,j) = xval;
00805       grad_y(i,j) = yval;
00806       double val = vcl_sqrt(vcl_pow(xval,2.0) + vcl_pow(yval,2.0));
00807       grad_mag(i,j) = val;
00808       grad_mag_img(i,j) = val;
00809       vgl_vector_2d<double> dir(xval, yval);
00810       input_directions(i,j) = dir;
00811     }
00812   }
00813 
00814   // Below is to demonstrate how to initialize the non-maximal suppression in different ways
00815 //  sdet_nonmax_suppression ns(nsp, grad_mag, input_directions);
00816   sdet_nonmax_suppression ns(nsp, grad_x, grad_y);
00817 //  sdet_nonmax_suppression ns(nsp, grad_i, grad_j);
00818 //  sdet_nonmax_suppression ns(nsp, grad_mag_img, input_directions);
00819   ns.apply();
00820   vcl_vector<vsol_point_2d_sptr>& points = ns.get_points();
00821   vcl_vector<vsol_line_2d_sptr>& lines = ns.get_lines();
00822   // not used below, just for demonstration purposes
00823   //vcl_vector<vgl_vector_2d<double> >& directions = ns.get_directions();
00824   int N = points.size();
00825   if (!N)
00826     return;
00827   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00828   if (!t2D)
00829     return;
00830   t2D->clear_all();
00831   if (!show_lines)
00832   {
00833     for (int i=0; i<N; i++)
00834       t2D->add_vsol_point_2d(points[i]);
00835   }
00836   if (show_lines)
00837   {
00838     for (int i=0; i<N; i++)
00839       t2D->add_vsol_line_2d(lines[i]);
00840   }
00841   t2D->post_redraw();
00842 }
00843 
00844 void segv_vil_segmentation_manager::vd_edges()
00845 {
00846   this->clear_display();
00847   static bool agr = true;
00848   static sdet_detector_params dp;
00849   static float nm = 2.0;
00850 
00851   vgui_dialog vd_dialog("VD Edges");
00852   vd_dialog.field("Gaussian sigma", dp.smooth);
00853   vd_dialog.field("Noise Threshold", nm);
00854   vd_dialog.checkbox("Automatic Threshold", dp.automatic_threshold);
00855   vd_dialog.checkbox("Agressive Closure", agr);
00856   vd_dialog.checkbox("Compute Junctions", dp.junctionp);
00857   if (!vd_dialog.ask())
00858     return;
00859   dp.noise_multiplier=nm;
00860   if (agr)
00861     dp.aggressive_junction_closure=1;
00862   else
00863     dp.aggressive_junction_closure=0;
00864   vil_image_resource_sptr img = selected_image();
00865   if (!img||!img->ni()||!img->nj())
00866   {
00867     vcl_cout << "In segv_vil_segmentation_manager::vd_edges() - no image\n";
00868     return;
00869   }
00870   sdet_detector det(dp);
00871   det.SetImage(img);
00872 
00873   det.DoContour();
00874   vcl_vector<vtol_edge_2d_sptr>* edges = det.GetEdges();
00875   if (edges)
00876     this->draw_edges(*edges, true);
00877 }
00878 
00879 void segv_vil_segmentation_manager::fit_lines()
00880 {
00881   this->clear_display();
00882   static sdet_detector_params dp;
00883   static bool agr = true;
00884   static float nm = 2.0;
00885 
00886   static sdet_fit_lines_params flp;
00887 
00888   vgui_dialog lf_dialog("Fit Lines");
00889   lf_dialog.field("Gaussian sigma", dp.smooth);
00890   lf_dialog.field("Noise Threshold", nm);
00891   lf_dialog.checkbox("Automatic Threshold", dp.automatic_threshold);
00892   lf_dialog.checkbox("Agressive Closure", agr);
00893   lf_dialog.checkbox("Compute Junctions", dp.junctionp);
00894   lf_dialog.field("Min Fit Length", flp.min_fit_length_);
00895   lf_dialog.field("RMS Distance", flp.rms_distance_);
00896 
00897   if (!lf_dialog.ask())
00898     return;
00899   dp.noise_multiplier=nm;
00900   if (agr)
00901     dp.aggressive_junction_closure=1;
00902   else
00903     dp.aggressive_junction_closure=0;
00904   dp.borderp = false;
00905   sdet_detector det(dp);
00906 
00907   vil_image_resource_sptr img = selected_image();
00908   if (!img||!img->ni()||!img->nj())
00909   {
00910     vcl_cout << "In segv_vil_segmentation_manager::vd_edges() - no image\n";
00911     return;
00912   }
00913 
00914   det.SetImage(img);
00915 
00916   det.DoContour();
00917   vcl_vector<vtol_edge_2d_sptr>* edges = det.GetEdges();
00918   if (!edges)
00919   {
00920     vcl_cout << "No edges to fit lines\n";
00921     return;
00922   }
00923   sdet_fit_lines fl(flp);
00924   fl.set_edges(*edges);
00925   fl.fit_lines();
00926   vcl_vector<vsol_line_2d_sptr> lines = fl.get_line_segs();
00927   this->draw_lines(lines);
00928 }
00929 
00930 void segv_vil_segmentation_manager::fit_conics()
00931 {
00932   this->clear_display();
00933   static sdet_detector_params dp;
00934   static bool agr = true;
00935   static float nm = 2.0;
00936 
00937   static sdet_fit_conics_params fcp;
00938 
00939   vgui_dialog lf_dialog("Fit Conics");
00940   lf_dialog.field("Gaussian sigma", dp.smooth);
00941   lf_dialog.field("Noise Threshold", nm);
00942   lf_dialog.checkbox("Automatic Threshold", dp.automatic_threshold);
00943   lf_dialog.checkbox("Agressive Closure", agr);
00944   lf_dialog.checkbox("Compute Junctions", dp.junctionp);
00945   lf_dialog.field("Min Fit Length", fcp.min_fit_length_);
00946   lf_dialog.field("RMS Distance", fcp.rms_distance_);
00947 
00948   if (!lf_dialog.ask())
00949     re