📄 cal_main.c
字号:
if (cc.f < 0) { /* try the other solution for the orthonormal matrix */ cc.r3 = -cc.r3; cc.r6 = -cc.r6; cc.r7 = -cc.r7; cc.r8 = -cc.r8; solve_RPY_transform (); cc_compute_approximate_f_and_Tz (); if (cc.f < 0) { fprintf (stderr, "error - possible handedness problem with data\n"); exit (-1); } } for (i = 0; i < cd.point_count; i++) { /* convert from world coordinates to camera coordinates */ /* Note: zw is implicitly assumed to be zero for these (coplanar) calculations */ xc = cc.r1 * cd.xw[i] + cc.r2 * cd.yw[i] + cc.Tx; yc = cc.r4 * cd.xw[i] + cc.r5 * cd.yw[i] + cc.Ty; zc = cc.r7 * cd.xw[i] + cc.r8 * cd.yw[i] + Tz; /* convert from camera coordinates to undistorted sensor coordinates */ Xu_1 = f * xc / zc; Yu_1 = f * yc / zc; /* convert from distorted sensor coordinates to undistorted sensor coordinates */ distortion_factor = 1 + kappa1 * r_squared[i]; Xu_2 = Xd[i] * distortion_factor; Yu_2 = Yd[i] * distortion_factor; /* record the error in the undistorted sensor coordinates */ err[i] = hypot (Xu_1 - Xu_2, Yu_1 - Yu_2); }}void cc_five_parm_optimization_with_late_distortion_removal (){#define NPARAMS 5 int i; /* Parameters needed by MINPACK's lmdif() */ integer m = cd.point_count; integer n = NPARAMS; doublereal x[NPARAMS]; doublereal *fvec; doublereal ftol = REL_SENSOR_TOLERANCE_ftol; doublereal xtol = REL_PARAM_TOLERANCE_xtol; doublereal gtol = ORTHO_TOLERANCE_gtol; integer maxfev = MAXFEV; doublereal epsfcn = EPSFCN; doublereal diag[NPARAMS]; integer mode = MODE; doublereal factor = FACTOR; integer nprint = 0; integer info; integer nfev; doublereal *fjac; integer ldfjac = m; integer ipvt[NPARAMS]; doublereal qtf[NPARAMS]; doublereal wa1[NPARAMS]; doublereal wa2[NPARAMS]; doublereal wa3[NPARAMS]; doublereal *wa4; /* allocate some workspace */ if (( fvec = (doublereal *) calloc ((unsigned int) m, (unsigned int) sizeof(doublereal))) == NULL ) { fprintf(stderr,"calloc: Cannot allocate workspace fvec\n"); exit(-1); } if (( fjac = (doublereal *) calloc ((unsigned int) m*n, (unsigned int) sizeof(doublereal))) == NULL ) { fprintf(stderr,"calloc: Cannot allocate workspace fjac\n"); exit(-1); } if (( wa4 = (doublereal *) calloc ((unsigned int) m, (unsigned int) sizeof(doublereal))) == NULL ) { fprintf(stderr,"calloc: Cannot allocate workspace wa4\n"); exit(-1); } /* use the current calibration constants as an initial guess */ x[0] = cc.f; x[1] = cc.Tz; x[2] = cc.kappa1; x[3] = cp.Cx; x[4] = cp.Cy; /* define optional scale factors for the parameters */ if ( mode == 2 ) { for (i = 0; i < NPARAMS; i++) diag[i] = 1.0; /* some user-defined values */ } /* perform the optimization */ lmdif_ (cc_five_parm_optimization_with_late_distortion_removal_error, &m, &n, x, fvec, &ftol, &xtol, >ol, &maxfev, &epsfcn, diag, &mode, &factor, &nprint, &info, &nfev, fjac, &ldfjac, ipvt, qtf, wa1, wa2, wa3, wa4); /* update the calibration and camera constants */ cc.f = x[0]; cc.Tz = x[1]; cc.kappa1 = x[2]; cp.Cx = x[3]; cp.Cy = x[4]; /* release allocated workspace */ free (fvec); free (fjac); free (wa4);#ifdef DEBUG /* print the number of function calls during iteration */ fprintf(stderr,"info: %d nfev: %d\n\n",info,nfev);#endif #undef NPARAMS}/************************************************************************/void cc_five_parm_optimization_with_early_distortion_removal_error (m_ptr, n_ptr, params, err) integer *m_ptr; /* pointer to number of points to fit */ integer *n_ptr; /* pointer to number of parameters */ doublereal *params; /* vector of parameters */ doublereal *err; /* vector of error from data */{ int i; double f, Tz, xc, yc, zc, Xu_1, Yu_1, Xu_2, Yu_2; /* in this routine radial lens distortion is taken into account */ /* before the rotation and translation constants are determined */ /* (this assumes we have the distortion reasonably modelled) */ f = params[0]; Tz = params[1]; cc.kappa1 = params[2]; cp.Cx = params[3]; cp.Cy = params[4]; cc_compute_Xd_Yd_and_r_squared (); /* remove the sensor distortion before computing the translation and rotation stuff */ cc_remove_sensor_plane_distortion_from_Xd_and_Yd (); cc_compute_U (); cc_compute_Tx_and_Ty (); cc_compute_R (); /* we need to do this just to see if we have to flip the rotation matrix */ cc_compute_approximate_f_and_Tz (); if (cc.f < 0) { /* try the other solution for the orthonormal matrix */ cc.r3 = -cc.r3; cc.r6 = -cc.r6; cc.r7 = -cc.r7; cc.r8 = -cc.r8; solve_RPY_transform (); cc_compute_approximate_f_and_Tz (); if (cc.f < 0) { fprintf (stderr, "error - possible handedness problem with data\n"); exit (-1); } } /* now calculate the squared error assuming zero distortion */ for (i = 0; i < cd.point_count; i++) { /* convert from world coordinates to camera coordinates */ /* Note: zw is implicitly assumed to be zero for these (coplanar) calculations */ xc = cc.r1 * cd.xw[i] + cc.r2 * cd.yw[i] + cc.Tx; yc = cc.r4 * cd.xw[i] + cc.r5 * cd.yw[i] + cc.Ty; zc = cc.r7 * cd.xw[i] + cc.r8 * cd.yw[i] + Tz; /* convert from camera coordinates to undistorted sensor coordinates */ Xu_1 = f * xc / zc; Yu_1 = f * yc / zc; /* convert from distorted sensor coordinates to undistorted sensor coordinates */ /* (already done, actually) */ Xu_2 = Xd[i]; Yu_2 = Yd[i]; /* record the error in the undistorted sensor coordinates */ err[i] = hypot (Xu_1 - Xu_2, Yu_1 - Yu_2); }}void cc_five_parm_optimization_with_early_distortion_removal (){#define NPARAMS 5 int i; /* Parameters needed by MINPACK's lmdif() */ integer m = cd.point_count; integer n = NPARAMS; doublereal x[NPARAMS]; doublereal *fvec; doublereal ftol = REL_SENSOR_TOLERANCE_ftol; doublereal xtol = REL_PARAM_TOLERANCE_xtol; doublereal gtol = ORTHO_TOLERANCE_gtol; integer maxfev = MAXFEV; doublereal epsfcn = EPSFCN; doublereal diag[NPARAMS]; integer mode = MODE; doublereal factor = FACTOR; integer nprint = 0; integer info; integer nfev; doublereal *fjac; integer ldfjac = m; integer ipvt[NPARAMS]; doublereal qtf[NPARAMS]; doublereal wa1[NPARAMS]; doublereal wa2[NPARAMS]; doublereal wa3[NPARAMS]; doublereal *wa4; /* allocate some workspace */ if (( fvec = (doublereal *) calloc ((unsigned int) m, (unsigned int) sizeof(doublereal))) == NULL ) { fprintf(stderr,"calloc: Cannot allocate workspace fvec\n"); exit(-1); } if (( fjac = (doublereal *) calloc ((unsigned int) m*n, (unsigned int) sizeof(doublereal))) == NULL ) { fprintf(stderr,"calloc: Cannot allocate workspace fjac\n"); exit(-1); } if (( wa4 = (doublereal *) calloc ((unsigned int) m, (unsigned int) sizeof(doublereal))) == NULL ) { fprintf(stderr,"calloc: Cannot allocate workspace wa4\n"); exit(-1); } /* use the current calibration and camera constants as a starting point */ x[0] = cc.f; x[1] = cc.Tz; x[2] = cc.kappa1; x[3] = cp.Cx; x[4] = cp.Cy; /* define optional scale factors for the parameters */ if ( mode == 2 ) { for (i = 0; i < NPARAMS; i++) diag[i] = 1.0; /* some user-defined values */ } /* perform the optimization */ lmdif_ (cc_five_parm_optimization_with_early_distortion_removal_error, &m, &n, x, fvec, &ftol, &xtol, >ol, &maxfev, &epsfcn, diag, &mode, &factor, &nprint, &info, &nfev, fjac, &ldfjac, ipvt, qtf, wa1, wa2, wa3, wa4); /* update the calibration and camera constants */ cc.f = x[0]; cc.Tz = x[1]; cc.kappa1 = x[2]; cp.Cx = x[3]; cp.Cy = x[4]; /* release allocated workspace */ free (fvec); free (fjac); free (wa4);#ifdef DEBUG /* print the number of function calls during iteration */ fprintf(stderr,"info: %d nfev: %d\n\n",info,nfev);#endif#undef NPARAMS}/************************************************************************/void cc_nic_optimization_error (m_ptr, n_ptr, params, err) integer *m_ptr; /* pointer to number of points to fit */ integer *n_ptr; /* pointer to number of parameters */ doublereal *params; /* vector of parameters */ doublereal *err; /* vector of error from data */{ int i; double xc, yc, zc, Xd_, Yd_, Xu_1, Yu_1, Xu_2, Yu_2, distortion_factor, Rx, Ry, Rz, Tx, Ty, Tz, kappa1, f, r1, r2, r4, r5, r7, r8, sa, sb, sg, ca, cb, cg; Rx = params[0]; Ry = params[1]; Rz = params[2]; Tx = params[3]; Ty = params[4]; Tz = params[5]; kappa1 = params[6]; f = params[7]; SINCOS (Rx, sa, ca); SINCOS (Ry, sb, cb); SINCOS (Rz, sg, cg); r1 = cb * cg; r2 = cg * sa * sb - ca * sg; r4 = cb * sg; r5 = sa * sb * sg + ca * cg; r7 = -sb; r8 = cb * sa; for (i = 0; i < cd.point_count; i++) { /* convert from world coordinates to camera coordinates */ /* Note: zw is implicitly assumed to be zero for these (coplanar) calculations */ xc = r1 * cd.xw[i] + r2 * cd.yw[i] + Tx; yc = r4 * cd.xw[i] + r5 * cd.yw[i] + Ty; zc = r7 * cd.xw[i] + r8 * cd.yw[i] + Tz; /* convert from camera coordinates to undistorted sensor plane coordinates */ Xu_1 = f * xc / zc; Yu_1 = f * yc / zc; /* convert from 2D image coordinates to distorted sensor coordinates */ Xd_ = cp.dpx * (cd.Xf[i] - cp.Cx) / cp.sx; Yd_ = cp.dpy * (cd.Yf[i] - cp.Cy); /* convert from distorted sensor coordinates to undistorted sensor plane coordinates */ distortion_factor = 1 + kappa1 * (SQR (Xd_) + SQR (Yd_)); Xu_2 = Xd_ * distortion_factor; Yu_2 = Yd_ * distortion_factor; /* record the error in the undistorted sensor coordinates */ err[i] = hypot (Xu_1 - Xu_2, Yu_1 - Yu_2); }}void cc_nic_optimization (){#define NPARAMS 8 int i; /* Parameters needed by MINPACK's lmdif() */ integer m = cd.point_count; integer n = NPARAMS; doublereal x[NPARAMS]; doublereal *fvec; doublereal ftol = REL_SENSOR_TOLERANCE_ftol; doublereal xtol = REL_PARAM_TOLERANCE_xtol; doublereal gtol = ORTHO_TOLERANCE_gtol; integer maxfev = MAXFEV; doublereal epsfcn = EPSFCN; doublereal diag[NPARAMS]; integer mode = MODE; doublereal factor = FACTOR; integer nprint = 0; integer info; integer nfev; doublereal *fjac; integer ldfjac = m; integer ipvt[NPARAMS]; doublereal qtf[NPARAMS]; doublereal wa1[NPARAMS]; doublereal wa2[NPARAMS]; doublereal wa3[NPARAMS]; doublereal *wa4; /* allocate some workspace */ if (( fvec = (doublereal *) calloc ((unsigned int) m, (unsigned int) sizeof(doublereal))) == NULL ) { fprintf(stderr,"calloc: Cannot allocate workspace fvec\n"); exit(-1); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -