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

📄 cal_main.cpp

📁 有关摄像头定标的C++代码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
    x[1] = cc.Ry;    x[2] = cc.Rz;    x[3] = cc.Tx;    x[4] = cc.Ty;    x[5] = cc.Tz;    x[6] = cc.kappa1;    x[7] = cc.f;    /* 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_nic_optimization_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 and camera constants */    cc.Rx = x[0];    cc.Ry = x[1];    cc.Rz = x[2];    apply_RPY_transform ();    cc.Tx = x[3];    cc.Ty = x[4];    cc.Tz = x[5];    cc.kappa1 = x[6];    cc.f = x[7];    /* 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_full_optimization_error (long *m_ptr, long *n_ptr, double *params, double *err){    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,              Cx,              Cy,              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];    Cx = params[8];    Cy = params[9];    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] - Cx) / cp.sx; 	Yd_ = cp.dpy * (cd.Yf[i] - 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_full_optimization (){#define NPARAMS 10    int       i;    /* Parameters needed by MINPACK's lmdif() */    long     m = cd.point_count;    long     n = NPARAMS;    double  x[NPARAMS];    double *fvec;    double  ftol = REL_SENSOR_TOLERANCE_ftol;    double  xtol = REL_PARAM_TOLERANCE_xtol;    double  gtol = ORTHO_TOLERANCE_gtol;    long     maxfev = MAXFEV;    double  epsfcn = EPSFCN;    double  diag[NPARAMS];    long     mode = MODE;    double  factor = FACTOR;    long     nprint = 0;    long     info;    long     nfev;    double *fjac;    long     ldfjac = m;    long     ipvt[NPARAMS];    double  qtf[NPARAMS];    double  wa1[NPARAMS];    double  wa2[NPARAMS];    double  wa3[NPARAMS];    double *wa4;    /* allocate some workspace */    if (( fvec = (double *) calloc ((unsigned int) m, (unsigned int) sizeof(double))) == NULL ) {       fprintf(stderr,"calloc: Cannot allocate workspace fvec\n");       exit(-1);    }    if (( fjac = (double *) calloc ((unsigned int) m*n, (unsigned int) sizeof(double))) == NULL ) {       fprintf(stderr,"calloc: Cannot allocate workspace fjac\n");       exit(-1);    }    if (( wa4 = (double *) calloc ((unsigned int) m, (unsigned int) sizeof(double))) == 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.Rx;    x[1] = cc.Ry;    x[2] = cc.Rz;    x[3] = cc.Tx;    x[4] = cc.Ty;    x[5] = cc.Tz;    x[6] = cc.kappa1;    x[7] = cc.f;    x[8] = cp.Cx;    x[9] = 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_full_optimization_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 and camera constants */    cc.Rx = x[0];    cc.Ry = x[1];    cc.Rz = x[2];    apply_RPY_transform ();    cc.Tx = x[3];    cc.Ty = x[4];    cc.Tz = x[5];    cc.kappa1 = x[6];    cc.f = x[7];    cp.Cx = x[8];    cp.Cy = x[9];    /* 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}//以上为共面标定;以下为非共面标定;/***********************************************************************\* Routines for noncoplanar camera calibration	 			*\***********************************************************************/void      ncc_compute_Xd_Yd_and_r_squared (){    int       i;    double    Xd_,              Yd_;    for (i = 0; i < cd.point_count; i++) {	Xd[i] = Xd_ = cp.dpx * (cd.Xf[i] - cp.Cx) / cp.sx;      /* [mm] */	Yd[i] = Yd_ = cp.dpy * (cd.Yf[i] - cp.Cy);              /* [mm] */	r_squared[i] = SQR (Xd_) + SQR (Yd_);                   /* [mm^2] */    }}void      ncc_compute_U (){    int       i;    dmat      M,              a,              b;    M = newdmat (0, (cd.point_count - 1), 0, 6, &errno);    if (errno) {	fprintf (stderr, "ncc compute U: unable to allocate matrix M\n");	exit (-1);    }    a = newdmat (0, 6, 0, 0, &errno);    if (errno) {	fprintf (stderr, "ncc compute U: unable to allocate vector a\n");	exit (-1);    }    b = newdmat (0, (cd.point_count - 1), 0, 0, &errno);    if (errno) {	fprintf (stderr, "ncc compute U: unable to allocate vector b\n");	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] * cd.zw[i];	M.el[i][3] = Yd[i];	M.el[i][4] = -Xd[i] * cd.xw[i];	M.el[i][5] = -Xd[i] * cd.yw[i];	M.el[i][6] = -Xd[i] * cd.zw[i];	b.el[i][0] = Xd[i];    }    if (solve_system (M, a, b)) {	fprintf (stderr, "ncc compute U: error - non-coplanar calibration tried with data which may possibly be coplanar\n\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];    U[5] = a.el[5][0];    U[6] = a.el[6][0];    freemat (M);    freemat (a);    freemat (b);}void      ncc_compute_Tx_and_Ty (){    int       i,              far_point;    double    Tx,              Ty,              Ty_squared,              x,              y,              r1,              r2,              r3,              r4,              r5,              r6,              distance,              far_distance;    /* first find the square of the magnitude of Ty */    Ty_squared = 1 / (SQR (U[4]) + SQR (U[5]) + SQR (U[6]));    /* 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;    r3 = U[2] * Ty;    Tx = U[3] * Ty;    r4 = U[4] * Ty;    r5 = U[5] * Ty;    r6 = U[6] * Ty;    x = r1 * cd.xw[far_point] + r2 * cd.yw[far_point] + r3 * cd.zw[far_point] + Tx;    y = r4 * cd.xw[far_point] + r5 * cd.yw[far_point] + r6 * cd.zw[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[3] * Ty;    cc.Ty = Ty;}void      ncc_compute_sx (){    cp.sx = sqrt (SQR (U[0]) + SQR (U[1]) + SQR (U[2])) * fabs (cc.Ty);}void      ncc_compute_R (){    double    r1,              r2,              r3,              r4,              r5,              r6,              r7,              r8,              r9;    r1 = U[0] * cc.Ty / cp.sx;    r2 = U[1] * cc.Ty / cp.sx;    r3 = U[2] * cc.Ty / cp.sx;    r4 = U[4] * cc.Ty;    r5 = U[5] * cc.Ty;    r6 = U[6] * cc.Ty;    /* 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      ncc_compute_better_R (){    double    r1,              r2,              r3,              r4,              r5,              r6,              r7,              sa,              ca,              sb,              cb,              sg,              cg;    r1 = U[0] * cc.Ty / cp.sx;    r2 = U[1] * cc.Ty / cp.sx;    r3 = U[2] * cc.Ty / cp.sx;    r4 = U[4] * cc.Ty;    r5 = U[5] * cc.Ty;    r6 = U[6] * cc.Ty;    /* use the outer product of the first two rows to get the last row */    r7 = r2 * r6 - r3 * r5;    /* now find the RPY angles corresponding to the estimated rotation matrix */    cc.Rz = atan2 (r4, r1);    SINCOS (cc.Rz, sg, cg);    cc.Ry = atan2 (-r7, r1 * cg + r4 * sg);    cc.Rx = atan2 (r3 * sg - r6 * cg, r5 * cg - r2 * sg);    SINCOS (cc.Rx, sa, ca);    SINCOS (cc.Ry, sb, cb);    /* now generate a more orthonormal rotation matrix from the RPY angles */    cc.r1 = cb * cg;    cc.r2 = cg * sa * sb - ca * sg;    cc.r3 = sa * sg + ca * cg * sb;    cc.r4 = cb * sg;    cc.r5 = sa * sb * sg + ca * cg;    cc.r6 = ca * sb * sg - cg * sa;    cc.r7 = -sb;    cc.r8 = cb * sa;    cc.r9 = ca * cb;}void      ncc_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, "ncc compute apx: unable to allocate matrix M\n");	exit (-1);    }    a = newdmat (0, 1, 0, 0, &errno);    if (errno) {	fprintf (stderr, "ncc compute apx: unable to allocate vector a\n");	exit (-1);    }    b = newdmat (0, (cd.point_count - 1), 0, 0, &errno);    if (errno) {	fprintf (stderr, "ncc 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.r6 * cd.zw[i] + cc.Ty;	M.el[i][1] = -Yd[i];	b.el[i][0] = (cc.r7 * cd.xw[i] + cc.r8 * cd.yw[i] + cc.r9 * cd.zw[i]) * Yd[i];    }    if (solve_system (M, a, b)) {	fprintf (stderr, "ncc 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      ncc_compute_exact_f_and_Tz_error (long *m_ptr, long *n_ptr, double *params, double *err){    int       i;    double    xc,              yc,              zc,              Xu_1,              Yu_1,              Xu_2,              Yu_2,              distortion_factor,              f,              Tz,              kappa1;    f = params[0];    Tz = params[1];    kappa1 = params[2];    for (i = 0; i < cd.point_count; i++) {	/* convert from world coordinates to camera coordinates */	xc = cc.r1 * cd.xw[i] + cc.r2 * cd.yw[i] + cc.r3 * cd.zw[i] + cc.Tx;	yc = cc.r4 * cd.xw[i] + cc.r5 * cd.yw[i] + cc.r6 * cd.zw[i] + cc.Ty;	zc = cc.r7 * cd.xw[i] + cc.r8 * cd.yw[i] + cc.r9 * cd.zw[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);    }}

⌨️ 快捷键说明

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