contrib/brl/bmvl/brct/brct_plane_sweeper.h

Go to the documentation of this file.
00001 #ifndef brct_plane_sweeper_h_
00002 #define brct_plane_sweeper_h_
00003 //:
00004 // \file
00005 // \brief Sweep a x-y plane and compute correllation and point matches.
00006 //
00007 //  The goal is to compute 3-d points from a stereo pair. It is assumed
00008 //  that homographies for two planes (z=0 and z=z0) are available.  These
00009 //  are currently computed by brct_plane_calibrator. By interpolation, we have
00010 //  Hl(z) and Hr(z) for the left(l) and right(r) cameras for
00011 //  any x-y plane, Z=z. Then at each z there are methods for computing matches
00012 //  of intensity through normalized cross correlation and by the distance
00013 //  between Harris corners.  A number of approaches are available, but the
00014 //  most successful is to map Harris corners from the right image to the world
00015 //  plane, using Hr^-1 then map the corners to the left image using Hl.
00016 //  The overall homography is Hl*Hr^-1. A match between a mapped corner and
00017 //  original left image corner is defined by 1) they are within a distance
00018 //  threshold and 2) the normalized cross-correlation at the match point is
00019 //  above a specified threshold.
00020 //
00021 //  Earlier attempts involved mapping the points and images onto the world
00022 //  plane and carrying out the matches there. It is probably better conditioned
00023 //  to consider errors in the projected image space rather than on the world plane.
00024 //
00025 // \author J.L. Mundy
00026 // \verbatim
00027 //  Initial version Feb. 2004
00028 // \endverbatim
00029 //
00030 //////////////////////////////////////////////////////////////////////
00031 
00032 #include <vcl_vector.h>
00033 #include <vcl_string.h>
00034 #include <vgl/algo/vgl_h_matrix_2d.h>
00035 #include <vil1/vil1_memory_image_of.h>
00036 #include <vil1/vil1_rgb.h>
00037 #include <vsol/vsol_box_2d_sptr.h>
00038 #include <vsol/vsol_point_3d_sptr.h>
00039 #include <vsol/vsol_point_2d_sptr.h>
00040 #include <bbas/bsol/bsol_point_index_2d.h>
00041 #include <brct/brct_plane_sweeper_params.h>
00042 
00043 class brct_plane_sweeper : public brct_plane_sweeper_params
00044 {
00045  public:
00046   brct_plane_sweeper(brct_plane_sweeper_params const& sp);
00047   ~brct_plane_sweeper();
00048 
00049   //:Accessors
00050   vil1_memory_image_of<unsigned char> z_corr_image(const int i);
00051 
00052   vcl_vector<vsol_point_2d_sptr> harris_corners(const int cam);
00053 
00054   //:Mutators
00055   bool set_image(const int cam, vil1_image const& image);
00056 
00057   // Utility functions
00058 
00059   //: Read the homographies for two parallel planes to each camera plane
00060   bool read_homographies(vcl_string const& homography_file);
00061 
00062   //: compute harris corners for each image
00063   bool compute_harris();
00064 
00065   //: Project the camera image to the world plane
00066   vil1_memory_image_of<unsigned char> project_image_to_plane(const int plane,
00067                                                              const int cam);
00068 
00069   //: Map the camera image (cam) to the other image plane
00070   bool map_image_to_image(const int from_cam, const double z,
00071                           vil1_memory_image_of<unsigned char>& mapped_to_image,
00072                           vil1_memory_image_of<unsigned char>& orig_to_image);
00073 
00074   //: Construct images corresponding to the overlap in the two cam projections
00075   bool overlapping_projections(const int plane,
00076                                vcl_vector<vil1_memory_image_of<float> >& imgs);
00077 
00078   //: Construct images corresponding to the overlap in the two cam projections
00079   bool overlapping_projections(const double z,
00080                                vcl_vector<vil1_memory_image_of<float> >& imgs);
00081 
00082 
00083   //: Construct images and corners corresponding to the overlap
00084   bool overlapping_projections(const double z,
00085                                vcl_vector<vil1_memory_image_of<float> >& imgs,
00086                                vcl_vector<vcl_vector<vsol_point_2d_sptr> >& corners);
00087 
00088   //: Compute the cross correlation between overlapping projections for plane
00089   vil1_memory_image_of<unsigned char>
00090     cross_correlate_projections(const int plane);
00091 
00092   //: Compute the cross correlation between overlapping projections for z pos
00093   vil1_memory_image_of<unsigned char>
00094     cross_correlate_projections(const double z);
00095 
00096   //: Compute the cross correlation at corresponding Harris corners
00097   bool
00098     cross_correlate_proj_corners(const double z,
00099                                  vil1_image& back,
00100                                  vcl_vector<vsol_point_2d_sptr>& matched_cnrs,
00101                                  vcl_vector<vsol_point_2d_sptr>& back_prj_cnrs,
00102                                  vcl_vector<vsol_point_2d_sptr>& orig_cnrs0);
00103 
00104   //: Compute a depth image based on max cross-correlation.
00105   bool depth_image(vil1_memory_image_of<unsigned char>& depth_out,
00106                    vil1_memory_image_of<unsigned char>& coor_out);
00107 
00108   //: Compute Harris point matches by plane sweeping
00109   bool harris_depth_match(vcl_vector<vsol_point_3d_sptr>& points_3d,
00110                           vcl_vector<vsol_point_2d_sptr>& proj_points);
00111 
00112   //: Correlation values by depth
00113   void corr_vals(const int col, const int row,
00114                  vcl_vector<float>& z, vcl_vector<float>& corr);
00115 
00116   //: Correlation values by depth
00117   vsol_point_2d_sptr map_point(vsol_point_2d_sptr const& p, const int cam,
00118                                const double z);
00119 
00120   //: Map harris corners from one image to the other via plane at z
00121   bool
00122     map_harris_corners(const int from_cam, const double z,
00123                        vcl_vector<vsol_point_2d_sptr>& mapped_to_points,
00124                        vcl_vector<vsol_point_2d_sptr>& orig_to_points);
00125 
00126   //:init harris matcher
00127   void init_harris_match(const int from_cam);
00128 
00129   //: Map and match harris corners from one image to the other via plane at z
00130   bool
00131     match_harris_corners(const int from_cam, const double z,
00132                          vcl_vector<vsol_point_2d_sptr>& matched_points,
00133                          vcl_vector<vsol_point_2d_sptr>& orig_to_points);
00134 
00135   bool harris_sweep(const int from_cam);
00136 
00137   vcl_vector<vsol_point_2d_sptr> matched_points_at_z_index(int z_index);
00138 
00139   vcl_vector<vsol_point_3d_sptr> proj_points_3d();
00140 
00141   vcl_vector<vsol_point_3d_sptr> world_points_3d();
00142 
00143   bool save_world_points(vcl_string const& out_file);
00144 
00145  protected:
00146   //local utility functions
00147 
00148   bool overlapping_box(vcl_vector<vgl_h_matrix_2d<double> > const& homgs,
00149                        vsol_box_2d_sptr& box);
00150   //: tx and ty shift the projections to the (0,0) image origin
00151   bool
00152     overlapping_projections(vcl_vector<vgl_h_matrix_2d<double> > const& homgs,
00153                             vcl_vector<vil1_memory_image_of<float> >& imgs,
00154                             double& tx, double& ty);
00155   bool
00156     overlapping_projections(vcl_vector<vgl_h_matrix_2d<double> > const& homgs,
00157                             vcl_vector<vil1_memory_image_of<float> >& imgs,
00158                             vcl_vector<vcl_vector<vsol_point_2d_sptr> >& corners,
00159                             double& tx, double& ty);
00160 
00161   void homographies_at_z(double z,
00162                          vcl_vector<vgl_h_matrix_2d<double> >& homgs);
00163   vsol_box_2d_sptr
00164     depth_image_box(const double zmin, const double zmax);
00165 
00166   vcl_vector<vsol_point_2d_sptr> project_corners(vgl_h_matrix_2d<double> const & H,
00167                                                  vcl_vector<vsol_point_2d_sptr> const& corners);
00168 
00169   bool
00170     correlate_corners(vcl_vector<vil1_memory_image_of<float> > const& imgs,
00171                       vcl_vector<vcl_vector<vsol_point_2d_sptr> > const& cnrs,
00172                       vcl_vector<vsol_point_2d_sptr>& matched_corners);
00173 
00174   vil1_memory_image_of<vil1_rgb<unsigned char> >
00175     overlay_matches(vcl_vector<vsol_point_2d_sptr>,
00176                     vil1_memory_image_of<float> const& back);
00177 
00178   vsol_point_2d_sptr map_point(vsol_point_2d_sptr const& p,
00179                                vgl_h_matrix_2d<double> const& Hcomp);
00180 
00181   bool map_points(const int from_cam, const double z,
00182                   vcl_vector<vsol_point_2d_sptr> const& from_points,
00183                   vcl_vector<vsol_point_2d_sptr>& to_points);
00184 
00185   bool intersecting_bounding_box(vgl_h_matrix_2d<double> const& Hcomp,
00186                                  vsol_box_2d_sptr const& from_box,
00187                                  vsol_box_2d_sptr const& to_box,
00188                                  vsol_box_2d_sptr & box);
00189 
00190   bool map_image(const int from_cam, const double z,
00191                  vil1_memory_image_of<float>& mapped_image);
00192 
00193   //flags
00194   bool harris_valid_;
00195   bool homographies_valid_;
00196 
00197   //members
00198   int n_planes_;
00199   int n_cams_;
00200   float del_;
00201   int to_cam_;
00202   //world plane
00203   vcl_vector<double> z_;
00204   //world plane camera
00205   vcl_vector< vcl_vector<vgl_h_matrix_2d<double> > > homographies_;
00206   //cam
00207   vcl_vector<vil1_image> images_;
00208   vcl_vector<vil1_memory_image_of<float> > smooth_images_;
00209   //z
00210   vcl_vector<vil1_memory_image_of<float> > z_corr_images_;
00211   //cam       corners
00212   vcl_vector<vcl_vector<vsol_point_2d_sptr> > harris_corners_;
00213   //z                  matched corners
00214   vcl_vector<vcl_vector<vsol_point_2d_sptr> > matched_corners_;
00215   bsol_point_index_2d pindx_;
00216 };
00217 
00218 #endif // brct_plane_sweeper_h_

Generated on Fri Nov 21 05:24:04 2008 for contrib/brl/bmvl/brct by  doxygen 1.5.1