vpgl_bundle_adjust.h
Go to the documentation of this file.
1 // This is vpgl/algo/vpgl_bundle_adjust.h
2 #ifndef vpgl_bundle_adjust_h_
3 #define vpgl_bundle_adjust_h_
4 //:
5 // \file
6 // \brief Bundle adjustment sparse least squares functions
7 // \author Matt Leotta
8 // \date April 18, 2005
9 // \verbatim
10 // Modifications
11 // Mar 23, 2010 MJL - Separate file for least square function class
12 // \endverbatim
13 
14 
15 #include <vector>
16 #include <vnl/vnl_vector.h>
17 #include <vgl/vgl_point_2d.h>
18 #include <vgl/vgl_point_3d.h>
21 #ifdef _MSC_VER
22 # include <vcl_msvc_warnings.h>
23 #endif
24 
25 
26 //: Static functions for bundle adjustment
28 {
29  public:
30  //: Constructor
32  //: Destructor
34 
35  void set_use_m_estimator(bool use_m) { use_m_estimator_ = use_m; }
36  void set_m_estimator_scale(double scale) { m_estimator_scale_ = scale; }
37  void set_use_gradient(bool use_gradient) { use_gradient_ = use_gradient; }
38  void set_self_calibrate(bool self_calibrate) { self_calibrate_ = self_calibrate; }
39  void set_normalize_data(bool normalize) { normalize_data_ = normalize; }
40  void set_verbose(bool verbose) { verbose_ = verbose; }
41  void set_max_iterations(unsigned maxitr) { max_iterations_ = maxitr; }
42  void set_x_tolerence(double xtol) { x_tol_ = xtol; }
43  void set_g_tolerence(double gtol) { g_tol_ = gtol; }
44  //: step size for finite differencing operations
45  void set_epsilon(double eps) { epsilon_ = eps; }
46 
47  //: Return the ending error
48  double end_error() const { return end_error_; }
49  //: Return the starting error
50  double start_error() const { return start_error_; }
51  //: Return the number of iterations
52  int num_iterations() const { return num_iterations_; }
53  //: Return the weights a the end of the optimization
54  const std::vector<double>& final_weights() const { return weights_; }
55 
56  //: Return the raw camera parameters
57  const vnl_vector<double>& cam_params() const { return a_; }
58  //: Return the raw world point parameters
59  const vnl_vector<double>& point_params() const { return b_; }
60 
61  //: Approximately depth invert the scene.
62  // Apply this and re-optimize to get out of a common local minimum.
63  // Find the mean axis between cameras and points, mirror the points about
64  // a plane perpendicular to this axis, and rotate the cameras 180 degrees
65  // around this axis
66  void depth_reverse(std::vector<vpgl_perspective_camera<double> >& cameras,
67  std::vector<vgl_point_3d<double> >& world_points);
68 
69  //: Bundle Adjust
70  //
71  // \param mask should have the same number of entries as \param cameras,
72  // and each entry of \param mask should be the same size as
73  // \param world_points. mask[i][j] is true if point j is visible from
74  // camera i
75  //
76  // \param image_points and is a linear list of the 2D locations of the
77  // 3D points as seen by the cameras. There is one image point for every
78  // true in \param mask. The following piece of code shows the structure
79  // of \param image_points
80  //
81  // for( int c = 0; c < num_cameras; c++ ){
82  // for( int dp = 0; dp < num_world_points; dp++ ){
83  // if( mask[c][dp] )
84  // img_points.push_back( all_image_points[c*num_world_points+dp] );
85  // }
86  // }
87  bool optimize(std::vector<vpgl_perspective_camera<double> >& cameras,
88  std::vector<vgl_point_3d<double> >& world_points,
89  const std::vector<vgl_point_2d<double> >& image_points,
90  const std::vector<std::vector<bool> >& mask);
91 
92  //: Write cameras and points to a file in VRML 2.0 for debugging
93  static void write_vrml(const std::string& filename,
94  const std::vector<vpgl_perspective_camera<double> >& cameras,
95  const std::vector<vgl_point_3d<double> >& world_points);
96 
97  private:
98  //: normalize image points to be mean centered with scale sqrt(2)
99  // \return parameters such that original point are recovered as (ns*x+nx, ns*y+ny)
100  void normalize_points(std::vector<vgl_point_2d<double> >& image_points,
101  double& nx, double& ny, double& ns);
102 
103  // reflect the points about a plane
104  void reflect_points(const vgl_plane_3d<double>& plane,
105  std::vector<vgl_point_3d<double> >& points);
106 
107  // rotation the cameras 180 degrees around an axis
108  void rotate_cameras(const vgl_vector_3d<double>& axis,
109  std::vector<vpgl_perspective_camera<double> >& cameras);
110 
111  //: The bundle adjustment error function
113  //: The last camera parameters
115  //: The last point parameters
117  //: The last global parameters
119  //: The last estimated weights
120  std::vector<double> weights_;
121 
127  bool verbose_;
128  unsigned int max_iterations_;
129  double x_tol_;
130  double g_tol_;
131  double epsilon_;
132 
133  double start_error_;
134  double end_error_;
136 };
137 
138 
139 #endif // vpgl_bundle_adjust_h_
void reflect_points(const vgl_plane_3d< double > &plane, std::vector< vgl_point_3d< double > > &points)
A class for the perspective camera model.
const vnl_vector< double > & cam_params() const
Return the raw camera parameters.
Static functions for bundle adjustment.
void normalize_points(std::vector< vgl_point_2d< double > > &image_points, double &nx, double &ny, double &ns)
normalize image points to be mean centered with scale sqrt(2).
vpgl_bundle_adjust()
Constructor.
bool optimize(std::vector< vpgl_perspective_camera< double > > &cameras, std::vector< vgl_point_3d< double > > &world_points, const std::vector< vgl_point_2d< double > > &image_points, const std::vector< std::vector< bool > > &mask)
Bundle Adjust.
vnl_vector< double > a_
The last camera parameters.
static void write_vrml(const std::string &filename, const std::vector< vpgl_perspective_camera< double > > &cameras, const std::vector< vgl_point_3d< double > > &world_points)
Write cameras and points to a file in VRML 2.0 for debugging.
const vnl_vector< double > & point_params() const
Return the raw world point parameters.
void depth_reverse(std::vector< vpgl_perspective_camera< double > > &cameras, std::vector< vgl_point_3d< double > > &world_points)
Approximately depth invert the scene.
std::vector< double > weights_
The last estimated weights.
Bundle adjustment sparse least squares base class.
void set_g_tolerence(double gtol)
base class bundle adjustment sparse least squares function.
void set_x_tolerence(double xtol)
void set_self_calibrate(bool self_calibrate)
const std::vector< double > & final_weights() const
Return the weights a the end of the optimization.
vnl_vector< double > b_
The last point parameters.
~vpgl_bundle_adjust()
Destructor.
void set_use_m_estimator(bool use_m)
This class implements the perspective camera class as described in Hartley & Zisserman as a finite ca...
void rotate_cameras(const vgl_vector_3d< double > &axis, std::vector< vpgl_perspective_camera< double > > &cameras)
void set_verbose(bool verbose)
void set_max_iterations(unsigned maxitr)
void set_normalize_data(bool normalize)
unsigned int max_iterations_
vnl_vector< double > c_
The last global parameters.
void set_use_gradient(bool use_gradient)
void set_epsilon(double eps)
step size for finite differencing operations.
vpgl_bundle_adjust_lsqr * ba_func_
The bundle adjustment error function.
int num_iterations() const
Return the number of iterations.
v & normalize(v &a)
double end_error() const
Return the ending error.
void set_m_estimator_scale(double scale)
double start_error() const
Return the starting error.