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 #include <vcl_fstream.h>
00015 #include <vul/vul_timer.h>
00016 #include <vbl/vbl_array_2d.h>
00017 #include <vnl/vnl_math.h>
00018 #include <vil/vil_image_view.h>
00019 #include <vil/vil_blocked_image_resource.h>
00020 #include <vil/vil_pyramid_image_resource.h>
00021 #include <vil/vil_load.h>
00022 #include <vil/vil_save.h>
00023 #include <vil/vil_new.h>
00024 #include <vil/vil_copy.h>
00025 #include <vil/vil_property.h>
00026 #include <vil/vil_flip.h>
00027 #include <vil/vil_resample_bicub.h>
00028 #include <vil/vil_convert.h>
00029 #include <vil/vil_config.h>
00030 #if HAS_J2K
00031 #include <vil/file_formats/vil_j2k_nitf2_pyramid_image_resource.h>
00032 #include <vil/file_formats/vil_j2k_pyramid_image_resource.h>
00033 #endif // HAS_J2K
00034 #include <vil/algo/vil_sobel_1x3.h>
00035 #include <vgl/vgl_vector_2d.h>
00036 #include <sdet/sdet_detector_params.h>
00037 #include <sdet/sdet_detector.h>
00038 #include <sdet/sdet_harris_detector_params.h>
00039 #include <sdet/sdet_harris_detector.h>
00040 #include <sdet/sdet_nonmax_suppression_params.h>
00041 #include <sdet/sdet_nonmax_suppression.h>
00042 #include <sdet/sdet_fit_lines_params.h>
00043 #include <sdet/sdet_fit_lines.h>
00044 #include <sdet/sdet_fit_conics.h>
00045 #include <sdet/sdet_grid_finder_params.h>
00046 #include <sdet/sdet_grid_finder.h>
00047 #include <vgui/vgui.h>
00048 #include <vgui/vgui_find.h>
00049 #include <vgui/vgui_tableau.h>
00050 #include <vgui/vgui_dialog.h>
00051 #include <vgui/vgui_style_sptr.h>
00052 #include <vgui/vgui_style.h>
00053 #include <vgui/vgui_viewer2D_tableau.h>
00054 #include <vgui/vgui_shell_tableau.h>
00055 #include <vgui/vgui_grid_tableau.h>
00056 #include <vgui/vgui_range_map_params.h>
00057 #include <bgui/bgui_image_tableau.h>
00058 #include <bgui/bgui_vtol2D_tableau.h>
00059 #include <bgui/bgui_picker_tableau.h>
00060 #include <bgui/bgui_range_adjuster_tableau.h>
00061 #include <bgui/bgui_image_utils.h>
00062 #include <vsol/vsol_point_2d.h>
00063 #include <vsol/vsol_point_2d_sptr.h>
00064 #include <vsol/vsol_conic_2d.h>
00065 #include <vsol/vsol_polyline_2d_sptr.h>
00066 #include <vsol/vsol_polyline_2d.h>
00067 #include <vtol/vtol_vertex_2d.h>
00068 #include <vtol/vtol_vertex.h>
00069 #include <vtol/vtol_edge_2d.h>
00070 #include <vtol/vtol_intensity_face.h>
00071 #include <brip/brip_vil1_float_ops.h>
00072 #include <brip/brip_vil_float_ops.h>
00073 #include <brip/brip_para_cvrg_params.h>
00074 #include <brip/brip_para_cvrg.h>
00075 #include <brip/brip_watershed_params.h>
00076 #include <brip/brip_max_scale_response.h>
00077 #include <sdet/sdet_watershed_region_proc_params.h>
00078 #include <sdet/sdet_watershed_region_proc.h>
00079 #include <sdet/sdet_region_proc_params.h>
00080 #include <sdet/sdet_region_proc.h>
00081 #include <strk/strk_region_info_params.h>
00082 #include <strk/strk_region_info.h>
00083 #include <strk/strk_io.h>
00084 
00085 ;
00086 segv_vil_segmentation_manager *segv_vil_segmentation_manager::instance_ = 0;
00087 
00088 segv_vil_segmentation_manager *segv_vil_segmentation_manager::instance()
00089 {
00090   if (!instance_)
00091   {
00092     instance_ = new segv_vil_segmentation_manager();
00093     instance_->init();
00094   }
00095   return segv_vil_segmentation_manager::instance_;
00096 }
00097 
00098 //-----------------------------------------------------------
00099 // constructors/destructor
00100 //
00101 segv_vil_segmentation_manager::segv_vil_segmentation_manager():vgui_wrapper_tableau()
00102 {
00103   first_ = true;
00104 }
00105 
00106 segv_vil_segmentation_manager::~segv_vil_segmentation_manager()
00107 {
00108 }
00109 
00110 //: Set up the tableaux
00111 void segv_vil_segmentation_manager::init()
00112 {
00113   bgui_image_tableau_sptr itab = bgui_image_tableau_new();
00114   bgui_vtol2D_tableau_sptr t2D = bgui_vtol2D_tableau_new(itab);
00115   bgui_picker_tableau_sptr picktab = bgui_picker_tableau_new(t2D);
00116   vgui_viewer2D_tableau_sptr v2D = vgui_viewer2D_tableau_new(picktab);
00117   grid_ = vgui_grid_tableau_new(1,1);
00118   grid_->set_grid_size_changeable(true);
00119   grid_->add_at(v2D, 0, 0);
00120   vgui_shell_tableau_sptr shell = vgui_shell_tableau_new(grid_);
00121   this->add_child(shell);
00122   first_ = true;
00123 }
00124 
00125 //: Calculate the range parameters for the input image
00126 vgui_range_map_params_sptr segv_vil_segmentation_manager::
00127 range_params(vil_image_resource_sptr const& image)
00128 {
00129   float gamma = 1.0;
00130   bool invert = false;
00131   bool gl_map = false;
00132   bool cache = true;
00133 
00134   //Check if the image is blocked
00135   vil_blocked_image_resource_sptr bir = blocked_image_resource(image);
00136   if (bir)
00137   { gl_map = true; cache = true;}
00138 
00139   //Check if the image is a pyramid
00140   bool pyr = image->get_property(vil_property_pyramid, 0);
00141   if (pyr)
00142   { gl_map = true; cache = true;}
00143 
00144   bgui_image_utils iu(image);
00145   iu.set_percent_limit(0.001);
00146 
00147   vgui_range_map_params_sptr rmps;
00148   if (iu.range_map_from_hist((float)gamma, invert, gl_map, cache, rmps))
00149     return rmps;
00150   if (iu.default_range_map(rmps, gamma, invert, gl_map, cache))
00151     return rmps;
00152   return 0;
00153 }
00154 
00155 //: set the image at the currently selected grid cell
00156 void segv_vil_segmentation_manager::
00157 set_selected_grid_image(vil_image_resource_sptr const& image,
00158                         vgui_range_map_params_sptr const& rmps)
00159 {
00160   bgui_image_tableau_sptr itab = this->selected_image_tab();
00161   if (!itab)
00162     this->add_image(image, rmps);
00163   else
00164   {
00165     itab->set_image_resource(image);
00166     itab->set_mapping(rmps);
00167   }
00168   itab->post_redraw();
00169 }
00170 
00171 //: Add an image at the specified grid cell
00172 void segv_vil_segmentation_manager::
00173 add_image_at(vil_image_resource_sptr const& image,
00174              const unsigned col, const unsigned row,
00175              vgui_range_map_params_sptr const& rmps)
00176 {
00177   vgui_range_map_params_sptr rmap = rmps;
00178   if (!rmps)
00179     rmap = range_params(image);
00180   bgui_image_tableau_sptr itab = bgui_image_tableau_new(image,rmps);
00181   //  itab->set_mapping(rmap);
00182   bgui_vtol2D_tableau_sptr t2D = bgui_vtol2D_tableau_new(itab);
00183   bgui_picker_tableau_sptr picktab = bgui_picker_tableau_new(t2D);
00184   vgui_viewer2D_tableau_sptr v2D = vgui_viewer2D_tableau_new(picktab);
00185   grid_->add_at(v2D, col, row);
00186   itab->post_redraw();
00187 }
00188 
00189 //: Add an image to the currently selected grid cell
00190 void segv_vil_segmentation_manager::
00191 add_image(vil_image_resource_sptr const& image,
00192           vgui_range_map_params_sptr const& rmps)
00193 {
00194   unsigned row=0, col=0;
00195   grid_->get_last_selected_position(&col, &row);
00196   this->add_image_at(image, col, row, rmps);
00197 }
00198 
00199 //-----------------------------------------------------------------------------
00200 //: remove the selected image
00201 //-----------------------------------------------------------------------------
00202 void segv_vil_segmentation_manager::remove_image()
00203 {
00204   unsigned row=0, col=0;
00205   grid_->get_last_selected_position(&col, &row);
00206   grid_->remove_at(col, row);
00207 }
00208 
00209 void segv_vil_segmentation_manager::convert_to_grey()
00210 {
00211   vil_image_resource_sptr img = this->selected_image();
00212   if (!img)
00213     return;
00214   vil_image_view<unsigned char> grey =
00215     brip_vil_float_ops::convert_to_byte(img);
00216   vil_image_resource_sptr gimg = vil_new_image_resource_of_view(grey);
00217   this->add_image(gimg);
00218 }
00219 
00220 //: Get the image tableau for the currently selected grid cell
00221 bgui_image_tableau_sptr segv_vil_segmentation_manager::selected_image_tab()
00222 {
00223   unsigned row=0, col=0;
00224   grid_->get_last_selected_position(&col, &row);
00225   vgui_tableau_sptr top_tab = grid_->get_tableau_at(col, row);
00226   if (top_tab)
00227   {
00228     bgui_image_tableau_sptr itab;
00229     itab.vertical_cast(vgui_find_below_by_type_name(top_tab,
00230                                                     vcl_string("vgui_image_tableau")));
00231     if (itab)
00232       return itab;
00233   }
00234   vcl_cout << "Unable to get bgui_image_tableau at (" << col
00235            << ", " << row << ")\n";
00236   return bgui_image_tableau_sptr();
00237 }
00238 
00239 //: Get the vtol2D tableau at the specified grid cell
00240 bgui_vtol2D_tableau_sptr
00241 segv_vil_segmentation_manager::vtol2D_tab_at(const unsigned col,
00242                                              const unsigned row)
00243 {
00244   vgui_tableau_sptr top_tab = grid_->get_tableau_at(col, row);
00245   if (top_tab)
00246   {
00247     bgui_vtol2D_tableau_sptr v2D;
00248     v2D.vertical_cast(vgui_find_below_by_type_name(top_tab,
00249                                                    vcl_string("bgui_vtol2D_tableau")));
00250     if (v2D)
00251       return v2D;
00252   }
00253   vcl_cout << "Unable to get bgui_vtol2D_tableau at (" << col
00254            << ", " << row << ")\n";
00255   return bgui_vtol2D_tableau_sptr();
00256 }
00257 
00258 //: Get the vtol2D tableau for the currently selected grid cell
00259 bgui_vtol2D_tableau_sptr segv_vil_segmentation_manager::selected_vtol2D_tab()
00260 {
00261   unsigned row=0, col=0;
00262   grid_->get_last_selected_position(&col, &row);
00263   return this->vtol2D_tab_at(col, row);
00264 }
00265 
00266 //: Get the picker tableau for the currently selected grid cell
00267 bgui_picker_tableau_sptr segv_vil_segmentation_manager::selected_picker_tab()
00268 {
00269   unsigned row=0, col=0;
00270   grid_->get_last_selected_position(&col, &row);
00271   vgui_tableau_sptr top_tab = grid_->get_tableau_at(col, row);
00272   if (top_tab)
00273   {
00274     bgui_picker_tableau_sptr pick;
00275     pick.vertical_cast(vgui_find_below_by_type_name(top_tab,
00276                                                     vcl_string("bgui_picker_tableau")));
00277     if (pick)
00278       return pick;
00279   }
00280   vcl_cout << "Unable to get bgui_picker_tableau at (" << col
00281            << ", " << row << ")\n";
00282   return bgui_picker_tableau_sptr();
00283 }
00284 
00285 
00286 vil_image_resource_sptr segv_vil_segmentation_manager::selected_image()
00287 {
00288   bgui_image_tableau_sptr itab = this->selected_image_tab();
00289   if (!itab)
00290     return 0;
00291   return itab->get_image_resource();
00292 }
00293 
00294 vil_image_resource_sptr segv_vil_segmentation_manager::image_at(const unsigned col,
00295                                                                 const unsigned row)
00296 {
00297   vgui_tableau_sptr top_tab = grid_->get_tableau_at(col, row);
00298   if (!top_tab)
00299     return 0;
00300 
00301   bgui_image_tableau_sptr itab;
00302   itab.vertical_cast(vgui_find_below_by_type_name(top_tab,
00303                                                   vcl_string("vgui_image_tableau")));
00304   if (!itab)
00305   {
00306     vcl_cout << "Unable to get bgui_image_tableau at (" << col
00307              << ", " << row << ")\n";
00308     return 0;
00309   }
00310   return itab->get_image_resource();
00311 }
00312 
00313 //-----------------------------------------------------------------------------
00314 //: Clear spatial objects from the selected display
00315 //-----------------------------------------------------------------------------
00316 void segv_vil_segmentation_manager::clear_display()
00317 {
00318   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00319   if (!t2D)
00320     return;
00321   t2D->clear_all();
00322 }
00323 
00324 //-----------------------------------------------------------------------------
00325 //: Clear spatial objects from all spatial panes
00326 //-----------------------------------------------------------------------------
00327 void segv_vil_segmentation_manager::clear_all()
00328 {
00329   unsigned ncols = grid_->cols(), nrows = grid_->rows();
00330   for (unsigned r=0; r<nrows; ++r)
00331     for (unsigned c=0; c<ncols; ++c)
00332     {
00333       bgui_vtol2D_tableau_sptr t = this->vtol2D_tab_at(c, r);
00334       if (t)
00335         t->clear_all();
00336     }
00337 }
00338 
00339 //-----------------------------------------------------------------------------
00340 //: Draw edges onto the tableau
00341 //-----------------------------------------------------------------------------
00342 void
00343 segv_vil_segmentation_manager::draw_edges(vcl_vector<vtol_edge_2d_sptr>& edges,
00344                                           bool verts)
00345 {
00346   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00347   if (!t2D)
00348     return;
00349   this->clear_display();
00350 #if 0
00351   vgui_image_tableau_sptr itab = t2D->get_image_tableau();
00352   if (!itab)
00353   {
00354     vcl_cout << "In segv_vil_segmentation_manager::draw_edges - null image tab\n";
00355     return;
00356   }
00357 #endif
00358   for (vcl_vector<vtol_edge_2d_sptr>::iterator eit = edges.begin();
00359        eit != edges.end(); eit++)
00360   {
00361     t2D->add_edge(*eit);
00362     //optionally display the edge vertices
00363     if (verts)
00364     {
00365       if ((*eit)->v1())
00366       {
00367         vtol_vertex_2d_sptr v1 = (*eit)->v1()->cast_to_vertex_2d();
00368         t2D->add_vertex(v1);
00369       }
00370       if ((*eit)->v2())
00371       {
00372         vtol_vertex_2d_sptr v2 = (*eit)->v2()->cast_to_vertex_2d();
00373         t2D->add_vertex(v2);
00374       }
00375     }
00376   }
00377   t2D->post_redraw();
00378 }
00379 
00380 
00381 //-----------------------------------------------------------------------------
00382 //: Draw polylines on the tableau
00383 //-----------------------------------------------------------------------------
00384 void segv_vil_segmentation_manager::
00385 draw_polylines(vcl_vector<vsol_polyline_2d_sptr > const& polys)
00386 {
00387   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00388   if (!t2D)
00389     return;
00390   //this->clear_display();
00391   vgui_image_tableau_sptr itab = t2D->get_image_tableau();
00392   if (!itab)
00393   {
00394     vcl_cout << "In segv_vil_segmentation_manager::draw_edges - null image tab\n";
00395     return;
00396   }
00397   for (vcl_vector<vsol_polyline_2d_sptr>::const_iterator pit = polys.begin();
00398        pit != polys.end(); pit++)
00399   {
00400     t2D->add_vsol_polyline_2d(*pit);
00401   }
00402 
00403   t2D->post_redraw();
00404 }
00405 
00406 //-----------------------------------------------------------------------------
00407 //: Draw line segments on the tableau
00408 //-----------------------------------------------------------------------------
00409 void segv_vil_segmentation_manager::
00410 draw_lines(vcl_vector<vsol_line_2d_sptr > const& lines,
00411            const vgui_style_sptr& style)
00412 {
00413   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00414   if (!t2D)
00415     return;
00416   //this->clear_display();
00417 #if 0
00418   vgui_image_tableau_sptr itab = t2D->get_image_tableau();
00419   if (!itab)
00420   {
00421     vcl_cout << "In segv_vil_segmentation_manager::draw_edges - null image tab\n";
00422     return;
00423   }
00424 #endif
00425   for (vcl_vector<vsol_line_2d_sptr>::const_iterator lit = lines.begin();
00426        lit != lines.end(); lit++)
00427   {
00428     t2D->add_vsol_line_2d(*lit,style);
00429   }
00430 
00431   t2D->post_redraw();
00432 }
00433 
00434 //-----------------------------------------------------------------------------
00435 //: Draw conic segments on the tableau
00436 //-----------------------------------------------------------------------------
00437 void segv_vil_segmentation_manager::
00438 draw_conics(vcl_vector<vsol_conic_2d_sptr > const& conics,
00439             const vgui_style_sptr& style)
00440 {
00441   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00442   if (!t2D)
00443     return;
00444   //this->clear_display();
00445 #if 0
00446   vgui_image_tableau_sptr itab = t2D->get_image_tableau();
00447   if (!itab)
00448   {
00449     vcl_cout << "In segv_vil_segmentation_manager::draw_edges - null image tab\n";
00450     return;
00451   }
00452 #endif
00453   for (vcl_vector<vsol_conic_2d_sptr>::const_iterator lit = conics.begin();
00454        lit != conics.end(); lit++)
00455   {
00456     t2D->add_vsol_conic_2d(*lit,style);
00457   }
00458   t2D->post_redraw();
00459 }
00460 
00461 //-----------------------------------------------------------------------------
00462 //: Draw points on the tableau
00463 //-----------------------------------------------------------------------------
00464 void segv_vil_segmentation_manager::
00465 draw_points(vcl_vector<vsol_point_2d_sptr> const& points, const vgui_style_sptr& style)
00466 {
00467   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00468   if (!t2D)
00469     return;
00470   //this->clear_display();
00471 #if 0
00472   vgui_image_tableau_sptr itab = t2D->get_image_tableau();
00473   if (!itab)
00474   {
00475     vcl_cout << "In segv_vil_segmentation_manager::draw_edges - null image tab\n";
00476     return;
00477   }
00478 #endif
00479   for (vcl_vector<vsol_point_2d_sptr>::const_iterator pit = points.begin();
00480        pit != points.end(); pit++)
00481   {
00482     t2D->add_vsol_point_2d(*pit,style);
00483   }
00484 
00485   t2D->post_redraw();
00486 }
00487 
00488 void segv_vil_segmentation_manager::draw_regions(vcl_vector<vtol_intensity_face_sptr>& regions,
00489                                                  bool verts)
00490 {
00491   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00492   if (!t2D)
00493     return;
00494   for (vcl_vector<vtol_intensity_face_sptr>::iterator rit = regions.begin();
00495        rit != regions.end(); rit++)
00496   {
00497     vtol_face_2d_sptr f = (*rit)->cast_to_face_2d();
00498     t2D->add_face(f);
00499     if (verts)
00500     {
00501       vcl_vector<vtol_vertex_sptr> vts;
00502       f->vertices(vts);
00503       for (vcl_vector<vtol_vertex_sptr>::iterator vit = vts.begin();
00504            vit != vts.end(); vit++)
00505       {
00506         vtol_vertex_2d_sptr v = (*vit)->cast_to_vertex_2d();
00507         t2D->add_vertex(v);
00508       }
00509     }
00510   }
00511   t2D->post_redraw();
00512 }
00513 
00514 void segv_vil_segmentation_manager::quit()
00515 {
00516   this->clear_all();
00517   vgui::quit();
00518 }
00519 
00520 void segv_vil_segmentation_manager::load_image()
00521 {
00522   static bool greyscale = false;
00523   static bool sblock = false;
00524   vgui_dialog load_image_dlg("Load image file");
00525   static vcl_string image_filename = "/home/dec/images/cal_image1.tif";
00526   static vcl_string ext = "*.*";
00527   load_image_dlg.file("Image Filename:", ext, image_filename);
00528   load_image_dlg.checkbox("greyscale ", greyscale);
00529   load_image_dlg.checkbox("blocked?:", sblock);
00530   if (!load_image_dlg.ask())
00531     return;
00532   //first check to see if the image is a pyramid
00533   bool pyrm = false;
00534   vil_image_resource_sptr image;
00535   vil_pyramid_image_resource_sptr pyr =
00536       vil_load_pyramid_resource(image_filename.c_str(), false);
00537   if (pyr)
00538   {
00539     image = pyr.ptr();
00540     pyrm = true;
00541   }
00542 
00543   if (!image)
00544     image = vil_load_image_resource(image_filename.c_str(), false);
00545 
00546   if (!image){
00547     vcl_cout << "Failed to load image path " << image_filename << '\n';
00548     return;
00549   }
00550 #if HAS_J2K
00551   // determine if the image can be made into a J2K-nitf pyramid
00552   char const* fmtp = image->file_format();
00553   vcl_string file_fmt = "";
00554   if (fmtp) file_fmt = fmtp;//fmtp can be 0 for undefined formats
00555   if (file_fmt == "nitf21")
00556   {
00557     vil_nitf2_image* nitf_resc = static_cast<vil_nitf2_image*>(image.ptr());
00558     if (nitf_resc->is_jpeg_2000_compressed())
00559     {
00560       vil_j2k_nitf2_pyramid_image_resource* j2k_nitf =
00561         new vil_j2k_nitf2_pyramid_image_resource(image);
00562       image = j2k_nitf;
00563       pyrm = true;
00564     }
00565   }
00566   else if (file_fmt == "j2k") {
00567     vil_j2k_pyramid_image_resource* j2k_pyr =
00568       new vil_j2k_pyramid_image_resource(image);
00569     image = j2k_pyr;
00570     pyrm = true;
00571   }
00572 #endif //HAS_J2K
00573   if (greyscale&&!pyrm)
00574   {
00575     vil_image_view<unsigned char> grey_view =
00576       brip_vil_float_ops::convert_to_grey(*image);
00577     image = vil_new_image_resource_of_view(grey_view);
00578   }
00579 
00580   if (sblock&&!pyrm)
00581   {
00582     vil_blocked_image_resource_sptr bimage = vil_new_blocked_image_facade(image);
00583     image = (vil_image_resource*)(vil_new_cached_image_resource(bimage)).ptr();
00584   }
00585 
00586   vgui_range_map_params_sptr rmps = range_params(image);
00587 
00588   if (first_)
00589   {
00590     this->set_selected_grid_image(image, rmps);
00591     first_ = false;
00592   }
00593   else
00594     this->add_image(image, rmps);
00595 }
00596 
00597 void segv_vil_segmentation_manager::load_image_nomenu(vcl_string const& path)
00598 {
00599   bool pyrm = false;
00600   vil_image_resource_sptr image;
00601   vil_pyramid_image_resource_sptr pyr =
00602       vil_load_pyramid_resource(path.c_str(), false);
00603   if (pyr)
00604   {
00605     image = pyr.ptr();
00606     pyrm = true;
00607   }
00608 
00609   if (!image)
00610     image = vil_load_image_resource(path.c_str(), false);
00611 
00612   if (!image){
00613     vcl_cout << "Failed to load image path " << path << '\n';
00614     return;
00615   }
00616 #if HAS_J2K
00617 // determine if the image can be made into a J2K-nitf pyramid
00618   char const* fmtp = image->file_format();
00619   vcl_string file_fmt = "";
00620   if (fmtp) file_fmt = fmtp;//fmtp can be 0 for undefined formats
00621   if (file_fmt == "nitf21")
00622   {
00623     vil_nitf2_image* nitf_resc = static_cast<vil_nitf2_image*>(image.ptr());
00624     if (nitf_resc->is_jpeg_2000_compressed())
00625     {
00626       vil_j2k_nitf2_pyramid_image_resource* j2k_nitf =
00627         new vil_j2k_nitf2_pyramid_image_resource(image);
00628       image = j2k_nitf;
00629       pyrm = true;
00630     }
00631   }
00632   else if (file_fmt == "j2k") {
00633     vil_j2k_pyramid_image_resource* j2k_pyr =
00634       new vil_j2k_pyramid_image_resource(image);
00635     image = j2k_pyr;
00636     pyrm = true;
00637   }
00638 #endif //HAS_J2K
00639   vgui_range_map_params_sptr rmps = range_params(image);
00640 
00641   if (first_)
00642   {
00643     this->set_selected_grid_image(image, rmps);
00644     first_ = false;
00645   }
00646   else
00647     this->add_image(image, rmps);
00648 }
00649 
00650 void segv_vil_segmentation_manager::save_image()
00651 {
00652   vgui_dialog file_dialog("Save Image");
00653   static vcl_string image_file;
00654   static vcl_string ext = "tif";
00655   static vcl_string type = "tiff";
00656   static unsigned size_block = 0;
00657   static bool byte = false;
00658   file_dialog.file("Image Filename:", ext, image_file);
00659   file_dialog.field("Image Format: ", type);
00660   file_dialog.field("BlockSize", size_block);
00661   file_dialog.checkbox("Convert to byte image", byte);
00662   if (!file_dialog.ask())
00663     return;
00664   vil_image_resource_sptr img = this->selected_image();
00665   if (!img)
00666   {
00667     vcl_cerr << "Null image in segv_vil_segmentation_manager::save_image\n";
00668     return;
00669   }
00670   vil_image_resource_sptr save_image = img;
00671   if (byte)
00672   {
00673     vil_image_view<unsigned char> byte_view = brip_vil_float_ops::convert_to_byte(img);
00674     save_image = vil_new_image_resource_of_view(byte_view);
00675   }
00676   if (size_block>0)
00677   {
00678     vil_blocked_image_resource_sptr bim =
00679       vil_new_blocked_image_resource(image_file.c_str(),
00680                                      save_image->ni(), save_image->nj(),
00681                                      save_image->nplanes(),
00682                                      save_image->pixel_format(),
00683                                      size_block, size_block,
00684                                      "tiff");
00685     vil_image_view_base_sptr view = save_image->get_view();
00686     if (view)
00687       bim->vil_image_resource::put_view(*view);
00688     return;
00689   }
00690 
00691   if (!vil_save_image_resource(save_image, image_file.c_str(), type.c_str()))
00692     vcl_cerr << "segv_vil_segmentation_manager::save_image operation failed\n";
00693 }
00694 
00695 void segv_vil_segmentation_manager::save_nitf_camera()
00696 {
00697   vil_image_resource_sptr img = this->selected_image();
00698   if (!img)
00699   {
00700     vcl_cerr << "Null image in segv_vil_segmentation_manager::save_camera\n";
00701     return;
00702   }
00703 
00704   vil_nitf2_image* nitf = 0;
00705   vcl_string format = img->file_format();
00706   vcl_string prefix = format.substr(0,4);
00707   if (prefix == "nitf") {
00708     nitf = (vil_nitf2_image*)img.ptr();
00709     vgui_dialog file_dialog("Save NITF Camera");
00710     static vcl_string image_file;
00711     static vcl_string ext = "rpc";
00712     file_dialog.file("Image Filename:", ext, image_file);
00713     if (!file_dialog.ask())
00714       return;
00715     vpgl_nitf_rational_camera rpcam(nitf, true);
00716     rpcam.save(image_file);
00717   }
00718 }
00719 
00720 void segv_vil_segmentation_manager::set_range_params()
00721 {
00722   bgui_image_tableau_sptr itab = this->selected_image_tab();
00723   if (!itab)
00724     return;
00725   vgui_range_map_params_sptr rmps = itab->map_params();
00726   if (!rmps)
00727   {
00728     vil_image_resource_sptr img = itab->get_image_resource();
00729     if (!img)
00730       return;
00731     rmps = range_params(img);
00732     if (!rmps)
00733       return;
00734   }
00735   unsigned nc = rmps->n_components_;
00736   static double min = static_cast<double>(rmps->min_L_),
00737     max = static_cast<double>(rmps->max_L_);
00738   static float gamma = rmps->gamma_L_;
00739   static bool invert = rmps->invert_;
00740   static bool gl_map = rmps->use_glPixelMap_;
00741   static bool cache = rmps->cache_mapped_pix_;
00742   if (nc==3)
00743   {
00744     min = static_cast<double>(rmps->min_R_);
00745     max = static_cast<double>(rmps->max_R_);
00746     gamma = rmps->gamma_R_;
00747   }
00748   vgui_dialog range_dlg("Set Range Map Params");
00749   range_dlg.field("Range min:", min);
00750   range_dlg.field("Range max:", max);
00751   range_dlg.field("Gamma:", gamma);
00752   range_dlg.checkbox("Invert:", invert);
00753   range_dlg.checkbox("Use GL Mapping", gl_map);
00754   range_dlg.checkbox("Cache Pixels", cache);
00755   if (!range_dlg.ask())
00756     return;
00757   if (nc==1)
00758     rmps= new vgui_range_map_params(min, max, gamma, invert,
00759                                     gl_map, cache);
00760   else if (nc == 3)
00761     rmps = new vgui_range_map_params(min, max, min, max, min, max,
00762                                      gamma, gamma, gamma, invert,
00763                                      gl_map, cache);
00764   else
00765     rmps = 0;
00766   itab->set_mapping(rmps);
00767 }
00768 
00769 void segv_vil_segmentation_manager::threshold_image()
00770 {
00771   vil_image_resource_sptr img = selected_image();
00772   if (!img)
00773   {
00774     vcl_cout << "In segv_segmentation_manager::threshold_image - no image\n";
00775     return;
00776   }
00777   static float thresh = 128.0f;
00778   vgui_dialog thresh_dlg("Threshold Image");
00779   thresh_dlg.field("Threshold", thresh);
00780   if (!thresh_dlg.ask())
00781     return;
00782   vil_image_view<float> fimage = brip_vil_float_ops::convert_to_float(*img);
00783   vil_image_view<float> timage =
00784     brip_vil_float_ops::threshold(fimage, thresh, 255);
00785   vil_image_view<unsigned char> cimage = brip_vil_float_ops::convert_to_byte(timage, 0, 255);
00786   this->add_image(vil_new_image_resource_of_view(cimage));
00787 }
00788 
00789 void segv_vil_segmentation_manager::harris_corners()
00790 {
00791   vil_image_resource_sptr img = selected_image();
00792   if (!img)
00793   {
00794     vcl_cout << "In segv_segmentation_manager::harris_measure) - no image\n";
00795     return;
00796   }
00797   static sdet_harris_detector_params hdp;
00798   vgui_dialog harris_dialog("harris");
00799   harris_dialog.field("sigma", hdp.sigma_);
00800   harris_dialog.field("thresh", hdp.thresh_);
00801   harris_dialog.field("N = 2n+1, (n)", hdp.n_);
00802   harris_dialog.field("Max No Corners(percent)", hdp.percent_corners_);
00803   harris_dialog.field("scale_factor", hdp.scale_factor_);
00804   harris_dialog.checkbox("Use vil harris\n corner strength ", hdp.use_vil_harris_);
00805   if (!harris_dialog.ask())
00806     return;
00807   sdet_harris_detector hd(hdp);
00808   hd.set_image_resource(img);
00809   hd.extract_corners();
00810   vcl_vector<vsol_point_2d_sptr>& points = hd.get_points();
00811   int N = points.size();
00812   if (!N)
00813     return;
00814   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00815   if (!t2D)
00816     return;
00817   t2D->clear_all();
00818   for (int i=0; i<N; i++)
00819     t2D->add_vsol_point_2d(points[i]);
00820   t2D->post_redraw();
00821 }
00822 
00823 void segv_vil_segmentation_manager::nonmaximal_suppression()
00824 {
00825   bool show_lines = false;
00826   vil_image_resource_sptr img = selected_image();
00827   if (!img)
00828   {
00829     vcl_cout << "In segv_segmentation_manager::nonmaximal_suppression) - no image\n";
00830     return;
00831   }
00832   static sdet_nonmax_suppression_params nsp;
00833   vgui_dialog nonmax_dialog("non-maximal suppression");
00834   nonmax_dialog.field("Gradient Magnitude Percentage Threshold ", nsp.thresh_);
00835   nonmax_dialog.choice("Number of Points Used for Parabola Fit ", "3", "9", nsp.pfit_type_);
00836   nonmax_dialog.checkbox("Show lines at the edge points? ", show_lines);
00837   if (!nonmax_dialog.ask())
00838     return;
00839 
00840   // prepare input for the nonmax suppression
00841   vil_image_view<vxl_byte> input, input_grey;
00842   vil_image_view<double> grad_i, grad_j, grad_mag_img;
00843   vbl_array_2d<double> grad_x, grad_y, grad_mag;
00844   vbl_array_2d<vgl_vector_2d <double> > input_directions;
00845 
00846   // vil_convert_cast(img->get_view(), input);
00847   switch ( img->get_view()->pixel_format() )
00848   {
00849 #define macro(F , T) \
00850     case F: vil_convert_cast( vil_image_view<T >(img->get_view()), input ); break;
00851     macro( VIL_PIXEL_FORMAT_UINT_32, vxl_uint_32 )
00852     macro( VIL_PIXEL_FORMAT_INT_32, vxl_int_32 )
00853     macro( VIL_PIXEL_FORMAT_UINT_16, vxl_uint_16 )
00854     macro( VIL_PIXEL_FORMAT_INT_16, vxl_int_16 )
00855     macro( VIL_PIXEL_FORMAT_BYTE, vxl_byte )
00856     macro( VIL_PIXEL_FORMAT_SBYTE, vxl_sbyte )
00857     macro( VIL_PIXEL_FORMAT_FLOAT, float )
00858     macro( VIL_PIXEL_FORMAT_DOUBLE, double )
00859     macro( VIL_PIXEL_FORMAT_BOOL, bool )
00860     default: img->get_view() = 0;
00861 #undef macro
00862   }
00863 
00864   int ni = input.ni();
00865   int nj = input.nj();
00866 
00867   grad_i.set_size(ni,nj);
00868   grad_j.set_size(ni,nj);
00869   grad_mag_img.set_size(ni,nj);
00870   input_grey.set_size(ni,nj);
00871   grad_x.resize(ni,nj);
00872   grad_y.resize(ni,nj);
00873   grad_mag.resize(ni,nj);
00874   input_directions.resize(ni,nj);
00875 
00876   if (input.nplanes() > 1)
00877     vil_convert_planes_to_grey(input, input_grey);
00878   else
00879     input_grey = input;
00880 
00881   vil_sobel_1x3 <vxl_byte, double> (input_grey, grad_i, grad_j);
00882   for (int j=0;j<nj; j++)
00883   {
00884     for (int i=0;i<ni; i++)
00885     {
00886       double xval = grad_i(i,j);
00887       double yval = grad_j(i,j);
00888       grad_x(i,j) = xval;
00889       grad_y(i,j) = yval;
00890       double val = vcl_sqrt(vcl_pow(xval,2.0) + vcl_pow(yval,2.0));
00891       grad_mag(i,j) = val;
00892       grad_mag_img(i,j) = val;
00893       vgl_vector_2d<double> dir(xval, yval);
00894       input_directions(i,j) = dir;
00895     }
00896   }
00897 
00898   // Below is to demonstrate how to initialize the non-maximal suppression in different ways
00899 //  sdet_nonmax_suppression ns(nsp, grad_mag, input_directions);
00900   sdet_nonmax_suppression ns(nsp, grad_x, grad_y);
00901 //  sdet_nonmax_suppression ns(nsp, grad_i, grad_j);
00902 //  sdet_nonmax_suppression ns(nsp, grad_mag_img, input_directions);
00903   ns.apply();
00904   vcl_vector<vsol_point_2d_sptr>& points = ns.get_points();
00905   vcl_vector<vsol_line_2d_sptr>& lines = ns.get_lines();
00906   // not used below, just for demonstration purposes
00907   //vcl_vector<vgl_vector_2d<double> >& directions = ns.get_directions();
00908   int N = points.size();
00909   if (!N)
00910     return;
00911   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00912   if (!t2D)
00913     return;
00914   t2D->clear_all();
00915   if (!show_lines)
00916   {
00917     for (int i=0; i<N; i++)
00918       t2D->add_vsol_point_2d(points[i]);
00919   }
00920   if (show_lines)
00921   {
00922     for (int i=0; i<N; i++)
00923       t2D->add_vsol_line_2d(lines[i]);
00924   }
00925   t2D->post_redraw();
00926 }
00927 
00928 void segv_vil_segmentation_manager::vd_edges()
00929 {
00930   this->clear_display();
00931   static bool agr = true;
00932   static sdet_detector_params dp;
00933   static float nm = 2.0;
00934 
00935   vgui_dialog vd_dialog("VD Edges");
00936   vd_dialog.field("Gaussian sigma", dp.smooth);
00937   vd_dialog.field("Noise Threshold", nm);
00938   vd_dialog.checkbox("Automatic Threshold", dp.automatic_threshold);
00939   vd_dialog.checkbox("Agressive Closure", agr);
00940   vd_dialog.checkbox("Compute Junctions", dp.junctionp);
00941   if (!vd_dialog.ask())
00942     return;
00943   dp.noise_multiplier=nm;
00944   if (agr)
00945     dp.aggressive_junction_closure=1;
00946   else
00947     dp.aggressive_junction_closure=0;
00948   vil_image_resource_sptr img = selected_image();
00949   if (!img||!img->ni()||!img->nj())
00950   {
00951     vcl_cout << "In segv_vil_segmentation_manager::vd_edges() - no image\n";
00952     return;
00953   }
00954   sdet_detector det(dp);
00955   det.SetImage(img);
00956 
00957   det.DoContour();
00958   vcl_vector<vtol_edge_2d_sptr>* edges = det.GetEdges();
00959   if (edges)
00960     this->draw_edges(*edges, true);
00961 }
00962 
00963 void segv_vil_segmentation_manager::fit_lines()
00964 {
00965   this->clear_display();
00966   static sdet_detector_params dp;
00967   static bool agr = true;
00968   static float nm = 2.0;
00969 
00970   static sdet_fit_lines_params flp;
00971 
00972   vgui_dialog lf_dialog("Fit Lines");
00973   lf_dialog.field("Gaussian sigma", dp.smooth);
00974   lf_dialog.field("Noise Threshold", nm);
00975   lf_dialog.checkbox("Automatic Threshold", dp.automatic_threshold);
00976   lf_dialog.checkbox("Agressive Closure", agr);
00977   lf_dialog.checkbox("Compute Junctions", dp.junctionp);
00978   lf_dialog.field("Min Fit Length", flp.min_fit_length_);
00979   lf_dialog.field("RMS Distance", flp.rms_distance_);
00980 
00981   if (!lf_dialog.ask())
00982     return;
00983   dp.noise_multiplier=nm;
00984   if (agr)
00985     dp.aggressive_junction_closure=1;
00986   else
00987     dp.aggressive_junction_closure=0;
00988   dp.borderp = false;
00989   sdet_detector det(dp);
00990 
00991   vil_image_resource_sptr img = selected_image();
00992   if (!img||!img->ni()||!img->nj())
00993   {
00994     vcl_cout << "In segv_vil_segmentation_manager::vd_edges() - no image\n";
00995     return;
00996   }
00997 
00998   det.SetImage(img);
00999 
01000   det.DoContour();
01001   vcl_vector<vtol_edge_2d_sptr>* edges = det.GetEdges();
01002   if (!edges)
01003   {
01004     vcl_cout << "No edges to fit lines\n";
01005     return;
01006   }
01007   sdet_fit_lines fl(flp);
01008   fl.set_edges(*edges);
01009   fl.fit_lines();
01010   vcl_vector<vsol_line_2d_sptr> lines = fl.get_line_segs();
01011   this->draw_lines(lines);
01012 }
01013 
01014 void segv_vil_segmentation_manager::fit_conics()
01015 {
01016   this->clear_display();
01017   static sdet_detector_params dp;
01018   static bool agr = true;
01019   static float nm = 2.0;
01020 
01021   static sdet_fit_conics_params fcp;
01022 
01023   vgui_dialog lf_dialog("Fit Conics");
01024   lf_dialog.field("Gaussian sigma", dp.smooth);
01025   lf_dialog.field("Noise Threshold", nm);
01026   lf_dialog.checkbox("Automatic Threshold", dp.automatic_threshold);
01027   lf_dialog.checkbox("Agressive Closure", agr);
01028   lf_dialog.checkbox("Compute Junctions", dp.junctionp);
01029   lf_dialog.field("Min Fit Length", fcp.min_fit_length_);
01030   lf_dialog.field("RMS Distance", fcp.rms_distance_);
01031 
01032   if (!lf_dialog.ask())
01033     return;
01034   dp.noise_multiplier=nm;
01035   if (agr)
01036     dp.aggressive_junction_closure=1;
01037   else
01038     dp.aggressive_junction_closure=0;
01039   dp.borderp = false;
01040   sdet_detector det(dp);
01041 
01042   vil_image_resource_sptr img = selected_image();
01043   if (!img||!img->ni()||!img->nj())
01044   {
01045     vcl_cout << "In segv_vil_segmentation_manager::vd_edges() - no image\n";
01046     return;
01047   }
01048 
01049   det.SetImage(img);
01050 
01051   det.DoContour();
01052   vcl_vector<vtol_edge_2d_sptr>* edges = det.GetEdges();
01053   if (!edges)
01054   {
01055     vcl_cout << "No edges to fit conics\n";
01056     return;
01057   }
01058   sdet_fit_conics fl(fcp);
01059   fl.set_edges(*edges);
01060   fl.fit_conics();
01061   vcl_vector<vsol_conic_2d_sptr> conics = fl.get_conic_segs();
01062   this->draw_conics(conics);
01063 
01064   vcl_vector<vsol_point_2d_sptr> center_points;
01065   double cx,cy,phi,width,height;
01066   // draw the center points of the conics
01067   for (unsigned int i=0; i<conics.size(); i++){
01068     if (conics[i]->is_real_ellipse()) {
01069       conics[i]->ellipse_parameters(cx,cy,phi,width,height);
01070       vsol_point_2d_sptr p = new vsol_point_2d(cx, cy);
01071       vcl_cout << i << " center (" << cx << ',' << cy << ')' << vcl_endl;
01072       center_points.push_back(p);
01073     }
01074   }
01075   vgui_style_sptr style = vgui_style::new_style(1.0f,0.0f,0.0f,5.0f,1.0f);
01076 
01077   this->draw_points(center_points, style);
01078 }
01079 
01080 void segv_vil_segmentation_manager::fit_overlay_conics()
01081 {
01082   //this->clear_display();
01083   static sdet_detector_params dp;
01084   static bool agr = true;
01085   static float nm = 2.0;
01086 
01087   static sdet_fit_conics_params fcp;
01088 
01089   vgui_dialog lf_dialog("Fit overlay Conics");
01090   static vcl_string image_filename = "/home/dec/images/cal_image1.tif";
01091   static vcl_string ext = "*.*";
01092   lf_dialog.file("Image Filename:", ext, image_filename);
01093   lf_dialog.field("Gaussian sigma", dp.smooth);
01094   lf_dialog.field("Noise Threshold", nm);
01095   lf_dialog.checkbox("Automatic Threshold", dp.automatic_threshold);
01096   lf_dialog.checkbox("Agressive Closure", agr);
01097   lf_dialog.checkbox("Compute Junctions", dp.junctionp);
01098   lf_dialog.field("Min Fit Length", fcp.min_fit_length_);
01099   lf_dialog.field("RMS Distance", fcp.rms_distance_);
01100 
01101   if (!lf_dialog.ask())
01102     return;
01103   dp.noise_multiplier=nm;
01104   if (agr)
01105     dp.aggressive_junction_closure=1;
01106   else
01107     dp.aggressive_junction_closure=0;
01108   dp.borderp = false;
01109   sdet_detector det(dp);
01110 
01111   vil_image_resource_sptr img = vil_load_image_resource(image_filename.c_str());
01112   if (!img||!img->ni()||!img->nj())
01113   {
01114     vcl_cout << "In segv_vil_segmentation_manager::vd_edges() - no image\n";
01115     return;
01116   }
01117 
01118   det.SetImage(img);
01119 
01120   det.DoContour();
01121   vcl_vector<vtol_edge_2d_sptr>* edges = det.GetEdges();
01122   if (!edges)
01123   {
01124     vcl_cout << "No edges to fit conics\n";
01125     return;
01126   }
01127   sdet_fit_conics fl(fcp);
01128   fl.set_edges(*edges);
01129   fl.fit_conics();
01130   vcl_vector<vsol_conic_2d_sptr> conics = fl.get_conic_segs();
01131   vgui_style_sptr style = vgui_style::new_style(1.0f,1.0f,0.0f,5.0f,1.0f);
01132   this->draw_conics(conics, style);
01133 
01134   vcl_vector<vsol_point_2d_sptr> center_points;
01135   double cx,cy,phi,width,height;
01136   // draw the center points of the conics
01137   for (unsigned int i=0; i<conics.size(); i++){
01138     if (conics[i]->is_real_ellipse()) {
01139       conics[i]->ellipse_parameters(cx,cy,phi,width,height);
01140       vsol_point_2d_sptr p = new vsol_point_2d(cx, cy);
01141       vcl_cout << i << " center (" << cx << ',' << cy << ')' << vcl_endl;
01142       center_points.push_back(p);
01143     }
01144   }
01145   vgui_style_sptr style2 = vgui_style::new_style(1.0f,0.0f,1.0f,5.0f,1.0f);
01146 
01147   this->draw_points(center_points, style2);
01148 }
01149 
01150 // ####################################################################
01151 // Added by J. Green to project 3D points into image using rpc camera
01152 
01153 void segv_vil_segmentation_manager::project_points()
01154 {
01155   this->clear_display(); // apparently this call is needed?
01156   vil_image_resource_sptr img = this->selected_image();
01157   vil_nitf2_image* nitf = 0;
01158   vcl_string format = img->file_format();
01159   vcl_string prefix = format.substr(0,4);
01160   if (prefix == "nitf")
01161     nitf = (vil_nitf2_image*)img.ptr();
01162   else
01163   {
01164     //Check if the image is a pyramid
01165     bool pyr = img->get_property(vil_property_pyramid, 0);
01166     if (!pyr)
01167     {
01168       vcl_cout << "Current image is not a NITF image\n";
01169       return;
01170     }
01171     //Get the base image
01172     vil_pyramid_image_resource* pimage =
01173       (vil_pyramid_image_resource*)img.ptr();
01174     vil_image_resource_sptr base = pimage->get_resource(0);
01175     format = base->file_format();
01176     if (format == "nitf" || format =="nitf20" )
01177       nitf = (vil_nitf2_image*)base.ptr();
01178     else
01179     {
01180       vcl_cout << "Current image is not a NITF image\n";
01181       return;
01182     }
01183   }
01184   //cast to an nitf2_image
01185 
01186   static double lat=32.722;  // Lattitude
01187   static double lon=-117.15; // Longitude
01188   static double elev=43;     // Elevation
01189 
01190   vgui_dialog lf_dialog("Project Points");
01191   lf_dialog.field("Lattitude", lat);
01192   lf_dialog.field("Longitude", lon);
01193   lf_dialog.field("Elevation", elev);
01194 
01195   if (!lf_dialog.ask())
01196   {
01197     vcl_cerr << "In project_points() dialog failed; returning.\n";
01198     return;
01199   }
01200   // calculate point location (x1, y1) for 1st camera
01201   double u = 0;
01202   double v = 0;
01203   vpgl_nitf_rational_camera rpcam(nitf, true);
01204   rpcam.project(lon, lat, elev, u,  v);
01205   vcl_cout << " camera projects to <" << u << ", " << v << '>' << vcl_endl;
01206   vcl_vector<vsol_point_2d_sptr> points;
01207   vsol_point_2d_sptr p1 = new vsol_point_2d(u, v);
01208   points.push_back(p1);
01209   vgui_style_sptr style1 = vgui_style::new_style(1.0f,0.0f,0.0f,10.0f,1.0f);   // first style, red
01210   // draw point
01211   this->draw_points(points, style1);
01212 }  // end of project_points method
01213 
01214 void segv_vil_segmentation_manager::regions()
01215 {
01216   this->clear_display();
01217   static bool debug = false;
01218   static bool agr = true;
01219   static bool residual = false;
01220   static sdet_detector_params dp;
01221   static float nm = 1.0;
01222   vgui_dialog region_dialog("Edgel Regions");
01223   region_dialog.field("Gaussian sigma", dp.smooth);
01224   region_dialog.field("Noise Threshold", nm);
01225   region_dialog.checkbox("Automatic Threshold", dp.automatic_threshold);
01226   region_dialog.checkbox("Agressive Closure", agr);
01227   region_dialog.checkbox("Compute Junctions", dp.junctionp);
01228   region_dialog.checkbox("Debug", debug);
01229   region_dialog.checkbox("Residual Image", residual);
01230   if (!region_dialog.ask())
01231     return;
01232   dp.noise_multiplier=nm;
01233   if (agr)
01234     dp.aggressive_junction_closure=1;
01235   else
01236     dp.aggressive_junction_closure=0;
01237 
01238   vil_image_resource_sptr img = selected_image();
01239   if (!img||!img->ni()||!img->nj())
01240   {
01241     vcl_cout << "In segv_vil_segmentation_manager::vd_edges() - no image\n";
01242     return;
01243   }
01244 
01245   sdet_region_proc_params rpp(dp, true, debug, 2);
01246   sdet_region_proc rp(rpp);
01247   rp.set_image_resource(img);
01248   rp.extract_regions();
01249   if (debug)
01250   {
01251     vil1_image ed_img = rp.get_edge_image();
01252 #if 0
01253     vgui_image_tableau_sptr itab =  t2D->get_image_tableau();
01254     if (!itab)
01255     {
01256       vcl_cout << "In segv_vil_segmentation_manager::regions() - null image tableau\n";
01257       return;
01258     }
01259     itab->set_image(ed_img);
01260     itab->post_redraw();
01261 #endif
01262 //    this->add_image(ed_img);
01263   }
01264   if (!debug)
01265   {
01266     vcl_vector<vtol_intensity_face_sptr>& regions = rp.get_regions();
01267     this->draw_regions(regions, true);
01268   }
01269   if (residual)
01270   {
01271     vil_image_view<float> res_img = rp.get_residual_image_view();
01272     if (!res_img)
01273       return;
01274     this->add_image(vil_new_image_resource_of_view(res_img));
01275   }
01276 }
01277 
01278 //: Show combined greyscale images in up to three planes as a color image.
01279 // Assume images are arranged by columns
01280 void segv_vil_segmentation_manager::display_images_as_color()
01281 {
01282   unsigned ncols =grid_->cols();
01283   if (ncols<2)
01284   {
01285     vcl_cout << "In segv_vil_segmentation_manager::display_images_as_color() -"
01286              << " not enough active panes\n";
01287     return;
01288   }
01289   vil_image_resource_sptr img0 = this->image_at(0,0);
01290   vil_image_resource_sptr img1 = this->image_at(1,0);
01291   if (!img0||!img1)
01292   {
01293     vcl_cout << "In segv_vil_segmentation_manager::display_images_as_color()() -"
01294              << " some input images are null\n";
01295     return;
01296   }
01297   vil_image_view<unsigned char> cimage0 =
01298     brip_vil_float_ops::convert_to_byte(img0);
01299   vil_image_view<unsigned char> cimage1 =
01300     brip_vil_float_ops::convert_to_byte(img1);
01301   vil_image_view<unsigned char> cimage2;
01302   if (ncols==3&&this->image_at(2,0))
01303   {
01304     vil_image_resource_sptr img2 = this->image_at(2,0);
01305     cimage2 = brip_vil_float_ops::convert_to_byte(img2);
01306   }
01307   else
01308   {
01309     unsigned w = cimage0.ni(), h = cimage0.nj();
01310     cimage2.set_size(w, h);
01311     cimage2.fill(0);
01312   }
01313   vil_image_view<vil_rgb<vxl_byte> > rgb = brip_vil_float_ops::combine_color_planes(cimage0, cimage1, cimage2);
01314   vil_image_resource_sptr color = vil_new_image_resource_of_view(rgb);
01315   if (ncols<3)
01316     grid_->add_column();
01317   unsigned col = 2, row = 0;
01318   this->add_image_at(color,col,row);
01319 }
01320 
01321 void segv_vil_segmentation_manager::intensity_profile()
01322 {
01323   bgui_picker_tableau_sptr ptab = selected_picker_tab();
01324   float start_col=0, end_col=0, start_row=0, end_row=0;
01325   ptab->pick_line(&start_col, &start_row, &end_col, &end_row);
01326   bgui_image_tableau_sptr itab = selected_image_tab();
01327   vcl_vector<double> pos, vals;
01328   itab->image_line(start_col, start_row, end_col, end_row, pos, vals);
01329   bgui_graph_tableau_sptr g = bgui_graph_tableau_new(512, 512);
01330   g->update(pos, vals);
01331   //popup a profile graph
01332   char location[100];
01333   vcl_sprintf(location, "scan:(%d, %d)<->(%d, %d)",
01334               static_cast<unsigned>(start_col),
01335               static_cast<unsigned>(start_row),
01336               static_cast<unsigned>(end_col),
01337               static_cast<unsigned>(end_row));
01338   vgui_dialog* ip_dialog = g->popup_graph(location);
01339   if (!ip_dialog->ask())
01340   {
01341     delete ip_dialog;
01342     return;
01343   }
01344 delete ip_dialog;
01345 }
01346 
01347 void segv_vil_segmentation_manager::inline_viewer()
01348 {
01349   bgui_image_tableau_sptr itab = this->selected_image_tab();
01350   bgui_range_adjuster_tableau_sptr h= bgui_range_adjuster_tableau_new(itab);
01351   h->set_hardware(true);
01352   h->update();
01353   vgui_viewer2D_tableau_sptr v = vgui_viewer2D_tableau_new(h);
01354   vgui_shell_tableau_sptr s = vgui_shell_tableau_new(v);
01355   //popup adjuster
01356   vgui_dialog test_inline("Histogram Range Adjuster");
01357   test_inline.inline_tableau(s, 280, 200);
01358   if (!test_inline.ask())
01359     return;
01360 }
01361 
01362 void segv_vil_segmentation_manager::intensity_histogram()
01363 {
01364   vil_image_resource_sptr img = selected_image();
01365   if (!img||!img->ni()||!img->nj())
01366   {
01367     vcl_cout << "In segv_vil_segmentation_manager::intensity_histogram() - no image\n";
01368     return;
01369   }
01370   bgui_image_utils iu(img);
01371   bgui_graph_tableau_sptr g = iu.hist_graph();
01372 
01373   if (!g)
01374   { vcl_cout << "In segv_vil_segmentation_manager::intensity_histogram()- color images not supported\n";
01375     return;
01376   }
01377 
01378   //popup a profile graph
01379   char location[100];
01380   vcl_sprintf(location, "Intensity Histogram");
01381   vgui_dialog* ip_dialog = g->popup_graph(location);
01382   if (!ip_dialog->ask())
01383   {
01384     delete ip_dialog;
01385     return;
01386   }
01387   delete ip_dialog;
01388 }
01389 
01390 //=== Image Arithmetic (Uses the Image Stack)
01391 //Add the image in pane 0 to the image in pane 1. Result in pane 2.
01392 void segv_vil_segmentation_manager::add_images()
01393 {
01394   unsigned ncols =grid_->cols();
01395   if (ncols<2)
01396   {
01397     vcl_cout << "In segv_vil_segmentation_manager::add_images() -"
01398              << " not enough active panes\n";
01399     return;
01400   }
01401   vil_image_resource_sptr img0 = this->image_at(0,0);
01402   vil_image_resource_sptr img1 = this->image_at(1,0);
01403   if (!img0||!img1)
01404   {
01405     vcl_cout << "In segv_vil_segmentation_manager::add_images() -"
01406              << " one or both input images are null\n";
01407     return;
01408   }
01409   vil_image_resource_sptr sum = brip_vil_float_ops::sum(img0, img1);
01410   vgui_range_map_params_sptr rmps = range_params(sum);
01411   if (ncols<3)
01412     grid_->add_column();
01413   unsigned col = 2, row = 0;
01414   this->add_image_at(sum,col,row, rmps);
01415 }
01416 
01417 //subtract the image in pane 1 from the image in pane 0. Result in pane 2
01418 void segv_vil_segmentation_manager::subtract_images()
01419 {
01420   unsigned ncols =grid_->cols();
01421   if (ncols<2)
01422   {
01423     vcl_cout << "In segv_vil_segmentation_manager::subtract_images() -"
01424              << " not enough active panes\n";
01425     return;
01426   }
01427   vil_image_resource_sptr img0 = this->image_at(0,0);
01428   vil_image_resource_sptr img1 = this->image_at(1,0);
01429   if (!img0||!img1)
01430   {
01431     vcl_cout << "In segv_vil_segmentation_manager::subtract_images() -"
01432              << " one or both input images are null\n";
01433     return;
01434   }
01435   vil_image_resource_sptr diff = brip_vil_float_ops::difference(img0, img1);
01436   vgui_range_map_params_sptr rmps = range_params(diff);
01437   if (ncols<3)
01438     grid_->add_column();
01439   unsigned col = 2, row = 0;
01440   this->add_image_at(diff,col,row);
01441 }
01442 
01443 void segv_vil_segmentation_manager::negate_image()
01444 {
01445     vil_image_resource_sptr img = selected_image();
01446   if (!img)
01447   {
01448     vcl_cout << "In segv_segmentation_manager::negate_image - no image\n";
01449     return;
01450   }
01451   vil_image_resource_sptr neg = brip_vil_float_ops::negate(img);
01452   if (neg)
01453     this->add_image(neg);
01454 }
01455 
01456 void segv_vil_segmentation_manager::entropy()
01457 {
01458   vgui_dialog entropy_dlg("Entropy of Image");
01459   static unsigned xrad = 15, yrad = 15, step = 10;
01460   static float sigma = 1.0f;
01461   static bool inten = true;
01462   static bool grad = true;
01463   static bool color = false;
01464   entropy_dlg.field("Region x radius",xrad);
01465   entropy_dlg.field("Region y radius",yrad);
01466   entropy_dlg.field("Step Size", step);
01467   entropy_dlg.field("Sigma", sigma);
01468   entropy_dlg.checkbox("Intensity", inten);
01469   entropy_dlg.checkbox("Gradient", grad);
01470   entropy_dlg.checkbox("Color", color);
01471   if (!entropy_dlg.ask())
01472     return;
01473   bgui_image_tableau_sptr itab = this->selected_image_tab();
01474   vil_image_resource_sptr img = itab->get_image_resource();
01475   vil_image_view<float> entropy =
01476     brip_vil_float_ops::entropy(xrad, yrad, step, img,
01477                                 sigma, inten, grad, color);
01478   vil_image_view<unsigned char> cent =
01479     brip_vil_float_ops::convert_to_byte(entropy);
01480 
01481   this->add_image(vil_new_image_resource_of_view(cent));
01482 }
01483 
01484 void segv_vil_segmentation_manager::minfo()
01485 {
01486   vgui_dialog minfo_dlg("Minfo of Image");
01487   static unsigned xrad = 15, yrad = 15, step = 10;
01488   static float sigma = 1.0f;
01489   static bool inten = true;
01490   static bool grad = true;
01491   static bool color = false;
01492   minfo_dlg.field("Region x radius",xrad);
01493   minfo_dlg.field("Region y radius",yrad);
01494   minfo_dlg.field("Step Size", step);
01495   minfo_dlg.field("Sigma", sigma);
01496   minfo_dlg.checkbox("Intensity", inten);
01497   minfo_dlg.checkbox("Gradient", grad);
01498   minfo_dlg.checkbox("Color", color);
01499   if (!minfo_dlg.ask())
01500     return;
01501   vil_image_resource_sptr img0 = this->image_at(0,0);
01502   vil_image_resource_sptr img1 = this->image_at(1,0);
01503   if (!img0||!img1)
01504   {
01505     vcl_cout << "In segv_vil_segmentation_manager::minfo() -"
01506              << " one or both input images are null\n";
01507     return;
01508   }
01509   vil_image_view<float> MI0, MI1;
01510   if (!brip_vil_float_ops::minfo(xrad, yrad, step, img0, img1, MI0, MI1,
01511                                 sigma, inten, grad, color))
01512     return;
01513   vil_image_view<unsigned char> MI0_char =
01514     brip_vil_float_ops::convert_to_byte(MI0);
01515 
01516   vil_image_view<unsigned char> MI1_char =
01517     brip_vil_float_ops::convert_to_byte(MI1);
01518 
01519   this->add_image_at(vil_new_image_resource_of_view(MI0_char), 0, 0);
01520   this->add_image_at(vil_new_image_resource_of_view(MI1_char), 1, 0);
01521 }
01522 
01523 void segv_vil_segmentation_manager::rotate_image()
01524 {
01525   vil_image_resource_sptr img = selected_image();
01526   if (!img)
01527   {
01528     vcl_cout << "In segv_vil_segmentation_manager::rotate_image - no image\n";
01529     return;
01530   }
01531   vil_image_view<float> flt =
01532     brip_vil_float_ops::convert_to_float(img);
01533   static double angle = 0;
01534   vgui_dialog rotate_dialog("Rotate Image");
01535   rotate_dialog.field("Rotation Angle (deg)", angle);
01536   if (!rotate_dialog.ask())
01537     return;
01538 
01539   vil_image_view<float> temp = brip_vil_float_ops::rotate(flt, angle);
01540 #if 0
01541   vil_image_view<unsigned char> tempr =
01542     brip_vil_float_ops::convert_to_byte(temp, 0, 255);
01543 #endif
01544   vil_image_resource_sptr out_image = vil_new_image_resource_of_view(temp);
01545   this->add_image(out_image);
01546 }
01547 
01548 void segv_vil_segmentation_manager::reduce_image()
01549 {
01550   vil_image_resource_sptr img = selected_image();
01551   if (!img)
01552   {
01553     vcl_cout << "In segv_vil_segmentation_manager::reduce_image - no image\n";
01554     return;
01555   }
01556   static float coef=0.6f;
01557   vgui_dialog expand_dialog("Reduce Image");
01558   expand_dialog.field("Filter coef", coef);
01559   if (!expand_dialog.ask())
01560     return;
01561 
01562   vil_image_view<float> flt =
01563     brip_vil_float_ops::convert_to_float(img);
01564 
01565   vil_image_view<float> reduced =
01566     brip_vil_float_ops::half_resolution(flt, coef);
01567 
01568   vil_image_resource_sptr out_image = vil_new_image_resource_of_view(reduced);
01569   this->add_image(out_image);
01570 }
01571 
01572 void segv_vil_segmentation_manager::reduce_image_bicubic()
01573 {
01574   vil_image_resource_sptr img = selected_image();
01575   if (!img)
01576   {
01577     vcl_cout << "In segv_vil_segmentation_manager::reduce_image_bicubic - no image\n";
01578     return;
01579   }
01580   vil_image_view<float> flt =
01581     brip_vil_float_ops::convert_to_float(img);
01582 
01583   vil_image_view<float> reduced;
01584   vil_resample_bicub(flt, reduced, flt.ni()/2, flt.nj()/2);
01585 
01586   vil_image_resource_sptr out_image =
01587     vil_new_image_resource_of_view(reduced);
01588   this->add_image(out_image);
01589 }
01590 
01591 void segv_vil_segmentation_manager::expand_image()
01592 {
01593   vil_image_resource_sptr img = selected_image();
01594   if (!img)
01595   {
01596     vcl_cout << "In segv_vil_segmentation_manager::expand_image - no image\n";
01597     return;
01598   }
01599   static float coef=0.6f;
01600   vgui_dialog expand_dialog("Expand Image");
01601   expand_dialog.field("Filter coef", coef);
01602   if (!expand_dialog.ask())
01603     return;
01604 
01605   vil_image_view<float> flt =
01606     brip_vil_float_ops::convert_to_float(img);
01607 
01608   vil_image_view<float> expanded =
01609     brip_vil_float_ops::double_resolution(flt, coef);
01610 
01611   vil_image_resource_sptr out_image = vil_new_image_resource_of_view(expanded);
01612   this->add_image(out_image);
01613 }
01614 
01615 void segv_vil_segmentation_manager::expand_image_bicubic()
01616 {
01617   vil_image_resource_sptr img = selected_image();
01618   if (!img)
01619   {
01620     vcl_cout << "In segv_vil_segmentation_manager::expand_image_bicubic - no image\n";
01621     return;
01622   }
01623   vil_image_view<float> flt =
01624     brip_vil_float_ops::convert_to_float(img);
01625 
01626   vil_image_view<float> expanded;
01627   vil_resample_bicub(flt, expanded, 2*flt.ni(), 2*flt.nj());
01628 
01629   vil_image_resource_sptr out_image = vil_new_image_resource_of_view(expanded);
01630   this->add_image(out_image);
01631 }
01632 
01633 void segv_vil_segmentation_manager::flip_image_lr()
01634 {
01635   vil_image_resource_sptr img = selected_image();
01636   if (!img)
01637   {
01638     vcl_cout << "In segv_vil_segmentation_manager::flip_image - no image\n";
01639     return;
01640   }
01641 
01642   vil_image_resource_sptr flipr = vil_flip_lr(img);
01643   vil_image_resource_sptr flipc = vil_new_image_resource(img->ni(), img->nj(),
01644                                                          flipr);
01645   vil_copy_deep(flipr, flipc);
01646   this->add_image(flipc);
01647 }
01648 
01649 void segv_vil_segmentation_manager::max_trace_scale()
01650 {
01651   static double scale_ratio = vnl_math::sqrt2;
01652   static double max_scale = 16.0f;
01653   vgui_dialog scale_dialog("Max Trace Scale");
01654   scale_dialog.field("Scale Ratio", scale_ratio);
01655   scale_dialog.field("Maximum Scale", max_scale);
01656   if (!scale_dialog.ask())
01657     return;
01658 
01659   vil_image_resource_sptr img = selected_image();
01660   if (!img)
01661   {
01662     vcl_cout<< "In segv_vil_segmentation_manager::max_trace_scale - no image\n";
01663     return;
01664   }
01665   vil_image_view<float> fimg = brip_vil_float_ops::convert_to_float(img);
01666   vil_image_view<float> scale_image;
01667   brip_max_scale_response<float> msr(fimg, scale_ratio, max_scale);
01668   scale_image = msr.scale_base();
01669   this->add_image(vil_new_image_resource_of_view(scale_image));
01670 }
01671 
01672 void segv_vil_segmentation_manager::color_order()
01673 {
01674   vil_image_resource_sptr img = selected_image();
01675   if (!img)
01676   {
01677     vcl_cout<< "In segv_vil_segmentation_manager::color order - no image\n";
01678     return;
01679   }
01680   static float equal_tol = 0.1f;
01681   vgui_dialog order_dialog("Color Order");
01682   order_dialog.field("Equal Tol", equal_tol);
01683   if (!order_dialog.ask())
01684     return;
01685 
01686   vil_image_view_base_sptr vb = img->get_view();
01687 
01688   //retains the image as color
01689   vil_image_view<float> fimg = *vil_convert_cast(float(), vb);
01690   //scale to 0,1 so tolerance is meaningful
01691   if (vb->pixel_format() == VIL_PIXEL_FORMAT_BYTE)
01692     vil_math_scale_values(fimg,1.0/255.0);
01693 
01694   vil_image_view<unsigned char> order_codes =
01695     brip_vil_float_ops::color_order(fimg, equal_tol);
01696 
01697   this->add_image(vil_new_image_resource_of_view(order_codes));
01698 }
01699 
01700 void segv_vil_segmentation_manager::create_polygon()
01701 {
01702   bgui_picker_tableau_sptr ptab = selected_picker_tab();
01703   if (!ptab){
01704     vcl_cerr << "In segv_vil_segmentation_managerd::create_polygon() - "
01705              << "no picker tableau\n";
01706     return;
01707   }
01708   vsol_polygon_2d_sptr poly2d;
01709   ptab->pick_polygon(poly2d);
01710   if (!poly2d)
01711   {
01712     vcl_cerr << "In segv_vil_segmentation_manager::create_polygon() - "
01713              << "picking failed\n";
01714     return;
01715   }
01716   bgui_vtol2D_tableau_sptr btab = selected_vtol2D_tab();
01717   if (!btab){
01718     vcl_cerr << "In segv_vil_segmentation_managerd::create_polygon() - "
01719              << "no vtol2D tableau\n";
01720     return;
01721   }
01722 //  btab->add(poly2d);
01723   mask_.push_back(poly2d);
01724   btab->post_redraw();
01725 }
01726 
01727 void segv_vil_segmentation_manager::clear_mask()
01728 {
01729   mask_.clear();
01730 }
01731 
01732 void segv_vil_segmentation_manager::save_mask()
01733 {
01734   mask_.clear();
01735 }
01736 
01737 void segv_vil_segmentation_manager::mser_conics()
01738 {
01739   vgui_dialog mser_dialog("Fit overlay Conics");
01740   static vcl_string conic_filename = "";
01741   static vcl_string ext = "*.*";
01742   mser_dialog.file("MSER Conic Filename:", ext, conic_filename);
01743   if (!mser_dialog.ask())
01744     return;
01745   vcl_ifstream istr(conic_filename.c_str());
01746   if (!istr.is_open())
01747     return;
01748   vcl_vector<vsol_conic_2d_sptr> conics;
01749 #if 0 //check in later
01750   sdet_read_mser_regions::read_mser_conics(istr, conics);
01751 #endif
01752   if (!conics.size())
01753     return;
01754   this->draw_conics(conics);
01755 }
01756 
01757 void segv_vil_segmentation_manager::image_as_vrml_points()
01758 {
01759   vil_image_resource_sptr img = selected_image();
01760   if (!img)
01761   {
01762     vcl_cout<< "In segv_vil_segmentation_manager::image_as_vrml_points - no image\n";
01763     return;
01764   }
01765   vgui_dialog vrml_dialog("VRML Intensity Display");
01766   static vcl_string vrml_filename = "";
01767   static vcl_string ext = "*.*";
01768   vrml_dialog.file("VRML Filename:", ext, vrml_filename);
01769   if (!vrml_dialog.ask())
01770     return;
01771   vcl_ofstream ostr(vrml_filename.c_str());
01772   if (!ostr.is_open())
01773     return;
01774   vil_image_view<float> fimg =
01775     brip_vil_float_ops::convert_to_float(img);
01776 #if 0
01777   sdet_vrml_display::write_vrml_header(ostr);
01778   sdet_vrml_display::write_vrml_height_map(ostr, fimg);
01779 #endif
01780 }
01781 
01782 void segv_vil_segmentation_manager::extrema()
01783 {
01784   vil_image_resource_sptr img = selected_image();
01785   if (!img)
01786   {
01787     vcl_cout<< "In segv_vil_segmentation_manager::extrema - no image\n";
01788     return;
01789   }
01790   static float lambda0 = 1.0f;
01791   static float lambda1 = 1.0f;
01792   static float theta = 0.0f;
01793   static bool bright = true;
01794   static bool color_overlay = true;
01795   static bool fast = true;
01796   static int choice = 1;
01797   vgui_dialog extrema_dialog("Detect Extrema");
01798   extrema_dialog.field("lambda0",lambda0);
01799   extrema_dialog.field("lambda1",lambda1);
01800   extrema_dialog.field("theta",theta);
01801   extrema_dialog.checkbox("Bright Extrema?(check)",bright);
01802   extrema_dialog.checkbox("ColorOverlay?(check)",color_overlay);
01803   extrema_dialog.choice("Display Mode", "Point Response Only",
01804                         "Point & Mask", "Point & Unclipped", choice);
01805   extrema_dialog.checkbox("Fast Alg.(check)", fast);
01806   if (!extrema_dialog.ask())
01807     return;
01808   vul_timer t;
01809   vil_image_view<float> fimg =
01810     brip_vil_float_ops::convert_to_float(img);
01811   vil_image_view<float> extr;
01812   bool output_mask = false, output_unclipped = false;
01813   if (choice == 1) output_mask = true;
01814   if (choice == 2) output_unclipped = true;
01815   if (fast)
01816     extr = brip_vil_float_ops::fast_extrema(fimg, lambda0, lambda1, theta, bright,
01817                                             output_mask, output_unclipped);
01818   else
01819     extr = brip_vil_float_ops::extrema(fimg, lambda0, lambda1, theta, bright,
01820                                        output_mask, output_unclipped);
01821 
01822   vcl_cout << "Extrema computation time " << t.real() << " msec\n";
01823   unsigned ni = extr.ni(), nj = extr.nj(), np = extr.nplanes();
01824   if (choice==0&&!color_overlay){
01825     if (np!=1)
01826       return;
01827     vil_image_resource_sptr resc = vil_new_image_resource_of_view(extr);
01828     this->add_image(resc);
01829     return;
01830   }
01831   if (choice==0&&color_overlay){
01832     if (np!=1)
01833       return;
01834     vil_image_resource_sptr resc = vil_new_image_resource_of_view(extr);
01835     vil_image_view<vil_rgb<vxl_byte> > rgb =
01836       brip_vil_float_ops::combine_color_planes(img, resc, img);
01837     this->add_image(vil_new_image_resource_of_view(rgb));
01838   }
01839   if (choice>0)
01840   {
01841     if (np!=2)
01842       return;
01843     vil_image_view<float> res(ni, nj), mask(ni, nj);
01844     for (unsigned j = 0; j<nj; ++j)
01845       for (unsigned i = 0; i<ni; ++i)
01846       {
01847         res(i,j) = extr(i,j,0);
01848         mask(i,j) = extr(i,j,1);
01849       }
01850     if (color_overlay){
01851       vil_image_resource_sptr res_resc = vil_new_image_resource_of_view(res);
01852       vil_image_resource_sptr msk_resc = vil_new_image_resource_of_view(mask);
01853       vil_image_view<vil_rgb<vxl_byte> > rgb =
01854         brip_vil_float_ops::combine_color_planes(img, res_resc, msk_resc);
01855       this->add_image(vil_new_image_resource_of_view(rgb));
01856     }
01857     if (output_mask&&!color_overlay)
01858     {
01859      this->add_image(vil_new_image_resource_of_view(res));
01860       this->add_image(vil_new_image_resource_of_view(mask));
01861     }
01862   }
01863 }
01864 
01865 void segv_vil_segmentation_manager::rot_extrema()
01866 {
01867   vil_image_resource_sptr img = selected_image();
01868   if (!img)
01869   {
01870     vcl_cout<< "In segv_vil_segmentation_manager::extrema - no image\n";
01871     return;
01872   }
01873   static float lambda0 = 1.0f;
01874   static float lambda1 = 1.0f;
01875   static float theta_inc = 0.0f;
01876   static bool bright = true;
01877   vgui_dialog extrema_dialog("Detect Extrema");
01878   extrema_dialog.field("lambda0",lambda0);
01879   extrema_dialog.field("lambda1",lambda1);
01880   extrema_dialog.field("theta increment",theta_inc);
01881   extrema_dialog.checkbox("Bright Extrema?(check)",bright);
01882   if (!extrema_dialog.ask())
01883     return;
01884   vul_timer t;
01885   vil_image_view<float> fimg = brip_vil_float_ops::convert_to_float(img);
01886   vil_image_view<float> output = brip_vil_float_ops::extrema_rotational(fimg, lambda0, lambda1, theta_inc, bright);
01887   unsigned ni = output.ni(), nj = output.nj();
01888   vil_image_view<float> res(ni, nj), mask(ni, nj);
01889   for (unsigned j = 0; j<nj; ++j)
01890     for (unsigned i = 0; i<ni; ++i)
01891     {
01892       res(i,j) = output(i,j,0);
01893       mask(i,j) = output(i,j,2);
01894     }
01895 
01896   vil_image_resource_sptr res_resc = vil_new_image_resource_of_view(res);
01897   vil_image_resource_sptr msk_resc = vil_new_image_resource_of_view(mask);
01898   vil_image_view<vil_rgb<vxl_byte> > rgb = brip_vil_float_ops::combine_color_planes(img, res_resc, msk_resc);
01899   this->add_image(vil_new_image_resource_of_view(rgb));
01900 }
01901 
01902 void segv_vil_segmentation_manager::beaudet()
01903 {
01904   vil_image_resource_sptr img = selected_image();
01905   if (!img)
01906   {
01907     vcl_cout<< "In segv_vil_segmentation_manager::beaudet - no image\n";
01908     return;
01909   }
01910   static float sigma = 1.0f;
01911   static bool determinant = true;
01912   vgui_dialog beaudet_dialog("beaudet");
01913   beaudet_dialog.field("sigma", sigma);
01914   beaudet_dialog.checkbox("Determinant(or Trace)", determinant);
01915   if (!beaudet_dialog.ask())
01916     return;
01917   int ni = img->ni(), nj = img->nj();
01918   vil_image_view<float> fimg = brip_vil_float_ops::convert_to_float(img);
01919   vil_image_view<float> smooth = brip_vil_float_ops::gaussian(fimg, sigma);
01920   vil_image_view<float> Ixx(ni,nj), Ixy(ni, nj), Iyy(ni, nj);
01921   brip_vil_float_ops::hessian_3x3(smooth, Ixx, Ixy, Iyy);
01922   vil_image_view<float> beau =
01923     brip_vil_float_ops::beaudet(Ixx, Ixy, Iyy, determinant);
01924   this->add_image(vil_new_image_resource_of_view(beau));
01925 }
01926 
01927 void segv_vil_segmentation_manager::parallel_coverage()
01928 {
01929   static brip_para_cvrg_params pcp;
01930   static bool combined=true;
01931   vgui_dialog para_dialog("Parallel Coverage");
01932   para_dialog.field("Sigma", pcp.sigma_);
01933   para_dialog.field("Projection Width", pcp.proj_width_);
01934   para_dialog.field("Projection Height", pcp.proj_height_);
01935   para_dialog.checkbox("Display Coverage and Direction Combined", combined);
01936   para_dialog.checkbox("Verbose", pcp.verbose_);
01937   if (!para_dialog.ask())
01938     return;
01939   vil_image_resource_sptr img = selected_image();
01940   brip_para_cvrg pc(pcp);
01941   pc.do_coverage(img);
01942   vil_image_resource_sptr cov_res;
01943   if (combined)
01944     cov_res = vil_new_image_resource_of_view(pc.get_combined_image());
01945   else
01946     cov_res = vil_new_image_resource_of_view(pc.get_detection_image());
01947   this->add_image(cov_res);
01948 }
01949 
01950 //: it recives and image of line definitions and draws the lines on the tableau.
01951 // The image is expected having three planes to save (x,y,theta) of each line. 
01952 // (x,y) is the position of the edge and theta is the direction angle in radians
01953 void segv_vil_segmentation_manager::draw_line_image()
01954 {
01955   vgui_dialog file_dlg("Edge File");
01956   static vcl_string filename = "";
01957   static vcl_string ext = "*.*";
01958   file_dlg.file("Edge filename:", ext, filename);
01959   if (!file_dlg.ask())
01960     return;
01961   vil_image_view_base_sptr img_sptr = vil_load(filename.c_str());
01962   if (img_sptr->pixel_format() == VIL_PIXEL_FORMAT_FLOAT) {
01963     vil_image_view<float> edge_image(img_sptr);
01964     if (edge_image.nplanes() < 3) {
01965       vcl_cout << "The image should have at least 3 planes" << vcl_endl;
01966       return;
01967     }
01968     bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
01969     if (!t2D)
01970       return;
01971 
01972     for (unsigned i=0; i<edge_image.ni(); i++) {
01973       for (unsigned j=0; j<edge_image.nj(); j++) {
01974         double x = edge_image(i,j,0);
01975         double y = edge_image(i,j,1);
01976         double theta = edge_image(i,j,2);
01977         double a,b;
01978         a = x-0.5*vcl_cos(theta);
01979         b = y-0.5*vcl_sin(theta);
01980         vgl_point_2d<double> p0(a,b);
01981         a = x+0.5*vcl_cos(theta); 
01982         b = y+0.5*vcl_sin(theta);
01983         vgl_point_2d<double> p1(a,b);
01984         // define a line
01985         vsol_line_2d_sptr line = new vsol_line_2d(p0,p1);
01986         t2D->add_vsol_line_2d(line);
01987       }
01988     }
01989   } else 
01990     vcl_cout << "Pixel format: " << img_sptr->pixel_format() << " is not implemented yet" << vcl_endl;
01991   
01992 }