contrib/brl/bseg/segv/segv_segmentation_manager.cxx
Go to the documentation of this file.
00001 // This is brl/bseg/segv/segv_segmentation_manager.cxx
00002 #include "segv_segmentation_manager.h"
00003 //:
00004 // \file
00005 // \author J.L. Mundy
00006 
00007 #include <vcl_cstdlib.h> // for vcl_exit()
00008 #include <vcl_iostream.h>
00009 #include <vcl_fstream.h>
00010 #include <vbl/vbl_array_2d.h>
00011 #include <vil1/vil1_image.h>
00012 #include <vil1/vil1_memory_image_of.h>
00013 #include <vil1/vil1_load.h>
00014 #include <vil1/vil1_crop.h>
00015 #include <vdgl/vdgl_digital_curve.h>
00016 #include <vdgl/vdgl_digital_curve_sptr.h>
00017 #include <sdet/sdet_detector_params.h>
00018 #include <sdet/sdet_detector.h>
00019 #include <sdet/sdet_harris_detector_params.h>
00020 #include <sdet/sdet_harris_detector.h>
00021 #include <sdet/sdet_fit_lines_params.h>
00022 #include <sdet/sdet_fit_lines.h>
00023 #include <sdet/sdet_grid_finder_params.h>
00024 #include <sdet/sdet_grid_finder.h>
00025 #include <vgui/vgui.h>
00026 #include <vgui/vgui_find.h>
00027 #include <vgui/vgui_tableau.h>
00028 #include <vgui/vgui_dialog.h>
00029 #include <vgui/vgui_style_sptr.h>
00030 #include <vgui/vgui_style.h>
00031 #include <vgui/vgui_viewer2D_tableau.h>
00032 #include <vgui/vgui_shell_tableau.h>
00033 #include <vgui/vgui_grid_tableau.h>
00034 #include <vgui/vgui_rubberband_tableau.h>
00035 #include <vgui/vgui_range_map_params.h>
00036 #include <bgui/bgui_image_tableau.h>
00037 #include <bgui/bgui_vtol2D_tableau.h>
00038 #include <bgui/bgui_vtol2D_rubberband_client.h>
00039 #include <bgui/bgui_picker_tableau.h>
00040 #include <vsol/vsol_point_2d.h>
00041 #include <vsol/vsol_point_2d_sptr.h>
00042 #include <vsol/vsol_curve_2d.h>
00043 #include <vsol/vsol_curve_2d_sptr.h>
00044 #include <vsol/vsol_polygon_2d_sptr.h>
00045 #include <vsol/vsol_polygon_2d.h>
00046 #include <vtol/vtol_vertex_2d.h>
00047 #include <vtol/vtol_vertex.h>
00048 #include <vtol/vtol_edge_2d.h>
00049 #include <vtol/vtol_one_chain_sptr.h>
00050 #include <vtol/vtol_one_chain.h>
00051 #include <vtol/vtol_intensity_face.h>
00052 #include <bsol/bsol_algs.h>
00053 #include <brip/brip_vil1_float_ops.h>
00054 #include <brip/brip_watershed_params.h>
00055 #include <sdet/sdet_watershed_region_proc_params.h>
00056 #include <sdet/sdet_watershed_region_proc.h>
00057 #include <sdet/sdet_region_proc_params.h>
00058 #include <sdet/sdet_region_proc.h>
00059 #include <strk/strk_region_info_params.h>
00060 #include <strk/strk_region_info.h>
00061 #include <strk/strk_io.h>
00062 
00063 segv_segmentation_manager *segv_segmentation_manager::instance_ = 0;
00064 
00065 segv_segmentation_manager *segv_segmentation_manager::instance()
00066 {
00067   if (!instance_)
00068   {
00069     instance_ = new segv_segmentation_manager();
00070     instance_->init();
00071   }
00072   return segv_segmentation_manager::instance_;
00073 }
00074 
00075 //-----------------------------------------------------------
00076 // constructors/destructor
00077 //
00078 segv_segmentation_manager::segv_segmentation_manager():vgui_wrapper_tableau()
00079 {
00080   first_ = true;
00081 }
00082 
00083 segv_segmentation_manager::~segv_segmentation_manager()
00084 {
00085 }
00086 
00087 //: Set up the tableaux
00088 void segv_segmentation_manager::init()
00089 {
00090   bgui_image_tableau_sptr itab = bgui_image_tableau_new();
00091   bgui_vtol2D_tableau_sptr t2D = bgui_vtol2D_tableau_new(itab);
00092   bgui_vtol2D_rubberband_client* rcl =  new bgui_vtol2D_rubberband_client(t2D);
00093   vgui_rubberband_tableau_sptr rubber = vgui_rubberband_tableau_new(rcl);
00094   vgui_composite_tableau_new comp(t2D,rubber);
00095   bgui_picker_tableau_sptr picktab = bgui_picker_tableau_new(comp);
00096   vgui_viewer2D_tableau_sptr v2D = vgui_viewer2D_tableau_new(picktab);
00097   grid_ = vgui_grid_tableau_new(1,1);
00098   grid_->set_grid_size_changeable(true);
00099   grid_->add_at(v2D, 0, 0);
00100   vgui_shell_tableau_sptr shell = vgui_shell_tableau_new(grid_);
00101   this->add_child(shell);
00102   first_ = true;
00103 }
00104 
00105 //: set the image at the currently selected grid cell
00106 void segv_segmentation_manager::set_selected_grid_image(vil1_image& image)
00107 {
00108   bgui_image_tableau_sptr itab = this->selected_image_tab();
00109   if (!itab)
00110     this->add_image(image);
00111   else
00112     itab->set_image(image);
00113   itab->post_redraw();
00114 }
00115 //: Add an image at the specified grid cell
00116 void segv_segmentation_manager::
00117 add_image_at(vil1_image& image, const unsigned col, const unsigned row)
00118 {
00119   bgui_image_tableau_sptr itab = bgui_image_tableau_new(image);
00120   bgui_vtol2D_tableau_sptr t2D = bgui_vtol2D_tableau_new(itab);
00121   bgui_vtol2D_rubberband_client* rcl =  new bgui_vtol2D_rubberband_client(t2D);
00122   vgui_rubberband_tableau_sptr rubber = vgui_rubberband_tableau_new(rcl);
00123   vgui_composite_tableau_new comp(t2D,rubber);
00124   bgui_picker_tableau_sptr picktab = bgui_picker_tableau_new(comp);
00125   vgui_viewer2D_tableau_sptr v2D = vgui_viewer2D_tableau_new(picktab);
00126   grid_->add_at(v2D, col, row);
00127   itab->post_redraw();
00128 }
00129 
00130 //: Add an image to the currently selected grid cell
00131 void segv_segmentation_manager::add_image(vil1_image& image)
00132 {
00133   unsigned row=0, col=0;
00134   grid_->get_last_selected_position(&col, &row);
00135   this->add_image_at(image, col, row);
00136 }
00137 
00138 //: Get the image tableau for the currently selected grid cell
00139 bgui_image_tableau_sptr segv_segmentation_manager::selected_image_tab()
00140 {
00141   unsigned row=0, col=0;
00142   grid_->get_last_selected_position(&col, &row);
00143   vgui_tableau_sptr top_tab = grid_->get_tableau_at(col, row);
00144   if (top_tab)
00145   {
00146     bgui_image_tableau_sptr itab;
00147     itab.vertical_cast(vgui_find_below_by_type_name(top_tab,
00148                                                     vcl_string("vgui_image_tableau")));
00149     if (itab)
00150       return itab;
00151   }
00152   vcl_cout << "Unable to get bgui_image_tableau at (" << col
00153            << ", " << row << ")\n";
00154   return bgui_image_tableau_sptr();
00155 }
00156 
00157 //: Get the vtol2D tableau for the currently selected grid cell
00158 bgui_vtol2D_tableau_sptr segv_segmentation_manager::selected_vtol2D_tab()
00159 {
00160   unsigned row=0, col=0;
00161   grid_->get_last_selected_position(&col, &row);
00162   vgui_tableau_sptr top_tab = grid_->get_tableau_at(col, row);
00163   if (top_tab)
00164   {
00165     bgui_vtol2D_tableau_sptr v2D;
00166     v2D.vertical_cast(vgui_find_below_by_type_name(top_tab,
00167                                                    vcl_string("bgui_vtol2D_tableau")));
00168     if (v2D)
00169       return v2D;
00170   }
00171   vcl_cout << "Unable to get bgui_vtol2D_tableau at (" << col
00172            << ", " << row << ")\n";
00173   return bgui_vtol2D_tableau_sptr();
00174 }
00175 
00176 //: Get the picker tableau for the currently selected grid cell
00177 bgui_picker_tableau_sptr segv_segmentation_manager::selected_picker_tab()
00178 {
00179   unsigned row=0, col=0;
00180   grid_->get_last_selected_position(&col, &row);
00181   vgui_tableau_sptr top_tab = grid_->get_tableau_at(col, row);
00182   if (top_tab)
00183   {
00184     bgui_picker_tableau_sptr pick;
00185     pick.vertical_cast(vgui_find_below_by_type_name(top_tab,
00186                                                     vcl_string("bgui_picker_tableau")));
00187     if (pick)
00188       return pick;
00189   }
00190   vcl_cout << "Unable to get bgui_picker_tableau at (" << col
00191            << ", " << row << ")\n";
00192   return bgui_picker_tableau_sptr();
00193 }
00194 
00195 //: Get the rubberband tableau at the selected grid cell
00196 vgui_rubberband_tableau_sptr segv_segmentation_manager::selected_rubber_tab()
00197 {
00198   unsigned row=0, col=0;
00199   grid_->get_last_selected_position(&col, &row);
00200   vgui_tableau_sptr top_tab = grid_->get_tableau_at(col, row);
00201   if (top_tab)
00202   {
00203     vgui_rubberband_tableau_sptr rubber;
00204     rubber.vertical_cast(vgui_find_below_by_type_name(top_tab,
00205                                                       vcl_string("vgui_rubberband_tableau")));
00206     if (rubber)
00207       return rubber;
00208   }
00209   vcl_cout << "Unable to get vgui_rubberband_tableau at (" << col
00210            << ", " << row << ")\n";
00211   return vgui_rubberband_tableau_sptr();
00212 }
00213 
00214 vil1_image segv_segmentation_manager::selected_image()
00215 {
00216   bgui_image_tableau_sptr itab = this->selected_image_tab();
00217   if (!itab)
00218     return vil1_image();
00219   return itab->get_image();
00220 }
00221 
00222 vil1_image segv_segmentation_manager::image_at(const unsigned col,
00223                                                const unsigned row)
00224 {
00225   vgui_tableau_sptr top_tab = grid_->get_tableau_at(col, row);
00226   if (!top_tab)
00227     return vil1_image();
00228 
00229   bgui_image_tableau_sptr itab;
00230   itab.vertical_cast(vgui_find_below_by_type_name(top_tab,
00231                                                   vcl_string("vgui_image_tableau")));
00232   if (!itab)
00233   {
00234     vcl_cout << "Unable to get bgui_image_tableau at (" << col
00235              << ", " << row << ")\n";
00236     return vil1_image();
00237   }
00238   return itab->get_image();
00239 }
00240 
00241 void segv_segmentation_manager::quit()
00242 {
00243   vcl_exit(1);
00244 }
00245 
00246 void segv_segmentation_manager::load_image()
00247 {
00248   static bool greyscale = true;
00249   vgui_dialog load_image_dlg("Load image file");
00250   static vcl_string image_filename = "/home/dec/images/cal_image1.tif";
00251   static vcl_string ext = "*.*";
00252   load_image_dlg.file("Image Filename:", ext, image_filename);
00253   load_image_dlg.checkbox("greyscale ", greyscale);
00254   if (!load_image_dlg.ask())
00255     return;
00256 
00257   vil1_image temp = vil1_load(image_filename.c_str()), image;
00258 
00259   if (greyscale)
00260     image = brip_vil1_float_ops::convert_to_grey(temp);
00261   else
00262     image = temp;
00263 
00264   if (first_)
00265   {
00266     this->set_selected_grid_image(image);
00267     first_ = false;
00268   }
00269   else
00270     this->add_image(image);
00271 }
00272 
00273 //-----------------------------------------------------------------------------
00274 //: Clear the display
00275 //-----------------------------------------------------------------------------
00276 void segv_segmentation_manager::clear_display()
00277 {
00278   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00279   if (!t2D)
00280     return;
00281   t2D->clear_all();
00282 }
00283 
00284 //-----------------------------------------------------------------------------
00285 //: Draw edges onto the tableau
00286 //-----------------------------------------------------------------------------
00287 void
00288 segv_segmentation_manager::draw_edges(vcl_vector<vtol_edge_2d_sptr>& edges,
00289                                       bool verts)
00290 {
00291   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00292   if (!t2D)
00293     return;
00294   this->clear_display();
00295 #if 0
00296   vgui_image_tableau_sptr itab = t2D->get_image_tableau();
00297   if (!itab)
00298   {
00299     vcl_cout << "In segv_segmentation_manager::draw_edges - null image tab\n";
00300     return;
00301   }
00302 #endif
00303   for (vcl_vector<vtol_edge_2d_sptr>::iterator eit = edges.begin();
00304        eit != edges.end(); eit++)
00305   {
00306     t2D->add_edge(*eit);
00307     //optionally display the edge vertices
00308     if (verts)
00309     {
00310       if ((*eit)->v1())
00311       {
00312         vtol_vertex_2d_sptr v1 = (*eit)->v1()->cast_to_vertex_2d();
00313         t2D->add_vertex(v1);
00314       }
00315       if ((*eit)->v2())
00316       {
00317         vtol_vertex_2d_sptr v2 = (*eit)->v2()->cast_to_vertex_2d();
00318         t2D->add_vertex(v2);
00319       }
00320     }
00321   }
00322   t2D->post_redraw();
00323 }
00324 
00325 
00326 //-----------------------------------------------------------------------------
00327 //: Draw polylines on the tableau
00328 //-----------------------------------------------------------------------------
00329 void segv_segmentation_manager::
00330 draw_polylines(vcl_vector<vsol_polyline_2d_sptr > const& polys)
00331 {
00332   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00333   if (!t2D)
00334     return;
00335   //this->clear_display();
00336   vgui_image_tableau_sptr itab = t2D->get_image_tableau();
00337   if (!itab)
00338   {
00339     vcl_cout << "In segv_segmentation_manager::draw_edges - null image tab\n";
00340     return;
00341   }
00342   for (vcl_vector<vsol_polyline_2d_sptr>::const_iterator pit = polys.begin();
00343        pit != polys.end(); pit++)
00344     {
00345       t2D->add_vsol_polyline_2d(*pit);
00346     }
00347 
00348   t2D->post_redraw();
00349 }
00350 
00351 //-----------------------------------------------------------------------------
00352 //: Draw line segments on the tableau
00353 //-----------------------------------------------------------------------------
00354 void segv_segmentation_manager::
00355 draw_lines(vcl_vector<vsol_line_2d_sptr > const& lines,
00356            const vgui_style_sptr& style)
00357 {
00358   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00359   if (!t2D)
00360     return;
00361   //this->clear_display();
00362 #if 0
00363   vgui_image_tableau_sptr itab = t2D->get_image_tableau();
00364   if (!itab)
00365   {
00366     vcl_cout << "In segv_segmentation_manager::draw_edges - null image tab\n";
00367     return;
00368   }
00369 #endif
00370   for (vcl_vector<vsol_line_2d_sptr>::const_iterator lit = lines.begin();
00371        lit != lines.end(); lit++)
00372     {
00373       t2D->add_vsol_line_2d(*lit,style);
00374     }
00375 
00376   t2D->post_redraw();
00377 }
00378 
00379 //-----------------------------------------------------------------------------
00380 //: Draw points on the tableau
00381 //-----------------------------------------------------------------------------
00382 void segv_segmentation_manager::
00383 draw_points(vcl_vector<vsol_point_2d_sptr> const& points, const vgui_style_sptr& style)
00384 {
00385   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00386   if (!t2D)
00387     return;
00388   //this->clear_display();
00389   vgui_image_tableau_sptr itab = t2D->get_image_tableau();
00390   if (!itab)
00391   {
00392     vcl_cout << "In segv_segmentation_manager::draw_edges - null image tab\n";
00393     return;
00394   }
00395   for (vcl_vector<vsol_point_2d_sptr>::const_iterator pit = points.begin();
00396        pit != points.end(); pit++)
00397     {
00398       t2D->add_vsol_point_2d(*pit,style);
00399     }
00400 
00401   t2D->post_redraw();
00402 }
00403 
00404 void segv_segmentation_manager::draw_regions(vcl_vector<vtol_intensity_face_sptr>& regions,
00405                                              bool verts)
00406 {
00407   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00408   if (!t2D)
00409     return;
00410   for (vcl_vector<vtol_intensity_face_sptr>::iterator rit = regions.begin();
00411        rit != regions.end(); rit++)
00412     {
00413       vtol_face_2d_sptr f = (*rit)->cast_to_face_2d();
00414       t2D->add_face(f);
00415       if (verts)
00416       {
00417         vcl_vector<vtol_vertex_sptr> vts;
00418         f->vertices(vts);
00419         for (vcl_vector<vtol_vertex_sptr>::iterator vit = vts.begin();
00420              vit != vts.end(); vit++)
00421           {
00422             vtol_vertex_2d_sptr v = (*vit)->cast_to_vertex_2d();
00423             t2D->add_vertex(v);
00424           }
00425       }
00426     }
00427   t2D->post_redraw();
00428 }
00429 
00430 void segv_segmentation_manager::original_image()
00431 {
00432 #if 0
00433   if (img_)
00434   {
00435     t2D->get_image_tableau()->set_image(img_);
00436     t2D->post_redraw();
00437   }
00438 #endif
00439 }
00440 
00441 void segv_segmentation_manager::roi()
00442 {
00443   vil1_image img = selected_image();
00444   if (!img)
00445   {
00446     vcl_cout << "In segv_segmentation_manager::roi() - no image\n";
00447     return;
00448   }
00449   float x0=0, y0=0, x1=0, y1=0;
00450   bgui_picker_tableau_sptr picktab = this->selected_picker_tab();
00451   vcl_cout << "Choose upper left corner of ROI.\n";
00452   picktab->pick_point(&x0,&y0);
00453   vcl_cout << "picked (x="<<x0<<", y="<<y0<<")\n"
00454            << "Choose lower right corner of ROI.\n";
00455   picktab->pick_point(&x1,&y1);
00456   vcl_cout << "picked (x="<<x1<<", y="<<y1<<")\n";
00457   if ( (x1 > x0) && (y1 > y0) )
00458   {
00459     int w = int(x1 - x0);
00460     int h = int(y1 - y0);
00461     vil1_image cropped = vil1_crop(img,int(x0),int(y0),w,h);
00462     vcl_cout << "cropped x=" <<x0<<" y=" <<y0<< " w=" <<w<<" h=" <<h<< '\n';
00463 #if 0
00464     if (cropped)
00465     {
00466       img = cropped;
00467       t2D->get_image_tableau()->set_image(cropped);
00468       t2D->post_redraw();
00469       return;
00470     }
00471 #endif
00472     if (cropped)
00473     {
00474       this->add_image(cropped);
00475       return;
00476     }
00477     vcl_cout << "crop failed.\n";
00478     return;
00479   }
00480   vcl_cout << "invalid ROI\n";
00481   return;
00482 }
00483 
00484 void segv_segmentation_manager::gaussian()
00485 {
00486   vil1_image img = selected_image();
00487   if (!img)
00488   {
00489     vcl_cout << "In segv_segmentation_manager::gaussian() - no image\n";
00490     return;
00491   }
00492   static float sigma = 1.0;
00493   vgui_dialog gauss_dialog("Gaussian Smooth");
00494   gauss_dialog.field("Gaussian sigma", sigma);
00495   if (!gauss_dialog.ask())
00496     return;
00497   vil1_memory_image_of<float> input(img);
00498   vil1_memory_image_of<float> smooth = brip_vil1_float_ops::gaussian(input, sigma);
00499   vil1_memory_image_of<unsigned char> char_smooth =
00500     brip_vil1_float_ops::convert_to_byte(smooth);
00501   this->add_image(char_smooth);
00502 }
00503 
00504 void segv_segmentation_manager::convolution()
00505 {
00506   vgui_dialog kernel_dlg("Load Kernel");
00507   static vcl_string kernel_filename = "c:/images";
00508   static vcl_string ext = "*.*";
00509   kernel_dlg.file("Kernel Filename:", ext, kernel_filename);
00510   if (!kernel_dlg.ask())
00511     return;
00512   vbl_array_2d<float> kernel = brip_vil1_float_ops::load_kernel(kernel_filename);
00513 
00514   //convert input image
00515   vil1_image img = selected_image();
00516   if (!img)
00517   {
00518     vcl_cout << "In segv_segmentation_manager::convolution() - no image\n";
00519     return;
00520   }
00521   vil1_memory_image_of<unsigned char> temp(img);
00522   vil1_memory_image_of<float> temp2 = brip_vil1_float_ops::convert_to_float(temp);
00523 
00524   //convolve
00525   vil1_memory_image_of<float> conv = brip_vil1_float_ops::convolve(temp2, kernel);
00526 
00527   //convert back to unsigned char
00528   vil1_memory_image_of<unsigned char> char_conv =
00529     brip_vil1_float_ops::convert_to_byte(conv);
00530 
00531   //display the image
00532   this->add_image(char_conv);
00533 }
00534 
00535 void segv_segmentation_manager::downsample()
00536 {
00537   vil1_image img = selected_image();
00538   if (!img)
00539   {
00540     vcl_cout << "In segv_segmentation_manager::downsample) - no image\n";
00541     return;
00542   }
00543   static float filter_factor = 0.36f;
00544   vgui_dialog downsample_dialog("Downsample");
00545   downsample_dialog.field("Bert-Adelson Factor", filter_factor);
00546   if (!downsample_dialog.ask())
00547     return;
00548   vil1_memory_image_of<unsigned char> input(img);
00549   vil1_memory_image_of<float> inputf = brip_vil1_float_ops::convert_to_float(input);
00550   vil1_memory_image_of<float> half_res =
00551     brip_vil1_float_ops::half_resolution(inputf, filter_factor);
00552   vil1_memory_image_of<unsigned char> char_half_res =
00553     brip_vil1_float_ops::convert_to_byte(half_res);
00554   this->add_image(char_half_res);
00555 }
00556 
00557 void segv_segmentation_manager::harris_measure()
00558 {
00559   vil1_image img = selected_image();
00560   if (!img)
00561   {
00562     vcl_cout << "In segv_segmentation_manager::harris_measure) - no image\n";
00563     return;
00564   }
00565   static sdet_harris_detector_params hdp;
00566   vgui_dialog harris_dialog("harris");
00567   harris_dialog.field("sigma", hdp.sigma_);
00568   harris_dialog.field("thresh", hdp.thresh_);
00569   harris_dialog.field("N = 2n+1, (n)", hdp.n_);
00570   harris_dialog.field("Max No Corners(percent)", hdp.percent_corners_);
00571   harris_dialog.field("scale_factor", hdp.scale_factor_);
00572   if (!harris_dialog.ask())
00573     return;
00574   sdet_harris_detector hd(hdp);
00575   hd.set_image(img);
00576   hd.extract_corners();
00577   vcl_vector<vsol_point_2d_sptr>& points = hd.get_points();
00578   int N = points.size();
00579   if (!N)
00580     return;
00581   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00582   if (!t2D)
00583     return;
00584   t2D->clear_all();
00585   for (int i=0; i<N; i++)
00586     t2D->add_vsol_point_2d(points[i]);
00587   t2D->post_redraw();
00588 }
00589 
00590 void segv_segmentation_manager::beaudet_measure()
00591 {
00592   vil1_image img = selected_image();
00593   if (!img)
00594   {
00595     vcl_cout <<"In segv_segmentation_manager::beaudet_measure) - no image\n";
00596     return;
00597   }
00598   static float sigma = 1.0f;
00599   //static float scale_factor = 0.04f;
00600   //static int n = 2;
00601   static float cmax=100;
00602   vgui_dialog harris_dialog("beaudet");
00603   harris_dialog.field("sigma", sigma);
00604   harris_dialog.field("range", cmax);
00605   if (!harris_dialog.ask())
00606     return;
00607   int w = img.width(), h = img.height();
00608   vil1_memory_image_of<unsigned char> input(img);
00609   vil1_memory_image_of<float> inputf = brip_vil1_float_ops::convert_to_float(input);
00610   vil1_memory_image_of<float> smooth = brip_vil1_float_ops::gaussian(inputf, sigma);
00611   vil1_memory_image_of<float> Ixx, Ixy, Iyy, b;
00612   Ixx.resize(w,h);  Ixy.resize(w,h);   Iyy.resize(w,h);
00613   brip_vil1_float_ops::hessian_3x3(smooth, Ixx, Ixy, Iyy);
00614   b = brip_vil1_float_ops::beaudet(Ixx, Ixy, Iyy);
00615   vil1_memory_image_of<unsigned char> uchar_b =
00616     brip_vil1_float_ops::convert_to_byte(b,0.0f, cmax);
00617   this->add_image(uchar_b);
00618 }
00619 
00620 void segv_segmentation_manager::vd_edges()
00621 {
00622   this->clear_display();
00623   static bool agr = true;
00624   static sdet_detector_params dp;
00625   static float nm = 2.0;
00626 
00627   vgui_dialog vd_dialog("VD Edges");
00628   vd_dialog.field("Gaussian sigma", dp.smooth);
00629   vd_dialog.field("Noise Threshold", nm);
00630   vd_dialog.checkbox("Automatic Threshold", dp.automatic_threshold);
00631   vd_dialog.checkbox("Agressive Closure", agr);
00632   vd_dialog.checkbox("Compute Junctions", dp.junctionp);
00633   if (!vd_dialog.ask())
00634     return;
00635   dp.noise_multiplier=nm;
00636   if (agr)
00637     dp.aggressive_junction_closure=1;
00638   else
00639     dp.aggressive_junction_closure=0;
00640   vil1_image img = selected_image();
00641   if (!img)
00642   {
00643     vcl_cout << "In segv_segmentation_manager::vd_edges() - no image\n";
00644     return;
00645   }
00646   sdet_detector det(dp);
00647   det.SetImage(img);
00648 
00649   det.DoContour();
00650   vcl_vector<vtol_edge_2d_sptr>* edges = det.GetEdges();
00651   if (edges)
00652     this->draw_edges(*edges, true);
00653 }
00654 
00655 void segv_segmentation_manager::regions()
00656 {
00657   this->clear_display();
00658   static bool debug = false;
00659   static bool agr = true;
00660   static bool residual = false;
00661   static sdet_detector_params dp;
00662   static float nm = 1.0;
00663   vgui_dialog region_dialog("Edgel Regions");
00664   region_dialog.field("Gaussian sigma", dp.smooth);
00665   region_dialog.field("Noise Threshold", nm);
00666   region_dialog.checkbox("Automatic Threshold", dp.automatic_threshold);
00667   region_dialog.checkbox("Agressive Closure", agr);
00668   region_dialog.checkbox("Compute Junctions", dp.junctionp);
00669   region_dialog.checkbox("Debug", debug);
00670   region_dialog.checkbox("Residual Image", residual);
00671   if (!region_dialog.ask())
00672     return;
00673   dp.noise_multiplier=nm;
00674   if (agr)
00675     dp.aggressive_junction_closure=1;
00676   else
00677     dp.aggressive_junction_closure=0;
00678 
00679   vil1_image img = selected_image();
00680   if (!img)
00681   {
00682     vcl_cout << "In segv_segmentation_manager::regions() - no image\n";
00683     return;
00684   }
00685   sdet_region_proc_params rpp(dp, true, debug, 2);
00686   sdet_region_proc rp(rpp);
00687   rp.set_image(img);
00688   rp.extract_regions();
00689   if (debug)
00690   {
00691     vil1_image ed_img = rp.get_edge_image();
00692 #if 0
00693     vgui_image_tableau_sptr itab =  t2D->get_image_tableau();
00694     if (!itab)
00695     {
00696       vcl_cout << "In segv_segmentation_manager::regions() - null image tableau\n";
00697       return;
00698     }
00699     itab->set_image(ed_img);
00700     itab->post_redraw();
00701 #endif
00702     this->add_image(ed_img);
00703   }
00704   if (!debug)
00705   {
00706     vcl_vector<vtol_intensity_face_sptr>& regions = rp.get_regions();
00707     this->draw_regions(regions, true);
00708   }
00709   if (residual)
00710   {
00711     vil1_image res_img = rp.get_residual_image();
00712 #if 0
00713     vgui_image_tableau_sptr itab =  t2D->get_image_tableau();
00714     if (!itab)
00715     {
00716       vcl_cout << "In segv_segmentation_manager::regions() - null image tableau\n";
00717       return;
00718     }
00719     itab->set_image(res_img);
00720     itab->post_redraw();
00721 #endif
00722     this->add_image(res_img);
00723   }
00724 }
00725 
00726 // Test calculated camera parameter matrices (K and M) by reading from a file.
00727 // file should be in the following format:
00728 // K n_views M1, M2, M3... Mn_views
00729 // where K is the 3x3 intrinsic parameter matrix,
00730 // n_views is an integer value > 0,
00731 // and M1 - Mn_views are the 4x4 homogeneous extrinsic parameter matrices.
00732 void segv_segmentation_manager::test_camera_parms()
00733 {
00734   this->clear_display();
00735   vgui_dialog tcp_dialog("Test Camera Parms");
00736   static vcl_string camera_parms_filename = "/home/dec/camera_parms.left.txt";
00737   static vcl_string input_points_filename = "/home/dec/point_correspondences.left.txt";
00738   static vcl_string ext = "*.*";
00739   static bool show_input_points = true;
00740   static bool show_calculated_points = true;
00741   static int view_num = 1;
00742   tcp_dialog.file("camera parms file:", ext, camera_parms_filename);
00743   tcp_dialog.file("input points file:", ext, input_points_filename);
00744   tcp_dialog.field("view number:",view_num);
00745   tcp_dialog.checkbox("show input points",show_input_points);
00746   tcp_dialog.checkbox("show calculated points",show_calculated_points);
00747 
00748   if (!tcp_dialog.ask())
00749     return;
00750 
00751   if (show_calculated_points)
00752   {
00753     // read K
00754     vnl_matrix_fixed<double,3,3> K;
00755     vcl_ifstream parms_instream(camera_parms_filename.c_str());
00756 
00757     double k_values[9];
00758     for (int i=0; i < 9; i++)
00759     {
00760       parms_instream >> k_values[i];
00761       //vcl_cout << "k_values["<<i<<"] = "<<k_values[i] << '\n';
00762     }
00763     K.put(0,0,k_values[0]); K.put(0,1,k_values[1]); K.put(0,2,k_values[2]);
00764     K.put(1,0,k_values[3]); K.put(1,1,k_values[4]); K.put(1,2,k_values[5]);
00765     K.put(2,0,k_values[6]); K.put(2,1,k_values[7]); K.put(2,2,k_values[8]);
00766 
00767     vcl_cout << "K =\n" << K << '\n';
00768 
00769     // read number of views
00770     int n_views = 0;
00771     parms_instream >> n_views;
00772 
00773     // read M
00774     vnl_matrix_fixed<double,3,4> M;
00775 
00776     double m_values[12];
00777     double dummy;
00778     for (int v = 1; v <= view_num; v++)
00779     {
00780       if (v > n_views)
00781       {
00782         vcl_cout << "error: view number > n_views\n";
00783         break;
00784       }
00785       for (int i=0; i < 12; i++)
00786       {
00787         parms_instream >> m_values[i];
00788         //vcl_cout << "m_values["<<i<<"] = "<<m_values[i] << '\n';
00789       }
00790       // read 4th row, should just be [0 0 0 1]
00791       for (int i=0; i < 4; i++)
00792       {
00793         parms_instream >> dummy;
00794       }
00795     }
00796     parms_instream.close();
00797 
00798     M.put(0,0,m_values[0]); M.put(0,1,m_values[1]); M.put(0,2,m_values[2]);  M.put(0,3,m_values[3]);
00799     M.put(1,0,m_values[4]); M.put(1,1,m_values[5]); M.put(1,2,m_values[6]);  M.put(1,3,m_values[7]);
00800     M.put(2,0,m_values[8]); M.put(2,1,m_values[9]); M.put(2,2,m_values[10]); M.put(2,3,m_values[11]);
00801 
00802     vcl_cout << "M =\n" << M << '\n';
00803 
00804     //transform the grid points to the image
00805     sdet_grid_finder_params gfp;
00806     sdet_grid_finder gf(gfp);
00807     vcl_vector<vsol_point_2d_sptr> calculated_points;
00808     gf.transform_grid_points(K,M,calculated_points);
00809 
00810     // draw points on image
00811     vgui_style_sptr style = vgui_style::new_style(1.0f,0.0f,0.0f,5.0f,1.0f);
00812     this->draw_points(calculated_points,style);
00813   }
00814   if (show_input_points)
00815   {
00816     vcl_vector<vsol_point_2d_sptr> input_points;
00817     vcl_ifstream points_instream(input_points_filename.c_str());
00818     int n_points = 0;
00819     // read number of points
00820     points_instream >> n_points;
00821     // read grid points, discard
00822     double dummy;
00823     double* points_x = new double[n_points];
00824     double* points_y = new double[n_points];
00825     for (int i = 0; i < n_points; i++)
00826     {
00827       points_instream >> dummy; //x
00828       points_instream >> dummy; //y
00829     }
00830 
00831     // read number of views
00832     int n_views = 0;
00833     points_instream >> n_views;
00834 
00835     // read grid points for view number
00836     for (int v = 1; v <= view_num; v++)
00837     {
00838       if (v > n_views)
00839       {
00840         vcl_cout << "error: view number > n_views\n";
00841         break;
00842       }
00843       for (int i = 0; i < n_points; i++)
00844       {
00845         points_instream >> points_x[i];
00846         points_instream >> points_y[i];
00847       }
00848     }
00849     for (int i = 0; i < n_points; i++)
00850     {
00851       vsol_point_2d_sptr point = new vsol_point_2d(points_x[i],points_y[i]);
00852       input_points.push_back(point);
00853     }
00854     vgui_style_sptr style = vgui_style::new_style(0.0f,1.0f,0.0f,4.0f,1.0f);
00855     this->draw_points(input_points,style);
00856     delete[] points_x;
00857     delete[] points_y;
00858   }
00859 
00860   return;
00861 }
00862 
00863 void segv_segmentation_manager::fit_lines()
00864 {
00865   this->clear_display();
00866   static sdet_grid_finder_params gfp;
00867   vcl_vector<vcl_string> choices;
00868   gfp.get_debug_choices(choices);
00869   static bool agr = true;
00870   static sdet_detector_params dp;
00871   dp.borderp=false;
00872   static sdet_fit_lines_params flp;
00873   static float nm = 2.0;
00874   static bool detect_grid=true;
00875   //static bool grid_debug=false;
00876   static bool matched_lines = false;
00877   static bool manual_pt_selection = false;
00878   vgui_dialog vd_dialog("Fit Lines");
00879   vd_dialog.field("Gaussian sigma", dp.smooth);
00880   vd_dialog.field("Noise Threshold", nm);
00881   vd_dialog.checkbox("Automatic Threshold", dp.automatic_threshold);
00882   vd_dialog.checkbox("Agressive Closure", agr);
00883   vd_dialog.checkbox("Compute Junctions", dp.junctionp);
00884   vd_dialog.field("Min Fit Length", flp.min_fit_length_);
00885   vd_dialog.field("RMS Distance", flp.rms_distance_);
00886   vd_dialog.field("Angle Tolerance", gfp.angle_tol_);
00887   vd_dialog.field("Line Count Threshold", gfp.thresh_);
00888   vd_dialog.checkbox("Detect Grid", detect_grid);
00889   vd_dialog.checkbox("Grid Debug Output", gfp.verbose_);
00890   vd_dialog.checkbox("Matched Lines", matched_lines);
00891   vd_dialog.checkbox("Use Manual Point Selection",manual_pt_selection);
00892   vd_dialog.choice("Choose Debug Line Display", choices, gfp.debug_state_);
00893   if (!vd_dialog.ask())
00894     return;
00895   dp.noise_multiplier=nm;
00896   if (agr)
00897     dp.aggressive_junction_closure=1;
00898   else
00899     dp.aggressive_junction_closure=0;
00900   dp.borderp = false;
00901   sdet_detector det(dp);
00902   vil1_image img = selected_image();
00903 
00904   if (!img)
00905   {
00906     vcl_cout << "In segv_segmentation_manager::lines() - no image\n";
00907     return;
00908   }
00909   det.SetImage(img);
00910 
00911   det.DoContour();
00912   vcl_vector<vtol_edge_2d_sptr>* edges = det.GetEdges();
00913   if (!edges)
00914   {
00915     vcl_cout << "No edges to fit lines\n";
00916     return;
00917   }
00918   sdet_fit_lines fl(flp);
00919   fl.set_edges(*edges);
00920   fl.fit_lines();
00921   vcl_vector<vsol_line_2d_sptr> lines = fl.get_line_segs();
00922   if (detect_grid)
00923   {
00924     sdet_grid_finder gf(gfp);
00925     if (!gf.set_lines(img.width(), img.height(), lines))
00926     {
00927       vcl_cout << "Less than two dominant groups\n";
00928       return;
00929     }
00930     vcl_vector<vsol_line_2d_sptr> mapped_lines;
00931     vcl_vector<vsol_line_2d_sptr> mapped_grid_lines;
00932     if (manual_pt_selection)
00933     {
00934       bgui_picker_tableau_sptr picktab = this->selected_picker_tab();
00935       if (!picktab)
00936         return;
00937       vsol_point_2d_sptr corners[4];
00938       vcl_cout << "Select the four corners of the grid, starting with "
00939                << "the upper left and moving clockwise.\n";
00940       for (int p=0; p<4; p++)
00941       {
00942         float x=0, y=0;
00943         picktab->pick_point(&x,&y);
00944         vcl_cout << "corner "<< p <<" (x=" << x << ", y=" << y <<")\n";
00945         corners[p] = new vsol_point_2d(x,y);
00946       }
00947       gf.compute_manual_homography(corners[0],corners[1],
00948                                    corners[2],corners[3]);
00949       if (!gfp.debug_state_)
00950         //gf.get_mapped_lines(mapped_lines);
00951         gf.get_backprojected_grid(mapped_lines);
00952       else
00953         gf.get_debug_lines(mapped_lines);
00954 
00955       this->draw_lines(mapped_lines);
00956       return;
00957     }
00958 
00959     gf.compute_homography();
00960     // double-check grid match
00961     if (!gf.check_grid_match(img))
00962     {
00963       // for now just display message - if this was a video process
00964       // we would want to disregard this homography and move on
00965       vcl_cout << "warning: grid match failed double-check\n";
00966     }
00967     if (!gfp.debug_state_)
00968       //gf.get_mapped_lines(mapped_lines);
00969       gf.get_backprojected_grid(mapped_lines);
00970     else
00971     {
00972       gf.get_debug_lines(mapped_lines);
00973       gf.get_debug_grid_lines(mapped_grid_lines);
00974       vgui_style_sptr style = vgui_style::new_style(1.0f,0.0f,0.0f,1.0f,1.0f);
00975       this->draw_lines(mapped_grid_lines,style);
00976     }
00977     this->draw_lines(mapped_lines);
00978     return;
00979   }
00980   this->draw_lines(lines);
00981 }
00982 
00983 void segv_segmentation_manager::test_face()
00984 {
00985   vil1_image img = selected_image();
00986   if (!img)
00987   {
00988     vcl_cout << "In segv_segmentation_manager::test_face() - no image\n";
00989     return;
00990   }
00991   int sx = img.cols(), sy = img.rows();
00992   if (sx<10||sy<10)
00993     return;
00994 
00995   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00996   if (!t2D)
00997     return;
00998   t2D->set_foreground(0.0, 1.0, 0.0);
00999   vsol_point_2d_sptr pa = new vsol_point_2d(1,1);
01000   vsol_point_2d_sptr pb = new vsol_point_2d(sx-2,1);
01001   vsol_point_2d_sptr pc = new vsol_point_2d(sx-2,sy-2);
01002   vsol_point_2d_sptr pd = new vsol_point_2d(1,sy-2);
01003   vsol_curve_2d_sptr cab = new vdgl_digital_curve(pa, pb);
01004   vsol_curve_2d_sptr cbc = new vdgl_digital_curve(pb, pc);
01005   vsol_curve_2d_sptr ccd = new vdgl_digital_curve(pc, pd);
01006   vsol_curve_2d_sptr cda = new vdgl_digital_curve(pd, pa);
01007   vtol_vertex_2d_sptr va = new vtol_vertex_2d(*pa);
01008   vtol_vertex_2d_sptr vb = new vtol_vertex_2d(*pb);
01009   vtol_vertex_2d_sptr vc = new vtol_vertex_2d(*pc);
01010   vtol_vertex_2d_sptr vd = new vtol_vertex_2d(*pd);
01011   vtol_edge_2d_sptr eab = new vtol_edge_2d(va, vb, cab);
01012   vtol_edge_2d_sptr ebc = new vtol_edge_2d(vb, vc, cbc);
01013   vtol_edge_2d_sptr ecd = new vtol_edge_2d(vc, vd, ccd);
01014   vtol_edge_2d_sptr eda = new vtol_edge_2d(vd, va, cda);
01015   vcl_vector<vtol_edge_sptr> edges;
01016   edges.push_back(eab->cast_to_edge());
01017   edges.push_back(ebc->cast_to_edge());
01018   edges.push_back(ecd->cast_to_edge());
01019   edges.push_back(eda->cast_to_edge());
01020   vtol_one_chain_sptr b_onch = new vtol_one_chain(edges,true);
01021   vtol_face_2d_sptr b_f = new  vtol_face_2d(b_onch);
01022 
01023   int px = sx/2, py = sy/2;
01024   vsol_point_2d_sptr p1 = new vsol_point_2d(px+3,py+3);
01025   vsol_point_2d_sptr p2 = new vsol_point_2d(px,py-3);
01026   vsol_point_2d_sptr p3 = new vsol_point_2d(px-3,py+3);
01027   vsol_curve_2d_sptr c12 = new vdgl_digital_curve(p1, p2);
01028   vsol_curve_2d_sptr c23 = new vdgl_digital_curve(p2, p3);
01029   vsol_curve_2d_sptr c31 = new vdgl_digital_curve(p3, p1);
01030   vtol_vertex_2d_sptr v1 = new vtol_vertex_2d(*p1);
01031   vtol_vertex_2d_sptr v2 = new vtol_vertex_2d(*p2);
01032   vtol_vertex_2d_sptr v3 = new vtol_vertex_2d(*p3);
01033   vtol_edge_2d_sptr e12 = new vtol_edge_2d(v1, v2, c12);
01034   vtol_edge_2d_sptr e23 = new vtol_edge_2d(v2, v3, c23);
01035   vtol_edge_2d_sptr e31 = new vtol_edge_2d(v3, v1, c31);
01036   edges.clear();
01037   edges.push_back(e12->cast_to_edge());
01038   edges.push_back(e23->cast_to_edge());
01039   edges.push_back(e31->cast_to_edge());
01040   vtol_one_chain_sptr t_onch = new vtol_one_chain(edges,true);
01041   vtol_face_2d_sptr t_f = new  vtol_face_2d(t_onch);
01042   t2D->add_face(b_f);   t2D->add_face(t_f);
01043   t2D->set_foreground(1.0, 0.0, 0.0);
01044   for (int x = 0; x<sx; x+=20)
01045     for (int y = 0; y<sy; y+=20)
01046     {
01047       vtol_vertex_2d_sptr v = new vtol_vertex_2d(x, y);
01048       t2D->add_vertex(v);
01049     }
01050 }
01051 
01052 void segv_segmentation_manager::test_digital_lines()
01053 {
01054   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
01055   if (!t2D)
01056     return;
01057   t2D->set_foreground(1.0, 1.0, 0.0);
01058   vsol_point_2d_sptr pa = new vsol_point_2d(0,0);
01059   vsol_point_2d_sptr pb = new vsol_point_2d(20,0);
01060   vsol_point_2d_sptr pc = new vsol_point_2d(10, 20);
01061   vsol_point_2d_sptr pd = new vsol_point_2d(20,20);
01062   vsol_point_2d_sptr pe = new vsol_point_2d(20, 10);
01063   vsol_point_2d_sptr pf = new vsol_point_2d(0, 20);
01064   vdgl_digital_curve_sptr cab = new vdgl_digital_curve(pa, pb);
01065   vdgl_digital_curve_sptr cac = new vdgl_digital_curve(pa, pc);
01066   vdgl_digital_curve_sptr cad = new vdgl_digital_curve(pa, pd);
01067   vdgl_digital_curve_sptr cae = new vdgl_digital_curve(pa, pe);
01068   vdgl_digital_curve_sptr caf = new vdgl_digital_curve(pa, pf);
01069 }
01070 
01071 void segv_segmentation_manager::display_IHS()
01072 {
01073   vil1_image img = selected_image();
01074   if (!img)
01075   {
01076     vcl_cout << "In segv_segmentation_manager::display_IHS() - no image\n";
01077     return;
01078   }
01079   vil1_memory_image_of<float> I,H,S;
01080 
01081   vil1_memory_image_of<vil1_rgb<unsigned char> > in_image(img), out_image;
01082   if (!in_image)
01083     return;
01084   brip_vil1_float_ops::convert_to_IHS(in_image, I, H, S);
01085   brip_vil1_float_ops::display_IHS_as_RGB(I, H, S, out_image);
01086   this->add_image(out_image);
01087 }
01088 
01089 vtol_face_2d_sptr
01090 segv_segmentation_manager::face_at(const int col, const int row)
01091 {
01092   vtol_face_2d_sptr out;
01093   bgui_vtol2D_tableau_sptr v2D;
01094   vgui_tableau_sptr top_tab = grid_->get_tableau_at(col, row);
01095   if (!top_tab)
01096     return out;
01097   v2D.vertical_cast(vgui_find_below_by_type_name(top_tab,
01098                                                  vcl_string("bgui_vtol2D_tableau")));
01099   if (!v2D)
01100     return out;
01101 
01102   vtol_topology_object_sptr to = v2D->get_temp();
01103   if (!to)
01104     return out;
01105   vtol_face_sptr f = to->cast_to_face();
01106   out = f->cast_to_face_2d();
01107   return out;
01108 }
01109 
01110 void segv_segmentation_manager::compute_mutual_info()
01111 {
01112   static strk_region_info_params rip;
01113   static bool mapped_ = false;
01114   vgui_dialog info_dialog("Mutual Information");
01115   info_dialog.field("Min Gradient Magnitude", rip.min_gradient_);
01116   info_dialog.field("Parzen Sigma", rip.parzen_sigma_);
01117   info_dialog.checkbox("Add Gradient Info", rip.gradient_info_);
01118   info_dialog.checkbox("Add Color Info", rip.color_info_);
01119   info_dialog.checkbox("Verbose", rip.verbose_);
01120   info_dialog.checkbox("Debug", rip.debug_);
01121   info_dialog.checkbox("Show Mapped Image", mapped_);
01122   if (!info_dialog.ask())
01123     return;
01124   vcl_cout << rip << '\n';
01125   //Get the v2D tableau at each grid.  V2D0 is considered the model
01126   unsigned row= 0, col=0, nrows = grid_->rows(), ncols = grid_->cols();
01127   grid_->get_last_selected_position(&col, &row);
01128   int other_row = nrows-1-row, other_col = ncols-1-col;
01129   vtol_face_2d_sptr f0 = face_at(col, row);
01130   vtol_face_2d_sptr fi = face_at(other_col, other_row);
01131   //if (!f0||!fi)
01132   //  return;
01133   vil1_image img_0 = this->image_at(col, row);
01134   vil1_image img_i = this->image_at(other_col, other_row);
01135   if (!img_0||!img_i)
01136     return;
01137   strk_region_info ri(rip);
01138   ri.set_face_0(f0);
01139   ri.set_face_i(fi);
01140   ri.set_image_0(img_0);
01141   ri.set_image_i(img_i);
01142   if (mapped_)
01143   {
01144     vil1_memory_image_of<unsigned char> im0 = ri.image_0();
01145     this->set_image_at(col, row, im0);
01146     vil1_memory_image_of<unsigned char> imi = ri.image_i();
01147     this->set_image_at(other_col, other_row, imi);
01148   }
01149   ri.evaluate_info();
01150 }
01151 
01152 void segv_segmentation_manager::compute_background_info()
01153 {
01154   if (!foreground_face_||!background_face_)
01155   {
01156     vcl_cout << "No foreground or background faces defined\n";
01157     return;
01158   }
01159   static strk_region_info_params rip;
01160   vgui_dialog bf_dialog("Background/Forground Entropy");
01161   bf_dialog.field("Min Gradient Magnitude", rip.min_gradient_);
01162   bf_dialog.field("Parzen Sigma", rip.parzen_sigma_);
01163   bf_dialog.checkbox("Add Gradient Info", rip.gradient_info_);
01164   bf_dialog.checkbox("Add Color Info", rip.color_info_);
01165   bf_dialog.checkbox("Verbose", rip.verbose_);
01166   bf_dialog.checkbox("Debug", rip.debug_);
01167   if (!bf_dialog.ask())
01168     return;
01169   vcl_cout << rip << '\n';
01170   unsigned row= 0, col=0;
01171   grid_->get_last_selected_position(&col, &row);
01172   vil1_image img_0 = this->image_at(col, row);
01173   if (!img_0)
01174     return;
01175   strk_region_info ri(rip);
01176   ri.set_face_0(foreground_face_);
01177   ri.set_background_face(background_face_);
01178   ri.set_image_0(img_0);
01179   ri.evaluate_background_info();
01180 }
01181 
01182 void segv_segmentation_manager::rotate_image()
01183 {
01184   vil1_image img = selected_image();
01185   if (!img)
01186   {
01187     vcl_cout << "In segv_segmentation_manager::rotate_image - no image\n";
01188     return;
01189   }
01190   vil1_memory_image_of<float> flt =
01191     brip_vil1_float_ops::convert_to_float(img);
01192   static double angle = 0;
01193   vgui_dialog rotate_dialog("Rotate Image");
01194   rotate_dialog.field("Rotation Angle (deg)", angle);
01195   if (!rotate_dialog.ask())
01196     return;
01197 
01198   vil1_memory_image_of<float> temp = brip_vil1_float_ops::rotate(flt, angle);
01199   vil1_memory_image_of<unsigned char> out_image =
01200     brip_vil1_float_ops::convert_to_byte(temp, 0, 255);
01201   this->add_image(out_image);
01202 }
01203 
01204 void segv_segmentation_manager::create_box()
01205 {
01206   vgui_rubberband_tableau_sptr rubber = this->selected_rubber_tab();
01207   rubber->rubberband_box();
01208 }
01209 
01210 void segv_segmentation_manager::create_polygon()
01211 {
01212   vgui_rubberband_tableau_sptr rubber = this->selected_rubber_tab();
01213   rubber->rubberband_polygon();
01214 }
01215 
01216 bool segv_segmentation_manager::
01217 set_image_at(const unsigned col, const unsigned row, vil1_image& image)
01218 {
01219   vgui_tableau_sptr top_tab = grid_->get_tableau_at(col, row);
01220   if (!top_tab)
01221     return false;
01222 
01223   bgui_image_tableau_sptr itab;
01224   itab.vertical_cast(vgui_find_below_by_type_name(top_tab,
01225                                                   vcl_string("vgui_image_tableau")));
01226   if (!itab)
01227   {
01228     vcl_cout << "Unable to get bgui_image_tableau at (" << col
01229              << ", " << row << ")\n";
01230     return false;
01231   }
01232   itab->set_image(image);
01233   return true;
01234 }
01235 
01236 void segv_segmentation_manager::set_foreground_face()
01237 {
01238   unsigned row= 0, col=0;
01239   grid_->get_last_selected_position(&col, &row);
01240   foreground_face_ = face_at(col, row);
01241 }
01242 
01243 void segv_segmentation_manager::set_background_face()
01244 {
01245   unsigned row= 0, col=0;
01246   grid_->get_last_selected_position(&col, &row);
01247   background_face_ = face_at(col, row);
01248 }
01249 
01250 
01251 void segv_segmentation_manager::compute_watershed_regions()
01252 {
01253   static bool print_region_info = false;
01254   static bool merge_regions = false;
01255   static sdet_watershed_region_proc_params wrpp;
01256   vgui_dialog watershed_dialog("Watershed Regions");
01257   watershed_dialog.field("Sigma", wrpp.wp_.sigma_);
01258   watershed_dialog.field("Thresh", wrpp.wp_.thresh_);
01259   watershed_dialog.checkbox("Merge Regions", merge_regions);
01260   watershed_dialog.field("Merge Order", wrpp.merge_priority_);
01261   watershed_dialog.field("Merge Tolerance", wrpp.merge_tol_);
01262   watershed_dialog.checkbox("Print Region info", print_region_info);
01263   watershed_dialog.checkbox("Verbose", wrpp.wp_.verbose_);
01264   if (!watershed_dialog.ask())
01265     return;
01266   vcl_cout << wrpp << '\n';
01267   unsigned row= 0, col=0;
01268   grid_->get_last_selected_position(&col, &row);
01269   vil1_image image = this->image_at(col, row);
01270   vtol_face_2d_sptr f = this->face_at(col,row);
01271   vsol_box_2d_sptr box;
01272   if (f)
01273     box = f->get_bounding_box();
01274   sdet_watershed_region_proc wrp(wrpp);
01275   wrp.set_image(image, box);
01276   wrp.extract_regions();
01277   if (print_region_info)
01278     wrp.print_region_info();
01279   //   vil1_image region_image = wrp.overlay_image();
01280   //
01281   vil1_image region_image = wrp.region_image();
01282   this->add_image(region_image);
01283   if (merge_regions)
01284     wrp.merge_regions();
01285   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
01286   if (!t2D)
01287     return;
01288   vcl_vector<vsol_polygon_2d_sptr> polys = wrp.boundaries();
01289   for (vcl_vector<vsol_polygon_2d_sptr>::iterator pit = polys.begin();
01290        pit != polys.end(); pit++)
01291     t2D->add_vsol_polygon_2d(*pit);
01292   t2D->post_redraw();
01293 }
01294 
01295 
01296 void segv_segmentation_manager::display_histogram_track()
01297 {
01298   vgui_dialog hist_dlg("Histogram Track File");
01299   static vcl_string hist_filename = "";
01300   static vcl_string ext = "*.*";
01301   hist_dlg.file("Tracked Histogram filename:", ext, hist_filename);
01302   if (!hist_dlg.ask())
01303     return;
01304   vcl_ifstream hist_instr(hist_filename.c_str());
01305   unsigned int start_frame, n_frames, n_pixels;
01306   unsigned int n_int_bins, n_grad_dir_bins, n_color_bins;
01307   float diameter, aspect_ratio;
01308   vnl_matrix<float> hist_data;
01309   if (!strk_io::read_histogram_data(hist_instr, start_frame, n_frames,
01310                                    n_pixels, diameter, aspect_ratio,
01311                                    n_int_bins, n_grad_dir_bins, n_color_bins,
01312                                    hist_data))
01313     return;
01314   vil1_memory_image_of<float> temp =
01315     brip_vil1_float_ops::convert_to_float(hist_data);
01316   vil1_memory_image_of<unsigned char> image =
01317     brip_vil1_float_ops::convert_to_byte(temp);
01318   this->add_image(image);
01319 }
01320 
01321 void segv_segmentation_manager::set_range_params()
01322 {
01323   bgui_image_tableau_sptr itab = this->selected_image_tab();
01324   if (!itab)
01325     return;
01326   static double min = 0.0, max = 255;
01327   static float gamma = 1.0;
01328   static bool invert = false;
01329   vgui_dialog range_dlg("Set Range Map Params");
01330   range_dlg.field("Range min:", min);
01331   range_dlg.field("Range max:", max);
01332   range_dlg.field("Gamma:", gamma);
01333   range_dlg.checkbox("Invert:", invert);
01334   if (!range_dlg.ask())
01335     return;
01336 
01337   vgui_range_map_params_sptr rmps = new vgui_range_map_params(min,
01338                                                               max,
01339                                                               gamma,
01340                                                               invert);
01341 
01342   itab->set_mapping(rmps);
01343 }