vpgl_camera_transform.h
Go to the documentation of this file.
1 // This is core/vpgl/algo/vpgl_camera_transform.h
2 #ifndef vpgl_camera_transform_h_
3 #define vpgl_camera_transform_h_
4 //:
5 // \file
6 // \brief Methods for computing a fixed transformation that moves and orients a camera so that given 3-d points project to the given 2-d image positions
7 // \author Ozge C. Ozcanli
8 // \date Jan 13, 2014
9 
10 #include <utility>
11 #include <vpgl/vpgl_proj_camera.h>
12 #ifdef _MSC_VER
13 # include <vcl_msvc_warnings.h>
14 #endif
18 
19 //: The cost function to be used by a Lev-Marq minimization routine, compute the residuals for each correspondence
20 // 6 dimensional param vector, x[0], x[1], x[2] constitute Rodriguez vector of rotation, x[3], x[4], x[5] constitute the translation of the camera
21 /// the fixed and the same rotation and translation are applied to all the cameras
23 {
24  public:
25  //: Constructor. The source image is mapped to the destination frame by dt. nbins is the number of histogram bins used to compute entropies.
26  vpgl_camera_transform_f(unsigned cnt_residuals, unsigned n_unknowns,
27  const std::vector<vpgl_perspective_camera<double> >& input_cams,
28  std::vector< std::vector< std::pair<vnl_vector_fixed<double, 2>, unsigned> > > cam_ids_img_pts,
29  std::vector<vnl_vector_fixed<double, 4> > pts_3d, bool minimize_R = true);
30 
31  //: The main function.
32  // Given the parameter vector x, compute the vector of residuals fx.
33  // Fx has been sized appropriately before the call.
34  void f(vnl_vector<double> const& x, vnl_vector<double>& fx) override;
35 
36  //: Calculate the Jacobian, given the parameter vector x using forward differencing
37  //virtual void gradf(vnl_vector<double> const& x, vnl_matrix<double>& jacobian);
38 
39  //: conversion of rodriguez params to R
40  virtual vnl_matrix_fixed<double,3,3> rod_to_matrix(double r0, double r1, double r2);
41  virtual void compute_cams(vnl_vector<double> const& x, std::vector<vpgl_perspective_camera<double> >& output_cams);
42  virtual void compute_cams_selective(vnl_vector<double> const& x, std::vector<vpgl_perspective_camera<double> >& output_cams);
43 
44  // for each 3d point j, there are n possible 2d image projections
45  std::vector< std::vector< std::pair<vnl_vector_fixed<double, 2>, unsigned> > > cam_ids_img_pts_;
46  std::vector<vnl_vector_fixed<double, 4> > pts_3d_;
47 
48  std::vector<vpgl_calibration_matrix<double> > Ks_;
49  std::vector<vnl_matrix_fixed<double, 3, 3> > Rs_;
50  std::vector<vgl_point_3d<double> > Cs_;
51  std::vector<vpgl_perspective_camera<double> > input_cams_;
52 
54 
55  protected:
56 
57 };
58 
60 {
61  public:
63 
64  //: apply fixeld transformation -- R_new = R*R_old
65  static void apply_fixed_transformation(const std::vector<vpgl_perspective_camera<double> >& input_cams,
67  std::vector<vpgl_perspective_camera<double> >& output_cams);
68 
69  //: compute the fixed transformation as R and camera center
70  static bool compute_fixed_transformation(const std::vector<vpgl_perspective_camera<double> >& input_cams,
71  const std::vector< std::vector< std::pair<vnl_vector_fixed<double, 2>, unsigned> > >& cam_ids_img_pts,
72  const std::vector<vnl_vector_fixed<double, 4> >& pts_3d,
73  std::vector<vpgl_perspective_camera<double> >& output_cams);
74 
75  //: sample offsets for camera centers in a box with the given dimensions (e.g. plus/minus dim_x) in meters
76  static std::vector<vnl_vector_fixed<double, 3> > sample_centers(double dim_x, double dim_y, double dim_z, double step);
77 
78  //: compute the fixed transformation by sampling centers in a given box and then optimizing for rotation
79  static bool compute_fixed_transformation_sample(const std::vector<vpgl_perspective_camera<double> >& input_cams,
80  const std::vector< std::vector< std::pair<vnl_vector_fixed<double, 2>, unsigned> > >& cam_ids_img_pts,
81  const std::vector<vnl_vector_fixed<double, 4> >& pts_3d,
82  std::vector<vpgl_perspective_camera<double> >& output_cams);
83 
84  static void normalize_img_pts(const std::vector<vpgl_perspective_camera<double> >& input_cams,
85  const std::vector< std::vector < std::pair<vnl_vector_fixed<double, 2>, unsigned> > >& cam_ids_img_pts,
86  std::vector<vpgl_perspective_camera<double> >& input_cams_norm,
87  std::vector< std::vector < std::pair<vnl_vector_fixed<double, 2>, unsigned> > >& cam_ids_img_pts_norm);
88 
89  //: normalize the points using the inverse of the K matrix
90  static void K_normalize_img_pts(const std::vector<vpgl_perspective_camera<double> >& input_cams, vnl_matrix_fixed<double, 3, 3> const& input_correspondence_covariance,
91  const std::vector< std::vector < std::pair<vnl_vector_fixed<double, 2>, unsigned> > >& cam_ids_img_pts,
92  std::vector< std::vector < std::pair< std::pair<vnl_vector_fixed<double, 3>, vnl_matrix_fixed<double, 3, 3> >, unsigned> > >& cam_ids_img_pts_norm);
93 
94  // compute both R and t -- // TODO: the formulation for the initial transformation need to be updated to multiple R from left (not right as in the current version)
95  static bool compute_initial_transformation(const std::vector<vpgl_perspective_camera<double> >& input_cams,
96  const std::vector< std::vector < std::pair<vnl_vector_fixed<double, 2>, unsigned> > >& cam_ids_img_pts,
97  const std::vector<vnl_vector_fixed<double, 4> >& pts_3d,
98  std::vector<vpgl_perspective_camera<double> >& output_cams);
99 
100  // compute only t
101  static bool compute_initial_transformation_t(const std::vector<vpgl_perspective_camera<double> >& input_cams,
102  const std::vector< std::vector < std::pair<vnl_vector_fixed<double, 2>, unsigned> > >& cam_ids_img_pts,
103  const std::vector<vnl_vector_fixed<double, 4> >& pts_3d,
104  std::vector<vpgl_perspective_camera<double> >& output_cams);
105 
106  // compute only R -- // TODO: the formulation for the initial transformation need to be updated to multiple R from left (not right as in the current version)
107  static bool compute_initial_transformation_R(const std::vector<vpgl_perspective_camera<double> >& input_cams,
108  const std::vector< std::vector < std::pair<vnl_vector_fixed<double, 2>, unsigned> > >& cam_ids_img_pts,
109  const std::vector<vnl_vector_fixed<double, 4> >& pts_3d,
110  std::vector<vpgl_perspective_camera<double> >& output_cams);
111 
113  // use quaternions
115 
116  //: pass the ids of cams in the input_cams vector, this method computes the variance between these two using their image correspondences (K normalized image points)
117  // correspondence_covariance is the 3x3 covariance matrix of the points in the K normalized image space
118  static bool compute_covariance(unsigned i, unsigned j, const std::vector<vpgl_perspective_camera<double> >& input_cams,
119  const std::vector< std::vector < std::pair< std::pair<vnl_vector_fixed<double, 3>, vnl_matrix_fixed<double, 3, 3> >, unsigned> > >& cam_ids_img_pts,
120  vnl_matrix_fixed<double, 3, 3>& rot_variance);
121 
122  private:
123  //: constructor private - static methods only
125 };
126 
127 #endif // vpgl_camera_transform_h_
static bool compute_initial_transformation_R(const std::vector< vpgl_perspective_camera< double > > &input_cams, const std::vector< std::vector< std::pair< vnl_vector_fixed< double, 2 >, unsigned > > > &cam_ids_img_pts, const std::vector< vnl_vector_fixed< double, 4 > > &pts_3d, std::vector< vpgl_perspective_camera< double > > &output_cams)
virtual void compute_cams(vnl_vector< double > const &x, std::vector< vpgl_perspective_camera< double > > &output_cams)
virtual void compute_cams_selective(vnl_vector< double > const &x, std::vector< vpgl_perspective_camera< double > > &output_cams)
static void K_normalize_img_pts(const std::vector< vpgl_perspective_camera< double > > &input_cams, vnl_matrix_fixed< double, 3, 3 > const &input_correspondence_covariance, const std::vector< std::vector< std::pair< vnl_vector_fixed< double, 2 >, unsigned > > > &cam_ids_img_pts, std::vector< std::vector< std::pair< std::pair< vnl_vector_fixed< double, 3 >, vnl_matrix_fixed< double, 3, 3 > >, unsigned > > > &cam_ids_img_pts_norm)
normalize the points using the inverse of the K matrix.
std::vector< vnl_vector_fixed< double, 4 > > pts_3d_
static bool normalize_to_rotation_matrix(const vnl_matrix_fixed< double, 3, 3 > &R, vnl_matrix_fixed< double, 3, 3 > &R_norm)
static bool compute_covariance(unsigned i, unsigned j, const std::vector< vpgl_perspective_camera< double > > &input_cams, const std::vector< std::vector< std::pair< std::pair< vnl_vector_fixed< double, 3 >, vnl_matrix_fixed< double, 3, 3 > >, unsigned > > > &cam_ids_img_pts, vnl_matrix_fixed< double, 3, 3 > &rot_variance)
pass the ids of cams in the input_cams vector, this method computes the variance between these two us...
void f(vnl_vector< double > const &x, vnl_vector< double > &fx) override
The main function.
static bool normalize_to_rotation_matrix_q(const vnl_matrix_fixed< double, 3, 3 > &R, vnl_matrix_fixed< double, 3, 3 > &R_norm)
std::vector< vnl_matrix_fixed< double, 3, 3 > > Rs_
vpgl_camera_transform()
constructor private - static methods only.
static std::vector< vnl_vector_fixed< double, 3 > > sample_centers(double dim_x, double dim_y, double dim_z, double step)
sample offsets for camera centers in a box with the given dimensions (e.g. plus/minus dim_x) in meter...
virtual vnl_matrix_fixed< double, 3, 3 > rod_to_matrix(double r0, double r1, double r2)
Calculate the Jacobian, given the parameter vector x using forward differencing.
static bool compute_initial_transformation_t(const std::vector< vpgl_perspective_camera< double > > &input_cams, const std::vector< std::vector< std::pair< vnl_vector_fixed< double, 2 >, unsigned > > > &cam_ids_img_pts, const std::vector< vnl_vector_fixed< double, 4 > > &pts_3d, std::vector< vpgl_perspective_camera< double > > &output_cams)
vpgl_camera_transform_f(unsigned cnt_residuals, unsigned n_unknowns, const std::vector< vpgl_perspective_camera< double > > &input_cams, std::vector< std::vector< std::pair< vnl_vector_fixed< double, 2 >, unsigned > > > cam_ids_img_pts, std::vector< vnl_vector_fixed< double, 4 > > pts_3d, bool minimize_R=true)
Constructor. The source image is mapped to the destination frame by dt. nbins is the number of histog...
static void normalize_img_pts(const std::vector< vpgl_perspective_camera< double > > &input_cams, const std::vector< std::vector< std::pair< vnl_vector_fixed< double, 2 >, unsigned > > > &cam_ids_img_pts, std::vector< vpgl_perspective_camera< double > > &input_cams_norm, std::vector< std::vector< std::pair< vnl_vector_fixed< double, 2 >, unsigned > > > &cam_ids_img_pts_norm)
std::vector< vgl_point_3d< double > > Cs_
The cost function to be used by a Lev-Marq minimization routine, compute the residuals for each corre...
static bool compute_fixed_transformation(const std::vector< vpgl_perspective_camera< double > > &input_cams, const std::vector< std::vector< std::pair< vnl_vector_fixed< double, 2 >, unsigned > > > &cam_ids_img_pts, const std::vector< vnl_vector_fixed< double, 4 > > &pts_3d, std::vector< vpgl_perspective_camera< double > > &output_cams)
compute the fixed transformation as R and camera center.
This class implements the perspective camera class as described in Hartley & Zisserman as a finite ca...
A camera model using the standard 3x4 matrix representation.
std::vector< vpgl_calibration_matrix< double > > Ks_
static bool compute_fixed_transformation_sample(const std::vector< vpgl_perspective_camera< double > > &input_cams, const std::vector< std::vector< std::pair< vnl_vector_fixed< double, 2 >, unsigned > > > &cam_ids_img_pts, const std::vector< vnl_vector_fixed< double, 4 > > &pts_3d, std::vector< vpgl_perspective_camera< double > > &output_cams)
compute the fixed transformation by sampling centers in a given box and then optimizing for rotation.
static void apply_fixed_transformation(const std::vector< vpgl_perspective_camera< double > > &input_cams, vnl_matrix_fixed< double, 3, 3 > &R, vgl_point_3d< double > &t, std::vector< vpgl_perspective_camera< double > > &output_cams)
apply fixeld transformation – R_new = R*R_old.
std::vector< vpgl_perspective_camera< double > > input_cams_
A class for the calibration matrix component of a perspective camera matrix.
std::vector< std::vector< std::pair< vnl_vector_fixed< double, 2 >, unsigned > > > cam_ids_img_pts_
static bool compute_initial_transformation(const std::vector< vpgl_perspective_camera< double > > &input_cams, const std::vector< std::vector< std::pair< vnl_vector_fixed< double, 2 >, unsigned > > > &cam_ids_img_pts, const std::vector< vnl_vector_fixed< double, 4 > > &pts_3d, std::vector< vpgl_perspective_camera< double > > &output_cams)