1 // This is brl/bseg/segv/segv_vil_segmentation_manager.cxx
2 #include <iostream>
3 #include <cstdio>
4 #include <fstream>
5 #include "segv_vil_segmentation_manager.h"
6 //:
7 // \file
8 // \author J.L. Mundy
9 
10 #if 1 //JLM
11 #include <vpgl/file_formats/vpgl_nitf_rational_camera.h>
12 #endif
13 // include for project points menu option
14 #ifdef _MSC_VER
15 #  include "vcl_msvc_warnings.h"
16 #endif
17 #include "vul/vul_timer.h"
18 #include "vbl/vbl_array_2d.h"
19 #include "vnl/vnl_math.h"
20 #include "vil/vil_image_view.h"
21 #include "vil/vil_blocked_image_resource.h"
22 #include "vil/vil_pyramid_image_resource.h"
23 #include "vil/vil_load.h"
24 #include "vil/vil_save.h"
25 #include "vil/vil_new.h"
26 #include "vil/vil_copy.h"
27 #include "vil/vil_property.h"
28 #include "vil/vil_flip.h"
29 #include "vil/vil_resample_bicub.h"
30 #include "vil/vil_convert.h"
31 #include <vil/vil_config.h>
32 #if HAS_J2K
33 #include <vil/file_formats/vil_j2k_nitf2_pyramid_image_resource.h>
34 #include <vil/file_formats/vil_j2k_pyramid_image_resource.h>
35 #endif // HAS_J2K
36 #include <vil/algo/vil_sobel_1x3.h>
37 #include "vgl/vgl_vector_2d.h"
38 #include <sdet/sdet_detector_params.h>
39 #include <sdet/sdet_detector.h>
40 #include <sdet/sdet_harris_detector_params.h>
41 #include <sdet/sdet_harris_detector.h>
42 #include <sdet/sdet_nonmax_suppression_params.h>
43 #include <sdet/sdet_nonmax_suppression.h>
44 #include <sdet/sdet_fit_lines_params.h>
45 #include <sdet/sdet_fit_lines.h>
46 #include <sdet/sdet_fit_conics.h>
47 #include <sdet/sdet_grid_finder_params.h>
48 #include <sdet/sdet_grid_finder.h>
49 #include "vgui/vgui.h"
50 #include "vgui/vgui_find.h"
51 #include "vgui/vgui_tableau.h"
52 #include "vgui/vgui_dialog.h"
53 #include "vgui/vgui_style_sptr.h"
54 #include "vgui/vgui_style.h"
55 #include "vgui/vgui_viewer2D_tableau.h"
56 #include "vgui/vgui_shell_tableau.h"
57 #include "vgui/vgui_grid_tableau.h"
58 #include "vgui/vgui_range_map_params.h"
59 #include <bgui/bgui_image_tableau.h>
60 #include <bgui/bgui_vtol2D_tableau.h>
61 #include <bgui/bgui_picker_tableau.h>
62 #include <bgui/bgui_range_adjuster_tableau.h>
63 #include <bgui/bgui_image_utils.h>
64 #include <vsol/vsol_point_2d.h>
65 #include <vsol/vsol_point_2d_sptr.h>
66 #include <vsol/vsol_box_2d.h>
67 #include <vsol/vsol_box_2d_sptr.h>
68 #include <vsol/vsol_conic_2d.h>
69 #include <vsol/vsol_polyline_2d_sptr.h>
70 #include <vsol/vsol_polyline_2d.h>
71 #include <vsol/vsol_polygon_2d.h>
72 #include <vsol/vsol_polygon_2d_sptr.h>
73 #include <vtol/vtol_vertex_2d.h>
74 #include <vtol/vtol_vertex.h>
75 #include <vtol/vtol_edge_2d.h>
76 #include <vtol/vtol_intensity_face.h>
77 #include <brip/brip_vil1_float_ops.h>
78 #include <brip/brip_vil_float_ops.h>
79 #include <brip/brip_para_cvrg_params.h>
80 #include <brip/brip_roi.h>
81 #include <brip/brip_para_cvrg.h>
82 #include <brip/brip_watershed_params.h>
83 #include <brip/brip_max_scale_response.h>
84 #include <sdet/sdet_vrml_display.h>
85 #include <sdet/sdet_watershed_region_proc_params.h>
86 #include <sdet/sdet_watershed_region_proc.h>
87 #include <sdet/sdet_region_proc_params.h>
88 #include <sdet/sdet_region_proc.h>
89 #include <sdet/sdet_third_order_edge_det_params.h>
90 #include <sdet/sdet_third_order_edge_det.h>
91 segv_vil_segmentation_manager *segv_vil_segmentation_manager::instance_ = nullptr;
92 
instance()93 segv_vil_segmentation_manager *segv_vil_segmentation_manager::instance()
94 {
95   if (!instance_)
96   {
97     instance_ = new segv_vil_segmentation_manager();
98     instance_->init();
99   }
100   return segv_vil_segmentation_manager::instance_;
101 }
102 
103 //-----------------------------------------------------------
104 // constructors/destructor
105 //
segv_vil_segmentation_manager()106 segv_vil_segmentation_manager::segv_vil_segmentation_manager():vgui_wrapper_tableau()
107 {
108   first_ = true;
109 }
110 
~segv_vil_segmentation_manager()111 segv_vil_segmentation_manager::~segv_vil_segmentation_manager()
112 {
113 }
114 
115 //: Set up the tableaux
init()116 void segv_vil_segmentation_manager::init()
117 {
118   bgui_image_tableau_sptr itab = bgui_image_tableau_new();
119   bgui_vtol2D_tableau_sptr t2D = bgui_vtol2D_tableau_new(itab);
120   bgui_picker_tableau_sptr picktab = bgui_picker_tableau_new(t2D);
121   vgui_viewer2D_tableau_sptr v2D = vgui_viewer2D_tableau_new(picktab);
122   grid_ = vgui_grid_tableau_new(1,1);
123   grid_->set_grid_size_changeable(true);
124   grid_->add_at(v2D, 0, 0);
125   vgui_shell_tableau_sptr shell = vgui_shell_tableau_new(grid_);
126   this->add_child(shell);
127   first_ = true;
128 }
129 
130 //: Calculate the range parameters for the input image
131 vgui_range_map_params_sptr segv_vil_segmentation_manager::
range_params(vil_image_resource_sptr const & image)132 range_params(vil_image_resource_sptr const& image)
133 {
134   float gamma = 1.0;
135   bool invert = false;
136   bool gl_map = false;
137   bool cache = true;
138 
139   //Check if the image is blocked
140   vil_blocked_image_resource_sptr bir = blocked_image_resource(image);
141   if (bir)
142   { gl_map = true; cache = true;}
143 
144   //Check if the image is a pyramid
145   bool pyr = image->get_property(vil_property_pyramid, nullptr);
146   if (pyr)
147   { gl_map = true; cache = true;}
148 
149   bgui_image_utils iu(image);
150   iu.set_percent_limit(0.001);
151 
152   vgui_range_map_params_sptr rmps;
153   if (iu.range_map_from_hist((float)gamma, invert, gl_map, cache, rmps))
154     return rmps;
155   if (iu.default_range_map(rmps, gamma, invert, gl_map, cache))
156     return rmps;
157   return nullptr;
158 }
159 
160 //: set the image at the currently selected grid cell
161 void segv_vil_segmentation_manager::
set_selected_grid_image(vil_image_resource_sptr const & image,vgui_range_map_params_sptr const & rmps)162 set_selected_grid_image(vil_image_resource_sptr const& image,
163                         vgui_range_map_params_sptr const& rmps)
164 {
165   bgui_image_tableau_sptr itab = this->selected_image_tab();
166   if (!itab)
167     this->add_image(image, rmps);
168   else
169   {
170     itab->set_image_resource(image);
171     itab->set_mapping(rmps);
172   }
173   itab->post_redraw();
174 }
175 
176 //: Add an image at the specified grid cell
177 void segv_vil_segmentation_manager::
add_image_at(vil_image_resource_sptr const & image,const unsigned col,const unsigned row,vgui_range_map_params_sptr const & rmps)178 add_image_at(vil_image_resource_sptr const& image,
179              const unsigned col, const unsigned row,
180              vgui_range_map_params_sptr const& rmps)
181 {
182   vgui_range_map_params_sptr rmap = rmps;
183   if (!rmps)
184     rmap = range_params(image);
185   bgui_image_tableau_sptr itab = bgui_image_tableau_new(image,rmps);
186   //  itab->set_mapping(rmap);
187   bgui_vtol2D_tableau_sptr t2D = bgui_vtol2D_tableau_new(itab);
188   bgui_picker_tableau_sptr picktab = bgui_picker_tableau_new(t2D);
189   vgui_viewer2D_tableau_sptr v2D = vgui_viewer2D_tableau_new(picktab);
190   grid_->add_at(v2D, col, row);
191   itab->post_redraw();
192 }
193 
194 //: Add an image to the currently selected grid cell
195 void segv_vil_segmentation_manager::
add_image(vil_image_resource_sptr const & image,vgui_range_map_params_sptr const & rmps)196 add_image(vil_image_resource_sptr const& image,
197           vgui_range_map_params_sptr const& rmps)
198 {
199   unsigned row=0, col=0;
200   grid_->get_last_selected_position(&col, &row);
201   this->add_image_at(image, col, row, rmps);
202 }
203 
204 //-----------------------------------------------------------------------------
205 //: remove the selected image
206 //-----------------------------------------------------------------------------
remove_image()207 void segv_vil_segmentation_manager::remove_image()
208 {
209   unsigned row=0, col=0;
210   grid_->get_last_selected_position(&col, &row);
211   grid_->remove_at(col, row);
212 }
213 
convert_to_grey()214 void segv_vil_segmentation_manager::convert_to_grey()
215 {
216   vil_image_resource_sptr img = this->selected_image();
217   if (!img)
218     return;
219   vil_image_view<unsigned char> grey =
220     brip_vil_float_ops::convert_to_byte(img);
221   vil_image_resource_sptr gimg = vil_new_image_resource_of_view(grey);
222   this->add_image(gimg);
223 }
224 
225 //: Get the image tableau for the currently selected grid cell
selected_image_tab()226 bgui_image_tableau_sptr segv_vil_segmentation_manager::selected_image_tab()
227 {
228   unsigned row=0, col=0;
229   grid_->get_last_selected_position(&col, &row);
230   vgui_tableau_sptr top_tab = grid_->get_tableau_at(col, row);
231   if (top_tab)
232   {
233     bgui_image_tableau_sptr itab;
234     itab.vertical_cast(vgui_find_below_by_type_name(top_tab,
235                                                     std::string("vgui_image_tableau")));
236     if (itab)
237       return itab;
238   }
239   std::cout << "Unable to get bgui_image_tableau at (" << col
240            << ", " << row << ")\n";
241   return bgui_image_tableau_sptr();
242 }
243 
244 //: Get the vtol2D tableau at the specified grid cell
245 bgui_vtol2D_tableau_sptr
vtol2D_tab_at(const unsigned col,const unsigned row)246 segv_vil_segmentation_manager::vtol2D_tab_at(const unsigned col,
247                                              const unsigned row)
248 {
249   vgui_tableau_sptr top_tab = grid_->get_tableau_at(col, row);
250   if (top_tab)
251   {
252     bgui_vtol2D_tableau_sptr v2D;
253     v2D.vertical_cast(vgui_find_below_by_type_name(top_tab,
254                                                    std::string("bgui_vtol2D_tableau")));
255     if (v2D)
256       return v2D;
257   }
258   std::cout << "Unable to get bgui_vtol2D_tableau at (" << col
259            << ", " << row << ")\n";
260   return bgui_vtol2D_tableau_sptr();
261 }
262 
263 //: Get the vtol2D tableau for the currently selected grid cell
selected_vtol2D_tab()264 bgui_vtol2D_tableau_sptr segv_vil_segmentation_manager::selected_vtol2D_tab()
265 {
266   unsigned row=0, col=0;
267   grid_->get_last_selected_position(&col, &row);
268   return this->vtol2D_tab_at(col, row);
269 }
270 
271 //: Get the picker tableau for the currently selected grid cell
selected_picker_tab()272 bgui_picker_tableau_sptr segv_vil_segmentation_manager::selected_picker_tab()
273 {
274   unsigned row=0, col=0;
275   grid_->get_last_selected_position(&col, &row);
276   vgui_tableau_sptr top_tab = grid_->get_tableau_at(col, row);
277   if (top_tab)
278   {
279     bgui_picker_tableau_sptr pick;
280     pick.vertical_cast(vgui_find_below_by_type_name(top_tab,
281                                                     std::string("bgui_picker_tableau")));
282     if (pick)
283       return pick;
284   }
285   std::cout << "Unable to get bgui_picker_tableau at (" << col
286            << ", " << row << ")\n";
287   return bgui_picker_tableau_sptr();
288 }
289 
290 
selected_image()291 vil_image_resource_sptr segv_vil_segmentation_manager::selected_image()
292 {
293   bgui_image_tableau_sptr itab = this->selected_image_tab();
294   if (!itab)
295     return nullptr;
296   return itab->get_image_resource();
297 }
298 
image_at(const unsigned col,const unsigned row)299 vil_image_resource_sptr segv_vil_segmentation_manager::image_at(const unsigned col,
300                                                                 const unsigned row)
301 {
302   vgui_tableau_sptr top_tab = grid_->get_tableau_at(col, row);
303   if (!top_tab)
304     return nullptr;
305 
306   bgui_image_tableau_sptr itab;
307   itab.vertical_cast(vgui_find_below_by_type_name(top_tab,
308                                                   std::string("vgui_image_tableau")));
309   if (!itab)
310   {
311     std::cout << "Unable to get bgui_image_tableau at (" << col
312              << ", " << row << ")\n";
313     return nullptr;
314   }
315   return itab->get_image_resource();
316 }
317 
318 //-----------------------------------------------------------------------------
319 //: Clear spatial objects from the selected display
320 //-----------------------------------------------------------------------------
clear_display()321 void segv_vil_segmentation_manager::clear_display()
322 {
323   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
324   if (!t2D)
325     return;
326   t2D->clear_all();
327 }
328 
329 //-----------------------------------------------------------------------------
330 //: Clear spatial objects from all spatial panes
331 //-----------------------------------------------------------------------------
clear_all()332 void segv_vil_segmentation_manager::clear_all()
333 {
334   unsigned ncols = grid_->cols(), nrows = grid_->rows();
335   for (unsigned r=0; r<nrows; ++r)
336     for (unsigned c=0; c<ncols; ++c)
337     {
338       bgui_vtol2D_tableau_sptr t = this->vtol2D_tab_at(c, r);
339       if (t)
340         t->clear_all();
341     }
342 }
343 
344 //-----------------------------------------------------------------------------
345 //: Draw edges onto the tableau
346 //-----------------------------------------------------------------------------
347 void
draw_edges(std::vector<vtol_edge_2d_sptr> & edges,bool verts)348 segv_vil_segmentation_manager::draw_edges(std::vector<vtol_edge_2d_sptr>& edges,
349                                           bool verts)
350 {
351   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
352   if (!t2D)
353     return;
354   this->clear_display();
355 #if 0
356   vgui_image_tableau_sptr itab = t2D->get_image_tableau();
357   if (!itab)
358   {
359     std::cout << "In segv_vil_segmentation_manager::draw_edges - null image tab\n";
360     return;
361   }
362 #endif
363   for (std::vector<vtol_edge_2d_sptr>::iterator eit = edges.begin();
364        eit != edges.end(); eit++)
365   {
366     t2D->add_edge(*eit);
367     //optionally display the edge vertices
368     if (verts)
369     {
370       if ((*eit)->v1())
371       {
372         vtol_vertex_2d_sptr v1 = (*eit)->v1()->cast_to_vertex_2d();
373         t2D->add_vertex(v1);
374       }
375       if ((*eit)->v2())
376       {
377         vtol_vertex_2d_sptr v2 = (*eit)->v2()->cast_to_vertex_2d();
378         t2D->add_vertex(v2);
379       }
380     }
381   }
382   t2D->post_redraw();
383 }
384 
385 
386 //-----------------------------------------------------------------------------
387 //: Draw polylines on the tableau
388 //-----------------------------------------------------------------------------
389 void segv_vil_segmentation_manager::
draw_polylines(std::vector<vsol_polyline_2d_sptr> const & polys)390 draw_polylines(std::vector<vsol_polyline_2d_sptr > const& polys)
391 {
392   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
393   if (!t2D)
394     return;
395   //this->clear_display();
396   vgui_image_tableau_sptr itab = t2D->get_image_tableau();
397   if (!itab)
398   {
399     std::cout << "In segv_vil_segmentation_manager::draw_edges - null image tab\n";
400     return;
401   }
402   for (std::vector<vsol_polyline_2d_sptr>::const_iterator pit = polys.begin();
403        pit != polys.end(); pit++)
404   {
405     t2D->add_vsol_polyline_2d(*pit);
406   }
407 
408   t2D->post_redraw();
409 }
410 
411 //-----------------------------------------------------------------------------
412 //: Draw line segments on the tableau
413 //-----------------------------------------------------------------------------
414 void segv_vil_segmentation_manager::
draw_lines(std::vector<vsol_line_2d_sptr> const & lines,const vgui_style_sptr & style)415 draw_lines(std::vector<vsol_line_2d_sptr > const& lines,
416            const vgui_style_sptr& style)
417 {
418   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
419   if (!t2D)
420     return;
421   //this->clear_display();
422 #if 0
423   vgui_image_tableau_sptr itab = t2D->get_image_tableau();
424   if (!itab)
425   {
426     std::cout << "In segv_vil_segmentation_manager::draw_edges - null image tab\n";
427     return;
428   }
429 #endif
430   for (std::vector<vsol_line_2d_sptr>::const_iterator lit = lines.begin();
431        lit != lines.end(); lit++)
432   {
433     t2D->add_vsol_line_2d(*lit,style);
434   }
435 
436   t2D->post_redraw();
437 }
438 
439 //-----------------------------------------------------------------------------
440 //: Draw conic segments on the tableau
441 //-----------------------------------------------------------------------------
442 void segv_vil_segmentation_manager::
draw_conics(std::vector<vsol_conic_2d_sptr> const & conics,const vgui_style_sptr & style)443 draw_conics(std::vector<vsol_conic_2d_sptr > const& conics,
444             const vgui_style_sptr& style)
445 {
446   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
447   if (!t2D)
448     return;
449   //this->clear_display();
450 #if 0
451   vgui_image_tableau_sptr itab = t2D->get_image_tableau();
452   if (!itab)
453   {
454     std::cout << "In segv_vil_segmentation_manager::draw_edges - null image tab\n";
455     return;
456   }
457 #endif
458   for (std::vector<vsol_conic_2d_sptr>::const_iterator lit = conics.begin();
459        lit != conics.end(); lit++)
460   {
461     t2D->add_vsol_conic_2d(*lit,style);
462   }
463   t2D->post_redraw();
464 }
465 
466 //-----------------------------------------------------------------------------
467 //: Draw points on the tableau
468 //-----------------------------------------------------------------------------
469 void segv_vil_segmentation_manager::
draw_points(std::vector<vsol_point_2d_sptr> const & points,const vgui_style_sptr & style)470 draw_points(std::vector<vsol_point_2d_sptr> const& points, const vgui_style_sptr& style)
471 {
472   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
473   if (!t2D)
474     return;
475   //this->clear_display();
476 #if 0
477   vgui_image_tableau_sptr itab = t2D->get_image_tableau();
478   if (!itab)
479   {
480     std::cout << "In segv_vil_segmentation_manager::draw_edges - null image tab\n";
481     return;
482   }
483 #endif
484   for (std::vector<vsol_point_2d_sptr>::const_iterator pit = points.begin();
485        pit != points.end(); pit++)
486   {
487     t2D->add_vsol_point_2d(*pit,style);
488   }
489 
490   t2D->post_redraw();
491 }
492 
draw_regions(std::vector<vtol_intensity_face_sptr> & regions,bool verts)493 void segv_vil_segmentation_manager::draw_regions(std::vector<vtol_intensity_face_sptr>& regions,
494                                                  bool verts)
495 {
496   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
497   if (!t2D)
498     return;
499   for (std::vector<vtol_intensity_face_sptr>::iterator rit = regions.begin();
500        rit != regions.end(); rit++)
501   {
502     vtol_face_2d_sptr f = (*rit)->cast_to_face_2d();
503     t2D->add_face(f);
504     if (verts)
505     {
506       std::vector<vtol_vertex_sptr> vts;
507       f->vertices(vts);
508       for (std::vector<vtol_vertex_sptr>::iterator vit = vts.begin();
509            vit != vts.end(); vit++)
510       {
511         vtol_vertex_2d_sptr v = (*vit)->cast_to_vertex_2d();
512         t2D->add_vertex(v);
513       }
514     }
515   }
516   t2D->post_redraw();
517 }
518 
quit()519 void segv_vil_segmentation_manager::quit()
520 {
521   this->clear_all();
522   vgui::quit();
523 }
524 
load_image()525 void segv_vil_segmentation_manager::load_image()
526 {
527   static bool greyscale = false;
528   static bool sblock = false;
529   vgui_dialog load_image_dlg("Load image file");
530   static std::string image_filename = "/home/dec/images/cal_image1.tif";
531   static std::string ext = "*.*";
532   load_image_dlg.file("Image Filename:", ext, image_filename);
533   load_image_dlg.checkbox("greyscale ", greyscale);
534   load_image_dlg.checkbox("blocked?:", sblock);
535   if (!load_image_dlg.ask())
536     return;
537   //first check to see if the image is a pyramid
538   bool pyrm = false;
539   vil_image_resource_sptr image;
540   vil_pyramid_image_resource_sptr pyr =
541       vil_load_pyramid_resource(image_filename.c_str(), false);
542   if (pyr)
543   {
544     image = pyr.ptr();
545     pyrm = true;
546   }
547 
548   if (!image)
549     image = vil_load_image_resource(image_filename.c_str(), false);
550 
551   if (!image) {
552     std::cout << "Failed to load image path " << image_filename << '\n';
553     return;
554   }
555 #if HAS_J2K
556   // determine if the image can be made into a J2K-nitf pyramid
557   char const* fmtp = image->file_format();
558   std::string file_fmt = "";
559   if (fmtp) file_fmt = fmtp;//fmtp can be 0 for undefined formats
560   if (file_fmt == "nitf21")
561   {
562     vil_nitf2_image* nitf_resc = static_cast<vil_nitf2_image*>(image.ptr());
563     if (nitf_resc->is_jpeg_2000_compressed())
564     {
565       vil_j2k_nitf2_pyramid_image_resource* j2k_nitf =
566         new vil_j2k_nitf2_pyramid_image_resource(image);
567       image = j2k_nitf;
568       pyrm = true;
569     }
570   }
571   else if (file_fmt == "j2k") {
572     vil_j2k_pyramid_image_resource* j2k_pyr =
573       new vil_j2k_pyramid_image_resource(image);
574     image = j2k_pyr;
575     pyrm = true;
576   }
577 #endif //HAS_J2K
578   if (greyscale&&!pyrm)
579   {
580     vil_image_view<unsigned char> grey_view =
581       brip_vil_float_ops::convert_to_grey(*image);
582     image = vil_new_image_resource_of_view(grey_view);
583   }
584 
585   if (sblock&&!pyrm)
586   {
587     vil_blocked_image_resource_sptr bimage = vil_new_blocked_image_facade(image);
588     image = (vil_image_resource*)(vil_new_cached_image_resource(bimage)).ptr();
589   }
590 
591   vgui_range_map_params_sptr rmps = range_params(image);
592 
593   if (first_)
594   {
595     this->set_selected_grid_image(image, rmps);
596     first_ = false;
597   }
598   else
599     this->add_image(image, rmps);
600 }
601 
load_image_nomenu(std::string const & path)602 void segv_vil_segmentation_manager::load_image_nomenu(std::string const& path)
603 {
604   bool pyrm = false;
605   vil_image_resource_sptr image;
606   vil_pyramid_image_resource_sptr pyr =
607       vil_load_pyramid_resource(path.c_str(), false);
608   if (pyr)
609   {
610     image = pyr.ptr();
611     pyrm = true;
612   }
613 
614   if (!image)
615     image = vil_load_image_resource(path.c_str(), false);
616 
617   if (!image) {
618     std::cout << "Failed to load image path " << path << '\n';
619     return;
620   }
621 #if HAS_J2K
622 // determine if the image can be made into a J2K-nitf pyramid
623   char const* fmtp = image->file_format();
624   std::string file_fmt = "";
625   if (fmtp) file_fmt = fmtp;//fmtp can be 0 for undefined formats
626   if (file_fmt == "nitf21")
627   {
628     vil_nitf2_image* nitf_resc = static_cast<vil_nitf2_image*>(image.ptr());
629     if (nitf_resc->is_jpeg_2000_compressed())
630     {
631       vil_j2k_nitf2_pyramid_image_resource* j2k_nitf =
632         new vil_j2k_nitf2_pyramid_image_resource(image);
633       image = j2k_nitf;
634       pyrm = true;
635     }
636   }
637   else if (file_fmt == "j2k") {
638     vil_j2k_pyramid_image_resource* j2k_pyr =
639       new vil_j2k_pyramid_image_resource(image);
640     image = j2k_pyr;
641     pyrm = true;
642   }
643 #endif //HAS_J2K
644   vgui_range_map_params_sptr rmps = range_params(image);
645 
646   if (first_)
647   {
648     this->set_selected_grid_image(image, rmps);
649     first_ = false;
650   }
651   else
652     this->add_image(image, rmps);
653 }
654 
save_image()655 void segv_vil_segmentation_manager::save_image()
656 {
657   vgui_dialog file_dialog("Save Image");
658   static std::string image_file;
659   static std::string ext = "tif";
660   static std::string type = "tiff";
661   static unsigned size_block = 0;
662   static bool byte = false;
663   file_dialog.file("Image Filename:", ext, image_file);
664   file_dialog.field("Image Format: ", type);
665   file_dialog.field("BlockSize", size_block);
666   file_dialog.checkbox("Convert to byte image", byte);
667   if (!file_dialog.ask())
668     return;
669   vil_image_resource_sptr img = this->selected_image();
670   if (!img)
671   {
672     std::cerr << "Null image in segv_vil_segmentation_manager::save_image\n";
673     return;
674   }
675   vil_image_resource_sptr save_image = img;
676   if (byte)
677   {
678     vil_image_view<unsigned char> byte_view = brip_vil_float_ops::convert_to_byte(img);
679     save_image = vil_new_image_resource_of_view(byte_view);
680   }
681   if (size_block>0)
682   {
683     vil_blocked_image_resource_sptr bim =
684       vil_new_blocked_image_resource(image_file.c_str(),
685                                      save_image->ni(), save_image->nj(),
686                                      save_image->nplanes(),
687                                      save_image->pixel_format(),
688                                      size_block, size_block,
689                                      "tiff");
690     vil_image_view_base_sptr view = save_image->get_view();
691     if (view)
692       bim->vil_image_resource::put_view(*view);
693     return;
694   }
695 
696   if (!vil_save_image_resource(save_image, image_file.c_str(), type.c_str()))
697     std::cerr << "segv_vil_segmentation_manager::save_image operation failed\n";
698 }
699 
save_nitf_camera()700 void segv_vil_segmentation_manager::save_nitf_camera()
701 {
702   vil_image_resource_sptr img = this->selected_image();
703   if (!img)
704   {
705     std::cerr << "Null image in segv_vil_segmentation_manager::save_camera\n";
706     return;
707   }
708 
709   vil_nitf2_image* nitf = nullptr;
710   std::string format = img->file_format();
711   std::string prefix = format.substr(0,4);
712   if (prefix == "nitf") {
713     nitf = (vil_nitf2_image*)img.ptr();
714     vgui_dialog file_dialog("Save NITF Camera");
715     static std::string image_file;
716     static std::string ext = "rpc";
717     file_dialog.file("Image Filename:", ext, image_file);
718     if (!file_dialog.ask())
719       return;
720     vpgl_nitf_rational_camera rpcam(nitf, true);
721     rpcam.save(image_file);
722   }
723 }
724 
set_range_params()725 void segv_vil_segmentation_manager::set_range_params()
726 {
727   bgui_image_tableau_sptr itab = this->selected_image_tab();
728   if (!itab)
729     return;
730   vgui_range_map_params_sptr rmps = itab->map_params();
731   if (!rmps)
732   {
733     vil_image_resource_sptr img = itab->get_image_resource();
734     if (!img)
735       return;
736     rmps = range_params(img);
737     if (!rmps)
738       return;
739   }
740   unsigned nc = rmps->n_components_;
741   static double min = static_cast<double>(rmps->min_L_),
742     max = static_cast<double>(rmps->max_L_);
743   static float gamma = rmps->gamma_L_;
744   static bool invert = rmps->invert_;
745   static bool gl_map = rmps->use_glPixelMap_;
746   static bool cache = rmps->cache_mapped_pix_;
747   if (nc==3)
748   {
749     min = static_cast<double>(rmps->min_R_);
750     max = static_cast<double>(rmps->max_R_);
751     gamma = rmps->gamma_R_;
752   }
753   vgui_dialog range_dlg("Set Range Map Params");
754   range_dlg.field("Range min:", min);
755   range_dlg.field("Range max:", max);
756   range_dlg.field("Gamma:", gamma);
757   range_dlg.checkbox("Invert:", invert);
758   range_dlg.checkbox("Use GL Mapping", gl_map);
759   range_dlg.checkbox("Cache Pixels", cache);
760   if (!range_dlg.ask())
761     return;
762   if (nc==1)
763     rmps= new vgui_range_map_params(min, max, gamma, invert,
764                                     gl_map, cache);
765   else if (nc == 3)
766     rmps = new vgui_range_map_params(min, max, min, max, min, max,
767                                      gamma, gamma, gamma, invert,
768                                      gl_map, cache);
769   else
770     rmps = nullptr;
771   itab->set_mapping(rmps);
772 }
773 
threshold_image()774 void segv_vil_segmentation_manager::threshold_image()
775 {
776   vil_image_resource_sptr img = selected_image();
777   if (!img)
778   {
779     std::cout << "In segv_segmentation_manager::threshold_image - no image\n";
780     return;
781   }
782   static float thresh = 128.0f;
783   vgui_dialog thresh_dlg("Threshold Image");
784   thresh_dlg.field("Threshold", thresh);
785   if (!thresh_dlg.ask())
786     return;
787   vil_image_view<float> fimage = brip_vil_float_ops::convert_to_float(*img);
788   vil_image_view<float> timage =
789     brip_vil_float_ops::threshold(fimage, thresh, 255);
790   vil_image_view<unsigned char> cimage = brip_vil_float_ops::convert_to_byte(timage, 0, 255);
791   this->add_image(vil_new_image_resource_of_view(cimage));
792 }
793 
harris_corners()794 void segv_vil_segmentation_manager::harris_corners()
795 {
796   vil_image_resource_sptr img = selected_image();
797   if (!img)
798   {
799     std::cout << "In segv_segmentation_manager::harris_measure) - no image\n";
800     return;
801   }
802   static sdet_harris_detector_params hdp;
803   vgui_dialog harris_dialog("harris");
804   harris_dialog.field("sigma", hdp.sigma_);
805   harris_dialog.field("thresh", hdp.thresh_);
806   harris_dialog.field("N = 2n+1, (n)", hdp.n_);
807   harris_dialog.field("Max No Corners(percent)", hdp.percent_corners_);
808   harris_dialog.field("scale_factor", hdp.scale_factor_);
809   harris_dialog.checkbox("Use vil harris\n corner strength ", hdp.use_vil_harris_);
810   if (!harris_dialog.ask())
811     return;
812   sdet_harris_detector hd(hdp);
813   hd.set_image_resource(img);
814   hd.extract_corners();
815   std::vector<vsol_point_2d_sptr>& points = hd.get_points();
816   int N = points.size();
817   if (!N)
818     return;
819   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
820   if (!t2D)
821     return;
822   t2D->clear_all();
823   for (int i=0; i<N; i++)
824     t2D->add_vsol_point_2d(points[i]);
825   t2D->post_redraw();
826 }
827 
nonmaximal_suppression()828 void segv_vil_segmentation_manager::nonmaximal_suppression()
829 {
830   bool show_lines = false;
831   vil_image_resource_sptr img = selected_image();
832   if (!img)
833   {
834     std::cout << "In segv_segmentation_manager::nonmaximal_suppression) - no image\n";
835     return;
836   }
837   static sdet_nonmax_suppression_params nsp;
838   vgui_dialog nonmax_dialog("non-maximal suppression");
839   nonmax_dialog.field("Gradient Magnitude Percentage Threshold ", nsp.thresh_);
840   nonmax_dialog.choice("Number of Points Used for Parabola Fit ", "3", "9", nsp.pfit_type_);
841   nonmax_dialog.checkbox("Show lines at the edge points? ", show_lines);
842   if (!nonmax_dialog.ask())
843     return;
844 
845   // prepare input for the nonmax suppression
846   vil_image_view<vxl_byte> input, input_grey;
847   vil_image_view<double> grad_i, grad_j, grad_mag_img;
848   vbl_array_2d<double> grad_x, grad_y, grad_mag;
849   vbl_array_2d<vgl_vector_2d <double> > input_directions;
850 
851   // vil_convert_cast(img->get_view(), input);
852   switch ( img->get_view()->pixel_format() )
853   {
854 #define macro(F , T) \
855     case F: vil_convert_cast( vil_image_view<T >(img->get_view()), input ); break;
856     macro( VIL_PIXEL_FORMAT_UINT_32, vxl_uint_32 )
857     macro( VIL_PIXEL_FORMAT_INT_32, vxl_int_32 )
858     macro( VIL_PIXEL_FORMAT_UINT_16, vxl_uint_16 )
859     macro( VIL_PIXEL_FORMAT_INT_16, vxl_int_16 )
860     macro( VIL_PIXEL_FORMAT_BYTE, vxl_byte )
861     macro( VIL_PIXEL_FORMAT_SBYTE, vxl_sbyte )
862     macro( VIL_PIXEL_FORMAT_FLOAT, float )
863     macro( VIL_PIXEL_FORMAT_DOUBLE, double )
864     macro( VIL_PIXEL_FORMAT_BOOL, bool )
865     default: img->get_view() = nullptr;
866 #undef macro
867   }
868 
869   int ni = input.ni();
870   int nj = input.nj();
871 
872   grad_i.set_size(ni,nj);
873   grad_j.set_size(ni,nj);
874   grad_mag_img.set_size(ni,nj);
875   input_grey.set_size(ni,nj);
876   grad_x.resize(ni,nj);
877   grad_y.resize(ni,nj);
878   grad_mag.resize(ni,nj);
879   input_directions.resize(ni,nj);
880 
881   if (input.nplanes() > 1)
882     vil_convert_planes_to_grey(input, input_grey);
883   else
884     input_grey = input;
885 
886   vil_sobel_1x3 <vxl_byte, double> (input_grey, grad_i, grad_j);
887   for (int j=0;j<nj; j++)
888   {
889     for (int i=0;i<ni; i++)
890     {
891       double xval = grad_i(i,j);
892       double yval = grad_j(i,j);
893       grad_x(i,j) = xval;
894       grad_y(i,j) = yval;
895       double val = std::sqrt(std::pow(xval,2.0) + std::pow(yval,2.0));
896       grad_mag(i,j) = val;
897       grad_mag_img(i,j) = val;
898       vgl_vector_2d<double> dir(xval, yval);
899       input_directions(i,j) = dir;
900     }
901   }
902 
903   // Below is to demonstrate how to initialize the non-maximal suppression in different ways
904 //  sdet_nonmax_suppression ns(nsp, grad_mag, input_directions);
905   sdet_nonmax_suppression ns(nsp, grad_x, grad_y);
906 //  sdet_nonmax_suppression ns(nsp, grad_i, grad_j);
907 //  sdet_nonmax_suppression ns(nsp, grad_mag_img, input_directions);
908   ns.apply();
909   std::vector<vsol_point_2d_sptr>& points = ns.get_points();
910   std::vector<vsol_line_2d_sptr>& lines = ns.get_lines();
911   // not used below, just for demonstration purposes
912   //std::vector<vgl_vector_2d<double> >& directions = ns.get_directions();
913   int N = points.size();
914   if (!N)
915     return;
916   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
917   if (!t2D)
918     return;
919   t2D->clear_all();
920   if (!show_lines)
921   {
922     for (int i=0; i<N; i++)
923       t2D->add_vsol_point_2d(points[i]);
924   }
925   if (show_lines)
926   {
927     for (int i=0; i<N; i++)
928       t2D->add_vsol_line_2d(lines[i]);
929   }
930   t2D->post_redraw();
931 }
932 
vd_edges()933 void segv_vil_segmentation_manager::vd_edges()
934 {
935   this->clear_display();
936   static bool agr = true;
937   static sdet_detector_params dp;
938   static float nm = 2.0;
939 
940   vgui_dialog vd_dialog("VD Edges");
941   vd_dialog.field("Gaussian sigma", dp.smooth);
942   vd_dialog.field("Noise Threshold", nm);
943   vd_dialog.checkbox("Automatic Threshold", dp.automatic_threshold);
944   vd_dialog.checkbox("Aggressive Closure", agr);
945   vd_dialog.checkbox("Compute Junctions", dp.junctionp);
946   vd_dialog.checkbox("DetectPeaks", dp.peaks_only);
947   if (!vd_dialog.ask())
948     return;
949   dp.noise_multiplier=nm;
950   if (agr)
951     dp.aggressive_junction_closure=1;
952   else
953     dp.aggressive_junction_closure=0;
954   vil_image_resource_sptr img = selected_image();
955   if (!img||!img->ni()||!img->nj())
956   {
957     std::cout << "In segv_vil_segmentation_manager::vd_edges() - no image\n";
958     return;
959   }
960   sdet_detector det(dp);
961   det.SetImage(img);
962 
963   det.DoContour();
964   std::vector<vtol_edge_2d_sptr>* edges = det.GetEdges();
965   if (edges)
966     this->draw_edges(*edges, true);
967 }
968 
third_order_edges()969 void segv_vil_segmentation_manager::third_order_edges()
970 {
971   this->clear_display();
972   static sdet_third_order_edge_det_params dp;
973   vgui_dialog todr_dialog("Third Order Edges");
974   todr_dialog.field("Sigma", dp.sigma_);
975   todr_dialog.field("Threshold", dp.thresh_);
976   todr_dialog.field("Parabola Type", dp.pfit_type_);
977   todr_dialog.field("Gradient Oper", dp.grad_op_);
978   todr_dialog.field("Convolution Algo", dp.conv_algo_);
979   if (!todr_dialog.ask())
980     return;
981   vil_image_resource_sptr img = selected_image();
982   if (!img||!img->ni()||!img->nj())
983   {
984     std::cout << "In segv_vil_segmentation_manager::third_order_edges() - no image\n";
985     return;
986   }
987   sdet_third_order_edge_det det(dp);
988   det.apply(img->get_view());
989   std::vector<vsol_line_2d_sptr> lines;
990   det.line_segs(lines);
991   if (lines.size())
992     this->draw_lines(lines);
993 }
994 
fit_lines()995 void segv_vil_segmentation_manager::fit_lines()
996 {
997   this->clear_display();
998   static sdet_detector_params dp;
999   static bool agr = true;
1000   static float nm = 2.0;
1001 
1002   static sdet_fit_lines_params flp;
1003 
1004   vgui_dialog lf_dialog("Fit Lines");
1005   lf_dialog.field("Gaussian sigma", dp.smooth);
1006   lf_dialog.field("Noise Threshold", nm);
1007   lf_dialog.checkbox("Automatic Threshold", dp.automatic_threshold);
1008   lf_dialog.checkbox("Aggressive Closure", agr);
1009   lf_dialog.checkbox("Compute Junctions", dp.junctionp);
1010   lf_dialog.field("Min Fit Length", flp.min_fit_length_);
1011   lf_dialog.field("RMS Distance", flp.rms_distance_);
1012 
1013   if (!lf_dialog.ask())
1014     return;
1015   dp.noise_multiplier=nm;
1016   if (agr)
1017     dp.aggressive_junction_closure=1;
1018   else
1019     dp.aggressive_junction_closure=0;
1020   dp.borderp = false;
1021   sdet_detector det(dp);
1022 
1023   vil_image_resource_sptr img = selected_image();
1024   if (!img||!img->ni()||!img->nj())
1025   {
1026     std::cout << "In segv_vil_segmentation_manager::vd_edges() - no image\n";
1027     return;
1028   }
1029 
1030   det.SetImage(img);
1031 
1032   det.DoContour();
1033   std::vector<vtol_edge_2d_sptr>* edges = det.GetEdges();
1034   if (!edges)
1035   {
1036     std::cout << "No edges to fit lines\n";
1037     return;
1038   }
1039   sdet_fit_lines fl(flp);
1040   fl.set_edges(*edges);
1041   fl.fit_lines();
1042   std::vector<vsol_line_2d_sptr> lines = fl.get_line_segs();
1043   this->draw_lines(lines);
1044 }
1045 
fit_conics()1046 void segv_vil_segmentation_manager::fit_conics()
1047 {
1048   this->clear_display();
1049   static sdet_detector_params dp;
1050   static bool agr = true;
1051   static float nm = 2.0;
1052 
1053   static sdet_fit_conics_params fcp;
1054 
1055   vgui_dialog lf_dialog("Fit Conics");
1056   lf_dialog.field("Gaussian sigma", dp.smooth);
1057   lf_dialog.field("Noise Threshold", nm);
1058   lf_dialog.checkbox("Automatic Threshold", dp.automatic_threshold);
1059   lf_dialog.checkbox("Aggressive Closure", agr);
1060   lf_dialog.checkbox("Compute Junctions", dp.junctionp);
1061   lf_dialog.field("Min Fit Length", fcp.min_fit_length_);
1062   lf_dialog.field("RMS Distance", fcp.rms_distance_);
1063 
1064   if (!lf_dialog.ask())
1065     return;
1066   dp.noise_multiplier=nm;
1067   if (agr)
1068     dp.aggressive_junction_closure=1;
1069   else
1070     dp.aggressive_junction_closure=0;
1071   dp.borderp = false;
1072   sdet_detector det(dp);
1073 
1074   vil_image_resource_sptr img = selected_image();
1075   if (!img||!img->ni()||!img->nj())
1076   {
1077     std::cout << "In segv_vil_segmentation_manager::vd_edges() - no image\n";
1078     return;
1079   }
1080 
1081   det.SetImage(img);
1082 
1083   det.DoContour();
1084   std::vector<vtol_edge_2d_sptr>* edges = det.GetEdges();
1085   if (!edges)
1086   {
1087     std::cout << "No edges to fit conics\n";
1088     return;
1089   }
1090   sdet_fit_conics fl(fcp);
1091   fl.set_edges(*edges);
1092   fl.fit_conics();
1093   std::vector<vsol_conic_2d_sptr> conics = fl.get_conic_segs();
1094   this->draw_conics(conics);
1095 
1096   std::vector<vsol_point_2d_sptr> center_points;
1097   double cx,cy,phi,width,height;
1098   // draw the center points of the conics
1099   for (unsigned int i=0; i<conics.size(); i++) {
1100     if (conics[i]->is_real_ellipse()) {
1101       conics[i]->ellipse_parameters(cx,cy,phi,width,height);
1102       vsol_point_2d_sptr p = new vsol_point_2d(cx, cy);
1103       std::cout << i << " center (" << cx << ',' << cy << ')' << std::endl;
1104       center_points.push_back(p);
1105     }
1106   }
1107   vgui_style_sptr style = vgui_style::new_style(1.0f,0.0f,0.0f,5.0f,1.0f);
1108 
1109   this->draw_points(center_points, style);
1110 }
1111 
fit_overlay_conics()1112 void segv_vil_segmentation_manager::fit_overlay_conics()
1113 {
1114   //this->clear_display();
1115   static sdet_detector_params dp;
1116   static bool agr = true;
1117   static float nm = 2.0;
1118 
1119   static sdet_fit_conics_params fcp;
1120 
1121   vgui_dialog lf_dialog("Fit overlay Conics");
1122   static std::string image_filename = "/home/dec/images/cal_image1.tif";
1123   static std::string ext = "*.*";
1124   lf_dialog.file("Image Filename:", ext, image_filename);
1125   lf_dialog.field("Gaussian sigma", dp.smooth);
1126   lf_dialog.field("Noise Threshold", nm);
1127   lf_dialog.checkbox("Automatic Threshold", dp.automatic_threshold);
1128   lf_dialog.checkbox("Aggressive Closure", agr);
1129   lf_dialog.checkbox("Compute Junctions", dp.junctionp);
1130   lf_dialog.field("Min Fit Length", fcp.min_fit_length_);
1131   lf_dialog.field("RMS Distance", fcp.rms_distance_);
1132 
1133   if (!lf_dialog.ask())
1134     return;
1135   dp.noise_multiplier=nm;
1136   if (agr)
1137     dp.aggressive_junction_closure=1;
1138   else
1139     dp.aggressive_junction_closure=0;
1140   dp.borderp = false;
1141   sdet_detector det(dp);
1142 
1143   vil_image_resource_sptr img = vil_load_image_resource(image_filename.c_str());
1144   if (!img||!img->ni()||!img->nj())
1145   {
1146     std::cout << "In segv_vil_segmentation_manager::vd_edges() - no image\n";
1147     return;
1148   }
1149 
1150   det.SetImage(img);
1151 
1152   det.DoContour();
1153   std::vector<vtol_edge_2d_sptr>* edges = det.GetEdges();
1154   if (!edges)
1155   {
1156     std::cout << "No edges to fit conics\n";
1157     return;
1158   }
1159   sdet_fit_conics fl(fcp);
1160   fl.set_edges(*edges);
1161   fl.fit_conics();
1162   std::vector<vsol_conic_2d_sptr> conics = fl.get_conic_segs();
1163   vgui_style_sptr style = vgui_style::new_style(1.0f,1.0f,0.0f,5.0f,1.0f);
1164   this->draw_conics(conics, style);
1165 
1166   std::vector<vsol_point_2d_sptr> center_points;
1167   double cx,cy,phi,width,height;
1168   // draw the center points of the conics
1169   for (unsigned int i=0; i<conics.size(); i++) {
1170     if (conics[i]->is_real_ellipse()) {
1171       conics[i]->ellipse_parameters(cx,cy,phi,width,height);
1172       vsol_point_2d_sptr p = new vsol_point_2d(cx, cy);
1173       std::cout << i << " center (" << cx << ',' << cy << ')' << std::endl;
1174       center_points.push_back(p);
1175     }
1176   }
1177   vgui_style_sptr style2 = vgui_style::new_style(1.0f,0.0f,1.0f,5.0f,1.0f);
1178 
1179   this->draw_points(center_points, style2);
1180 }
1181 
1182 // ####################################################################
1183 // Added by J. Green to project 3D points into image using rpc camera
1184 
project_points()1185 void segv_vil_segmentation_manager::project_points()
1186 {
1187   this->clear_display(); // apparently this call is needed?
1188   vil_image_resource_sptr img = this->selected_image();
1189   vil_nitf2_image* nitf = nullptr;
1190   std::string format = img->file_format();
1191   std::string prefix = format.substr(0,4);
1192   if (prefix == "nitf")
1193     nitf = (vil_nitf2_image*)img.ptr();
1194   else
1195   {
1196     //Check if the image is a pyramid
1197     bool pyr = img->get_property(vil_property_pyramid, nullptr);
1198     if (!pyr)
1199     {
1200       std::cout << "Current image is not a NITF image\n";
1201       return;
1202     }
1203     //Get the base image
1204     vil_pyramid_image_resource* pimage =
1205       (vil_pyramid_image_resource*)img.ptr();
1206     vil_image_resource_sptr base = pimage->get_resource(0);
1207     format = base->file_format();
1208     if (format == "nitf" || format =="nitf20" )
1209       nitf = (vil_nitf2_image*)base.ptr();
1210     else
1211     {
1212       std::cout << "Current image is not a NITF image\n";
1213       return;
1214     }
1215   }
1216   //cast to an nitf2_image
1217 
1218   static double lat=32.722;  // Latitude
1219   static double lon=-117.15; // Longitude
1220   static double elev=43;     // Elevation
1221 
1222   vgui_dialog lf_dialog("Project Points");
1223   lf_dialog.field("Latitude", lat);
1224   lf_dialog.field("Longitude", lon);
1225   lf_dialog.field("Elevation", elev);
1226 
1227   if (!lf_dialog.ask())
1228   {
1229     std::cerr << "In project_points() dialog failed; returning.\n";
1230     return;
1231   }
1232   // calculate point location (x1, y1) for 1st camera
1233   double u = 0;
1234   double v = 0;
1235   vpgl_nitf_rational_camera rpcam(nitf, true);
1236   rpcam.project(lon, lat, elev, u,  v);
1237   std::cout << " camera projects to <" << u << ", " << v << '>' << std::endl;
1238   std::vector<vsol_point_2d_sptr> points;
1239   vsol_point_2d_sptr p1 = new vsol_point_2d(u, v);
1240   points.push_back(p1);
1241   vgui_style_sptr style1 = vgui_style::new_style(1.0f,0.0f,0.0f,10.0f,1.0f);   // first style, red
1242   // draw point
1243   this->draw_points(points, style1);
1244 }  // end of project_points method
1245 
regions()1246 void segv_vil_segmentation_manager::regions()
1247 {
1248   this->clear_display();
1249   static bool debug = false;
1250   static bool agr = true;
1251   static bool residual = false;
1252   static sdet_detector_params dp;
1253   static float nm = 1.0;
1254   vgui_dialog region_dialog("Edgel Regions");
1255   region_dialog.field("Gaussian sigma", dp.smooth);
1256   region_dialog.field("Noise Threshold", nm);
1257   region_dialog.checkbox("Automatic Threshold", dp.automatic_threshold);
1258   region_dialog.checkbox("Aggressive Closure", agr);
1259   region_dialog.checkbox("Compute Junctions", dp.junctionp);
1260   region_dialog.checkbox("Debug", debug);
1261   region_dialog.checkbox("Residual Image", residual);
1262   if (!region_dialog.ask())
1263     return;
1264   dp.noise_multiplier=nm;
1265   if (agr)
1266     dp.aggressive_junction_closure=1;
1267   else
1268     dp.aggressive_junction_closure=0;
1269 
1270   vil_image_resource_sptr img = selected_image();
1271   if (!img||!img->ni()||!img->nj())
1272   {
1273     std::cout << "In segv_vil_segmentation_manager::vd_edges() - no image\n";
1274     return;
1275   }
1276 
1277   sdet_region_proc_params rpp(dp, true, debug, 2);
1278   sdet_region_proc rp(rpp);
1279   rp.set_image_resource(img);
1280   rp.extract_regions();
1281   if (debug)
1282   {
1283     vil1_image ed_img = rp.get_edge_image();
1284 #if 0
1285     vgui_image_tableau_sptr itab =  t2D->get_image_tableau();
1286     if (!itab)
1287     {
1288       std::cout << "In segv_vil_segmentation_manager::regions() - null image tableau\n";
1289       return;
1290     }
1291     itab->set_image(ed_img);
1292     itab->post_redraw();
1293 #endif
1294 //    this->add_image(ed_img);
1295   }
1296   if (!debug)
1297   {
1298     std::vector<vtol_intensity_face_sptr>& regions = rp.get_regions();
1299     this->draw_regions(regions, true);
1300   }
1301   if (residual)
1302   {
1303     vil_image_view<float> res_img = rp.get_residual_image_view();
1304     if (!res_img)
1305       return;
1306     this->add_image(vil_new_image_resource_of_view(res_img));
1307   }
1308 }
1309 
1310 //: Show combined greyscale images in up to three planes as a color image.
1311 // Assume images are arranged by columns
display_images_as_color()1312 void segv_vil_segmentation_manager::display_images_as_color()
1313 {
1314   unsigned ncols =grid_->cols();
1315   if (ncols<2)
1316   {
1317     std::cout << "In segv_vil_segmentation_manager::display_images_as_color() -"
1318              << " not enough active panes\n";
1319     return;
1320   }
1321   vil_image_resource_sptr img0 = this->image_at(0,0);
1322   vil_image_resource_sptr img1 = this->image_at(1,0);
1323   if (!img0||!img1)
1324   {
1325     std::cout << "In segv_vil_segmentation_manager::display_images_as_color()() -"
1326              << " some input images are null\n";
1327     return;
1328   }
1329   vil_image_view<unsigned char> cimage0 =
1330     brip_vil_float_ops::convert_to_byte(img0);
1331   vil_image_view<unsigned char> cimage1 =
1332     brip_vil_float_ops::convert_to_byte(img1);
1333   vil_image_view<unsigned char> cimage2;
1334   if (ncols==3&&this->image_at(2,0))
1335   {
1336     vil_image_resource_sptr img2 = this->image_at(2,0);
1337     cimage2 = brip_vil_float_ops::convert_to_byte(img2);
1338   }
1339   else
1340   {
1341     unsigned w = cimage0.ni(), h = cimage0.nj();
1342     cimage2.set_size(w, h);
1343     cimage2.fill(0);
1344   }
1345   vil_image_view<vil_rgb<vxl_byte> > rgb = brip_vil_float_ops::combine_color_planes(cimage0, cimage1, cimage2);
1346   vil_image_resource_sptr color = vil_new_image_resource_of_view(rgb);
1347   if (ncols<3)
1348     grid_->add_column();
1349   unsigned col = 2, row = 0;
1350   this->add_image_at(color,col,row);
1351 }
1352 
intensity_profile()1353 void segv_vil_segmentation_manager::intensity_profile()
1354 {
1355   bgui_image_tableau_sptr itab = selected_image_tab();
1356  itab->lock_linenum(true);
1357   bgui_picker_tableau_sptr ptab = selected_picker_tab();
1358   float start_col=0, end_col=0, start_row=0, end_row=0;
1359   ptab->pick_line(&start_col, &start_row, &end_col, &end_row);
1360   std::vector<double> pos, vals;
1361   itab->image_line(start_col, start_row, end_col, end_row, pos, vals);
1362   bgui_graph_tableau_sptr g = bgui_graph_tableau_new(512, 512);
1363   g->update(pos, vals);
1364   //popup a profile graph
1365   char location[100];
1366   std::sprintf(location, "scan:(%d, %d)<->(%d, %d)",
1367               static_cast<unsigned>(start_col),
1368               static_cast<unsigned>(start_row),
1369               static_cast<unsigned>(end_col),
1370               static_cast<unsigned>(end_row));
1371   vgui_dialog* ip_dialog = g->popup_graph(location);
1372   if (!ip_dialog->ask())
1373   {
1374     delete ip_dialog;
1375     itab->lock_linenum(false);
1376     return;
1377   }
1378 delete ip_dialog;
1379  itab->lock_linenum(false);
1380 }
1381 
display_roi()1382 void segv_vil_segmentation_manager::display_roi()
1383 {
1384   if (!roi_) {
1385     std::cout << " Null ROI\n";
1386     return;
1387   }
1388   //assume only one region
1389   if (roi_->n_regions() != 1)
1390   {
1391     std::cout << " Can't handle a roi with more than one region\n";
1392     return;
1393   }
1394   int cmin=roi_->cmin(0), cmax=roi_->cmax(0),
1395     rmin=roi_->rmin(0), rmax = roi_->rmax(0);
1396 
1397   // display the roi as a vsol polygon
1398   vsol_point_2d_sptr p0 = new vsol_point_2d(cmin, rmin);
1399   vsol_point_2d_sptr p1 = new vsol_point_2d(cmax, rmin);
1400   vsol_point_2d_sptr p2 = new vsol_point_2d(cmax, rmax);
1401   vsol_point_2d_sptr p3 = new vsol_point_2d(cmin, rmax);
1402   std::vector<vsol_point_2d_sptr> pts;
1403   pts.push_back(p0);pts.push_back(p1);pts.push_back(p2);pts.push_back(p3);
1404   vsol_polygon_2d_sptr poly = new vsol_polygon_2d(pts);
1405   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
1406   if (!t2D)
1407     return;
1408   t2D->clear_all();
1409   t2D->add_vsol_polygon_2d(poly);
1410 }
1411 
create_roi()1412 void segv_vil_segmentation_manager::create_roi()
1413 {
1414   bgui_picker_tableau_sptr ptab = selected_picker_tab();
1415   float start_col=0, end_col=0, start_row=0, end_row=0;
1416   ptab->pick_box(&start_col, &start_row, &end_col, &end_row);
1417   roi_ = new brip_roi();
1418   roi_->add_region(int(start_col+0.5f), int(start_row+0.5f),
1419                    int(end_col-start_col+0.5f), int(end_row-start_row+0.5f));
1420   this->display_roi();
1421 }
1422 
crop_image()1423 void segv_vil_segmentation_manager::crop_image()
1424 {
1425   vil_image_resource_sptr img = this->selected_image();
1426   if (!img) {
1427     std::cout << "No image to crop\n";
1428     return;
1429   }
1430   if (!roi_) {
1431     std::cout << "No crop roi specified\n";
1432     return;
1433   }
1434   vil_image_resource_sptr chip;
1435   if (!brip_vil_float_ops::chip(img, roi_, chip))
1436   {
1437     std::cout << "Crop operation failed\n";
1438     return;
1439   }
1440   this->add_image(chip);
1441 }
1442 
gaussian()1443 void segv_vil_segmentation_manager::gaussian()
1444 {
1445   vil_image_resource_sptr img = this->selected_image();
1446   if (!img) {
1447     std::cout << "No image to smooth\n";
1448     return;
1449   }
1450   vil_image_view<float> view = brip_vil_float_ops::convert_to_float(img);
1451   static float sigma = 1.0f;
1452   vgui_dialog gauss_dialog("Gaussian Smoothing");
1453   gauss_dialog.field("Gauss sigma", sigma);
1454   if (!gauss_dialog.ask())
1455     return;
1456   vil_image_view<float> gauss = brip_vil_float_ops::gaussian(view, sigma);
1457   vil_image_resource_sptr gaussr = vil_new_image_resource_of_view(gauss);
1458   this->add_image(gaussr);
1459 }
1460 
abs_value()1461 void segv_vil_segmentation_manager::abs_value()
1462 {
1463   vil_image_resource_sptr img = this->selected_image();
1464   if (!img) {
1465     std::cout << "No image to for absolute value\n";
1466     return;
1467   }
1468   vil_image_view<float> view = brip_vil_float_ops::convert_to_float(img);
1469   vil_image_view<float> abs = brip_vil_float_ops::absolute_value(view);
1470   vil_image_resource_sptr absr = vil_new_image_resource_of_view(abs);
1471   this->add_image(absr);
1472 }
1473 
inline_viewer()1474 void segv_vil_segmentation_manager::inline_viewer()
1475 {
1476   bgui_image_tableau_sptr itab = this->selected_image_tab();
1477   bgui_range_adjuster_tableau_sptr h= bgui_range_adjuster_tableau_new(itab);
1478   h->set_hardware(true);
1479   h->update();
1480   vgui_viewer2D_tableau_sptr v = vgui_viewer2D_tableau_new(h);
1481   vgui_shell_tableau_sptr s = vgui_shell_tableau_new(v);
1482   //popup adjuster
1483   vgui_dialog test_inline("Histogram Range Adjuster");
1484   test_inline.inline_tableau(s, 280, 200);
1485   if (!test_inline.ask())
1486     return;
1487 }
1488 
intensity_histogram()1489 void segv_vil_segmentation_manager::intensity_histogram()
1490 {
1491   vil_image_resource_sptr img = selected_image();
1492   if (!img||!img->ni()||!img->nj())
1493   {
1494     std::cout << "In segv_vil_segmentation_manager::intensity_histogram() - no image\n";
1495     return;
1496   }
1497   bgui_image_utils iu(img);
1498   bgui_graph_tableau_sptr g = iu.hist_graph();
1499 
1500   if (!g)
1501   { std::cout << "In segv_vil_segmentation_manager::intensity_histogram()- color images not supported\n";
1502     return;
1503   }
1504 
1505   //popup a profile graph
1506   char location[100];
1507   std::sprintf(location, "Intensity Histogram");
1508   vgui_dialog* ip_dialog = g->popup_graph(location);
1509   if (!ip_dialog->ask())
1510   {
1511     delete ip_dialog;
1512     return;
1513   }
1514   delete ip_dialog;
1515 }
1516 
1517 //=== Image Arithmetic (Uses the Image Stack)
1518 //Add the image in pane 0 to the image in pane 1. Result in pane 2.
add_images()1519 void segv_vil_segmentation_manager::add_images()
1520 {
1521   unsigned ncols =grid_->cols();
1522   if (ncols<2)
1523   {
1524     std::cout << "In segv_vil_segmentation_manager::add_images() -"
1525              << " not enough active panes\n";
1526     return;
1527   }
1528   vil_image_resource_sptr img0 = this->image_at(0,0);
1529   vil_image_resource_sptr img1 = this->image_at(1,0);
1530   if (!img0||!img1)
1531   {
1532     std::cout << "In segv_vil_segmentation_manager::add_images() -"
1533              << " one or both input images are null\n";
1534     return;
1535   }
1536   vil_image_resource_sptr sum = brip_vil_float_ops::sum(img0, img1);
1537   vgui_range_map_params_sptr rmps = range_params(sum);
1538   if (ncols<3)
1539     grid_->add_column();
1540   unsigned col = 2, row = 0;
1541   this->add_image_at(sum,col,row, rmps);
1542 }
1543 
1544 //subtract the image in pane 1 from the image in pane 0. Result in pane 2
subtract_images()1545 void segv_vil_segmentation_manager::subtract_images()
1546 {
1547   unsigned ncols =grid_->cols();
1548   if (ncols<2)
1549   {
1550     std::cout << "In segv_vil_segmentation_manager::subtract_images() -"
1551              << " not enough active panes\n";
1552     return;
1553   }
1554   vil_image_resource_sptr img0 = this->image_at(0,0);
1555   vil_image_resource_sptr img1 = this->image_at(1,0);
1556   if (!img0||!img1)
1557   {
1558     std::cout << "In segv_vil_segmentation_manager::subtract_images() -"
1559              << " one or both input images are null\n";
1560     return;
1561   }
1562   vil_image_resource_sptr diff = brip_vil_float_ops::difference(img0, img1);
1563   vgui_range_map_params_sptr rmps = range_params(diff);
1564   if (ncols<3)
1565     grid_->add_column();
1566   unsigned col = 2, row = 0;
1567   this->add_image_at(diff,col,row);
1568 }
1569 
negate_image()1570 void segv_vil_segmentation_manager::negate_image()
1571 {
1572     vil_image_resource_sptr img = selected_image();
1573   if (!img)
1574   {
1575     std::cout << "In segv_segmentation_manager::negate_image - no image\n";
1576     return;
1577   }
1578   vil_image_resource_sptr neg = brip_vil_float_ops::negate(img);
1579   if (neg)
1580     this->add_image(neg);
1581 }
1582 
entropy()1583 void segv_vil_segmentation_manager::entropy()
1584 {
1585   vgui_dialog entropy_dlg("Entropy of Image");
1586   static unsigned xrad = 15, yrad = 15, step = 10;
1587   static float sigma = 1.0f;
1588   static bool inten = true;
1589   static bool grad = true;
1590   static bool color = false;
1591   entropy_dlg.field("Region x radius",xrad);
1592   entropy_dlg.field("Region y radius",yrad);
1593   entropy_dlg.field("Step Size", step);
1594   entropy_dlg.field("Sigma", sigma);
1595   entropy_dlg.checkbox("Intensity", inten);
1596   entropy_dlg.checkbox("Gradient", grad);
1597   entropy_dlg.checkbox("Color", color);
1598   if (!entropy_dlg.ask())
1599     return;
1600   bgui_image_tableau_sptr itab = this->selected_image_tab();
1601   vil_image_resource_sptr img = itab->get_image_resource();
1602   vil_image_view<float> entropy =
1603     brip_vil_float_ops::entropy(xrad, yrad, step, img,
1604                                 sigma, inten, grad, color);
1605   vil_image_view<unsigned char> cent =
1606     brip_vil_float_ops::convert_to_byte(entropy);
1607 
1608   this->add_image(vil_new_image_resource_of_view(cent));
1609 }
1610 
minfo()1611 void segv_vil_segmentation_manager::minfo()
1612 {
1613   vgui_dialog minfo_dlg("Minfo of Image");
1614   static unsigned xrad = 15, yrad = 15, step = 10;
1615   static float sigma = 1.0f;
1616   static bool inten = true;
1617   static bool grad = true;
1618   static bool color = false;
1619   minfo_dlg.field("Region x radius",xrad);
1620   minfo_dlg.field("Region y radius",yrad);
1621   minfo_dlg.field("Step Size", step);
1622   minfo_dlg.field("Sigma", sigma);
1623   minfo_dlg.checkbox("Intensity", inten);
1624   minfo_dlg.checkbox("Gradient", grad);
1625   minfo_dlg.checkbox("Color", color);
1626   if (!minfo_dlg.ask())
1627     return;
1628   vil_image_resource_sptr img0 = this->image_at(0,0);
1629   vil_image_resource_sptr img1 = this->image_at(1,0);
1630   if (!img0||!img1)
1631   {
1632     std::cout << "In segv_vil_segmentation_manager::minfo() -"
1633              << " one or both input images are null\n";
1634     return;
1635   }
1636   vil_image_view<float> MI0, MI1;
1637   if (!brip_vil_float_ops::minfo(xrad, yrad, step, img0, img1, MI0, MI1,
1638                                 sigma, inten, grad, color))
1639     return;
1640   vil_image_view<unsigned char> MI0_char =
1641     brip_vil_float_ops::convert_to_byte(MI0);
1642 
1643   vil_image_view<unsigned char> MI1_char =
1644     brip_vil_float_ops::convert_to_byte(MI1);
1645 
1646   this->add_image_at(vil_new_image_resource_of_view(MI0_char), 0, 0);
1647   this->add_image_at(vil_new_image_resource_of_view(MI1_char), 1, 0);
1648 }
1649 
rotate_image()1650 void segv_vil_segmentation_manager::rotate_image()
1651 {
1652   vil_image_resource_sptr img = selected_image();
1653   if (!img)
1654   {
1655     std::cout << "In segv_vil_segmentation_manager::rotate_image - no image\n";
1656     return;
1657   }
1658   vil_image_view<float> flt =
1659     brip_vil_float_ops::convert_to_float(img);
1660   static double angle = 0;
1661   vgui_dialog rotate_dialog("Rotate Image");
1662   rotate_dialog.field("Rotation Angle (deg)", angle);
1663   if (!rotate_dialog.ask())
1664     return;
1665 
1666   vil_image_view<float> temp = brip_vil_float_ops::rotate(flt, angle);
1667 #if 0
1668   vil_image_view<unsigned char> tempr =
1669     brip_vil_float_ops::convert_to_byte(temp, 0, 255);
1670 #endif
1671   vil_image_resource_sptr out_image = vil_new_image_resource_of_view(temp);
1672   this->add_image(out_image);
1673 }
1674 
reduce_image()1675 void segv_vil_segmentation_manager::reduce_image()
1676 {
1677   vil_image_resource_sptr img = selected_image();
1678   if (!img)
1679   {
1680     std::cout << "In segv_vil_segmentation_manager::reduce_image - no image\n";
1681     return;
1682   }
1683   static float coef=0.6f;
1684   vgui_dialog expand_dialog("Reduce Image");
1685   expand_dialog.field("Filter coef", coef);
1686   if (!expand_dialog.ask())
1687     return;
1688 
1689   vil_image_view<float> flt =
1690     brip_vil_float_ops::convert_to_float(img);
1691 
1692   vil_image_view<float> reduced =
1693     brip_vil_float_ops::half_resolution(flt, coef);
1694 
1695   vil_image_resource_sptr out_image = vil_new_image_resource_of_view(reduced);
1696   this->add_image(out_image);
1697 }
1698 
reduce_image_bicubic()1699 void segv_vil_segmentation_manager::reduce_image_bicubic()
1700 {
1701   vil_image_resource_sptr img = selected_image();
1702   if (!img)
1703   {
1704     std::cout << "In segv_vil_segmentation_manager::reduce_image_bicubic - no image\n";
1705     return;
1706   }
1707   vil_image_view<float> flt =
1708     brip_vil_float_ops::convert_to_float(img);
1709 
1710   vil_image_view<float> reduced;
1711   vil_resample_bicub(flt, reduced, flt.ni()/2, flt.nj()/2);
1712 
1713   vil_image_resource_sptr out_image =
1714     vil_new_image_resource_of_view(reduced);
1715   this->add_image(out_image);
1716 }
1717 
expand_image()1718 void segv_vil_segmentation_manager::expand_image()
1719 {
1720   vil_image_resource_sptr img = selected_image();
1721   if (!img)
1722   {
1723     std::cout << "In segv_vil_segmentation_manager::expand_image - no image\n";
1724     return;
1725   }
1726   static float coef=0.6f;
1727   vgui_dialog expand_dialog("Expand Image");
1728   expand_dialog.field("Filter coef", coef);
1729   if (!expand_dialog.ask())
1730     return;
1731 
1732   vil_image_view<float> flt =
1733     brip_vil_float_ops::convert_to_float(img);
1734 
1735   vil_image_view<float> expanded =
1736     brip_vil_float_ops::double_resolution(flt, coef);
1737 
1738   vil_image_resource_sptr out_image = vil_new_image_resource_of_view(expanded);
1739   this->add_image(out_image);
1740 }
1741 
expand_image_bicubic()1742 void segv_vil_segmentation_manager::expand_image_bicubic()
1743 {
1744   vil_image_resource_sptr img = selected_image();
1745   if (!img)
1746   {
1747     std::cout << "In segv_vil_segmentation_manager::expand_image_bicubic - no image\n";
1748     return;
1749   }
1750   vil_image_view<float> flt =
1751     brip_vil_float_ops::convert_to_float(img);
1752 
1753   vil_image_view<float> expanded;
1754   vil_resample_bicub(flt, expanded, 2*flt.ni(), 2*flt.nj());
1755 
1756   vil_image_resource_sptr out_image = vil_new_image_resource_of_view(expanded);
1757   this->add_image(out_image);
1758 }
1759 
flip_image_lr()1760 void segv_vil_segmentation_manager::flip_image_lr()
1761 {
1762   vil_image_resource_sptr img = selected_image();
1763   if (!img)
1764   {
1765     std::cout << "In segv_vil_segmentation_manager::flip_image - no image\n";
1766     return;
1767   }
1768 
1769   vil_image_resource_sptr flipr = vil_flip_lr(img);
1770   vil_image_resource_sptr flipc = vil_new_image_resource(img->ni(), img->nj(),
1771                                                          flipr);
1772   vil_copy_deep(flipr, flipc);
1773   this->add_image(flipc);
1774 }
1775 
max_trace_scale()1776 void segv_vil_segmentation_manager::max_trace_scale()
1777 {
1778   static double scale_ratio = vnl_math::sqrt2;
1779   static double max_scale = 16.0f;
1780   vgui_dialog scale_dialog("Max Trace Scale");
1781   scale_dialog.field("Scale Ratio", scale_ratio);
1782   scale_dialog.field("Maximum Scale", max_scale);
1783   if (!scale_dialog.ask())
1784     return;
1785 
1786   vil_image_resource_sptr img = selected_image();
1787   if (!img)
1788   {
1789     std::cout<< "In segv_vil_segmentation_manager::max_trace_scale - no image\n";
1790     return;
1791   }
1792   vil_image_view<float> fimg = brip_vil_float_ops::convert_to_float(img);
1793   vil_image_view<float> scale_image;
1794   brip_max_scale_response<float> msr(fimg, scale_ratio, max_scale);
1795   scale_image = msr.scale_base();
1796   this->add_image(vil_new_image_resource_of_view(scale_image));
1797 }
1798 
color_order()1799 void segv_vil_segmentation_manager::color_order()
1800 {
1801   vil_image_resource_sptr img = selected_image();
1802   if (!img)
1803   {
1804     std::cout<< "In segv_vil_segmentation_manager::color order - no image\n";
1805     return;
1806   }
1807   static float equal_tol = 0.1f;
1808   vgui_dialog order_dialog("Color Order");
1809   order_dialog.field("Equal Tol", equal_tol);
1810   if (!order_dialog.ask())
1811     return;
1812 
1813   vil_image_view_base_sptr vb = img->get_view();
1814 
1815   //retains the image as color
1816   vil_image_view<float> fimg = *vil_convert_cast(float(), vb);
1817   //scale to 0,1 so tolerance is meaningful
1818   if (vb->pixel_format() == VIL_PIXEL_FORMAT_BYTE)
1819     vil_math_scale_values(fimg,1.0/255.0);
1820 
1821   vil_image_view<unsigned char> order_codes =
1822     brip_vil_float_ops::color_order(fimg, equal_tol);
1823 
1824   this->add_image(vil_new_image_resource_of_view(order_codes));
1825 }
1826 
create_polygon()1827 void segv_vil_segmentation_manager::create_polygon()
1828 {
1829   bgui_picker_tableau_sptr ptab = selected_picker_tab();
1830   if (!ptab) {
1831     std::cerr << "In segv_vil_segmentation_managerd::create_polygon() - no picker tableau\n";
1832     return;
1833   }
1834   vsol_polygon_2d_sptr poly2d;
1835   ptab->pick_polygon(poly2d);
1836   if (!poly2d)
1837   {
1838     std::cerr << "In segv_vil_segmentation_manager::create_polygon() - picking failed\n";
1839     return;
1840   }
1841   bgui_vtol2D_tableau_sptr btab = selected_vtol2D_tab();
1842   if (!btab) {
1843     std::cerr << "In segv_vil_segmentation_managerd::create_polygon() - no vtol2D tableau\n";
1844     return;
1845   }
1846 //  btab->add(poly2d);
1847   mask_.push_back(poly2d);
1848   btab->post_redraw();
1849 }
1850 
clear_mask()1851 void segv_vil_segmentation_manager::clear_mask()
1852 {
1853   mask_.clear();
1854 }
1855 
save_mask()1856 void segv_vil_segmentation_manager::save_mask()
1857 {
1858   mask_.clear();
1859 }
1860 
mser_conics()1861 void segv_vil_segmentation_manager::mser_conics()
1862 {
1863   vgui_dialog mser_dialog("Fit overlay Conics");
1864   static std::string conic_filename = "";
1865   static std::string ext = "*.*";
1866   mser_dialog.file("MSER Conic Filename:", ext, conic_filename);
1867   if (!mser_dialog.ask())
1868     return;
1869   std::ifstream istr(conic_filename.c_str());
1870   if (!istr.is_open())
1871     return;
1872   std::vector<vsol_conic_2d_sptr> conics;
1873 #if 0 //check in later
1874   sdet_read_mser_regions::read_mser_conics(istr, conics);
1875 #endif
1876   if (!conics.size())
1877     return;
1878   this->draw_conics(conics);
1879 }
1880 
image_as_vrml_points()1881 void segv_vil_segmentation_manager::image_as_vrml_points()
1882 {
1883   vil_image_resource_sptr img = selected_image();
1884   if (!img)
1885   {
1886     std::cout<< "In segv_vil_segmentation_manager::image_as_vrml_points - no image\n";
1887     return;
1888   }
1889   vgui_dialog vrml_dialog("VRML Intensity Display");
1890   static std::string vrml_filename = "";
1891   static std::string ext = "*.*";
1892   vrml_dialog.file("VRML Filename:", ext, vrml_filename);
1893   if (!vrml_dialog.ask())
1894     return;
1895   std::ofstream ostr(vrml_filename.c_str());
1896   if (!ostr.is_open())
1897     return;
1898   vil_image_view<float> fimg =
1899     brip_vil_float_ops::convert_to_float(img);
1900 #if 1
1901   sdet_vrml_display::write_vrml_header(ostr);
1902   sdet_vrml_display::write_vrml_height_map(ostr, fimg);
1903 #endif
1904 }
1905 
extrema()1906 void segv_vil_segmentation_manager::extrema()
1907 {
1908   vil_image_resource_sptr img = selected_image();
1909   if (!img)
1910   {
1911     std::cout<< "In segv_vil_segmentation_manager::extrema - no image\n";
1912     return;
1913   }
1914   static float lambda0 = 1.0f;
1915   static float lambda1 = 1.0f;
1916   static float theta = 0.0f;
1917   static bool bright = true;
1918   static bool color_overlay = true;
1919   static bool fast = true;
1920   static int choice = 1;
1921   std::vector<std::string> choices;
1922   choices.push_back("Point Response Only");
1923   choices.push_back("Point & Mask");
1924   choices.push_back("Point & Unclipped");
1925   choices.push_back("SignedPerPixel");
1926   choices.push_back("AbsolutePerPixel");
1927   vgui_dialog extrema_dialog("Detect Extrema");
1928   extrema_dialog.field("lambda0",lambda0);
1929   extrema_dialog.field("lambda1",lambda1);
1930   extrema_dialog.field("theta",theta);
1931   extrema_dialog.checkbox("Bright Extrema?(check)",bright);
1932   extrema_dialog.checkbox("ColorOverlay?(check)",color_overlay);
1933   extrema_dialog.choice("Display Mode", choices, choice);
1934   extrema_dialog.checkbox("Fast Alg.(check)", fast);
1935   if (!extrema_dialog.ask())
1936     return;
1937   vul_timer t;
1938   vil_image_view<float> fimg =
1939     brip_vil_float_ops::convert_to_float(img);
1940   vil_image_view<float> extr;
1941   bool output_mask = false, output_unclipped = false, mag_only = false;
1942   bool scale_invariant = false, non_max_suppress = true;
1943   if (choice == 1) output_mask = true;
1944   if (choice == 2) output_unclipped = true;
1945   if (choice == 3) {
1946     scale_invariant = true;
1947     output_unclipped = true;
1948     non_max_suppress = false;
1949   }
1950   if (choice == 4)  {
1951     scale_invariant = true;
1952     mag_only = true;
1953     output_unclipped = false;
1954     non_max_suppress = false;
1955   }
1956   if (fast)
1957     extr = brip_vil_float_ops::fast_extrema(fimg, lambda0, lambda1, theta, bright, mag_only, output_mask, output_unclipped, scale_invariant, non_max_suppress);
1958   else
1959     extr = brip_vil_float_ops::extrema(fimg, lambda0, lambda1, theta, bright, mag_only, output_mask, output_unclipped, scale_invariant, non_max_suppress);
1960 
1961   std::cout << "Extrema computation time " << t.real() << " msec\n";
1962   if (choice ==3 || choice == 4) {
1963     vil_image_resource_sptr resc = vil_new_image_resource_of_view(extr);
1964     this->add_image(resc);
1965     return;
1966   }
1967   unsigned ni = extr.ni(), nj = extr.nj(), np = extr.nplanes();
1968   if (choice==0&&!color_overlay) {
1969     if (np!=1)
1970       return;
1971     vil_image_resource_sptr resc = vil_new_image_resource_of_view(extr);
1972     this->add_image(resc);
1973     return;
1974   }
1975   if (choice==0&&color_overlay) {
1976     if (np!=1)
1977       return;
1978     vil_image_resource_sptr resc = vil_new_image_resource_of_view(extr);
1979     vil_image_view<vil_rgb<vxl_byte> > rgb =
1980       brip_vil_float_ops::combine_color_planes(img, resc, img);
1981     this->add_image(vil_new_image_resource_of_view(rgb));
1982   }
1983   if (choice>0)
1984   {
1985     if (np!=2)
1986       return;
1987     vil_image_view<float> res(ni, nj), mask(ni, nj);
1988     for (unsigned j = 0; j<nj; ++j)
1989       for (unsigned i = 0; i<ni; ++i)
1990       {
1991         res(i,j) = extr(i,j,0);
1992         mask(i,j) = extr(i,j,1);
1993       }
1994     if (color_overlay) {
1995       vil_image_resource_sptr res_resc = vil_new_image_resource_of_view(res);
1996       vil_image_resource_sptr msk_resc = vil_new_image_resource_of_view(mask);
1997       vil_image_view<vil_rgb<vxl_byte> > rgb =
1998         brip_vil_float_ops::combine_color_planes(img, res_resc, msk_resc);
1999       this->add_image(vil_new_image_resource_of_view(rgb));
2000     }
2001     if (output_mask&&!color_overlay)
2002     {
2003      this->add_image(vil_new_image_resource_of_view(res));
2004       this->add_image(vil_new_image_resource_of_view(mask));
2005     }
2006   }
2007 }
2008 
rot_extrema()2009 void segv_vil_segmentation_manager::rot_extrema()
2010 {
2011   vil_image_resource_sptr img = selected_image();
2012   if (!img)
2013   {
2014     std::cout<< "In segv_vil_segmentation_manager::extrema - no image\n";
2015     return;
2016   }
2017   static float lambda0 = 1.0f;
2018   static float lambda1 = 1.0f;
2019   static float theta_inc = 0.0f;
2020   static bool bright = true;
2021   vgui_dialog extrema_dialog("Detect Extrema");
2022   extrema_dialog.field("lambda0",lambda0);
2023   extrema_dialog.field("lambda1",lambda1);
2024   extrema_dialog.field("theta increment",theta_inc);
2025   extrema_dialog.checkbox("Bright Extrema?(check)",bright);
2026   if (!extrema_dialog.ask())
2027     return;
2028   vul_timer t;
2029   vil_image_view<float> fimg = brip_vil_float_ops::convert_to_float(img);
2030   vil_image_view<float> output = brip_vil_float_ops::extrema_rotational(fimg, lambda0, lambda1, theta_inc, bright);
2031   unsigned ni = output.ni(), nj = output.nj();
2032   vil_image_view<float> res(ni, nj), mask(ni, nj);
2033   for (unsigned j = 0; j<nj; ++j)
2034     for (unsigned i = 0; i<ni; ++i)
2035     {
2036       res(i,j) = output(i,j,0);
2037       mask(i,j) = output(i,j,2);
2038     }
2039 
2040   vil_image_resource_sptr res_resc = vil_new_image_resource_of_view(res);
2041   vil_image_resource_sptr msk_resc = vil_new_image_resource_of_view(mask);
2042   vil_image_view<vil_rgb<vxl_byte> > rgb = brip_vil_float_ops::combine_color_planes(img, res_resc, msk_resc);
2043   this->add_image(vil_new_image_resource_of_view(rgb));
2044 }
2045 
beaudet()2046 void segv_vil_segmentation_manager::beaudet()
2047 {
2048   vil_image_resource_sptr img = selected_image();
2049   if (!img)
2050   {
2051     std::cout<< "In segv_vil_segmentation_manager::beaudet - no image\n";
2052     return;
2053   }
2054   static float sigma = 1.0f;
2055   static bool determinant = true;
2056   vgui_dialog beaudet_dialog("beaudet");
2057   beaudet_dialog.field("sigma", sigma);
2058   beaudet_dialog.checkbox("Determinant(or Trace)", determinant);
2059   if (!beaudet_dialog.ask())
2060     return;
2061   int ni = img->ni(), nj = img->nj();
2062   vil_image_view<float> fimg = brip_vil_float_ops::convert_to_float(img);
2063   vil_image_view<float> smooth = brip_vil_float_ops::gaussian(fimg, sigma);
2064   vil_image_view<float> Ixx(ni,nj), Ixy(ni, nj), Iyy(ni, nj);
2065   brip_vil_float_ops::hessian_3x3(smooth, Ixx, Ixy, Iyy);
2066   vil_image_view<float> beau =
2067     brip_vil_float_ops::beaudet(Ixx, Ixy, Iyy, determinant);
2068   this->add_image(vil_new_image_resource_of_view(beau));
2069 }
2070 
parallel_coverage()2071 void segv_vil_segmentation_manager::parallel_coverage()
2072 {
2073   static brip_para_cvrg_params pcp;
2074   static bool combined=true;
2075   vgui_dialog para_dialog("Parallel Coverage");
2076   para_dialog.field("Sigma", pcp.sigma_);
2077   para_dialog.field("Projection Width", pcp.proj_width_);
2078   para_dialog.field("Projection Height", pcp.proj_height_);
2079   para_dialog.checkbox("Display Coverage and Direction Combined", combined);
2080   para_dialog.checkbox("Verbose", pcp.verbose_);
2081   if (!para_dialog.ask())
2082     return;
2083   vil_image_resource_sptr img = selected_image();
2084   brip_para_cvrg pc(pcp);
2085   pc.do_coverage(img);
2086   vil_image_resource_sptr cov_res;
2087   if (combined)
2088     cov_res = vil_new_image_resource_of_view(pc.get_combined_image());
2089   else
2090     cov_res = vil_new_image_resource_of_view(pc.get_detection_image());
2091   this->add_image(cov_res);
2092 }
2093 
2094 //: it receives an image of line definitions and draws the lines on the tableau.
2095 // The image is expected to have three planes to save (x,y,theta) of each line.
2096 // (x,y) is the position of the edge and theta is the direction angle in radians
draw_line_image()2097 void segv_vil_segmentation_manager::draw_line_image()
2098 {
2099   vgui_dialog file_dlg("Edge File");
2100   static std::string filename = "";
2101   static std::string ext = "*.*";
2102   file_dlg.file("Edge filename:", ext, filename);
2103   if (!file_dlg.ask())
2104     return;
2105   vil_image_view_base_sptr img_sptr = vil_load(filename.c_str());
2106   if (img_sptr->pixel_format() == VIL_PIXEL_FORMAT_FLOAT) {
2107     vil_image_view<float> edge_image(img_sptr);
2108     if (edge_image.nplanes() < 3) {
2109       std::cout << "The image should have at least 3 planes" << std::endl;
2110       return;
2111     }
2112     bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
2113     if (!t2D)
2114       return;
2115 
2116     for (unsigned i=0; i<edge_image.ni(); i++) {
2117       for (unsigned j=0; j<edge_image.nj(); j++) {
2118         double x = edge_image(i,j,0);
2119         double y = edge_image(i,j,1);
2120         double theta = edge_image(i,j,2);
2121         double a,b;
2122         a = x-0.5*std::cos(theta);
2123         b = y-0.5*std::sin(theta);
2124         vgl_point_2d<double> p0(a,b);
2125         a = x+0.5*std::cos(theta);
2126         b = y+0.5*std::sin(theta);
2127         vgl_point_2d<double> p1(a,b);
2128         // define a line
2129         vsol_line_2d_sptr line = new vsol_line_2d(p0,p1);
2130         t2D->add_vsol_line_2d(line);
2131       }
2132     }
2133   }
2134   else
2135     std::cout << "Pixel format: " << img_sptr->pixel_format() << " is not implemented yet" << std::endl;
2136 }
2137 
gradient_mag_angle()2138 void segv_vil_segmentation_manager::gradient_mag_angle()
2139 {
2140   static vgui_style_sptr style =
2141     vgui_style::new_style(0.8f, 0.2f, 0.9f, 1.0f, 3.0f);
2142 
2143   static float sigma = 1.0f;
2144   static bool sep_mag_displ = false;
2145   static bool display_on_image = true;
2146   static unsigned display_interval = 4;
2147   static double vector_scale = 0.7;
2148   vgui_dialog grad_mag_ang_dialog("grad_mag_ang");
2149   grad_mag_ang_dialog.field("sigma", sigma);
2150   grad_mag_ang_dialog.field("DisplayInterval", display_interval);
2151   grad_mag_ang_dialog.field("VectorScale", vector_scale);
2152   grad_mag_ang_dialog.checkbox("Display Mag as Separate Img", sep_mag_displ);
2153   grad_mag_ang_dialog.checkbox("Display Vectors on Img", display_on_image);
2154   grad_mag_ang_dialog.field("Red", style->rgba[0]);
2155   grad_mag_ang_dialog.field("Green", style->rgba[1]);
2156   grad_mag_ang_dialog.field("Blue", style->rgba[2]);
2157   if (!grad_mag_ang_dialog.ask())
2158     return;
2159 
2160   this->clear_display();
2161   vil_image_resource_sptr img = selected_image();
2162   if (!img||!img->ni()||!img->nj())
2163   {
2164     std::cout << "In segv_vil_segmentation_manager::gradient_mag_angle() -  no image\n";
2165     return;
2166   }
2167   unsigned ni = img->ni(), nj = img->nj();
2168   vil_image_view<float> fview = brip_vil_float_ops::convert_to_float(img);
2169   vil_image_view<float> smooth = brip_vil_float_ops::gaussian(fview, sigma);
2170   vil_image_view<float> mag(ni, nj), gx(ni, nj), gy(ni, nj);
2171   brip_vil_float_ops::gradient_mag_comp_3x3(smooth, mag, gx, gy);
2172   std::vector<vsol_line_2d_sptr > lines;
2173   if (sep_mag_displ) {
2174     for (unsigned j = 2; j<nj-2; j+=display_interval)
2175       for (unsigned i = 2; i<ni-2; i+=display_interval) {
2176         double cx =  i, cy = j;
2177         vsol_point_2d_sptr c = new vsol_point_2d(cx, cy);
2178         double dx = 0, dy = 0;
2179         float m = mag(i,j);
2180         float ggx = gx(i,j), ggy = gy(i,j);
2181         if (m>0.1) {
2182           dx = ggx/m; dy = ggy/m;
2183         }
2184         vsol_point_2d_sptr e = new vsol_point_2d(cx+vector_scale*dx,
2185                                                  cy+vector_scale*dy);
2186         vsol_line_2d_sptr l = new vsol_line_2d(c, e);
2187         lines.push_back(l);
2188       }
2189     if (!display_on_image) {
2190       vil_image_view<float> blank(ni, nj);
2191       blank.fill(0.0f);
2192       this->add_image(vil_new_image_resource_of_view(blank));
2193     }
2194     this->draw_lines(lines, style);
2195     this->add_image(vil_new_image_resource_of_view(mag));
2196     return;
2197   }
2198   double gmax = 0.0;
2199   for (unsigned j = 2; j<nj-2; j+=display_interval)
2200     for (unsigned i = 2; i<ni-2; i+=display_interval)
2201       if (mag(i,j)>gmax) gmax = mag(i,j);
2202 
2203   for (unsigned j = 2; j<nj-2; j+=display_interval)
2204     for (unsigned i = 2; i<ni-2; i+=display_interval)
2205     {
2206       double cx =  i, cy = j;
2207       vsol_point_2d_sptr c = new vsol_point_2d(cx, cy);
2208       double dx = gx(i,j)/gmax, dy = gy(i,j)/gmax;
2209       vsol_point_2d_sptr e = new vsol_point_2d(cx+vector_scale*dx,
2210                                                cy+vector_scale*dy);
2211       vsol_line_2d_sptr l = new vsol_line_2d(c, e);
2212       lines.push_back(l);
2213     }
2214   if (!display_on_image) {
2215     vil_image_view<float> blank(ni, nj);
2216     blank.fill(0.0f);
2217     this->add_image(vil_new_image_resource_of_view(blank));
2218   }
2219   this->draw_lines(lines, style);
2220 }
2221 
fft()2222 void segv_vil_segmentation_manager::fft()
2223 {
2224   vil_image_resource_sptr img = selected_image();
2225   if (!img||!img->ni()||!img->nj())
2226   {
2227     std::cout << "In segv_vil_segmentation_manager::fft() - no image\n";
2228     return;
2229   }
2230   vgui_dialog dlg("FFT");
2231   static bool use_mag = true;//else phase
2232   dlg.checkbox("Display FFT Mag?", use_mag);
2233   if (!dlg.ask())
2234     return;
2235   vil_image_view<float> fview = brip_vil_float_ops::convert_to_float(img);
2236   vil_image_view<float> fview_p2;
2237   if (!brip_vil_float_ops::resize_to_power_of_two(fview, fview_p2))
2238     return;
2239   vil_image_view<float> magr, phase;
2240   brip_vil_float_ops::fourier_transform(fview_p2, magr, phase);
2241   if (use_mag)
2242     this->add_image(vil_new_image_resource_of_view(magr));
2243   else
2244     this->add_image(vil_new_image_resource_of_view(phase));
2245 }
2246