1 #ifndef _CAMCALIBRATION_H
2 #define _CAMCALIBRATION_H
5 #define M_PI 3.1415926535897932384626433832795
100 bool AddHomography(
int c, std::vector<CamCalibration::s_struct_points> p, CvMat* ready );
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 );
178 char* png_descriptor =
"",
int c_start = 0,
int h_start = 0 );
201 static void Mat3x4Mul( CvMat* m_A, CvMat* m_B, CvMat* m_C );
231 struct s_struct_plane {
235 std::vector<CvMat*> v_m_wp;
236 std::vector<CvMat*> v_m_pp;
248 for(
int i = 0; i < (int)v_m_wp.size(); i++ ) cvReleaseMat(&v_m_wp[i]);
250 for(
int i = 0; i < (int)v_m_pp.size(); i++ ) cvReleaseMat(&v_m_pp[i]);
267 struct s_struct_homography {
268 s_struct_plane* s_plane_object;
270 CvMat* m_estim_r_t_matrix;
271 CvMat* m_jacobian_matrix;
274 s_struct_homography(){
275 s_plane_object =
new s_struct_plane();
278 m_estim_r_t_matrix = cvCreateMat( 3, 4, CV_64FC1 );
279 m_jacobian_matrix = cvCreateMat( 3, 9, CV_64FC1 );
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 );
294 struct s_struct_intrinsic {
311 struct s_struct_camera {
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;
321 s_struct_camera(
int width,
int 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 );
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);
343 std::vector<s_struct_camera*> v_camera;
348 struct s_struct_solution {
350 s_struct_solution(
int c,
int h ) : c(c), h(h) {}
362 struct s_struct_optimal {
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;
400 s_struct_optimal s_optimal;
412 std::vector<double> v_opt_param;
431 enum matrix_desc { m_calibration,
445 int errorgraphic_counter;
450 int stat_HomographyNum;
455 time_t stat_ExpStartTime;
460 double GetRandomValue(
double min,
double max );
465 void AddGaussianNoise( CvMat* mat,
double noise );
475 void SetIntrinsicParam(
int c,
double f,
double a,
double u,
double v );
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 );
496 void CreateCalibrationMatrixFromIntrinsicParameters(
int c );
504 void CreateEstimatedCalibrationMatrixFromEstimatedIntrinsicParameters(
int c );
512 void CreateEstimatedInverseCalibrationMatrixFromEstimatedIntrinsicParameters(
int c );
521 void CreateRotationTranslationMatrixFromInverseCalibrationMatrixAndHomography(
int c,
int h );
529 void CreateAllRotationTranslationMatrices(
int c );
537 void CreateRotationTranslationIdentityMatrix(
int c );
545 void CreateRotationTranslationMatrix(
int c,
double rx,
double ry,
double rz,
546 double tx,
double ty,
double tz );
554 void CreatePlaneObjectWorldPoints(
int c,
int h );
562 void CreatePlaneObjectPlanePoints(
int c,
int h,
double noise );
570 void CreateWorldPlaneHomography(
int c,
int h, CvMat* ready );
579 bool HomographySingular(
int c,
int h,
double p_InitialGuess_a,
double p_InitialGuess_b,
double p_InitialGuess_c );
584 double VectorAngle( CvMat* v1, CvMat* v2, CvMat* v3, CvMat* v4 );
589 double PointDistance( CvMat* v1, CvMat* v2 );
594 void DeleteWorldPlaneHomography(
int c,
int h );
599 std::vector<s_struct_solution> GetRandomSolution(
int num );
607 double GetScreenDistance(
int c,
int h1,
int h2 );
615 double GetSolutionQuality( std::vector<s_struct_solution> solution,
double p_PreFilter_a,
double p_PreFilter_b,
double p_PreFilter_c );
624 bool FilterBestHomographiesGreedyMethod(
int num );
640 bool FilterBestHomographiesFillingMethod(
int num,
double p_PreFilter_a );
653 bool FilterBestHomographiesRandomMethod(
int num,
int p_Solutions,
double p_PreFilter_a,
double p_PreFilter_b,
double p_PreFilter_c );
662 bool ExtractIntrinsicParameters(
int c,
double p_InitialGuess_a,
double p_InitialGuess_b,
double p_InitialGuess_c );
669 double GetFrobeniusDistance(
int c,
int h1,
int h2 );
676 bool GetDistalHomographiesForCalibration(
int c,
int *h1,
int *h2 );
684 bool FilterHomographiesAfterOptimization(
double p_PostFilter );
695 bool OptimizeCalibrationByMinimalParameterMethod(
int iter,
double eps,
double p_PostFilter );
705 bool CreateMatrixCH();
710 void GetParameterSetFromOptimalStructure();
715 void SetParameterSetToOptimalStructure();
722 void CreateEstimated3ChainMatrixRT(
int h1,
int c1,
int h2,
int c2 );
730 void CreateEstimatedOptimalHomographyMatrixRT(
int h1,
int c1,
int h2 );
737 void CreateEstimatedOptimalCameraMatrixRT(
int c,
int h );
742 void CreateEstimatedOptimalCameraMatrixC(
int c );
752 bool CreateOptimalCameraStructure();
760 void ReleaseOptimalCameraStructure();
767 int GetParameterNumber();
772 int GetObservationNumber();
779 CvMat* GetM(
enum matrix_desc desc,
int c,
int h );
784 void Translate3DVector( CvMat* v,
double x,
double y,
double z );
791 void Rotate3DVector( CvMat* v,
double x,
double y,
double z );
798 static void Mat3x4ToMat4x4( CvMat* m_A, CvMat* m_B );
805 static void Mat4x4ToMat3x4( CvMat* m_A, CvMat* m_B );
812 static void updateCB(
double *params,
void **user_data);
831 void PrintOptimizedResultErrors(
double *params );
838 void PrintStatisticToFile();