bazar  1.3.1
CamCalibration.h
Go to the documentation of this file.
1 #ifndef _CAMCALIBRATION_H
2 #define _CAMCALIBRATION_H
3 
4 #ifndef M_PI
5 #define M_PI 3.1415926535897932384626433832795
6 #endif
7 
8 #include <vector>
9 #include <math.h>
10 #include <time.h>
11 #include <iostream>
12 #include <stdio.h>
13 #include <cv.h>
14 #include <geometry/homography.h>
15 
16 struct ProjObs;
17 
38 
39 public:
44 
52 
61  void AddCamera( int width, int height );
62 
71  bool AddHomography( int c );
72 
83  struct s_struct_points {
84  double u,v,x,y;
86  s_struct_points( double u, double v, double x, double y ) : u(u), v(v), x(x), y(y) {}
87  };
88 
100  bool AddHomography( int c, std::vector<CamCalibration::s_struct_points> p, CvMat* ready );
101 
110  bool StoreHomographiesToFile( char* file_name );
111 
119  bool LoadHomographiesFromFile( char* file_name );
120 
142  bool Calibrate( int p_HomographyNum, int p_PreFilter, int p_Solutions, double p_PreFilter_a, double p_PreFilter_b, double p_PreFilter_c,
143  double p_InitialGuess_a, double p_InitialGuess_b, double p_InitialGuess_c,
144  int p_Iterations, double p_Epsilon, double p_PostFilter );
145 
162 
177  void PrintOptimizedResultsToFile2( char* file_descriptor, bool create_png = false, char* sequence_descriptor = "",
178  char* png_descriptor = "", int c_start = 0, int h_start = 0 );
179 
181  // Functions below this line are used by CamCalibration and CamAugmentation only ! //
183 
189  static void ExtractRotationTranslationFrom3x4Matrix( CvMat* RotTrans , CvMat* Rot, CvMat*Trans );
190 
196  static void ComposeRotationTranslationTo3x4Matrix( CvMat* RotTrans , CvMat* Rot, CvMat*Trans );
197 
201  static void Mat3x4Mul( CvMat* m_A, CvMat* m_B, CvMat* m_C );
202 
206  static void Mat3x4Inverse( CvMat* m_A, CvMat* m_B );
207 
211  static void HomogenousNormalizeVector( CvMat* mat );
212 
214 
215  void ClearAll();
216 
217 private:
218 
231  struct s_struct_plane {
232  int p;
233  double *px;
234  double *py;
235  std::vector<CvMat*> v_m_wp;
236  std::vector<CvMat*> v_m_pp;
237  double rx,ry,rz;
238  double tx,ty,tz;
239 
240  s_struct_plane() {
241  px = NULL;
242  py = NULL;
243  }
244 
245  ~s_struct_plane() {
246  delete px;
247  delete py;
248  for( int i = 0; i < (int)v_m_wp.size(); i++ ) cvReleaseMat(&v_m_wp[i]);
249  v_m_wp.clear();
250  for( int i = 0; i < (int)v_m_pp.size(); i++ ) cvReleaseMat(&v_m_pp[i]);
251  v_m_pp.clear();
252  }
253  };
254 
267  struct s_struct_homography {
268  s_struct_plane* s_plane_object;
269  homography* m_homography;
270  CvMat* m_estim_r_t_matrix;
271  CvMat* m_jacobian_matrix;
272  bool b_discard;
273 
274  s_struct_homography(){
275  s_plane_object = new s_struct_plane();
276  b_discard = true; // specifies discarding candidates
277  m_homography = NULL; // NULL = not valid!
278  m_estim_r_t_matrix = cvCreateMat( 3, 4, CV_64FC1 );
279  m_jacobian_matrix = cvCreateMat( 3, 9, CV_64FC1 );
280  }
281  ~s_struct_homography(){
282  if( s_plane_object ) delete s_plane_object;
283  if( m_homography ) delete m_homography;
284  cvReleaseMat(&m_estim_r_t_matrix);
285  cvReleaseMat(&m_jacobian_matrix );
286  }
287  };
288 
294  struct s_struct_intrinsic {
295  double focal;
296  double aspect;
297  double u0,v0;
298  };
299 
311  struct s_struct_camera {
312  int w,h;
313  s_struct_intrinsic s_intrinsic;
314  s_struct_intrinsic s_estim_int;
315  std::vector<s_struct_homography*> v_homography;
316  CvMat* m_calibration_matrix;
317  CvMat* m_rot_trans_matrix;
318  CvMat* m_estim_calib_matrix;
319  CvMat* m_estinvcalib_matrix;
320 
321  s_struct_camera( int width, int height ){
322  w = width;
323  h = height;
324  m_calibration_matrix = cvCreateMat( 3, 3, CV_64FC1 );
325  m_rot_trans_matrix = cvCreateMat( 3, 4, CV_64FC1 );
326  m_estim_calib_matrix = cvCreateMat( 3, 3, CV_64FC1 );
327  m_estinvcalib_matrix = cvCreateMat( 3, 3, CV_64FC1 );
328  }
329 
330  ~s_struct_camera(){
331  for( int i = 0; i < (int)v_homography.size(); i++ ) delete v_homography[i];
332  v_homography.clear();
333  cvReleaseMat(&m_calibration_matrix);
334  cvReleaseMat(&m_rot_trans_matrix );
335  cvReleaseMat(&m_estim_calib_matrix);
336  cvReleaseMat(&m_estinvcalib_matrix);
337  }
338  };
339 
343  std::vector<s_struct_camera*> v_camera;
344 
348  struct s_struct_solution {
349  int c,h;
350  s_struct_solution( int c, int h ) : c(c), h(h) {}
351  };
352 
362  struct s_struct_optimal {
363  int c_max, h_max;
364  std::vector<CvMat*> v_camera_c;
365  std::vector<CvMat*> v_camera_r_t;
366  std::vector<CvMat*> v_camera_r_t_jacobian;
367  std::vector<CvMat*> v_homography_r_t;
368  std::vector<CvMat*> v_homography_r_t_jacobian;
370  /*deleteall(){
371  for( int i = 0; i < (int)v_camera_c.size(); i++ )
372  if( v_camera_c[i] ) cvReleaseMat( &v_camera_c[i] );
373  v_camera_c.clear();
374  for( int i = 0; i < (int)v_camera_r_t.size(); i++ )
375  if( v_camera_r_t[i] ) cvReleaseMat( &v_camera_r_t[i] );
376  v_camera_r_t.clear();
377  for( int i = 0; i < (int)v_camera_r_t_jacobian.size(); i++ )
378  if( v_camera_r_t_jacobian[i] ) cvReleaseMat( &v_camera_r_t_jacobian[i] );
379  v_camera_r_t_jacobian.clear();
380  for( int i = 0; i < (int)v_homography_r_t.size(); i++ )
381  if( v_homography_r_t[i] ) cvReleaseMat( &v_homography_r_t[i] );
382  v_homography_r_t.clear();
383  for( int i = 0; i < (int)v_homography_r_t_jacobian.size(); i++ )
384  if( v_homography_r_t_jacobian[i] ) cvReleaseMat( &v_homography_r_t_jacobian[i] );
385  v_homography_r_t_jacobian.clear();
386  }*/
387  s_struct_optimal(){
388  }
389  ~s_struct_optimal(){
390  //deleteall();
391  }
392  };
393 
400  s_struct_optimal s_optimal;
401 
412  std::vector<double> v_opt_param;
413 
424  CvMat *m_CH;
425 
431  enum matrix_desc { m_calibration,
432  m_rot_trans,
433  m_estim_calib,
434  m_estinvcalib,
435  m_estim_r_t,
436  m_jacobian };
437 
445  int errorgraphic_counter;
446 
450  int stat_HomographyNum;
451 
455  time_t stat_ExpStartTime;
456 
460  double GetRandomValue( double min, double max );
461 
465  void AddGaussianNoise( CvMat* mat, double noise );
466 
475  void SetIntrinsicParam( int c, double f, double a, double u, double v );
476 
486  void SetPlaneObject( int c, int h, int p, double px[], double py[],
487  double rx, double ry, double rz, double tx, double ty, double tz );
488 
496  void CreateCalibrationMatrixFromIntrinsicParameters( int c );
497 
504  void CreateEstimatedCalibrationMatrixFromEstimatedIntrinsicParameters( int c );
505 
512  void CreateEstimatedInverseCalibrationMatrixFromEstimatedIntrinsicParameters( int c );
513 
521  void CreateRotationTranslationMatrixFromInverseCalibrationMatrixAndHomography( int c, int h );
522 
529  void CreateAllRotationTranslationMatrices( int c );
530 
537  void CreateRotationTranslationIdentityMatrix( int c );
538 
545  void CreateRotationTranslationMatrix( int c, double rx, double ry, double rz,
546  double tx, double ty, double tz );
547 
554  void CreatePlaneObjectWorldPoints( int c, int h );
555 
562  void CreatePlaneObjectPlanePoints( int c, int h, double noise );
563 
570  void CreateWorldPlaneHomography( int c, int h, CvMat* ready );
571 
579  bool HomographySingular( int c, int h, double p_InitialGuess_a, double p_InitialGuess_b, double p_InitialGuess_c );
580 
584  double VectorAngle( CvMat* v1, CvMat* v2, CvMat* v3, CvMat* v4 );
585 
589  double PointDistance( CvMat* v1, CvMat* v2 );
590 
594  void DeleteWorldPlaneHomography( int c, int h );
595 
599  std::vector<s_struct_solution> GetRandomSolution( int num );
600 
607  double GetScreenDistance( int c, int h1, int h2 );
608 
615  double GetSolutionQuality( std::vector<s_struct_solution> solution, double p_PreFilter_a, double p_PreFilter_b, double p_PreFilter_c );
616 
624  bool FilterBestHomographiesGreedyMethod( int num );
625 
640  bool FilterBestHomographiesFillingMethod( int num, double p_PreFilter_a );
641 
653  bool FilterBestHomographiesRandomMethod( int num, int p_Solutions, double p_PreFilter_a, double p_PreFilter_b, double p_PreFilter_c );
654 
662  bool ExtractIntrinsicParameters( int c, double p_InitialGuess_a, double p_InitialGuess_b, double p_InitialGuess_c );
663 
669  double GetFrobeniusDistance( int c, int h1, int h2 );
670 
676  bool GetDistalHomographiesForCalibration( int c, int *h1, int *h2 );
677 
684  bool FilterHomographiesAfterOptimization( double p_PostFilter );
685 
695  bool OptimizeCalibrationByMinimalParameterMethod( int iter, double eps, double p_PostFilter );
696 
705  bool CreateMatrixCH();
706 
710  void GetParameterSetFromOptimalStructure();
711 
715  void SetParameterSetToOptimalStructure();
716 
722  void CreateEstimated3ChainMatrixRT( int h1, int c1, int h2, int c2 );
723 
730  void CreateEstimatedOptimalHomographyMatrixRT( int h1, int c1, int h2 );
731 
737  void CreateEstimatedOptimalCameraMatrixRT( int c, int h );
738 
742  void CreateEstimatedOptimalCameraMatrixC( int c );
743 
752  bool CreateOptimalCameraStructure();
753 
760  void ReleaseOptimalCameraStructure();
761 
767  int GetParameterNumber();
768 
772  int GetObservationNumber();
773 
779  CvMat* GetM( enum matrix_desc desc, int c, int h );
780 
784  void Translate3DVector( CvMat* v, double x, double y, double z );
785 
791  void Rotate3DVector( CvMat* v, double x, double y, double z );
792 
798  static void Mat3x4ToMat4x4( CvMat* m_A, CvMat* m_B );
799 
805  static void Mat4x4ToMat3x4( CvMat* m_A, CvMat* m_B );
806 
812  static void updateCB(double *params, void **user_data);
813 
826  //static void projFunc( double *x, double *params, int na, double *f, double *grad, int *ind, LsqData *Data );
827 
831  void PrintOptimizedResultErrors( double *params );
832 
838  void PrintStatisticToFile();
839 
840  friend struct ProjObs;
841 };
842 
843 #endif