Points&Forces (survey)
Software tools facilitating the task of surveying architecture
p_compute.cxx
Go to the documentation of this file.
1 /*
2  Copyright 2008-2019 Pierre SMARS (smars@yuntech.edu.tw)
3  This program is free software: you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation, either version 2 of the License, or
6  (at your option) any later version.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 #include <iostream>
17 #include <fstream>
18 #include <vector>
19 #include <vgl/vgl_homg_point_2d.h>
20 #include <vgl/vgl_homg_point_3d.h>
21 #include <vgl/vgl_distance.h>
22 #include <mvl/PMatrix.h>
23 #include <mvl/PMatrixComputeLinear.h>
24 #include <mvl/HomgNorm2D.h>
25 #include <mvl/PMatrixDecompCR.h>
26 #include <vnl/vnl_least_squares_function.h>
27 #include <vnl/algo/vnl_levenberg_marquardt.h>
28 
29 #include "a_point.h"
30 #include "SimpleOpt.h"
31 
33 
34 CSimpleOpt::SOption g_rgOptions[] = {
35  { OPT_NONLIN, _T("-nl"), SO_NONE },
36  { OPT_NONLIN, _T("--non-linear"), SO_NONE },
37  { OPT_INTERNAL, _T("-i"), SO_REQ_SEP },
38  { OPT_INTERNAL, _T("--internal"), SO_REQ_SEP },
39  { OPT_HOMOGENEOUS, _T("-hg"), SO_NONE },
40  { OPT_HOMOGENEOUS, _T("--homogeneous"), SO_NONE },
41  { OPT_SKEW, _T("-ns"), SO_NONE },
42  { OPT_SKEW, _T("--no-skew"), SO_NONE },
43  { OPT_SQUARE, _T("-sp"), SO_NONE },
44  { OPT_SQUARE, _T("--square-pixels"), SO_NONE },
45  { OPT_RADIAL, _T("-r"), SO_NONE },
46  { OPT_RADIAL, _T("--radial"), SO_NONE },
47  { OPT_VERB, _T("-v"), SO_NONE },
48  { OPT_VERB, _T("--verbose"), SO_NONE },
49  { OPT_QUIET, _T("-q"), SO_NONE },
50  { OPT_QUIET, _T("--quiet"), SO_NONE },
51  { OPT_OUT, _T("-o"), SO_REQ_SEP },
52  { OPT_HELP, _T("-?"), SO_NONE },
53  { OPT_HELP, _T("-h"), SO_NONE },
54  { OPT_HELP, _T("--help"), SO_NONE },
55  SO_END_OF_OPTIONS
56 };
57 
58 bool verbose = false;
59 //---------------------------------------------------------------------------
60 int error(int val)
61 {
62 #include "p_compute.help"
63  return val;
64 }
65 //***************************************************************************
68 {
69  public:
70  a_camera_function( std::vector<vgl_homg_point_2d<double> > & points_image,
71  std::vector<vgl_homg_point_3d<double> > & points_world) :
72  vnl_least_squares_function(12, points_image.size()*2+2, no_gradient),
73  points_image_(points_image),
74  points_world_(points_world),
75  w_(0.),
76  square_pixels_(false),
77  no_skew_(false)
78  {};
79  void f(const vnl_vector<double>& x, vnl_vector<double>& fx);
80  void set_w(const double w) {w_=w;}
81  double get_w() const {return w_;}
82  void set_constraints(const bool sp, const bool sk) {square_pixels_=sp; no_skew_=sk;};
83  double get_constraint(const vnl_vector<double>& x);
84  protected:
85  std::vector<vgl_homg_point_2d<double> > & points_image_;
86  std::vector<vgl_homg_point_3d<double> > & points_world_;
87  double w_;
89  bool no_skew_;
90 };
91 //---------------------------------------------------------------------------
92 void a_camera_function::f(const vnl_vector<double>& x, vnl_vector<double>& fx)
93 {
94  vnl_double_3x4 p0;
95  p0(0,0) = x[0]; p0(0,1) = x[1]; p0(0,2) = x[2]; p0(0,3) = x[3];
96  p0(1,0) = x[4]; p0(1,1) = x[5]; p0(1,2) = x[6]; p0(1,3) = x[7];
97  p0(2,0) = x[8]; p0(2,1) = x[9]; p0(2,2) = x[10]; p0(2,3) = x[11];
98  double no = sqrt(p0(2,0)*p0(2,0)+p0(2,1)*p0(2,1)+p0(2,2)*p0(2,2));
99  p0 /= no;
100  PMatrix P(p0);
101  double d = 0;
102  for (int i = 0; i< points_image_.size(); i++)
103  {
104  vgl_homg_point_2d<double> im = P.project(points_world_[i]);
105  fx[i*2] = points_image_[i].x()-im.x()/im.w();
106  fx[i*2+1] = points_image_[i].y()-im.y()/im.w();
107  if (verbose)
108  {
109  double dist = vgl_distance(points_image_[i],im);
110  d += dist*dist;
111  }
112  }
113  PMatrixDecompCR decomp(P);
114  vnl_double_3x3 K = decomp.C;
115  if (no_skew_)
116  fx[2*points_image_.size()] = w_*K(0,1);
117  else
118  fx[2*points_image_.size()] = 0.;
119  if (square_pixels_)
120  fx[2*points_image_.size()+1] = w_*(K(0,0)-K(1,1));
121  else
122  fx[2*points_image_.size()+1] = 0.;
123  if (verbose)
124  std::cerr << sqrt(d/points_image_.size()) << " ";
125 }
126 //---------------------------------------------------------------------------
127 double a_camera_function::get_constraint(const vnl_vector<double>& x)
128 {
129  vnl_double_3x4 p0;
130  p0(0,0) = x[0]; p0(0,1) = x[1]; p0(0,2) = x[2]; p0(0,3) = x[3];
131  p0(1,0) = x[4]; p0(1,1) = x[5]; p0(1,2) = x[6]; p0(1,3) = x[7];
132  p0(2,0) = x[8]; p0(2,1) = x[9]; p0(2,2) = x[10]; p0(2,3) = x[11];
133  double no = sqrt(p0(2,0)*p0(2,0)+p0(2,1)*p0(2,1)+p0(2,2)*p0(2,2));
134  p0 /= no;
135  PMatrix P(p0);
136  PMatrixDecompCR decomp(P);
137  vnl_double_3x3 K = decomp.C;
138  double d = K(0,1)*K(0,1);
139  d += (K(0,0)-K(1,1))*(K(0,0)-K(1,1));
140  return sqrt(d);
141 }
142 //***************************************************************************
145 {
146  public:
147  a_camera_function_internal( std::vector<vgl_homg_point_2d<double> > & points_image,
148  std::vector<vgl_homg_point_3d<double> > & points_world) :
149  vnl_least_squares_function(12, points_image.size()*2+5, no_gradient),
150  points_image_(points_image),
151  points_world_(points_world),
152  w_(0.)
153  {};
154  void f(const vnl_vector<double>& x, vnl_vector<double>& fx);
155  void set_w(const double w) {w_=w;}
156  double get_w() const {return w_;}
157  void set_K(const vnl_double_3x3& K) {K_=K;}
158  void set_hk(const vnl_double_3x3& K) {hk_=K;}
159  void set_hK(const vnl_double_4x4& K) {hK_=K;}
160  double get_constraint(const vnl_vector<double>& x);
161  protected:
162  std::vector<vgl_homg_point_2d<double> > & points_image_;
163  std::vector<vgl_homg_point_3d<double> > & points_world_;
164  double w_;
165  vnl_double_3x3 K_;
166  vnl_double_3x3 hk_;
167  vnl_double_4x4 hK_;
168 };
169 //---------------------------------------------------------------------------
170 void a_camera_function_internal::f(const vnl_vector<double>& x, vnl_vector<double>& fx)
171 {
172  vnl_double_3x4 p0;
173  p0(0,0) = x[0]; p0(0,1) = x[1]; p0(0,2) = x[2]; p0(0,3) = x[3];
174  p0(1,0) = x[4]; p0(1,1) = x[5]; p0(1,2) = x[6]; p0(1,3) = x[7];
175  p0(2,0) = x[8]; p0(2,1) = x[9]; p0(2,2) = x[10]; p0(2,3) = x[11];
176  double no = sqrt(p0(2,0)*p0(2,0)+p0(2,1)*p0(2,1)+p0(2,2)*p0(2,2));
177  p0 /= no;
178  PMatrix P(p0);
179  double d = 0;
180  for (int i = 0; i< points_image_.size(); i++)
181  {
182  vgl_homg_point_2d<double> im = P.project(points_world_[i]);
183  fx[i*2] = points_image_[i].x()-im.x()/im.w();
184  fx[i*2+1] = points_image_[i].y()-im.y()/im.w();
185  if (verbose)
186  {
187  double dist = vgl_distance(points_image_[i],im);
188  d += dist*dist;
189  }
190  }
191  vnl_double_3x4 pmat2 = hk_*p0*hK_;
192  no = sqrt(pmat2(2,0)*pmat2(2,0)+pmat2(2,1)*pmat2(2,1)+pmat2(2,2)*pmat2(2,2));
193  pmat2 /= no;
194  PMatrix P2(pmat2);
195  PMatrixDecompCR decomp(P2);
196  vnl_double_3x3 K = decomp.C;
197  fx[2*points_image_.size()] = w_*(K(0,0)-K_(0,0));
198  fx[2*points_image_.size()+1] = w_*(K(0,1)-K_(0,1));
199  fx[2*points_image_.size()+2] = w_*(K(0,2)-K_(0,2));
200  fx[2*points_image_.size()+3] = w_*(K(1,1)-K_(1,1));
201  fx[2*points_image_.size()+4] = w_*(K(1,2)-K_(1,2));
202  if (verbose)
203  std::cerr << sqrt(d/points_image_.size()) << " ";
204 }
205 //---------------------------------------------------------------------------
206 double a_camera_function_internal::get_constraint(const vnl_vector<double>& x)
207 {
208  vnl_double_3x4 p0;
209  p0(0,0) = x[0]; p0(0,1) = x[1]; p0(0,2) = x[2]; p0(0,3) = x[3];
210  p0(1,0) = x[4]; p0(1,1) = x[5]; p0(1,2) = x[6]; p0(1,3) = x[7];
211  p0(2,0) = x[8]; p0(2,1) = x[9]; p0(2,2) = x[10]; p0(2,3) = x[11];
212  vnl_double_3x4 pmat = hk_*p0*hK_;
213  double no = sqrt(pmat(2,0)*pmat(2,0)+pmat(2,1)*pmat(2,1)+pmat(2,2)*pmat(2,2));
214  pmat /= no;
215  PMatrix P(pmat);
216  PMatrixDecompCR decomp(P);
217  vnl_double_3x3 K = decomp.C;
218  double d = (K(0,0)-K_(0,0))*(K(0,0)-K_(0,0));
219  d += (K(0,1)-K_(0,1))*(K(0,1)-K_(0,1));
220  d += (K(0,2)-K_(0,2))*(K(0,2)-K_(0,2));
221  d += (K(1,1)-K_(1,1))*(K(1,1)-K_(1,1));
222  d += (K(1,2)-K_(1,2))*(K(1,2)-K_(1,2));
223  return sqrt(d);
224 }
225 //***************************************************************************
228 {
229  public:
230  a_camera2_function( std::vector<vgl_homg_point_2d<double> > & points_image,
231  std::vector<vgl_homg_point_3d<double> > & points_world) :
232  vnl_least_squares_function(17, points_image.size()*2, no_gradient),
233  points_image_(points_image),
234  points_world_(points_world)
235  {};
236  void f(const vnl_vector<double>& x, vnl_vector<double>& fx);
237  protected:
238  std::vector<vgl_homg_point_2d<double> > & points_image_;
239  std::vector<vgl_homg_point_3d<double> > & points_world_;
240 };
241 //---------------------------------------------------------------------------
242 void a_camera2_function::f(const vnl_vector<double>& x, vnl_vector<double>& fx)
243 {
244  vnl_double_3x4 p0;
245  p0(0,0) = x[0]; p0(0,1) = x[1]; p0(0,2) = x[2]; p0(0,3) = x[3];
246  p0(1,0) = x[4]; p0(1,1) = x[5]; p0(1,2) = x[6]; p0(1,3) = x[7];
247  p0(2,0) = x[8]; p0(2,1) = x[9]; p0(2,2) = x[10]; p0(2,3) = x[11];
248  PMatrix P(p0);
249  double u0 = x[12];
250  double v0 = x[13];
251  vgl_homg_point_2d<double> center(u0,v0);
252  double k1 = x[14];
253  double k2 = x[15];
254  double k3 = x[16];
255  double k0 = 1.-k1-k2-k3;
256  double d = 0;
257  for (int i = 0; i< points_image_.size(); i++)
258  {
259  vgl_homg_point_2d<double> im = P.project(points_world_[i]);
260  double r= vgl_distance(center,im);
261  double L = k0+k1*r+k2*r*r+k3*r*r*r;
262  double u = points_image_[i].x();
263  double v = points_image_[i].y();
264  double un = u0+L*(u-u0);
265  double vn = v0+L*(v-v0);
266  fx[i*2] = un-im.x()/im.w();
267  fx[i*2+1] = vn-im.y()/im.w();
268  if (verbose)
269  {
270  double dist = vgl_distance(points_image_[i],im);
271  d += dist*dist;
272  }
273  }
274  if (verbose)
275  std::cerr << sqrt(d/points_image_.size()) << " ";
276 }
277 //***************************************************************************
278 //---------------------------------------------------------------------------
279 int main(int argc, char**argv)
280 {
281  bool nonlinear = false;
282  bool homogeneous = false;
283  bool squarepixels = false;
284  bool noskew = false;
285  bool quiet = false;
286  bool radial = false;
287  std::string out_code("p");
288  std::string internal_file;
289  double cu0, cv0;
290  double k0, k1, k2, k3;
291  CSimpleOpt args(argc, argv, g_rgOptions);
292  while (args.Next())
293  {
294  if (args.LastError() == SO_SUCCESS)
295  {
296  if (args.OptionId() == OPT_HELP)
297  return error(0);
298  else if (args.OptionId() == OPT_NONLIN)
299  nonlinear = true;
300  else if (args.OptionId() == OPT_INTERNAL)
301  internal_file = args.OptionArg();
302  else if (args.OptionId() == OPT_HOMOGENEOUS)
303  homogeneous = true;
304  else if (args.OptionId() == OPT_SQUARE)
305  squarepixels = true;
306  else if (args.OptionId() == OPT_SKEW)
307  noskew = true;
308  else if (args.OptionId() == OPT_RADIAL)
309  radial = true;
310  else if (args.OptionId() == OPT_VERB)
311  verbose = true;
312  else if (args.OptionId() == OPT_QUIET)
313  quiet = true;
314  else if (args.OptionId() == OPT_OUT)
315  out_code = args.OptionArg();
316  else
317  return error(-1);
318  // handle option, using OptionId(), OptionText() and OptionArg()
319  } else {
320  std::cerr << "Invalid argument: " << args.OptionText() << std::endl;
321  return error(args.LastError());
322  // handle error, one of: SO_OPT_INVALID, SO_OPT_MULTIPLE,
323  // SO_ARG_INVALID, SO_ARG_INVALID_TYPE, SO_ARG_MISSING
324  }
325  }
326  if (args.FileCount() != 0) return error(-2);
327  if (radial|| squarepixels|| noskew|| (internal_file != "")) nonlinear = true;
328  vnl_double_3x3 * K0;
329  if (internal_file != "")
330  {
331  std::ifstream in(internal_file.c_str());
332  if (!in)
333  {
334  std::cerr << "Cannot open file '" << internal_file << "'" << std::endl;
335  return error(-2);
336  }
337  K0 = new vnl_double_3x3;
338  int nx,ny;
339  in >> nx >> ny;
340  if ((nx!=3)||(ny!=3))
341  {
342  std::cerr << "Internal orientation matrix has wrong dimension: " << nx << "x" << ny << " and should be 3x3" << std::endl;
343  return error(-3);
344  }
345  in >> *K0;
346  in.close();
347  }
348  std::vector<vgl_homg_point_2d<double> > points_image;
349  std::vector<vgl_homg_point_3d<double> > points_world;
350  // Read points
351  while (std::cin.good()) {
352  double x,y,z,t;
353  std::cin >> x >> y;
354  if (homogeneous)
355  std::cin >> t;
356  else
357  t = 1.;
358  points_image.push_back(vgl_homg_point_2d<double>(x,y,t));
359  std::cin >> x >> y >> z;
360  if (homogeneous)
361  std::cin >> t;
362  points_world.push_back(vgl_homg_point_3d<double>(x,y,z,t));
363  std::cin >> std::ws; // Eat whitespace
364  }
365  //----------------------------------------
366  //normalisation
367  int npts = points_image.size();
368  //1. calculate centre of point cloud
369  double um = 0; double vm = 0;
370  double xm = 0; double ym = 0; double zm = 0;
371  for (int i=0; i< npts; i++)
372  {
373  um += points_image[i].x();
374  vm += points_image[i].y();
375  xm += points_world[i].x();
376  ym += points_world[i].y();
377  zm += points_world[i].z();
378  }
379  um /= npts; vm /= npts;
380  xm /= npts; ym /= npts; zm /= npts;
381  //2. compute average size of point cloud
382  a_point im0(um,vm,0.);
383  a_point wo0(xm,ym,zm);
384  double dim = 0.;
385  double dwm = 0.;
386  for (int i=0; i< npts; i++)
387  {
388  a_point im(points_image[i].x(),points_image[i].y(),0.);
389  double di = dist(im,im0);
390  dim += di*di;
391  a_point wo(points_world[i].x(),points_world[i].y(),points_world[i].z());
392  double dw = dist(wo,wo0);
393  dwm += dw*dw;
394  }
395  dim = sqrt(dim)/npts;
396  dwm = sqrt(dwm)/npts;
397  //3. scale and translate point cloud
398  std::vector<vgl_homg_point_2d<double> > points_image_n;
399  std::vector<vgl_homg_point_3d<double> > points_world_n;
400  for (int i=0; i< npts; i++)
401  {
402  double u = (points_image[i].x()-um)/dim;
403  double v = (points_image[i].y()-vm)/dim;
404  double x = (points_world[i].x()-xm)/dwm;
405  double y = (points_world[i].y()-ym)/dwm;
406  double z = (points_world[i].z()-zm)/dwm;
407  points_image_n.push_back(vgl_homg_point_2d<double>(u,v,1.0));
408  points_world_n.push_back(vgl_homg_point_3d<double>(x,y,z,1.0));
409  }
410  //4. prepare the transformation matrices
411  vnl_double_3x3 hk(0.);
412  hk.fill_diagonal(dim);
413  hk(0,2) = um;
414  hk(1,2) = vm;
415  hk(2,2) = 1.;
416  vnl_double_4x4 hK(0.);
417  hK.fill_diagonal(1./dwm);
418  hK(0,3) = -xm/dwm;
419  hK(1,3) = -ym/dwm;
420  hK(2,3) = -zm/dwm;
421  hK(3,3) = 1.;
422  //end of normalisation
423  //----------------------------------------
424  auto P = new PMatrix;
425  PMatrix P2;
426  auto pmat = new vnl_double_3x4;
427  PMatrixComputeLinear computor;
428  if (!computor.compute(points_image_n, points_world_n,P))
429  {
430  std::cerr << "Error" << std::endl;
431  return error(-4);
432  }
433  if (nonlinear)
434  {
435  if (!quiet)
436  {
437  P->get(pmat);
438  vnl_double_3x4 pmat2 = hk*(*pmat)*hK;
439  double no = sqrt(pmat2(2,0)*pmat2(2,0)+pmat2(2,1)*pmat2(2,1)+pmat2(2,2)*pmat2(2,2));
440  pmat2 /= no;
441  P2.set(pmat2);
442  double m = 0;
443  for (int i=0; i<npts; i++)
444  {
445  vgl_homg_point_2d<double> pt = P2.project(points_world[i]);
446  double d = vgl_distance(pt,points_image[i]);
447  m += d*d;
448  }
449  std::cerr << "Matrix P before optimisation:" << std::endl;
450  std::cerr << P2 << std::endl;
451  std::cerr << "RMS error before optimisation: " << sqrt(m/points_world.size()) << std::endl;
452  }
453  vnl_vector<double> x;
454  if (radial)
455  x.set_size(17);
456  else
457  x.set_size(12);
458  unsigned int vv = 0;
460  if (radial)
461  f = new a_camera2_function(points_image_n,points_world_n);
462  else if (internal_file != "")
463  f = new a_camera_function_internal(points_image_n,points_world_n);
464  else
465  f = new a_camera_function(points_image_n,points_world_n);
466  for (int i = 0; i< 3; i++)
467  {
468  for (int j = 0; j< 4; j++)
469  {
470  x[vv] = P->get(i,j);
471  vv++;
472  }
473  }
474  if (radial)
475  {
476  x[12] = 0; x[13] = 0;
477  x[14] = 0; x[15] = 0; x[16] = 0;
478  }
479  vnl_levenberg_marquardt levmarq(*f);
480  if ((!radial)&&((squarepixels)||(noskew)))
481  {
483  f2->set_constraints(squarepixels,noskew);
484  double w0 = 0.1;
485  f2->set_w(w0);
486  for (int j=0;j<5; j++)
487  {
488  levmarq.minimize(x);
489  if (!quiet)
490  std::cerr << w0 << " " << f2->get_constraint(x) << std::endl;
491  w0 *= 10.;
492  f2->set_w(w0);
493  }
494  } else if (internal_file != "")
495  {
497  f2->set_K(*K0);
498  f2->set_hk(hk);
499  f2->set_hK(hK);
500  double w0 = 0.1;
501  f2->set_w(w0);
502  for (int j=0;j<5; j++)
503  {
504  levmarq.minimize(x);
505  if (!quiet)
506  std::cerr << w0 << " " << f2->get_constraint(x) << std::endl;
507  w0 *= 10.;
508  f2->set_w(w0);
509  }
510  }
511  //levmarq.set_max_function_evals(10000);
512  levmarq.minimize(x);
513  if (!quiet)
514  {
515  std::cerr << "Levenberg Marquardt" << std::endl
516  << "min: " << levmarq.get_end_error() << " at " << x << std::endl
517  << "iterations: " << levmarq.get_num_iterations() << " "
518  << "evaluations: " << levmarq.get_num_evaluations() << std::endl;
519  levmarq.diagnose_outcome();
520  }
521  vnl_double_3x4 p0;
522  p0(0,0) = x[0]; p0(0,1) = x[1]; p0(0,2) = x[2]; p0(0,3) = x[3];
523  p0(1,0) = x[4]; p0(1,1) = x[5]; p0(1,2) = x[6]; p0(1,3) = x[7];
524  p0(2,0) = x[8]; p0(2,1) = x[9]; p0(2,2) = x[10]; p0(2,3) = x[11];
525  P->set(p0);
526  if (radial)
527  {
528  cu0 = x[12]*dim+um;
529  cv0 = x[13]*dim+vm;
530  k1 = x[14]/dim;
531  k2 = x[15]/dim/dim;
532  k3 = x[16]/dim/dim/dim;
533  k0 = 1.-k1*dim-k2*dim*dim-k3*dim*dim*dim;
534  std::cerr << k0 << std::endl;
535  std::cout << "-xc=" << cu0 << " -yc=" << cv0 << " -k1=" << k1 << " -k2=" << k2 << " -k3=" << k3 << std::endl;
536  }
537  }
538  //denormalisation
539  P->get(pmat);
540  vnl_double_3x4 pmat2 = hk*(*pmat)*hK;
541  double no = sqrt(pmat2(2,0)*pmat2(2,0)+pmat2(2,1)*pmat2(2,1)+pmat2(2,2)*pmat2(2,2));
542  pmat2 /= no;
543  P2.set(pmat2);
544  double m = 0;
545  vgl_homg_point_2d<double> center;
546  if (radial)
547  center.set(cu0,cv0);
548  for (int i=0; i<points_world.size(); i++)
549  {
550  double d;
551  vgl_homg_point_2d<double> pt = P2.project(points_world[i]);
552  if (radial)
553  {
554  double r= vgl_distance(center,pt);
555  double L = 1+k1*r+k2*r*r+k3*r*r*r;
556  double u = points_image[i].x();
557  double v = points_image[i].y();
558  double un = cu0+L*(u-cu0);
559  double vn = cv0+L*(v-cv0);
560  vgl_homg_point_2d<double> im2(un,vn,1.);
561  d = vgl_distance(pt,im2);
562  }
563  else
564  d = vgl_distance(pt,points_image[i]);
565  m += d*d;
566  }
567  if (!quiet)
568  std::cerr << "RMS error: " << sqrt(m/points_world.size()) << std::endl;
569  if (out_code=="i")
570  {
571  PMatrixDecompCR decomp(P2);
572  std::cout << "3 3" << std::endl;
573  std::cout << decomp.C << std::endl;;
574  } else if (out_code=="e")
575  {
576  PMatrixDecompCR decomp(P2);
577  std::cout << "3 4" << std::endl;
578  std::cout << decomp.Po << std::endl;
579 
580  } else
581  {
582  std::cout << "3 4\n";
583  std::cout << P2 << std::endl;
584  }
585  delete pmat;
586 }
587 
camera solver with radial distorsions
Definition: p_compute.cxx:228
void f(const vnl_vector< double > &x, vnl_vector< double > &fx)
Definition: p_compute.cxx:242
std::vector< vgl_homg_point_3d< double > > & points_world_
Definition: p_compute.cxx:239
std::vector< vgl_homg_point_2d< double > > & points_image_
Definition: p_compute.cxx:238
a_camera2_function(std::vector< vgl_homg_point_2d< double > > &points_image, std::vector< vgl_homg_point_3d< double > > &points_world)
Definition: p_compute.cxx:230
camera solver with internal calibration
Definition: p_compute.cxx:145
void set_w(const double w)
Definition: p_compute.cxx:155
a_camera_function_internal(std::vector< vgl_homg_point_2d< double > > &points_image, std::vector< vgl_homg_point_3d< double > > &points_world)
Definition: p_compute.cxx:147
std::vector< vgl_homg_point_3d< double > > & points_world_
Definition: p_compute.cxx:163
double get_constraint(const vnl_vector< double > &x)
Definition: p_compute.cxx:206
void set_K(const vnl_double_3x3 &K)
Definition: p_compute.cxx:157
void set_hk(const vnl_double_3x3 &K)
Definition: p_compute.cxx:158
std::vector< vgl_homg_point_2d< double > > & points_image_
Definition: p_compute.cxx:162
void set_hK(const vnl_double_4x4 &K)
Definition: p_compute.cxx:159
void f(const vnl_vector< double > &x, vnl_vector< double > &fx)
Definition: p_compute.cxx:170
camera solver
Definition: p_compute.cxx:68
std::vector< vgl_homg_point_2d< double > > & points_image_
Definition: p_compute.cxx:85
void f(const vnl_vector< double > &x, vnl_vector< double > &fx)
Definition: p_compute.cxx:92
double get_w() const
Definition: p_compute.cxx:81
double get_constraint(const vnl_vector< double > &x)
Definition: p_compute.cxx:127
void set_constraints(const bool sp, const bool sk)
Definition: p_compute.cxx:82
std::vector< vgl_homg_point_3d< double > > & points_world_
Definition: p_compute.cxx:86
void set_w(const double w)
Definition: p_compute.cxx:80
a_camera_function(std::vector< vgl_homg_point_2d< double > > &points_image, std::vector< vgl_homg_point_3d< double > > &points_world)
Definition: p_compute.cxx:70
double v(const uint32_t step, const uint32_t n)
Definition: generate.cxx:42
int error(int val)
Definition: p_compute.cxx:60
CSimpleOpt::SOption g_rgOptions[]
Definition: p_compute.cxx:34
int main(int argc, char **argv)
Definition: p_compute.cxx:279
bool verbose
Definition: p_compute.cxx:58
@ OPT_HOMOGENEOUS
Definition: p_compute.cxx:32
@ OPT_SQUARE
Definition: p_compute.cxx:32
@ OPT_INTERNAL
Definition: p_compute.cxx:32
@ OPT_RADIAL
Definition: p_compute.cxx:32
@ OPT_SKEW
Definition: p_compute.cxx:32
@ OPT_QUIET
Definition: p_compute.cxx:32
@ OPT_OUT
Definition: p_compute.cxx:32
@ OPT_HELP
Definition: p_compute.cxx:32
@ OPT_VERB
Definition: p_compute.cxx:32
@ OPT_NONLIN
Definition: p_compute.cxx:32
int xm
Definition: pixelpos.cxx:73
int ym
Definition: pixelpos.cxx:74
std::istringstream in
Definition: ply2tri.cxx:32
Definition: stlb2stla.cxx:21
a_pmat pmat
Definition: view_li.cxx:84