52 image_width = copied.image_width;
53 image_height = copied.image_height;
55 original_cx = copied.original_cx;
56 original_cy = copied.original_cy;
57 original_fx = copied.original_fx;
58 original_fy = copied.original_fy;
60 original_image_width = copied.original_image_width;
61 original_image_height = copied.original_image_height;
66 have_to_recompute_invAR = have_to_recompute_optical_centre = have_to_recompute_P =
true;
71 void projection_matrix::getAAt(
const double P[3][4],
double AAt[3][3])
73 double t7 = P[0][0]*P[0][0];
74 double t8 = P[0][1]*P[0][1];
75 double t9 = P[0][2]*P[0][2];
76 double t14 = P[0][0]*P[1][0]+P[0][1]*P[1][1]+P[0][2]*P[1][2];
77 double t18 = P[0][0]*P[2][0]+P[0][1]*P[2][1]+P[0][2]*P[2][2];
78 double t19 = P[1][0]*P[1][0];
79 double t20 = P[1][1]*P[1][1];
80 double t21 = P[1][2]*P[1][2];
81 double t26 = P[1][0]*P[2][0]+P[1][1]*P[2][1]+P[1][2]*P[2][2];
82 double t27 = P[2][0]*P[2][0];
83 double t28 = P[2][1]*P[2][1];
84 double t29 = P[2][2]*P[2][2];
89 AAt[1][1] = t19+t20+t21;
93 AAt[2][2] = t27+t28+t29;
101 double inv_l = sqrt(P[2][0] * P[2][0] + P[2][1] * P[2][1] + P[2][2] * P[2][2]);
102 for(
int i = 0; i < 3; i++)
103 for(
int j = 0; j < 4; j++)
104 P[i][j] = inv_l * P[i][j];
115 fy = sqrt(AAt[1][1] - cy * cy);
116 double s = (AAt[1][0] - cx * cy) / fy;
117 fx = sqrt(AAt[0][0] - cx * cx - s * s);
119 R[1][0] = (P[1][0] - cy * R[2][0]) / fy;
120 R[0][0] = (P[0][0] - s * R[1][0] - cx * R[2][0]) / fx;
122 R[1][1] = (P[1][1] - cy * R[2][1]) / fy;
123 R[0][1] = (P[0][1] - s * R[1][1] - cx * R[2][1]) / fx;
125 R[1][2] = (P[1][2] - cy * R[2][2]) / fy;
126 R[0][2] = (P[0][2] - s * R[1][2] - cx * R[2][2]) / fx;
128 T[1] = (P[1][3] - cy * T[2]) / fy;
129 T[0] = (P[0][3] - s * T[1] - cx * T[2]) / fx;
133 for(
int i = 0; i < 3; i++)
138 have_to_recompute_invAR = have_to_recompute_optical_centre = have_to_recompute_P =
true;
149 file.open(tdir_filename);
151 if (!file.good())
return false;
154 for (
int j=0; j<3; ++j)
155 for (
int i=0; i<4; ++i)
158 set_3x4_matrix(P, w, h);
163 static double diag(
double x,
double y)
165 return sqrt(x*x+y*y);
168 static double diagDiff(
const int W,
const int H,
double dx,
double dy)
170 double mdiag =
diag(
double(W),
double(H));
171 return fabs(
double(mdiag -
diag(2 * dx, 2 * dy)));
183 if (!load_tdir(tdir_filename, 0, 0))
return false;
185 static const int modes[] = {
198 double bestDelta =
diagDiff(modes[0], modes[1], cx, cy);
199 for(
int i = 1; modes[2 * i] != -1; i++) {
200 double delta =
diagDiff(modes[2 * i], modes[2 * i + 1], cx, cy);
201 if (delta < bestDelta) {
207 image_width = modes[2 * best];
208 image_height = modes[best + 1];
215 char calibration_name[500];
218 fscanf(f,
"%s", calibration_name);
220 fscanf(f,
"%d", &dummy);
223 fscanf(f,
"%d", &dummy);
225 read_internal_parameters_from_tdir_file(calibration_name);
227 fscanf(f,
"%lf %lf %lf", &(optical_centre[0]), &(optical_centre[1]), &(optical_centre[2]));
228 fscanf(f,
"%lf %lf %lf", &(R[0][0]), &(R[0][1]), &(R[0][2]));
229 fscanf(f,
"%lf %lf %lf", &(R[1][0]), &(R[1][1]), &(R[1][2]));
230 fscanf(f,
"%lf %lf %lf", &(R[2][0]), &(R[2][1]), &(R[2][2]));
231 fscanf(f,
"%lf %lf %lf", &(T[0]), &(T[1]), &(T[2]));
233 have_to_recompute_invAR = have_to_recompute_optical_centre = have_to_recompute_P =
true;
236 void projection_matrix::read_one_line(FILE * f,
char * line)
253 FILE * f = fopen(filename,
"r");
260 read_one_line(f, line);
261 }
while (strcmp(line,
"Camera") != 0);
263 read_one_line(f, line);
276 double focale_length, pixel_ratio, principal_point_u, principal_point_v, K;
277 double Oc[3], local_R[3][3], local_t[3];
279 if (fscanf(f,
"\t%d", &camera_index) != 1)
281 cerr <<
"Error while reading matchmover output (1)." << endl;
285 if (fscanf(f,
"\t F ( %lf ) Pr ( %lf ) Pp ( %lf %lf ) K ( %lf )",
286 &focale_length, &pixel_ratio, &principal_point_u, &principal_point_v, &K) != 5)
288 cerr <<
"Error while reading matchmover output (2)." << endl;
292 if (fscanf(f,
"\tOc ( %lf %lf %lf )", &(Oc[0]), &(Oc[1]), &(Oc[2])) != 3)
294 cerr <<
"Error while reading matchmover output (3)." << endl;
298 if (fscanf(f,
"\tRot ( %lf %lf %lf", &(local_R[0][0]), &(local_R[0][1]), &(local_R[0][2])) != 3)
300 cerr <<
"Error while reading matchmover output (4)." << endl;
304 if (fscanf(f,
"%lf %lf %lf", &(local_R[1][0]), &(local_R[1][1]), &(local_R[1][2])) != 3)
306 cerr <<
"Error while reading matchmover output (5)." << endl;
310 if (fscanf(f,
" %lf %lf %lf )", &(local_R[2][0]), &(local_R[2][1]), &(local_R[2][2])) != 3)
312 cerr <<
"Error while reading matchmover output (6)." << endl;
316 set_original_internal_parameters(
int(2 * principal_point_u),
int(2 * principal_point_v),
317 focale_length, pixel_ratio * focale_length,
318 principal_point_u, principal_point_v);
323 set_external_parameters(local_R, local_t);
330 o <<
"[" << P.fx <<
"\t" << 0 <<
"\t" << P.cx <<
"\t][" << P.R[0][0] <<
"\t" << P.R[0][1] <<
"\t" << P.R[0][2] <<
" | " << P.T[0] <<
" ]" << endl;
331 o <<
"[" << 0 <<
"\t" << P.fy <<
"\t" << P.cy <<
"\t][" << P.R[1][0] <<
"\t" << P.R[1][1] <<
"\t" << P.R[1][2] <<
" | " << P.T[1] <<
" ]" << endl;
332 o <<
"[" << 0 <<
"\t" << 0 <<
"\t" << 1 <<
"\t][" << P.R[2][0] <<
"\t" << P.R[2][1] <<
"\t" << P.R[2][2] <<
" | " << P.T[2] <<
" ]" << endl;
339 fprintf(file,
"%s\n", calibration_filename);
340 fprintf(file,
"0\n");
341 fprintf(file,
"-1\n");
343 if (have_to_recompute_optical_centre)
344 compute_optical_centre();
346 fprintf(file,
"%lf %lf %lf ", optical_centre[0], optical_centre[1], optical_centre[2]);
347 fprintf(file,
"%lf %lf %lf ", R[0][0], R[0][1], R[0][2]);
348 fprintf(file,
"%lf %lf %lf ", R[1][0], R[1][1], R[1][2]);
349 fprintf(file,
"%lf %lf %lf ", R[2][0], R[2][1], R[2][2]);
350 fprintf(file,
"%lf %lf %lf ", T[0], T[1], T[2]);
357 double _fx,
double _fy,
double _cx,
double _cy)
359 image_width = original_image_width = _image_width;
360 image_height = original_image_height = _image_height;
362 fx = original_fx = _fx;
363 fy = original_fy = _fy;
364 cx = original_cx = _cx;
365 cy = original_cy = _cy;
367 have_to_recompute_invAR = have_to_recompute_P =
true;
372 fx = original_fx = _fx;
373 fy = original_fy = _fy;
374 cx = original_cx = _cx;
375 cy = original_cy = _cy;
377 have_to_recompute_invAR = have_to_recompute_P =
true;
382 image_width = new_width;
383 image_height = new_height;
385 double sx = (double)new_width / original_image_width;
386 double sy = (double)new_height / original_image_height;
388 fx = sx * original_fx;
389 cx = sx * original_cx;
390 fy = sy * original_fy;
391 cy = sy * original_cy;
393 have_to_recompute_invAR = have_to_recompute_P =
true;
400 FILE * tdir_file = fopen(tdir_filename,
"r");
403 perror(tdir_filename);
407 for(
int i = 0; i < 12 ; i++)
409 if (fscanf(tdir_file,
"%lf", &tdir_mat[i]) == EOF)
411 cout<<
"Error loading file (corrupt or wrong format)"<<endl;
418 original_fx = fx = tdir_mat[0];
419 original_fy = fy = tdir_mat[5];
420 original_cx = cx = tdir_mat[2];
421 original_cy = cy = tdir_mat[6];
423 have_to_recompute_invAR = have_to_recompute_optical_centre = have_to_recompute_P =
true;
432 double Lx,
double Ly,
double Lz,
433 double angle_vertical)
435 double rli1[3], rli2[3], rli3[3];
443 k[0] = sin(angle_vertical) * rli3[1];
444 k[1] = sin(angle_vertical) * rli3[0];
445 k[2] = cos(angle_vertical);
450 for(
int i = 0; i < 3; i++)
457 for(
int i = 0; i < 3; i++)
458 T[i] = -(R[i][0] * Cx + R[i][1] * Cy + R[i][2] * Cz);
460 have_to_recompute_invAR = have_to_recompute_optical_centre = have_to_recompute_P =
true;
464 double Tx,
double Ty,
double Tz)
472 have_to_recompute_invAR = have_to_recompute_optical_centre = have_to_recompute_P =
true;
477 set_external_parameters(state[0], state[1], state[2], state[3], state[4], state[5]);
483 double * t = state + 3;
485 double length = sqrt(om[0] * om[0] + om[1] * om[1] + om[2] * om[2]);
487 if (length > (2 * 3.14159 - 1))
489 double new_length = fmod(length, 2 * 3.14159);
490 double r = new_length / length;
498 CvMat rotMat = cvMat(3, 3, CV_64FC1, rm);
499 CvMat rotVec = cvMat(1, 3, CV_64FC1, rv);
501 cvSetReal1D(&rotVec, 0, om[0]);
502 cvSetReal1D(&rotVec, 1, om[1]);
503 cvSetReal1D(&rotVec, 2, om[2]);
506 cvRodrigues(&rotMat, &rotVec, JR, CV_RODRIGUES_V2M);
509 for(
int i = 0; i < 3; i++)
511 for(
int j = 0; j < 3; j++)
512 R[i][j] =
cvmGet(&rotMat, i, j);
516 have_to_recompute_invAR = have_to_recompute_optical_centre = have_to_recompute_P =
true;
523 CvMat rotMat = cvMat(3, 3, CV_64FC1, rm);
524 CvMat rotVec = cvMat(1, 3, CV_64FC1, rv);
527 for(
int i = 0; i < 3; i++)
528 for(
int j = 0; j < 3; j++)
529 cvmSet(&rotMat, i, j, R[i][j]);
531 cvRodrigues(&rotMat, &rotVec, 0, CV_RODRIGUES_M2V);
544 for(
int i = 0; i < 3; i++)
546 for(
int j = 0; j < 3; j++)
547 R[i][j] = wc2vc[i][j];
551 have_to_recompute_invAR = have_to_recompute_optical_centre = have_to_recompute_P =
true;
556 for(
int i = 0; i < 3; i++)
558 for(
int j = 0; j < 3; j++)
559 R[i][j] = wc2vc[i][j];
563 have_to_recompute_invAR = have_to_recompute_optical_centre = have_to_recompute_P =
true;
571 have_to_recompute_invAR = have_to_recompute_optical_centre = have_to_recompute_P =
true;
576 R[0][0] = 1; R[0][1] = 0; R[0][2] = 0;
577 R[1][0] = 0; R[1][1] = 1; R[1][2] = 0;
578 R[2][0] = 0; R[2][1] = 0; R[2][2] = 1;
580 have_to_recompute_invAR = have_to_recompute_optical_centre = have_to_recompute_P =
true;
585 T[0] = T[1] = T[2] = 0.;
587 have_to_recompute_optical_centre = have_to_recompute_P =
true;
592 double AH1[3], AH2[3];
593 double inv_fx = 1. / fx, inv_fy = 1. / fy;
595 AH1[0] = inv_fx *
cvmGet(H, 0, 0) - cx * inv_fx *
cvmGet(H, 2, 0);
596 AH1[1] = inv_fy *
cvmGet(H, 1, 0) - cy * inv_fy *
cvmGet(H, 2, 0);
599 AH2[0] = inv_fx *
cvmGet(H, 0, 1) - cx * inv_fx *
cvmGet(H, 2, 1);
600 AH2[1] = inv_fy *
cvmGet(H, 1, 1) - cy * inv_fy *
cvmGet(H, 2, 1);
603 T[0] = inv_fx *
cvmGet(H, 0, 2) - cx * inv_fx *
cvmGet(H, 2, 2);
604 T[1] = inv_fy *
cvmGet(H, 1, 2) - cy * inv_fy *
cvmGet(H, 2, 2);
607 double norm2_AH1 = AH1[0] * AH1[0] + AH1[1] * AH1[1] + AH1[2] * AH1[2];
608 double norm2_AH2 = AH2[0] * AH2[0] + AH2[1] * AH2[1] + AH2[2] * AH2[2];
609 double inv_l = 1. / sqrt( sqrt(norm2_AH1 * norm2_AH2) );
616 double c[3], p[3], d[3];
624 double R1p[3], R2p[3], R3p[3];
633 for(
int i = 0; i < 3; i++)
640 cout << *
this << endl;
642 have_to_recompute_invAR = have_to_recompute_optical_centre = have_to_recompute_P =
true;
648 have_to_recompute_optical_centre = have_to_recompute_P =
true;
655 have_to_recompute_optical_centre = have_to_recompute_P =
true;
662 have_to_recompute_optical_centre = have_to_recompute_P =
true;
670 get_external_parameters(&par[0], &par[1], &par[2], &par[3], &par[4], &par[5]);
672 set_external_parameters(par[0], par[1], par[2], par[3], par[4], par[5]);
674 have_to_recompute_invAR = have_to_recompute_optical_centre = have_to_recompute_P =
true;
681 get_external_parameters(&par[0], &par[1], &par[2], &par[3], &par[4], &par[5]);
683 set_external_parameters(par[0], par[1], par[2], par[3], par[4], par[5]);
685 have_to_recompute_invAR = have_to_recompute_optical_centre = have_to_recompute_P =
true;
692 get_external_parameters(&par[0], &par[1], &par[2], &par[3], &par[4], &par[5]);
694 set_external_parameters(par[0], par[1], par[2], par[3], par[4], par[5]);
696 have_to_recompute_invAR = have_to_recompute_optical_centre = have_to_recompute_P =
true;
701 get_external_parameters(&state[0], &state[1], &state[2], &state[3], &state[4], &state[5]);
705 double * Tx,
double * Ty,
double * Tz)
718 Mc[0] = R[0][0] * M[0] + R[0][1] * M[1] + R[0][2] * M[2] + T[0];
719 Mc[1] = R[1][0] * M[0] + R[1][1] * M[1] + R[1][2] * M[2] + T[1];
720 Mc[2] = R[2][0] * M[0] + R[2][1] * M[1] + R[2][2] * M[2] + T[2];
725 M2[0] = R[0][0] * M[0] + R[0][1] * M[1] + R[0][2] * M[2] + T[0];
726 M2[1] = R[1][0] * M[0] + R[1][1] * M[1] + R[1][2] * M[2] + T[1];
727 M2[2] = R[2][0] * M[0] + R[2][1] * M[1] + R[2][2] * M[2] + T[2];
739 if (have_to_recompute_P)
750 if (have_to_recompute_optical_centre)
751 compute_optical_centre();
753 *Cx = optical_centre[0];
754 *Cy = optical_centre[1];
755 *Cz = optical_centre[2];
760 if (have_to_recompute_optical_centre)
761 compute_optical_centre();
763 return optical_centre;
771 double * V =
new double[3];
780 if (have_to_recompute_invAR)
783 V[0] = invAR[0][0] * u + invAR[0][1] * v + invAR[0][2];
784 V[1] = invAR[1][0] * u + invAR[1][1] * v + invAR[1][2];
785 V[2] = invAR[2][0] * u + invAR[2][1] * v + invAR[2][2];
792 if (have_to_recompute_invAR)
795 *Vx = invAR[0][0] * u + invAR[0][1] * v + invAR[0][2];
796 *Vy = invAR[1][0] * u + invAR[1][1] * v + invAR[1][2];
797 *Vz = invAR[2][0] * u + invAR[2][1] * v + invAR[2][2];
799 double inv_norme = 1. /
gfla_norme(*Vx, *Vy, *Vz);
817 double tdir[3][4],
S[3][3], Stdir[3][4];
819 double zmin = planes[4], zmax = planes[5], zratio = 2.0 / (zmax - zmin);
821 for(
int i = 0; i < 3; i++)
822 for(
int j = 0; j < 4; j++)
831 S[0][0] = 2. / xdim; S[0][1] = 0.; S[0][2] = -1.;
832 S[1][0] = 0.; S[1][1] = 2. / ydim; S[1][2] = -1.;
833 S[2][0] = 0.; S[2][1] = 0.; S[2][2] = 1.;
837 for(
int j = 0; j < 4; j++)
839 gl_mat[j][0] = float(Stdir[0][j]);
840 gl_mat[j][1] = float(Stdir[1][j]);
841 gl_mat[j][3] = float(Stdir[2][j]);
845 for(
int j = 0; j < 4; j++)
846 gl_mat[j][2] =
float( planes[j] * zratio );
847 gl_mat[3][2]= float( -(1.0 + gl_mat[3][2] + zmin * zratio) );
849 for(
int i = 0; i < 4; i++)
850 gl_mat[i][1] = -gl_mat[i][1];
852 for(
int i = 0; i < 4; i++)
853 for(
int j = 0; j < 4; j++)
854 gl_vector[j + 4 * i] = gl_mat[i][j];
859 gl_vector[0*4+0] = R[0][0]; gl_vector[0*4+1] = R[1][0]; gl_vector[0*4+2] = R[2][0]; gl_vector[0*4+3] = 0.;
860 gl_vector[1*4+0] = R[0][1]; gl_vector[1*4+1] = R[1][1]; gl_vector[1*4+2] = R[2][1]; gl_vector[1*4+3] = 0.;
861 gl_vector[2*4+0] = R[0][2]; gl_vector[2*4+1] = R[1][2]; gl_vector[2*4+2] = R[2][2]; gl_vector[2*4+3] = 0.;
862 gl_vector[3*4+0] = T[0]; gl_vector[3*4+1] = T[1]; gl_vector[3*4+2] = T[2]; gl_vector[3*4+3] = 1.;
866 double X2,
double Y2,
double Z2,
867 double X3,
double Y3,
double Z3)
871 double M1M2[3], M1M3[3];
872 M1M2[0] = X2 - X1; M1M2[1] = Y2 - Y1; M1M2[2] = Z2 - Z1;
873 M1M3[0] = X3 - X1; M1M3[1] = Y3 - Y1; M1M3[2] = Z3 - Z1;
881 void projection_matrix::compute_invAR(
void)
883 double t4, t6, t8, t10, t12, t14, t16, t19, t24, t27, t28, t30, t32, t34, t35, t37, t39, t43, t52, t55, t57, t59;
886 t6 = R[0][0]*R[2][2];
887 t8 = R[0][0]*R[2][1];
888 t10 = R[1][0]*R[2][2];
889 t12 = R[2][1]*R[1][0];
890 t14 = R[2][0]*R[0][1];
891 t16 = R[2][0]*R[0][2];
892 t19 = 1/(t6*R[1][1]-t8*R[1][2]-t10*R[0][1]+t12*R[0][2]+t14*R[1][2]-t16*R[1][1]);
908 invAR[0][0] = (R[1][1]*R[2][2]-R[2][1]*R[1][2])*t4*t19;
909 invAR[0][1] = -(R[2][2]*R[0][1]-R[2][1]*R[0][2])*t24*t19;
910 invAR[0][2] = (t27*t28+t27*t30+t32*t28-t34*t35-t34*t37-t39*t35)*t4*t43;
911 invAR[1][0] = -(t10-R[2][0]*R[1][2])*t4*t19;
912 invAR[1][1] = (t6-t16)*t24*t19;
913 invAR[1][2] = -(t52*t28+t52*t30+t55*t28-t34*t57-t34*t59-t39*t57)*t4*t43;
914 invAR[2][0] = (t12-R[2][0]*R[1][1])*t4*t19;
915 invAR[2][1] = -(t8-t14)*t24*t19;
916 invAR[2][2] = (t52*t35+t52*t37+t55*t35-t27*t57-t27*t59-t32*t57)*t4*t43;
918 have_to_recompute_invAR =
false;
921 void projection_matrix::compute_optical_centre(
void)
925 optical_centre[0] = -(R[0][0] * T[0] + R[1][0] * T[1] + R[2][0] * T[2]);
926 optical_centre[1] = -(R[0][1] * T[0] + R[1][1] * T[1] + R[2][1] * T[2]);
927 optical_centre[2] = -(R[0][2] * T[0] + R[1][2] * T[1] + R[2][2] * T[2]);
929 have_to_recompute_optical_centre =
false;
932 void projection_matrix::compute_P(
void)
936 A[0][0] = fx; A[0][1] = 0.; A[0][2] = cx;
937 A[1][0] = 0.; A[1][1] = fy; A[1][2] = cy;
938 A[2][0] = 0.; A[2][1] = 0.; A[2][2] = 1.;
940 for(
int i = 0; i < 3; i++)
941 for(
int j = 0; j < 4; j++)
944 for(
int k = 0; k < 3; k++)
946 P[i][j] += A[i][k] * R[k][j];
948 P[i][j] += A[i][k] * T[k];
951 have_to_recompute_P =
false;