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

📄 cal_main.cpp

📁 有关摄像头定标的C++代码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
    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 (long *m_ptr, long *n_ptr, double *params, double *err){    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 ();    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() */     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 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, &gtol, &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 (long *m_ptr,long *n_ptr,double *params,double *err){    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() */     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.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, &gtol, &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 (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,              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() */    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;

⌨️ 快捷键说明

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