core/vpgl/algo/vpgl_optimize_camera.h
Go to the documentation of this file.
00001 // This is core/vpgl/algo/vpgl_optimize_camera.h
00002 #ifndef vpgl_optimize_camera_h_
00003 #define vpgl_optimize_camera_h_
00004 //:
00005 // \file
00006 // \brief Methods for projecting geometric structures onto the image
00007 // \author Matt Leotta
00008 // \date March 7, 2005
00009 //
00010 #include <vnl/vnl_least_squares_function.h>
00011 #include <vgl/vgl_point_2d.h>
00012 #include <vgl/vgl_point_3d.h>
00013 #include <vgl/vgl_homg_point_3d.h>
00014 #include <vpgl/vpgl_perspective_camera.h>
00015 
00016 //: this class optimizes the rotation of a perspective camera given an initial estimate and a known internal calibration and position
00017 class vpgl_orientation_lsqr : public vnl_least_squares_function
00018 {
00019  public:
00020   //: Constructor
00021   // \note image points are not homogeneous because require finite points to measure projection error
00022   vpgl_orientation_lsqr(const vpgl_calibration_matrix<double>& K,
00023                         const vgl_point_3d<double>& c,
00024                         const vcl_vector<vgl_homg_point_3d<double> >& world_points,
00025                         const vcl_vector<vgl_point_2d<double> >& image_points );
00026   //: Destructor
00027   virtual ~vpgl_orientation_lsqr() {}
00028 
00029   //: The main function.
00030   //  Given the parameter vector x, compute the vector of residuals fx.
00031   //  Fx has been sized appropriately before the call.
00032   //  The parameters in x are the {wx, wy, wz}
00033   //  where w is the Rodrigues vector of the rotation.
00034   virtual void f(vnl_vector<double> const& x, vnl_vector<double>& fx);
00035 
00036 #if 0
00037   //: Called after each LM iteration to print debugging etc.
00038   virtual void trace(int iteration, vnl_vector<double> const& x, vnl_vector<double> const& fx);
00039 #endif
00040 
00041  protected:
00042   //: The fixed internal camera calibration
00043   vpgl_calibration_matrix<double> K_;
00044   //: The fixed camera center
00045   vgl_point_3d<double> c_;
00046   //: The known points in the world
00047   vcl_vector<vgl_homg_point_3d<double> > world_points_;
00048   //: The corresponding points in the image
00049   vcl_vector<vgl_point_2d<double> > image_points_;
00050 };
00051 
00052 
00053 //: this class optimizes the rotation/translation of a perspective camera given an initial estimate and a known internal calibration
00054 class vpgl_orientation_position_lsqr : public vnl_least_squares_function
00055 {
00056  public:
00057   //: Constructor
00058   // \note image points are not homogeneous because require finite points to measure projection error
00059   vpgl_orientation_position_lsqr(const vpgl_calibration_matrix<double>& K,
00060                                  const vcl_vector<vgl_homg_point_3d<double> >& world_points,
00061                                  const vcl_vector<vgl_point_2d<double> >& image_points );
00062   //: Destructor
00063   virtual ~vpgl_orientation_position_lsqr() {}
00064 
00065   //: The main function.
00066   //  Given the parameter vector x, compute the vector of residuals fx.
00067   //  Fx has been sized appropriately before the call.
00068   //  The parameters in x are really two three component vectors {wx, wy, wz, tx, ty, tz}
00069   //  where w is the Rodrigues vector of the rotation and t is the translation.
00070   virtual void f(vnl_vector<double> const& x, vnl_vector<double>& fx);
00071 
00072 #if 0
00073   //: Called after each LM iteration to print debugging etc.
00074   virtual void trace(int iteration, vnl_vector<double> const& x, vnl_vector<double> const& fx);
00075 #endif
00076 
00077  protected:
00078   //: The fixed internal camera calibration
00079   vpgl_calibration_matrix<double> K_;
00080   //: The known points in the world
00081   vcl_vector<vgl_homg_point_3d<double> > world_points_;
00082   //: The corresponding points in the image
00083   vcl_vector<vgl_point_2d<double> > image_points_;
00084 };
00085 
00086 //: this class optimizes the rotation/translation/calibration of a perspective camera given an initial estimate
00087 class vpgl_orientation_position_calibration_lsqr : public vnl_least_squares_function
00088 {
00089  public:
00090   //: Constructor
00091   // \note image points are not homogeneous because require finite points to measure projection error
00092   vpgl_orientation_position_calibration_lsqr(const vcl_vector<vgl_homg_point_3d<double> >& world_points,
00093                                              const vcl_vector<vgl_point_2d<double> >& image_points );
00094   //: Destructor
00095   virtual ~vpgl_orientation_position_calibration_lsqr() {}
00096 
00097   //: The main function.
00098   //  Given the parameter vector x, compute the vector of residuals fx.
00099   //  Fx has been sized appropriately before the call.
00100   //  The parameters in x are really two three component vectors {wx, wy, wz, tx, ty, tz}
00101   //  where w is the Rodrigues vector of the rotation and t is the translation.
00102   virtual void f(vnl_vector<double> const& x, vnl_vector<double>& fx);
00103 
00104 #if 0
00105   //: Called after each LM iteration to print debugging etc.
00106   virtual void trace(int iteration, vnl_vector<double> const& x, vnl_vector<double> const& fx);
00107 #endif
00108 
00109  protected:
00110   //: The known points in the world
00111   vcl_vector<vgl_homg_point_3d<double> > world_points_;
00112   //: The corresponding points in the image
00113   vcl_vector<vgl_point_2d<double> > image_points_;
00114 };
00115 
00116 
00117 class vpgl_optimize_camera
00118 {
00119  public:
00120   ~vpgl_optimize_camera();
00121 
00122   //: optimize orientation for a perspective camera
00123   static vpgl_perspective_camera<double>
00124     opt_orient(const vpgl_perspective_camera<double>& camera,
00125                const vcl_vector<vgl_homg_point_3d<double> >& world_points,
00126                const vcl_vector<vgl_point_2d<double> >& image_points );
00127 
00128   //: optimize orientation and position for a perspective camera
00129   static vpgl_perspective_camera<double>
00130     opt_orient_pos(const vpgl_perspective_camera<double>& camera,
00131                    const vcl_vector<vgl_homg_point_3d<double> >& world_points,
00132                    const vcl_vector<vgl_point_2d<double> >& image_points );
00133 
00134   //: optimize orientation, position and internal calibration(no skew)for a perspective camera
00135   static vpgl_perspective_camera<double>
00136     opt_orient_pos_cal(const vpgl_perspective_camera<double>& camera,
00137                        const vcl_vector<vgl_homg_point_3d<double> >& world_points,
00138                        const vcl_vector<vgl_point_2d<double> >& image_points,
00139                        const double xtol = 0.0001, const unsigned nevals=10000);
00140 
00141 
00142  private:
00143   //: Constructor private - static methods only
00144   vpgl_optimize_camera();
00145 };
00146 
00147 #endif // vpgl_optimize_camera_h_