⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cal_main.c

📁 标准tsai摄像机标定源代码。(两步标定)
💻 C
📖 第 1 页 / 共 5 页
字号:
	exit (-1);    }    for (i = 0; i < cd.point_count; i++) {	M.el[i][0] = Yd[i] * cd.xw[i];	M.el[i][1] = Yd[i] * cd.yw[i];	M.el[i][2] = Yd[i];	M.el[i][3] = -Xd[i] * cd.xw[i];	M.el[i][4] = -Xd[i] * cd.yw[i];	b.el[i][0] = Xd[i];    }    if (solve_system (M, a, b)) {	fprintf (stderr, "cc compute U: unable to solve system  Ma=b\n");	exit (-1);    }    U[0] = a.el[0][0];    U[1] = a.el[1][0];    U[2] = a.el[2][0];    U[3] = a.el[3][0];    U[4] = a.el[4][0];    freemat (M);    freemat (a);    freemat (b);}void      cc_compute_Tx_and_Ty (){    int       i,              far_point;    double    Tx,              Ty,              Ty_squared,              x,              y,              Sr,              r1p,              r2p,              r4p,              r5p,              r1,              r2,              r4,              r5,              distance,              far_distance;    r1p = U[0];    r2p = U[1];    r4p = U[3];    r5p = U[4];    /* first find the square of the magnitude of Ty */    if ((fabs (r1p) < EPSILON) && (fabs (r2p) < EPSILON))	Ty_squared = 1 / (SQR (r4p) + SQR (r5p));    else if ((fabs (r4p) < EPSILON) && (fabs (r5p) < EPSILON))	Ty_squared = 1 / (SQR (r1p) + SQR (r2p));    else if ((fabs (r1p) < EPSILON) && (fabs (r4p) < EPSILON))	Ty_squared = 1 / (SQR (r2p) + SQR (r5p));    else if ((fabs (r2p) < EPSILON) && (fabs (r5p) < EPSILON))	Ty_squared = 1 / (SQR (r1p) + SQR (r4p));    else {	Sr = SQR (r1p) + SQR (r2p) + SQR (r4p) + SQR (r5p);	Ty_squared = (Sr - sqrt (SQR (Sr) - 4 * SQR (r1p * r5p - r4p * r2p))) /	 (2 * SQR (r1p * r5p - r4p * r2p));    }    /* find a point that is far from the image center */    far_distance = 0;    far_point = 0;    for (i = 0; i < cd.point_count; i++)	if ((distance = r_squared[i]) > far_distance) {	    far_point = i;	    far_distance = distance;	}    /* now find the sign for Ty */    /* start by assuming Ty > 0 */    Ty = sqrt (Ty_squared);    r1 = U[0] * Ty;    r2 = U[1] * Ty;    Tx = U[2] * Ty;    r4 = U[3] * Ty;    r5 = U[4] * Ty;    x = r1 * cd.xw[far_point] + r2 * cd.yw[far_point] + Tx;    y = r4 * cd.xw[far_point] + r5 * cd.yw[far_point] + Ty;    /* flip Ty if we guessed wrong */    if ((SIGNBIT (x) != SIGNBIT (Xd[far_point])) ||	(SIGNBIT (y) != SIGNBIT (Yd[far_point])))	Ty = -Ty;    /* update the calibration constants */    cc.Tx = U[2] * Ty;    cc.Ty = Ty;}void      cc_compute_R (){    double    r1,              r2,              r3,              r4,              r5,              r6,              r7,              r8,              r9;    r1 = U[0] * cc.Ty;    r2 = U[1] * cc.Ty;    r3 = sqrt (1 - SQR (r1) - SQR (r2));    r4 = U[3] * cc.Ty;    r5 = U[4] * cc.Ty;    r6 = sqrt (1 - SQR (r4) - SQR (r5));    if (!SIGNBIT (r1 * r4 + r2 * r5))	r6 = -r6;    /* use the outer product of the first two rows to get the last row */    r7 = r2 * r6 - r3 * r5;    r8 = r3 * r4 - r1 * r6;    r9 = r1 * r5 - r2 * r4;    /* update the calibration constants */    cc.r1 = r1;    cc.r2 = r2;    cc.r3 = r3;    cc.r4 = r4;    cc.r5 = r5;    cc.r6 = r6;    cc.r7 = r7;    cc.r8 = r8;    cc.r9 = r9;    /* fill in cc.Rx, cc.Ry and cc.Rz */    solve_RPY_transform ();}void      cc_compute_approximate_f_and_Tz (){    int       i;    dmat      M,              a,              b;    M = newdmat (0, (cd.point_count - 1), 0, 1, &errno);    if (errno) {	fprintf (stderr, "cc compute apx: unable to allocate matrix M\n");	exit (-1);    }    a = newdmat (0, 1, 0, 0, &errno);    if (errno) {	fprintf (stderr, "cc compute apx: unable to allocate vector a\n");	exit (-1);    }    b = newdmat (0, (cd.point_count - 1), 0, 0, &errno);    if (errno) {	fprintf (stderr, "cc compute apx: unable to allocate vector b\n");	exit (-1);    }    for (i = 0; i < cd.point_count; i++) {	M.el[i][0] = cc.r4 * cd.xw[i] + cc.r5 * cd.yw[i] + cc.Ty;	M.el[i][1] = -Yd[i];	b.el[i][0] = (cc.r7 * cd.xw[i] + cc.r8 * cd.yw[i]) * Yd[i];    }    if (solve_system (M, a, b)) {	fprintf (stderr, "cc compute apx: unable to solve system  Ma=b\n");	exit (-1);    }    /* update the calibration constants */    cc.f = a.el[0][0];    cc.Tz = a.el[1][0];    cc.kappa1 = 0.0;		/* this is the assumption that our calculation was made under */    freemat (M);    freemat (a);    freemat (b);}/************************************************************************/void      cc_compute_exact_f_and_Tz_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,              kappa1,              xc,              yc,              zc,              Xu_1,              Yu_1,              Xu_2,              Yu_2,              distortion_factor;    f = params[0];    Tz = params[1];    kappa1 = params[2];    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 * (SQR (Xd[i]) + SQR (Yd[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_compute_exact_f_and_Tz (){#define NPARAMS 3    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;    /* 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_compute_exact_f_and_Tz_error,            &m, &n, x, fvec, &ftol, &xtol, &gtol, &maxfev, &epsfcn,            diag, &mode, &factor, &nprint, &info, &nfev, fjac, &ldfjac,            ipvt, qtf, wa1, wa2, wa3, wa4);    /* update the calibration constants */    cc.f = x[0];    cc.Tz = x[1];    cc.kappa1 = x[2];    /* 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_three_parm_optimization (){    int       i;    for (i = 0; i < cd.point_count; i++)	if (cd.zw[i]) {	    fprintf (stderr, "error - coplanar calibration tried with data outside of Z plane\n\n");	    exit (-1);	}    cc_compute_Xd_Yd_and_r_squared ();    cc_compute_U ();    cc_compute_Tx_and_Ty ();    cc_compute_R ();    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);	}    }    cc_compute_exact_f_and_Tz ();}/************************************************************************/void      cc_remove_sensor_plane_distortion_from_Xd_and_Yd (){    int       i;    double    Xu,              Yu;    for (i = 0; i < cd.point_count; i++) {	distorted_to_undistorted_sensor_coord (Xd[i], Yd[i], &Xu, &Yu);	Xd[i] = Xu;	Yd[i] = Yu;	r_squared[i] = SQR (Xu) + SQR (Yu);    }}/************************************************************************/void      cc_five_parm_optimization_with_late_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,              kappa1,              xc,              yc,              zc,              Xu_1,              Yu_1,              Xu_2,              Yu_2,              distortion_factor;    /* in this routine radial lens distortion is only taken into account */    /* after the rotation and translation constants have been determined */    f = params[0];    Tz = params[1];    kappa1 = params[2];    cp.Cx = params[3];    cp.Cy = params[4];    cc_compute_Xd_Yd_and_r_squared ();    cc_compute_U ();    cc_compute_Tx_and_Ty ();    cc_compute_R ();    cc_compute_approximate_f_and_Tz ();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -