contrib/brl/bmvl/bmvv/bmvv_cal_manager.h
Go to the documentation of this file.
00001 // This is brl/bmvl/bmvv/bmvv_cal_manager.h
00002 #ifndef bmvv_cal_manager_h_
00003 #define bmvv_cal_manager_h_
00004 //----------------------------------------------------------------------------
00005 //:
00006 // \file
00007 // \brief Manager for camera calibration
00008 // \author
00009 //   J.L. Mundy
00010 //
00011 // Currently works on a single view. Provides interactive tools for
00012 // corresponding 3-d points to 2-d image locations. Also provides for
00013 // the solution of planar camera models (homographies).  The homography can be
00014 // extended to a vgl_p_matrix by selecting a number of points off the
00015 // ground plane. In addition there are several simple 3-d modeling tools:
00016 //      o Backproject a polygon onto a X-Y world plane (specified Z)
00017 //      o Create a 3-d box from three points on the roof face (specified Z)
00018 //
00019 // \verbatim
00020 //  Modifications:
00021 //   J.L. Mundy Jan 15, 2005    Initial version.
00022 // \endverbatim
00023 //-----------------------------------------------------------------------------
00024 #include <vcl_vector.h>
00025 #include <vcl_map.h>
00026 #include <vcl_fstream.h>
00027 #include <vil/vil_image_resource_sptr.h>
00028 #include <vgl/vgl_point_2d.h>
00029 #include <vgl/vgl_point_3d.h>
00030 #include <vgl/vgl_line_segment_2d.h>
00031 #include <vgl/algo/vgl_p_matrix.h>
00032 #include <vsol/vsol_point_2d_sptr.h>
00033 #include <vsol/vsol_polygon_2d_sptr.h>
00034 #include <vsol/vsol_polygon_3d_sptr.h>
00035 #include <vgui/vgui_wrapper_tableau.h>
00036 #include <bgui/bgui_image_tableau_sptr.h>
00037 #include <vgui/vgui_range_map_params_sptr.h>
00038 #include <vgui/vgui_event.h>
00039 #include <vgui/vgui_soview2D.h>
00040 #include <bgui/bgui_vtol2D_tableau_sptr.h>
00041 #include <bgui/bgui_picker_tableau_sptr.h>
00042 
00043 class vgui_window;
00044 class vgui_soview;
00045 //: A manager for displaying segmentation results.
00046 class bmvv_cal_manager : public vgui_wrapper_tableau
00047 {
00048  public:
00049   bmvv_cal_manager();
00050  ~bmvv_cal_manager();
00051   static bmvv_cal_manager *instance();
00052 
00053   //: methods for menu callbacks
00054   void quit();
00055   void load_image();
00056   void save_image();
00057 
00058   //: 3-d points and polygons representing a world model
00059   void load_world();
00060 
00061   //: viewing functions
00062   void adjust_contrast();
00063   void set_range_params();
00064   void clear_display();
00065   void clear_selected();
00066   void load_image_and_camera();
00067 
00068   //: pairs of corresponding 2-d image and 3-d world points
00069   void load_corrs();
00070   void save_corrs();
00071   void pick_correspondence();
00072   void remove_correspondence();
00073 
00074   //: world-to-image correspondences and vertical lines for constrained cameras
00075   void pick_vertical();
00076   void save_constraints();
00077 
00078   //: camera modeling
00079   void set_identity_camera();
00080   void solve_camera();
00081   void compute_ransac_homography();
00082   void save_camera();
00083 
00084   //: backproject an image onto the world plane
00085   void map_image_to_world();
00086 
00087   // == modeling functions ==
00088 
00089   //: set the create XY plane height (Z)
00090   void set_height();
00091   //: define a polygon on the create plane
00092   void pick_polygon();
00093   //: clear all current world information
00094   void clear_world();
00095   //: insert a created polygon into the world data structures
00096   void confirm_polygon();
00097   //: save the world information in cal format
00098   void save_world();
00099   //: save the world information in ply2 format (a single indexed face set)
00100   void save_world_ply2();
00101   //: read the world information in ply2 format (a single indexed face set)
00102   void read_world_ply2();
00103   //: Convert boxes in a merged indexed face set to individual boxes in VRML
00104   // (A temporary tool to unmerge files consisting of all boxes)
00105   void ply2_to_vrml();
00106   //: Create a box in the world from three top face corner points on the create plane.  The bottom plane is at Z=0.
00107   void create_box();
00108   //: access to the window
00109   vgui_window* get_window(){return win_;}
00110   void set_window(vgui_window* win){win_=win;}
00111   void init();
00112 
00113   //: the virtual handle function
00114   virtual bool handle(const vgui_event&);
00115 
00116   void load_image_file(vcl_string image_filename, bool greyscale);
00117  protected:
00118   //: internal utility methods
00119   void draw_corr_point(const float x, const float y);
00120   void draw_vsol_points(const int cam,
00121                         vcl_vector<vsol_point_2d_sptr> const & points,
00122                         bool clear = true,
00123                         const float r = 0,
00124                         const float g = 1,
00125                         const float b = 0);
00126 
00127   void draw_vsol_point(const int cam,
00128                        vsol_point_2d_sptr const & point,
00129                        bool clear = false,
00130                        const float r = 0,
00131                        const float g = 1,
00132                        const float b = 0);
00133 
00134 void draw_vsol_polygon_3d(vsol_polygon_3d_sptr const & poly,
00135                           bool clear, const float r=0, const float g=1,
00136                           const float b=0);
00137 
00138   void draw_polygons_3d(bool clear, const float r,
00139                         const float g, const float b);
00140 
00141   void add_image(vil_image_resource_sptr const& image,
00142                  vgui_range_map_params_sptr rmps = 0,
00143                  bool greyscale =false);
00144   vil1_image get_image_at(unsigned col, unsigned row);
00145   bool read_corrs(vcl_ifstream& str);
00146   bool draw_correspondences();
00147   bool draw_projected_world_points();
00148   void add_poly_vertices_to_world_pts(vsol_polygon_3d_sptr const& poly);
00149   void clear_correspondences();
00150   void project_world();
00151   int  get_selected_proj_world_pt(vgl_point_2d<double>&  pt);
00152   vgui_range_map_params_sptr
00153     range_params(vil_image_resource_sptr const& image);
00154  private:
00155   vil_image_resource_sptr img_;
00156   vgui_window* win_;
00157   bgui_image_tableau_sptr itab_;
00158   bgui_picker_tableau_sptr ptab_;
00159   bgui_vtol2D_tableau_sptr btab_;
00160   vcl_map<int, int> point_3d_map_;
00161   static bmvv_cal_manager *instance_;
00162   vcl_vector<vgl_point_2d<double> > corrs_;
00163   vcl_vector<bool>  corrs_valid_;
00164   vcl_vector<vgl_point_2d<double> >  proj_image_pts_;
00165   vcl_vector<vgui_soview2D_point* >  corr_sovs_;
00166   vcl_vector<vgl_point_3d<double> > world_;
00167   //: the current camera
00168   vgl_p_matrix<double> cam_;
00169   //: the previous camera before the current solution
00170   vgl_p_matrix<double> prev_cam_;
00171   //: lines in the image corresponding to the vertical direction
00172   vcl_vector<vgl_line_segment_2d<double> > verticals_;
00173   //: the Z height of the create plane
00174   double height_;
00175   //: the polygon just created
00176   vsol_polygon_3d_sptr new_poly_;
00177   //: all the polygons in the world model
00178   vcl_vector<vsol_polygon_3d_sptr> polys_;
00179   //: the 2-d projected form of the polygons
00180   vcl_vector<vsol_polygon_2d_sptr> proj_polys_;
00181   //: an indexed face set for all the polygons in the world
00182   vcl_vector<vcl_vector<unsigned> > indexed_face_set_;
00183 };
00184 
00185 #endif // bmvv_cal_manager_h_