00001
00002 #include "bmvv_multiview_manager.h"
00003
00004
00005
00006
00007 #include <vcl_cstdlib.h>
00008 #include <vcl_iostream.h>
00009 #include <vil1/vil1_load.h>
00010 #include <vgl/vgl_homg_point_2d.h>
00011 #include <vgl/vgl_homg_line_2d.h>
00012 #include <vdgl/vdgl_digital_curve.h>
00013 #include <vdgl/vdgl_interpolator.h>
00014 #include <vdgl/vdgl_edgel_chain.h>
00015 #include <vsol/vsol_curve_2d.h>
00016 #include <bxml/bxml_vtol_io.h>
00017 #include <vgui/vgui.h>
00018 #include <vgui/vgui_find.h>
00019 #include <vgui/vgui_macro.h>
00020 #include <vgui/vgui_dialog.h>
00021 #include <vgui/vgui_soview.h>
00022 #include <vgui/vgui_image_tableau.h>
00023 #include <vgui/vgui_viewer2D_tableau.h>
00024 #include <vgui/vgui_grid_tableau.h>
00025 #include <vgui/vgui_shell_tableau.h>
00026 #include <bgui/bgui_image_tableau.h>
00027 #include <bgui/bgui_vtol2D_tableau.h>
00028 #include <bgui/bgui_picker_tableau.h>
00029 #include <sdet/sdet_detector_params.h>
00030 #include <sdet/sdet_detector.h>
00031 #include <vtol/vtol_face_2d_sptr.h>
00032 #include <vtol/vtol_intensity_face.h>
00033 #include <brip/brip_vil1_float_ops.h>
00034 #include <sdet/sdet_region_proc_params.h>
00035 #include <sdet/sdet_region_proc.h>
00036
00037
00038 #include <bdgl/bdgl_curve_description.h>
00039 #include <bdgl/bdgl_curve_tracking.h>
00040 #include <bdgl/bdgl_curve_matching.h>
00041 #include <bdgl/bdgl_curve_clustering.h>
00042 #include <bdgl/bdgl_curve_algs.h>
00043
00044
00045 bmvv_multiview_manager *bmvv_multiview_manager::instance_ = 0;
00046
00047
00048
00049 bmvv_multiview_manager *bmvv_multiview_manager::instance()
00050 {
00051 if (!instance_)
00052 {
00053 instance_ = new bmvv_multiview_manager();
00054 instance_->init();
00055 }
00056 return bmvv_multiview_manager::instance_;
00057 }
00058
00059
00060
00061
00062 bmvv_multiview_manager::
00063 bmvv_multiview_manager() : vgui_wrapper_tableau()
00064 {
00065 }
00066
00067 bmvv_multiview_manager::~bmvv_multiview_manager()
00068 {
00069 }
00070
00071
00072
00073
00074
00075 void bmvv_multiview_manager::init()
00076 {
00077 grid_ = vgui_grid_tableau_new(2,1);
00078 grid_->set_grid_size_changeable(true);
00079 for (unsigned int col=0, row=0; col<2; ++col)
00080 {
00081 vgui_image_tableau_sptr itab = bgui_image_tableau_new();
00082 bgui_vtol2D_tableau_sptr btab = bgui_vtol2D_tableau_new(itab);
00083 vtol_tabs_.push_back(btab);
00084 bgui_picker_tableau_new pcktab(btab);
00085 vgui_viewer2D_tableau_sptr v2d = vgui_viewer2D_tableau_new(pcktab);
00086 grid_->add_at(v2d, col, row);
00087 }
00088 vgui_shell_tableau_sptr shell = vgui_shell_tableau_new(grid_);
00089 this->add_child(shell);
00090 }
00091
00092
00093
00094
00095
00096
00097
00098 bool bmvv_multiview_manager::handle(const vgui_event &e)
00099 {
00100 return this->child.handle(e);
00101 }
00102
00103
00104
00105
00106
00107 bgui_picker_tableau_sptr
00108 bmvv_multiview_manager::get_picker_tableau_at(unsigned col, unsigned row)
00109 {
00110 vgui_tableau_sptr top_tab = grid_->get_tableau_at(col, row);
00111 if (top_tab)
00112 {
00113 bgui_picker_tableau_sptr tt;
00114 tt.vertical_cast(vgui_find_below_by_type_name(top_tab,
00115 vcl_string("bgui_picker_tableau")));
00116 if (tt)
00117 return tt;
00118 }
00119 vgui_macro_warning << "Unable to get bgui_picker_tableau at (" << col << ", "
00120 << row << ")\n";
00121 return 0;
00122 }
00123
00124
00125
00126
00127
00128 bgui_picker_tableau_sptr bmvv_multiview_manager::get_selected_picker_tableau()
00129 {
00130 unsigned int row=0, col=0;
00131 grid_->get_last_selected_position(&col, &row);
00132 return this->get_picker_tableau_at(col, row);
00133 }
00134
00135
00136
00137
00138
00139
00140 bgui_vtol2D_tableau_sptr bmvv_multiview_manager::get_vtol2D_tableau_at(unsigned col, unsigned row)
00141 {
00142 if (row!=0)
00143 return 0;
00144 bgui_vtol2D_tableau_sptr btab = 0;
00145 if (col==0||col==1)
00146 btab = vtol_tabs_[col];
00147 return btab;
00148 }
00149
00150
00151
00152
00153
00154 bgui_vtol2D_tableau_sptr bmvv_multiview_manager::get_selected_vtol2D_tableau()
00155 {
00156 unsigned int row =0, col=0;
00157 grid_->get_last_selected_position(&col, &row);
00158 return this->get_vtol2D_tableau_at(col, row);
00159 }
00160
00161 void bmvv_multiview_manager::quit()
00162 {
00163 vcl_exit(1);
00164 }
00165
00166
00167
00168
00169 void bmvv_multiview_manager::load_image_file(vcl_string image_filename, bool , unsigned col, unsigned row)
00170 {
00171 img_ = vil1_load(image_filename.c_str());
00172 bgui_vtol2D_tableau_sptr btab = this->get_vtol2D_tableau_at(col, row);
00173 if (btab)
00174 {
00175 vgui_image_tableau_sptr itab = btab->get_image_tableau();
00176 itab->set_image(img_);
00177 return;
00178 }
00179 vcl_cout << "In bmvv_multiview_manager::load_image_file() - null tableau\n";
00180 }
00181
00182
00183
00184
00185 void bmvv_multiview_manager::load_image()
00186 {
00187 static bool greyscale = false;
00188 vgui_dialog load_image_dlg("Load Image");
00189 static vcl_string image_filename = "";
00190 static vcl_string ext = "*.*";
00191 load_image_dlg.file("Image Filename:", ext, image_filename);
00192 load_image_dlg.checkbox("greyscale ", greyscale);
00193 if (!load_image_dlg.ask())
00194 return;
00195 vil1_image temp = vil1_load(image_filename.c_str());
00196 if (greyscale)
00197 {
00198 vil1_memory_image_of<unsigned char> temp1 =
00199 brip_vil1_float_ops::convert_to_grey(temp);
00200 img_ = temp1;
00201 }
00202 else
00203 img_ = temp;
00204
00205 bgui_vtol2D_tableau_sptr btab = this->get_selected_vtol2D_tableau();
00206 if (btab)
00207 {
00208 vgui_image_tableau_sptr itab = btab->get_image_tableau();
00209 itab->set_image(img_);
00210 itab->post_redraw();
00211 return;
00212 }
00213 vcl_cout << "In bmvv_multiview_manager::load_image() - null tableau\n";
00214 }
00215
00216
00217
00218
00219 void bmvv_multiview_manager::clear_display()
00220 {
00221 bgui_vtol2D_tableau_sptr btab = this->get_selected_vtol2D_tableau();
00222 if (btab)
00223 btab->clear_all();
00224 else
00225 vcl_cout << "In bmvv_multiview_manager::clear_display() - null tableau\n";
00226 }
00227
00228
00229
00230
00231 void bmvv_multiview_manager::clear_selected()
00232 {
00233 for (vcl_vector<bgui_vtol2D_tableau_sptr>::iterator bit = vtol_tabs_.begin();
00234 bit != vtol_tabs_.end(); bit++)
00235 if (*bit)
00236 (*bit)->deselect_all();
00237 }
00238
00239
00240
00241
00242
00243 void bmvv_multiview_manager::draw_regions(vcl_vector<vtol_intensity_face_sptr>& regions,
00244 bool verts)
00245 {
00246 vcl_vector<vtol_face_2d_sptr> faces;
00247 for (vcl_vector<vtol_intensity_face_sptr>::iterator rit = regions.begin();
00248 rit != regions.end(); rit++)
00249 faces.push_back((*rit)->cast_to_face_2d());
00250
00251 bgui_vtol2D_tableau_sptr btab = this->get_selected_vtol2D_tableau();
00252 if (btab)
00253 btab->add_faces(faces, verts);
00254 else
00255 vcl_cout << "In bmvv_multiview_manager::draw_regions() - null tableau\n";
00256 }
00257
00258
00259
00260
00261 void bmvv_multiview_manager::vd_edges()
00262 {
00263 static bool agr = true;
00264 static bool clear = true;
00265 static sdet_detector_params dp;
00266 vgui_dialog vd_dialog("VD Edges");
00267 vd_dialog.field("Gaussian sigma", dp.smooth);
00268 vd_dialog.field("Noise Threshold", dp.noise_multiplier);
00269 vd_dialog.checkbox("Automatic Threshold", dp.automatic_threshold);
00270 vd_dialog.checkbox("Agressive Closure", agr);
00271 vd_dialog.checkbox("Compute Junctions", dp.junctionp);
00272 vd_dialog.checkbox("Clear", clear);
00273 if (!vd_dialog.ask())
00274 return;
00275 if (agr)
00276 dp.aggressive_junction_closure=1;
00277 else
00278 dp.aggressive_junction_closure=0;
00279 sdet_detector det(dp);
00280
00281 bgui_vtol2D_tableau_sptr btab = this->get_selected_vtol2D_tableau();
00282 if (btab)
00283 {
00284 vgui_image_tableau_sptr itab = btab->get_image_tableau();
00285 img_ = itab->get_image();
00286 }
00287 else
00288 {
00289 vcl_cout << "In bmvv_multiview_manager::vd_edges() - null tableau\n";
00290 return;
00291 }
00292 det.SetImage(img_);
00293 det.DoContour();
00294 vcl_vector<vtol_edge_2d_sptr>* edges = det.GetEdges();
00295
00296
00297 if (btab&&edges)
00298 {
00299 if (clear)
00300 btab->clear_all();
00301 btab->add_edges(*edges, true);
00302 btab->post_redraw();
00303 }
00304 else
00305 {
00306 vcl_cout << "In bmvv_multiview_manager::vd_edges() - null edges or null tableau\n";
00307 return;
00308 }
00309 }
00310
00311 void bmvv_multiview_manager::regions()
00312 {
00313 this->clear_display();
00314 static bool agr = true;
00315 static sdet_detector_params dp;
00316 vgui_dialog vd_dialog("VD Edges");
00317 vd_dialog.field("Gaussian sigma", dp.smooth);
00318 vd_dialog.field("Noise Threshold", dp.noise_multiplier);
00319 vd_dialog.checkbox("Automatic Threshold", dp.automatic_threshold);
00320 vd_dialog.checkbox("Agressive Closure", agr);
00321 if (!vd_dialog.ask())
00322 return;
00323 if (agr)
00324 dp.aggressive_junction_closure=1;
00325 else
00326 dp.aggressive_junction_closure=0;
00327 bool debug = false;
00328 sdet_region_proc_params rpp(dp);
00329 sdet_region_proc rp(rpp);
00330 rp.set_image(img_);
00331 rp.extract_regions();
00332
00333 if (debug)
00334 {
00335 bgui_vtol2D_tableau_sptr btab = this->get_selected_vtol2D_tableau();
00336 if (!btab)
00337 {
00338 vcl_cout << "In bmvv_multiview_manager::regions() - null tableau\n";
00339 return;
00340 }
00341 vil1_image ed_img = rp.get_edge_image();
00342 vgui_image_tableau_sptr itab = btab->get_image_tableau();
00343 if (!itab)
00344 {
00345 vcl_cout << "In bmvv_multiview_manager::regions() - null image tableau\n";
00346 return;
00347 }
00348 itab->set_image(ed_img);
00349 return;
00350 }
00351 vcl_vector<vtol_intensity_face_sptr>& regions = rp.get_regions();
00352 this->draw_regions(regions, true);
00353 }
00354
00355 #ifdef HAS_XERCES
00356 void bmvv_multiview_manager::read_xml_edges()
00357 {
00358 this->clear_display();
00359 vgui_dialog load_image_dlg("Load XML edges");
00360 static vcl_string xml_filename = "";
00361 static vcl_string ext = "*.*";
00362 load_image_dlg.file("XML filename:", ext, xml_filename);
00363 if (!load_image_dlg.ask())
00364 return;
00365 bgui_vtol2D_tableau_sptr btab = this->get_selected_vtol2D_tableau();
00366 if (!btab)
00367 {
00368 vcl_cout << "In bmvv_multiview_manager::regions() - null tableau\n";
00369 return;
00370 }
00371 vcl_vector<vtol_edge_2d_sptr> edges;
00372 if (bxml_vtol_io::read_edges(xml_filename, edges))
00373 btab->add_edges(edges, true);
00374 }
00375 #endif
00376
00377
00378
00379
00380
00381 FMatrix bmvv_multiview_manager::test_fmatrix()
00382 {
00383 vnl_matrix<double> m(3, 3, 0.0);
00384 m.put(1,2, 1.0);
00385 m.put(2,1,-1.0);
00386 return m;
00387 }
00388
00389
00390
00391
00392
00393 void bmvv_multiview_manager::show_epipolar_line()
00394 {
00395 this->clear_display();
00396 vgui::out << "pick point in left image\n";
00397 unsigned int col=0, row=0;
00398 bgui_picker_tableau_sptr pkt = this->get_picker_tableau_at(col, row);
00399 if (!pkt)
00400 {
00401 vcl_cout << "In bmvv_multiview_manager::show_epipolar_line() - null tableau\n";
00402 return;
00403 }
00404 float x = 0, y=0;
00405 pkt->pick_point(&x, &y);
00406 vgui::out << "p(" << x << ' ' << y << ")\n";
00407 vcl_cout << "p(" << x << ' ' << y << ")\n";
00408
00409 col = 1;
00410 bgui_vtol2D_tableau_sptr v2D = this->get_vtol2D_tableau_at(col,row);
00411
00412
00413 FMatrix f = this->test_fmatrix();
00414 vgl_homg_point_2d<double> pl(x,y);
00415 vgl_homg_line_2d<double> lr = f.image2_epipolar_line(pl);
00416
00417 if (v2D)
00418 {
00419 v2D->add_infinite_line(lr.a(), lr.b(), lr.c());
00420 v2D->post_redraw();
00421 }
00422 }
00423
00424
00425
00426
00427 void bmvv_multiview_manager::select_curve_corres()
00428 {
00429 for (vcl_vector<bgui_vtol2D_tableau_sptr>::iterator bit = vtol_tabs_.begin();
00430 bit != vtol_tabs_.end(); bit++)
00431 if (*bit)
00432 {
00433 vcl_vector<vgui_soview*> sovs = (*bit)->get_selected_soviews();
00434 for (unsigned int i = 0; i<sovs.size(); i++)
00435 {
00436 vgui_soview* sov = sovs[i];
00437 int id = sov->get_id();
00438 vcl_cout << "id = " << id << '\n';
00439 vtol_edge_2d_sptr e = (*bit)->get_mapped_edge(id);
00440 vsol_curve_2d_sptr c = e->curve();
00441 vdgl_digital_curve_sptr dc = c->cast_to_vdgl_digital_curve();
00442
00443 vdgl_interpolator_sptr interp = dc->get_interpolator();
00444 vdgl_edgel_chain_sptr ec = interp->get_edgel_chain();
00445 bdgl_curve_description descr(ec);
00446 descr.info();
00447 if (dc) (*bit)->add_digital_curve(dc);
00448 }
00449 }
00450 this->clear_selected();
00451 }
00452
00453
00454
00455
00456 void bmvv_multiview_manager::track_edges()
00457 {
00458
00459 this->clear_display();
00460 bdgl_curve_tracking_params tp_;
00461
00462 static bdgl_curve_clustering_params cp;
00463 static bdgl_curve_matching_params mp;
00464 static double ex,ey;
00465
00466 vgui_dialog* tr_dialog = new vgui_dialog("Curve Tracking");
00467 tr_dialog->checkbox("Matching", tp_.mp.matching_);
00468 tr_dialog->checkbox("Transitive closure", tp_.transitive_closure);
00469 tr_dialog->field("Window size", tp_.window_size);
00470 tr_dialog->field("Estimated Motion", tp_.mp.motion_in_pixels);
00471 tr_dialog->field("No of Top matches",tp_.mp.no_of_top_choices);
00472 tr_dialog->field("Min Length of curves",tp_.min_length_of_curves);
00473 tr_dialog->field("epipole x",ex);
00474 tr_dialog->field("epipole y",ey);
00475
00476 tr_dialog->checkbox("Clustering", tp_.clustering_);
00477 tr_dialog->field("No of clusters ",tp_.cp.no_of_clusters);
00478 tr_dialog->field("Min Euc Distance",tp_.cp.min_cost_threshold);
00479 tr_dialog->field("Fg and Bg Threshold",tp_.cp.foreg_backg_threshold);
00480
00481
00482 static sdet_detector_params dp;
00483 static bool agr = true;
00484 vgui_dialog* dp_dialog = new vgui_dialog("Edge Detection");
00485 dp_dialog->field("Gaussian sigma", dp.smooth);
00486 dp_dialog->field("Noise Threshold", dp.noise_multiplier);
00487 dp_dialog->checkbox("Automatic Threshold", dp.automatic_threshold);
00488 dp_dialog->checkbox("Compute Junctions", dp.junctionp);
00489 dp_dialog->checkbox("Agressive Closure", agr);
00490 if (!tr_dialog->ask())
00491 return;
00492 if (!dp_dialog->ask())
00493 return;
00494 if (agr)
00495 dp.aggressive_junction_closure=1;
00496 else
00497 dp.aggressive_junction_closure=0;
00498
00499 sdet_detector det1(dp);
00500 sdet_detector det2(dp);
00501
00502 bdgl_curve_tracking tracks (tp_);
00503
00504 vcl_vector<vtol_edge_2d_sptr> * edges;
00505 vil1_image img =this->get_image_at(0,0);
00506 vil1_memory_image_of<unsigned char> cimg;
00507 if (img.components()==3)
00508 {
00509 vil1_memory_image_of<float> fimg = brip_vil1_float_ops::convert_to_float(img);
00510
00511 cimg = brip_vil1_float_ops::convert_to_byte(fimg);
00512 }
00513 else
00514 cimg = vil1_memory_image_of<unsigned char>(img);
00515
00516 img =this->get_image_at(0,0);
00517 vcl_vector<vtol_edge_2d_sptr> ecl;
00518
00519
00520 det1.SetImage(cimg);
00521 det1.DoContour();
00522 edges = det1.GetEdges();
00523
00524 for (unsigned int i=0;i<(*edges).size();i++)
00525 {
00526 vsol_curve_2d_sptr c = (*edges)[i]->curve();
00527 vdgl_digital_curve_sptr dc = c->cast_to_vdgl_digital_curve();
00528 if (dc->length()>tp_.min_length_of_curves)
00529 ecl.push_back((*edges)[i]);
00530 }
00531 tracks.input_curves_.push_back(ecl);
00532 ecl.clear();
00533
00534
00535 img =this->get_image_at(1,0);
00536
00537 if (img.components()==3)
00538 {
00539 vil1_memory_image_of<float> fimg = brip_vil1_float_ops::convert_to_float(img);
00540
00541 cimg = brip_vil1_float_ops::convert_to_byte(fimg);
00542 }
00543 else
00544 cimg = vil1_memory_image_of<unsigned char>(img);
00545
00546 det2.SetImage(cimg);
00547 det2.DoContour();
00548 edges = det2.GetEdges();
00549
00550 for (unsigned int i=0;i<(*edges).size();i++)
00551 {
00552 vsol_curve_2d_sptr c = (*edges)[i]->curve();
00553 vdgl_digital_curve_sptr dc = c->cast_to_vdgl_digital_curve();
00554 if (dc->length()>tp_.min_length_of_curves)
00555 ecl.push_back((*edges)[i]);
00556 }
00557 tracks.input_curves_.push_back(ecl);
00558 ecl.clear();
00559
00560
00561
00562 tracks.track();
00563 return;
00564
00565
00566
00567 for (int i=0;i<tracks.get_output_size_at(0);++i)
00568 {
00569
00570 bdgl_tracker_curve_sptr test_curve1=tracks.get_output_curve(0,i);
00571 vdgl_digital_curve_sptr dc = bdgl_curve_algs::create_digital_curves
00572 (test_curve1->desc->points_);
00573
00574 draw_colored_digital_curve(0,0, dc, test_curve1->match_id_ );
00575 }
00576
00577
00578 for (int i=0;i<tracks.get_output_size_at(1);++i)
00579 {
00580
00581 bdgl_tracker_curve_sptr test_curve1=tracks.get_output_curve(0,i);
00582 vdgl_digital_curve_sptr dc = bdgl_curve_algs::create_digital_curves
00583 (test_curve1->desc->points_);
00584
00585 draw_colored_digital_curve(1,0, dc, test_curve1->match_id_ );
00586 }
00587 }
00588
00589
00590 void bmvv_multiview_manager::draw_colored_digital_curve(unsigned col, unsigned row, vdgl_digital_curve_sptr dc, int label)
00591 {
00592 float r,g,b;
00593
00594 bgui_vtol2D_tableau_sptr btab = this->get_vtol2D_tableau_at(col,row);
00595 if (!btab)
00596 {
00597 vcl_cout << "In bmvv_multiview_manager::track_edges() - null tableau\n";
00598 return;
00599 }
00600 else
00601 {
00602 btab->disable_highlight();
00603 set_changing_colors( label, &r, &g, &b );
00604 btab->set_digital_curve_style(r, g, b, 3.0);
00605 btab->add_digital_curve( dc );
00606 btab->post_redraw();
00607 }
00608
00609 return;
00610 }
00611
00612
00613
00614 void bmvv_multiview_manager::set_changing_colors(int num, float *r, float *g, float *b)
00615 {
00616 int strength = num/6;
00617 int pattern = num%6;
00618 strength %= 20;
00619 float s = 1.0f - strength * 0.05f;
00620
00621 switch (pattern)
00622 {
00623 case 0 : (*r) = s; (*g) = 0; (*b) = 0; break;
00624 case 1 : (*r) = 0; (*g) = s; (*b) = 0; break;
00625 case 2 : (*r) = 0; (*g) = 0; (*b) = s; break;
00626 case 3 : (*r) = s; (*g) = s; (*b) = 0; break;
00627 case 4 : (*r) = 0; (*g) = s; (*b) = s; break;
00628 case 5 : (*r) = s; (*g) = 0; (*b) = s; break;
00629 default: (*r) = 0; (*g) = 0; (*b) = 0; break;
00630 }
00631
00632
00633 return;
00634 }
00635
00636
00637
00638
00639
00640
00641 vil1_image bmvv_multiview_manager::get_image_at(unsigned col, unsigned row)
00642 {
00643 bgui_vtol2D_tableau_sptr btab = this->get_vtol2D_tableau_at(col, row);
00644 if (btab)
00645 {
00646 vgui_image_tableau_sptr itab = btab->get_image_tableau();
00647 return itab->get_image();
00648 }
00649 vcl_cout << "In bmvv_multiview_manager::get_image_at() - null tableau\n";
00650 return 0;
00651 }