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