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