vpgl_ba_fixed_k_lsqr.cxx
Go to the documentation of this file.
1 // This is vpgl/algo/vpgl_ba_fixed_k_lsqr.cxx
2 #include <utility>
3 #include "vpgl_ba_fixed_k_lsqr.h"
4 //:
5 // \file
6 
7 #include <vnl/vnl_vector_ref.h>
9 
10 
11 
12 //: Constructor
15  const std::vector<vgl_point_2d<double> >& image_points,
16  const std::vector<std::vector<bool> >& mask)
17  : vpgl_bundle_adjust_lsqr(6,3,0,image_points,mask),
18  K_(std::move(K))
19 {
20  for (auto & i : K_)
21  Km_.push_back(i.get_matrix());
22 }
23 
24 
25 //: Constructor
26 // Each image point is assigned an inverse covariance (error projector) matrix
27 // \note image points are not homogeneous because they require finite points to measure projection error
30  const std::vector<vgl_point_2d<double> >& image_points,
31  const std::vector<vnl_matrix<double> >& inv_covars,
32  const std::vector<std::vector<bool> >& mask)
33  : vpgl_bundle_adjust_lsqr(6,3,0,image_points,inv_covars,mask),
34  K_(std::move(K))
35 {
36  for (auto & i : K_)
37  Km_.push_back(i.get_matrix());
38 }
39 
40 
41 //: compute the Jacobian Aij
42 void vpgl_ba_fixed_k_lsqr::jac_Aij(unsigned int i,
43  unsigned int /*j*/,
44  vnl_double_3x4 const& Pi,
45  vnl_vector<double> const& ai,
46  vnl_vector<double> const& bj,
47  vnl_vector<double> const& /*c*/,
48  vnl_matrix<double>& Aij)
49 {
50  // the translation part
51  // --------------------
52  vnl_double_3x3 M = Pi.extract(3,3);
53  // This is semi const incorrect - there is no vnl_vector_ref_const
54  const vnl_vector_ref<double> C(3,const_cast<double*>(ai.data_block())+3);
55  vnl_matrix<double> Aij_sub(2,3);
56  jac_camera_center(M,C,bj,Aij_sub);
57  Aij.update(Aij_sub,0,3);
58 
59 
60  // the rotation part
61  // -----------------
62  // This is semi const incorrect - there is no vnl_vector_ref_const
63  const vnl_vector_ref<double> r(3,const_cast<double*>(ai.data_block()));
64  jac_camera_rotation(Km_[i],C,r,bj,Aij);
65 }
66 
67 //: compute the Jacobian Bij
68 void vpgl_ba_fixed_k_lsqr::jac_Bij(unsigned int /*i*/,
69  unsigned int /*j*/,
70  vnl_double_3x4 const& Pi,
71  vnl_vector<double> const& /*ai*/,
72  vnl_vector<double> const& bj,
73  vnl_vector<double> const& /*c*/,
74  vnl_matrix<double>& Bij)
75 {
76  jac_inhomg_3d_point(Pi, bj, Bij);
77 }
78 
79 //: compute the Jacobian Cij
80 void vpgl_ba_fixed_k_lsqr::jac_Cij(unsigned int /*i*/,
81  unsigned int /*j*/,
82  vnl_double_3x4 const& /*Pi*/,
83  vnl_vector<double> const& /*ai*/,
84  vnl_vector<double> const& /*bj*/,
85  vnl_vector<double> const& /*c*/,
86  vnl_matrix<double>& /*Cij*/)
87 {
88  // do nothing, c parameters are not used
89 }
90 
91 //: construct the \param j-th perspective camera from a pointer to the j-th parameters of \param b and parameters \param c
94  const double* bj,
95  const vnl_vector<double>& /*c*/) const
96 {
97  return {bj[0], bj[1], bj[2]};
98 }
99 
100 //: construct the \param j-th perspective camera from a pointer to the j-th parameters of \param b and parameters \param c
103  const double* bj,
104  const vnl_vector<double>& /*c*/) const
105 {
106  return vnl_vector_fixed<double,4>(bj[0], bj[1], bj[2], 1.0);
107 }
108 
109 //: construct the \param i-th perspective camera from a pointer to the i-th parameters of \param a and parameters \param c
112  const double* ai,
113  const vnl_vector<double>& /*c*/) const
114 {
115  vnl_vector<double> w(ai,3);
116  vgl_homg_point_3d<double> t(ai[3], ai[4], ai[5]);
118 }
119 
120 //: compute a 3x4 camera matrix of camera \param i from a pointer to the i-th parameters of \param a and parameters \param c
123  const double* ai,
124  const vnl_vector<double>& /*c*/) const
125 {
126  const vnl_vector_ref<double> r(3,const_cast<double*>(ai));
127  vnl_double_3x3 M = Km_[i]*rod_to_matrix(r);
128  vnl_double_3x4 P;
129  P.set_columns(0, M.as_ref());
130  const vnl_vector_ref<double> center(3,const_cast<double*>(ai+3));
131  P.set_column(3,-(M*center));
132  return P;
133 }
134 
135 
136 //: Create the parameter vector \p a from a vector of cameras
139 {
140  vnl_vector<double> a(6*cameras.size(),0.0);
141  for (unsigned int i=0; i<cameras.size(); ++i)
142  {
143  const vpgl_perspective_camera<double>& cam = cameras[i];
144  const vgl_point_3d<double>& c = cam.get_camera_center();
145  const vgl_rotation_3d<double>& R = cam.get_rotation();
146 
147  // compute the Rodrigues vector from the rotation
149 
150  double* ai = a.data_block() + i*6;
151  ai[0]=w[0]; ai[1]=w[1]; ai[2]=w[2];
152  ai[3]=c.x(); ai[4]=c.y(); ai[5]=c.z();
153  }
154  return a;
155 }
156 
157 
158 //: Create the parameter vector \p b from a vector of 3D points
161 {
162  vnl_vector<double> b(3*world_points.size(),0.0);
163  for (unsigned int j=0; j<world_points.size(); ++j){
164  const vgl_point_3d<double>& point = world_points[j];
165  double* bj = b.data_block() + j*3;
166  bj[0]=point.x(); bj[1]=point.y(); bj[2]=point.z();
167  }
168  return b;
169 }
vnl_matrix_ref< T > as_ref()
void jac_Aij(unsigned int i, unsigned int j, vnl_double_3x4 const &Pi, vnl_vector< double > const &ai, vnl_vector< double > const &bj, vnl_vector< double > const &c, vnl_matrix< double > &Aij) override
compute the Jacobian Aij.
static void jac_camera_rotation(vnl_double_3x3 const &K, vnl_vector< double > const &C, vnl_vector< double > const &r, vnl_vector< double > const &pt, vnl_matrix< double > &J)
compute the 2x3 Jacobian of camera projection with respect to camera rotation df/dr where $f(r) = K*r...
static vnl_double_3x3 rod_to_matrix(vnl_vector< double > const &r)
Fast conversion of rotation from Rodrigues vector to matrix.
static void jac_camera_center(vnl_double_3x3 const &M, vnl_vector< double > const &C, vnl_vector< double > const &pt, vnl_matrix< double > &J)
compute the 2x3 Jacobian of camera projection with respect to camera center df/dC where $f(C) = [M | ...
double const * data_block() const
vnl_vector_fixed< T, 3 > as_rodrigues() const
vnl_double_3x4 param_to_cam_matrix(int i, const double *ai, const vnl_vector< double > &c) const override
compute a 3x4 camera matrix of camera
std::vector< vpgl_calibration_matrix< double > > K_
The fixed internal camera calibration.
Type & z()
Bundle adjustment sparse least squares class for fixed intrinsics.
const vgl_rotation_3d< T > & get_rotation() const
vgl_homg_point_3d< double > param_to_point(int j, const double *bj, const vnl_vector< double > &c) const override
construct the
std::vector< vnl_double_3x3 > Km_
The fixed internal camera calibration in matrix form.
void jac_Bij(unsigned int i, unsigned int j, vnl_double_3x4 const &Pi, vnl_vector< double > const &ai, vnl_vector< double > const &bj, vnl_vector< double > const &c, vnl_matrix< double > &Bij) override
compute the Jacobian Bij.
base class bundle adjustment sparse least squares function.
vnl_matrix< T > extract(unsigned r, unsigned c, unsigned top=0, unsigned left=0) const
const vgl_point_3d< T > & get_camera_center() const
vnl_vector_fixed< double, 4 > param_to_pt_vector(int j, const double *bj, const vnl_vector< double > &c) const override
construct the
void jac_Cij(unsigned int i, unsigned int j, vnl_double_3x4 const &Pi, vnl_vector< double > const &ai, vnl_vector< double > const &bj, vnl_vector< double > const &c, vnl_matrix< double > &Cij) override
compute the Jacobian Cij.
static void jac_inhomg_3d_point(vnl_double_3x4 const &P, vnl_vector< double > const &pt, vnl_matrix< double > &J)
compute the 2x3 Jacobian of camera projection with respect to point location df/dpt where $f(pt) = P*...
Type & x()
This class implements the perspective camera class as described in Hartley & Zisserman as a finite ca...
vpgl_ba_fixed_k_lsqr(std::vector< vpgl_calibration_matrix< double > > K, const std::vector< vgl_point_2d< double > > &image_points, const std::vector< std::vector< bool > > &mask)
Constructor.
static vnl_vector< double > create_param_vector(const std::vector< vpgl_perspective_camera< double > > &cameras)
Create the parameter vector a from a vector of cameras.
vnl_matrix_fixed & set_column(unsigned i, T const *v)
vnl_matrix< double > & update(vnl_matrix< double > const &, unsigned top=0, unsigned left=0)
vpgl_perspective_camera< double > param_to_cam(int i, const double *ai, const vnl_vector< double > &c) const override
construct the
vnl_matrix_fixed & set_columns(unsigned starting_column, vnl_matrix< T > const &M)
Type & y()