7 static double TOL = 1E-5;
13 bool Rpp(
const Mat &model_3D,
const Mat &iprts,
14 Mat &Rlu, Mat &tlu,
int &it1,
double &obj_err1,
double &img_err1, vector<Solution> &sol)
18 ObjPose(model_3D, iprts, Rlu, Rlu, tlu, it1, obj_err1, img_err1);
21 ObjPose(model_3D, iprts, cv::Mat(), Rlu, tlu, it1, obj_err1, img_err1);
34 double lowestErr = 1e6;
36 for(
unsigned int i=0; i < sol.size(); i++) {
37 ObjPose(model_3D, iprts, sol[i].R, Rlu, sol[i].t, it1, obj_err1, img_err1);
40 sol[i].obj_err = obj_err1;
41 sol[i].img_err = img_err1;
43 if(img_err1 < lowestErr) {
51 obj_err1 = sol[bestIdx].obj_err;
52 img_err1 = sol[bestIdx].img_err;
66 void ObjPose(
const Mat _P, Mat Qp, Mat initR,
67 Mat &R, Mat &t,
int &it,
double &obj_err,
double &img_err)
74 Mat pbar =
Sum(P,2) / n;
77 for(
int i=0; i < n; i++) {
78 P.at<
double>(0,i) -= pbar.at<
double>(0,0);
79 P.at<
double>(1,i) -= pbar.at<
double>(1,0);
80 P.at<
double>(2,i) -= pbar.at<
double>(2,0);
88 for(
int i=0; i < n; i++) {
89 F[i].create(3,3,CV_64F);
91 V.at<
double>(0,0) = Qp.at<
double>(0,i);
92 V.at<
double>(1,0) = Qp.at<
double>(1,i);
93 V.at<
double>(2,0) = Qp.at<
double>(2,i);
97 F[i] = (V*V.t()) / ret.at<
double>(0,0);
101 Mat sumF = Mat::zeros(3,3,CV_64F);
103 for(
int i=0; i < n; i++) {
107 Mat tFactor = (Mat::eye(3,3,CV_64F)-sumF/n).inv()/n;
116 Mat sum_ = Mat::zeros(3,1,CV_64F);
118 Mat _eye = Mat::eye(3,3,CV_64F);
121 for(
int i=0; i < n; i++) {
122 PP.at<
double>(0,0) = P.at<
double>(0,i);
123 PP.at<
double>(1,0) = P.at<
double>(1,i);
124 PP.at<
double>(2,0) = P.at<
double>(2,i);
126 sum_ = sum_ + (F[i] - _eye)*Ri*PP;
132 Qi =
Xform(P, Ri, ti);
137 Mat QiCol(3,1,CV_64F);
139 for(
int i=0; i < n; i++) {
140 QiCol.at<
double>(0,0) = Qi.at<
double>(0,i);
141 QiCol.at<
double>(1,0) = Qi.at<
double>(1,i);
142 QiCol.at<
double>(2,0) = Qi.at<
double>(2,i);
144 vec = (_eye - F[i])*QiCol;
146 double x = vec.at<
double>(0,0);
147 double y = vec.at<
double>(1,0);
148 double z = vec.at<
double>(2,0);
150 old_err += (x*x + y*y + z*z);
155 AbsKernel(P, Qp, F, tFactor, Ri, ti, Qi, old_err);
165 AbsKernel(P, Qi, F, tFactor, Ri, ti, Qi, new_err);
171 while(fabs((old_err-new_err)/old_err) >
TOL && (new_err >
EPSILON)) {
174 AbsKernel(P, Qi, F, tFactor, Ri, ti, Qi, new_err);
182 obj_err = sqrt(new_err/n);
185 Mat Pcol(3,1,CV_64F);
190 for(
int i=0; i < n; i++) {
191 Pcol.at<
double>(0,0) = P.at<
double>(0,i);
192 Pcol.at<
double>(1,0) = P.at<
double>(1,i);
193 Pcol.at<
double>(2,0) = P.at<
double>(2,i);
195 Qproj = Ri*Pcol + ti;
197 double xx = (Qproj.at<
double>(0,0)/Qproj.at<
double>(2,0)) - Qp.at<
double>(0,0);
198 double yy = (Qproj.at<
double>(1,0)/Qproj.at<
double>(2,0)) - Qp.at<
double>(1,0);
200 img_err += (xx*xx + yy*yy);
203 img_err = sqrt(img_err/n);
210 Mat
EstimateT(
const Mat &R,
const Mat &G,
const vector <Mat> &F,
const Mat &P)
212 Mat sum = Mat::zeros(3,1,CV_64F);
216 for(
int i=0; i < P.cols; i++) {
217 PP.at<
double>(0,0) = P.at<
double>(0,i);
218 PP.at<
double>(1,0) = P.at<
double>(1,i);
219 PP.at<
double>(2,0) = P.at<
double>(2,i);
229 void AbsKernel(Mat P, Mat Q,
const vector <Mat> &F,
const Mat &G,
230 Mat &R, Mat &t, Mat &Qout,
double &err2)
236 for(
int i=0; i < n; i++) {
237 QQ.at<
double>(0,0) = Q.at<
double>(0,i);
238 QQ.at<
double>(1,0) = Q.at<
double>(1,i);
239 QQ.at<
double>(2,0) = Q.at<
double>(2,i);
243 Q.at<
double>(0,i) = QQ.at<
double>(0,0);
244 Q.at<
double>(1,i) = QQ.at<
double>(1,0);
245 Q.at<
double>(2,i) = QQ.at<
double>(2,0);
248 Mat pbar =
Sum(P,2)/n;
249 Mat qbar =
Sum(Q,2)/n;
252 for(
int i=0; i < n; i++) {
253 P.at<
double>(0,i) -= pbar.at<
double>(0,0);
254 P.at<
double>(1,i) -= pbar.at<
double>(1,0);
255 P.at<
double>(2,i) -= pbar.at<
double>(2,0);
260 Mat M = Mat::zeros(3,3,CV_64F);
263 for(
int i=0; i < n; i++) {
264 PP.at<
double>(0,0) = P.at<
double>(0,i);
265 PP.at<
double>(1,0) = P.at<
double>(1,i);
266 PP.at<
double>(2,0) = P.at<
double>(2,i);
268 QQ.at<
double>(0,0) = Q.at<
double>(0,i);
269 QQ.at<
double>(1,0) = Q.at<
double>(1,i);
270 QQ.at<
double>(2,0) = Q.at<
double>(2,i);
277 R = decomp.vt.t()*(decomp.u.t());
278 Mat v = decomp.vt.t();
280 if(
sign(determinant(R)) == 1) {
283 if(t.at<
double>(2,0) < 0) {
287 v.at<
double>(0,2) = -v.at<
double>(0,2);
288 v.at<
double>(1,2) = -v.at<
double>(1,2);
289 v.at<
double>(2,2) = -v.at<
double>(2,2);
298 v.at<
double>(0,2) = -v.at<
double>(0,2);
299 v.at<
double>(1,2) = -v.at<
double>(1,2);
300 v.at<
double>(2,2) = -v.at<
double>(2,2);
305 if(t.at<
double>(2,0) < 0) {
306 R = -v*(decomp.u.t());
313 Mat _eye = Mat::eye(3,3,CV_64F);
317 Qout =
Xform(P, R, t);
319 for(
int i=0; i < n; i++) {
320 QQ.at<
double>(0,0) = Qout.at<
double>(0,i);
321 QQ.at<
double>(1,0) = Qout.at<
double>(1,i);
322 QQ.at<
double>(2,0) = Qout.at<
double>(2,i);
324 vec = (_eye - F[i])*QQ;
326 double x = vec.at<
double>(0,0);
327 double y = vec.at<
double>(1,0);
328 double z = vec.at<
double>(2,0);
330 err2 += (x*x + y*y + z*z);
336 Mat ret(R.rows, R.cols, CV_64F);
338 for(
int i=0; i < R.cols; i++) {
340 double mag = R.at<
double>(0,i)*R.at<
double>(0,i) +
341 R.at<
double>(1,i)*R.at<
double>(1,i) +
342 R.at<
double>(2,i)*R.at<
double>(2,i);
344 double m = 1.f/sqrt(mag);
346 ret.at<
double>(0,i) = R.at<
double>(0,i)*m;
347 ret.at<
double>(1,i) = R.at<
double>(1,i)*m;
348 ret.at<
double>(2,i) = R.at<
double>(2,i)*m;
358 double mag = sqrt(V[0]*V[0] + V[1]*V[1] + V[2]*V[2]);
360 ret.at<
double>(0,0) = V[0] / mag;
361 ret.at<
double>(1,0) = V[1] / mag;
362 ret.at<
double>(2,0) = V[2] / mag;
377 Vec3d rotation_axis = q_vector;
379 rotation_axis[0] /= n;
380 rotation_axis[1] /= n;
381 rotation_axis[2] /= n;
383 rotation_axis[0] *= sin(q_angle*0.5);
384 rotation_axis[1] *= sin(q_angle*0.5);
385 rotation_axis[2] *= sin(q_angle*0.5);
410 R.at<
double>(0,0) = a*a + b*b - c*c -d*d;
411 R.at<
double>(0,1) = 2*(b*c - a*d);
412 R.at<
double>(0,2) = 2*(b*d + a*c);
414 R.at<
double>(1,0) = 2*(b*c + a*d);
415 R.at<
double>(1,1) = a*a + c*c - b*b - d*d;
416 R.at<
double>(1,2) = 2*(c*d - a*b);
418 R.at<
double>(2,0) = 2*(b*d - a*c);
419 R.at<
double>(2,1) = 2*(c*d + a*b);
420 R.at<
double>(2,2) = a*a + d*d - b*b -c*c;
429 double winkel = acos(v2.dot(v1));
448 double mag = a.at<
double>(0,0)*a.at<
double>(0,0);
451 fprintf(stderr,
"Error in GetRotationbyVector()\n");
458 Mat
Sum(
const Mat &m,
int dim)
462 Mat ret(1, m.cols, CV_64F);
464 for(
int j=0; j < m.cols; j++) {
467 for(
int i=0; i < m.rows; i++) {
468 sum += m.at<
double>(i,j);
471 ret.at<
double>(0,j) = sum;
477 Mat ret(m.rows, 1, CV_64F);
479 for(
int i=0; i < m.rows; i++) {
482 for(
int j=0; j < m.cols; j++) {
483 sum += m.at<
double>(i,j);
486 ret.at<
double>(i,0) = sum;
493 Mat
Mul(
const Mat &a,
const Mat &b)
495 assert(a.rows == b.rows && a.cols == b.cols);
497 Mat ret(a.rows, a.cols, CV_64F);
499 for(
int i=0; i < a.rows; i++) {
500 for(
int j=0; j < a.cols; j++) {
501 ret.at<
double>(i,j) = a.at<
double>(i,j)*b.at<
double>(i,j);
510 Mat ret(1, m.cols, CV_64F);
512 for(
int j=0; j < m.cols; j++) {
515 for(
int i=0; i < m.rows; i++) {
516 sum += m.at<
double>(i,j);
519 ret.at<
double>(0,j) = sum / m.cols;
527 Mat ret(3, pts.size(), CV_64F);
529 for(
unsigned int i=0; i < pts.size(); i++) {
530 ret.at<
double>(0,i) = pts[i].
x;
531 ret.at<
double>(1,i) = pts[i].
y;
532 ret.at<
double>(2,i) = pts[i].
z;
540 Mat ret(3, pts.size(), CV_64F);
542 for(
unsigned int i=0; i < pts.size(); i++) {
543 ret.at<
double>(0,i) = pts[i].
x;
544 ret.at<
double>(1,i) = pts[i].
y;
545 ret.at<
double>(2,i) = 1.
f;
555 ret.at<
double>(0,0) = v[0];
556 ret.at<
double>(1,0) = v[1];
557 ret.at<
double>(2,0) = v[2];
565 double cosA = cos(angs[2]);
566 double sinA = sin(angs[2]);
567 double cosB = cos(angs[1]);
568 double sinB = sin(angs[1]);
569 double cosC = cos(angs[0]);
570 double sinC = sin(angs[0]);
572 double cosAsinB = cosA*sinB;
573 double sinAsinB = sinA*sinB;
577 R.at<
double>(0,0) = cosA*cosB;
578 R.at<
double>(0,1) = cosAsinB*sinC-sinA*cosC;
579 R.at<
double>(0,2) = cosAsinB*cosC+sinA*sinC;
581 R.at<
double>(1,0) = sinA*cosB;
582 R.at<
double>(1,1) = sinAsinB*sinC+cosA*cosC;
583 R.at<
double>(1,2) = sinAsinB*cosC-cosA*sinC;
585 R.at<
double>(2,0) = -sinB;
586 R.at<
double>(2,1) = cosB*sinC;
587 R.at<
double>(2,2) = cosB*cosC;
598 double R11 = R.at<
double>(0,0);
599 double R12 = R.at<
double>(0,1);
600 double R13 = R.at<
double>(0,2);
602 double R21 = R.at<
double>(1,0);
603 double R22 = R.at<
double>(1,1);
604 double R23 = R.at<
double>(1,2);
606 double R31 = R.at<
double>(2,0);
607 double R32 = R.at<
double>(2,1);
608 double R33 = R.at<
double>(2,2);
611 double cosB = sqrt(R11*R11 + R21*R21);
613 if(fabs (cosB) > 1e-15) {
614 double sinA = R21 / cosB;
615 double cosA = R11 / cosB;
616 double sinC = R32 / cosB;
617 double cosC = R33 / cosB;
618 angs[0] = atan2(sinC,cosC);
619 angs[1] = atan2(sinB,cosB);
620 angs[2] = atan2(sinA,cosA);
623 double sinC = (R12 - R23) / 2;
624 double cosC = (R22 + R13) / 2;
625 angs[0] = atan2(sinC,cosC);
650 fprintf(stderr,
"rpyMat: Error not correct Solution\n");
662 bool status =
RpyAng(R, ang_zyx);
668 if(fabs(ang_zyx[0]) > M_PI_2) {
670 while ( fabs(ang_zyx[0]) > M_PI_2 ) {
672 ang_zyx[0] = ang_zyx[0]+M_PI;
673 ang_zyx[1] = 3*M_PI-ang_zyx[1];
674 ang_zyx[2] = ang_zyx[2]+M_PI;
676 ang_zyx[0] -= 2*M_PI;
677 ang_zyx[1] -= 2*M_PI;
678 ang_zyx[2] -= 2*M_PI;
681 ang_zyx[0] = ang_zyx[0]+M_PI;
682 ang_zyx[1] = 3*M_PI-ang_zyx[1];
683 ang_zyx[2] = ang_zyx[2]+M_PI;
693 bool Get2ndPose_Exact(
const Mat &v,
const Mat &P,
const Mat &R,
const Mat &t, vector<Solution> &ret)
700 _cent[0] = cent.at<
double>(0,0);
701 _cent[1] = cent.at<
double>(1,0);
702 _cent[2] = cent.at<
double>(2,0);
730 vector<Solution> sol;
739 for(
unsigned int i=0; i < sol.size(); i++) {
743 sol[i].R = Rim.t()*sol[i].R;
747 sol[i].t = Rim.t()*sol[i].t;
759 double cl = atan2(R.at<
double>(2,1), R.at<
double>(2,0));
760 Mat Rz =
RpyMat(Vec3d(0,0,cl));
773 if(R_.at<
double>(2,1) > 1e-3) {
774 fprintf(stderr,
"error in DecomposeR 1\n");
779 bool status =
RpyAng_X(R_, ang_zyx);
785 if(fabs(ang_zyx[0]) > 1e-3) {
786 fprintf(stderr,
"error in DecomposeR 2\n");
790 Rz2 = Rz*
RpyMat(Vec3d(0,0,M_PI));
793 if(R_.at<
double>(2,1) > 1e-3) {
794 fprintf(stderr,
"error in DecomposeR 3\n");
812 Mat ret(m.rows, m.cols, CV_64F);
814 for(
int i=0; i < m.rows; i++) {
815 for(
int j=0; j < m.cols; j++) {
816 ret.at<
double>(i,j) = m.at<
double>(i,j)*m.at<
double>(i,j);
823 Mat
Xform(
const Mat &P,
const Mat &R,
const Mat &t)
825 Mat ret(3,P.cols,CV_64F);
827 for(
int i=0; i < P.cols; i++) {
828 double x = P.at<
double>(0,i);
829 double y = P.at<
double>(1,i);
830 double z = P.at<
double>(2,i);
832 ret.at<
double>(0,i) = R.at<
double>(0,0)*x + R.at<
double>(0,1)*y + R.at<
double>(0,2)*z + t.at<
double>(0,0);
833 ret.at<
double>(1,i) = R.at<
double>(1,0)*x + R.at<
double>(1,1)*y + R.at<
double>(1,2)*z + t.at<
double>(1,0);
834 ret.at<
double>(2,i) = R.at<
double>(2,0)*x + R.at<
double>(2,1)*y + R.at<
double>(2,2)*z + t.at<
double>(2,0);
844 return decomp.w.at<
double>(0,0);
874 Mat Ry =
RpyMat(Vec3d(0,ang_zyx[1],0));
875 Mat Rz =
RpyMat(Vec3d(0,0,ang_zyx[2]));
888 for(
unsigned int i=0; i < bl.size(); i++) {
889 bl[i] = bl[i]/180*M_PI;
893 vector <Mat> V(v.cols);
897 for(
int i=0; i < v.cols; i++) {
898 tmp.at<
double>(0,0) = v.at<
double>(0,i);
899 tmp.at<
double>(1,0) = v.at<
double>(1,i);
900 tmp.at<
double>(2,0) = v.at<
double>(2,i);
904 V[i] = tmp*tmp.t() / a.at<
double>(0,0);
907 vector <Solution> sol(bl.size());
909 for(
unsigned int j=0; j < bl.size(); j++) {
913 Ry =
RpyMat(Vec3d(0,bl[j],0));
914 sol[j].R = Rz*Ry*RzN.t();
920 sol[j].t.at<
double>(0,0) = Tnew.at<
double>(0,j);
921 sol[j].t.at<
double>(1,0) = Tnew.at<
double>(1,j);
922 sol[j].t.at<
double>(2,0) = Tnew.at<
double>(2,j);
926 Mat _eye = Mat::eye(3,3,CV_64F);
927 Mat Pcol(3,1,CV_64F);
929 for(
int i=0; i < v.cols; i++) {
930 Pcol.at<
double>(0,0) = P.at<
double>(0,i);
931 Pcol.at<
double>(1,0) = P.at<
double>(1,i);
932 Pcol.at<
double>(2,0) = P.at<
double>(2,i);
934 Mat a =
Sum(
Sq((_eye - V[i])*(sol[j].R*Pcol + sol[j].t)));
936 E = E + a.at<
double>(0,0);
948 vector <double> &al, Mat &tnew, vector <double> &at)
957 vector <Mat> V(v.cols);
960 for(
int i=0; i < v.cols; i++) {
961 vv.at<
double>(0,0) = v.at<
double>(0,i);
962 vv.at<
double>(1,0) = v.at<
double>(1,i);
963 vv.at<
double>(2,0) = v.at<
double>(2,i);
966 double a = tmp.at<
double>(0,0);
967 V[i] = (vv*vv.t()) / a;
971 Mat G = Mat::zeros(3,3,CV_64F);
973 for(
int i=0; i < v.cols; i++) {
977 Mat _eye = Mat::eye(3,3,CV_64F);
979 G = (_eye - G/v.cols).inv()/v.cols;
986 Mat opt_t = Mat::zeros(3,3,CV_64F);
988 for(
int i=0; i < v.cols; i++) {
989 double v11 = V[i].at<
double>(0,0);
990 double v12 = V[i].at<
double>(0,1);
991 double v13 = V[i].at<
double>(0,2);
992 double v21 = V[i].at<
double>(1,0);
993 double v22 = V[i].at<
double>(1,1);
994 double v23 = V[i].at<
double>(1,2);
995 double v31 = V[i].at<
double>(2,0);
996 double v32 = V[i].at<
double>(2,1);
997 double v33 = V[i].at<
double>(2,2);
999 double px = p.at<
double>(0,i);
1000 double py = p.at<
double>(1,i);
1001 double pz = p.at<
double>(2,i);
1003 double r1 = Rz.at<
double>(0,0);
1004 double r2 = Rz.at<
double>(0,1);
1005 double r3 = Rz.at<
double>(0,2);
1007 double r4 = Rz.at<
double>(1,0);
1008 double r5 = Rz.at<
double>(1,1);
1009 double r6 = Rz.at<
double>(1,2);
1011 double r7 = Rz.at<
double>(2,0);
1012 double r8 = Rz.at<
double>(2,1);
1013 double r9 = Rz.at<
double>(2,2);
1015 opt_t.at<
double>(0,0) += (((v11-1)*r2+v12*r5+v13*r8)*py+(-(v11-1)*r1-v12*r4-v13*r7)*px+(-(v11-1)*r3-v12*r6-v13*r9)*pz);
1016 opt_t.at<
double>(0,1) += ((2*(v11-1)*r1+2*v12*r4+2*v13*r7)*pz+(-2*(v11-1)*r3-2*v12*r6-2*v13*r9)*px);
1017 opt_t.at<
double>(0,2) += ((v11-1)*r1+v12*r4+v13*r7)*px+((v11-1)*r3+v12*r6+v13*r9)*pz+((v11-1)*r2+v12*r5+v13*r8)*py;
1019 opt_t.at<
double>(1,0) += ((v21*r2+(v22-1)*r5+v23*r8)*py+(-v21*r1-(v22-1)*r4-v23*r7)*px+(-v21*r3-(v22-1)*r6-v23*r9)*pz);
1020 opt_t.at<
double>(1,1) += ((2*v21*r1+2*(v22-1)*r4+2*v23*r7)*pz+(-2*v21*r3-2*(v22-1)*r6-2*v23*r9)*px);
1021 opt_t.at<
double>(1,2) += (v21*r1+(v22-1)*r4+v23*r7)*px+(v21*r3+(v22-1)*r6+v23*r9)*pz+(v21*r2+(v22-1)*r5+v23*r8)*py;
1023 opt_t.at<
double>(2,0) += ((v31*r2+v32*r5+(v33-1)*r8)*py+(-v31*r1-v32*r4-(v33-1)*r7)*px+(-v31*r3-v32*r6-(v33-1)*r9)*pz);
1024 opt_t.at<
double>(2,1) += ((2*v31*r1+2*v32*r4+2*(v33-1)*r7)*pz+(-2*v31*r3-2*v32*r6-2*(v33-1)*r9)*px);
1025 opt_t.at<
double>(2,2) += (v31*r1+v32*r4+(v33-1)*r7)*px+(v31*r3+v32*r6+(v33-1)*r9)*pz+(v31*r2+v32*r5+(v33-1)*r8)*py;
1030 Mat E_2 = Mat::zeros(1,5,CV_64F);
1034 for(
int i=0; i < v.cols; i++) {
1035 double px = p.at<
double>(0,i);
1036 double py = p.at<
double>(1,i);
1037 double pz = p.at<
double>(2,i);
1039 Mat Rpi(3,3,CV_64F);
1041 Rpi.at<
double>(0,0) = -px;
1042 Rpi.at<
double>(0,1) = 2*pz;
1043 Rpi.at<
double>(0,2) = px;
1045 Rpi.at<
double>(1,0) = py;
1046 Rpi.at<
double>(1,1) = 0;
1047 Rpi.at<
double>(1,2) = py;
1049 Rpi.at<
double>(2,0) = -pz;
1050 Rpi.at<
double>(2,1) = -2*px;
1051 Rpi.at<
double>(2,2) = pz;
1053 Mat E = (_eye - V[i])*(Rz*Rpi + opt_t);
1059 e0.at<
double>(0,0) = E.at<
double>(0,2);
1060 e0.at<
double>(1,0) = E.at<
double>(1,2);
1061 e0.at<
double>(2,0) = E.at<
double>(2,2);
1063 e1.at<
double>(0,0) = E.at<
double>(0,1);
1064 e1.at<
double>(1,0) = E.at<
double>(1,1);
1065 e1.at<
double>(2,0) = E.at<
double>(2,1);
1067 e2.at<
double>(0,0) = E.at<
double>(0,0);
1068 e2.at<
double>(1,0) = E.at<
double>(1,0);
1069 e2.at<
double>(2,0) = E.at<
double>(2,0);
1078 Mat sum1 =
Sum(
Sq(e2));
1082 Mat sum2 =
Sum(2*
Mul(e1,e2));
1087 Mat sum3 =
Sum(2*
Mul(e0,e2) +
Sq(e1));
1092 Mat sum4 =
Sum(2*
Mul(e0,e1));
1093 Mat sum5 =
Sum(
Sq(e0));
1099 E_2.at<
double>(0,0) += sum1.at<
double>(0,0);
1100 E_2.at<
double>(0,1) += sum2.at<
double>(0,0);
1101 E_2.at<
double>(0,2) += sum3.at<
double>(0,0);
1102 E_2.at<
double>(0,3) += sum4.at<
double>(0,0);
1103 E_2.at<
double>(0,4) += sum5.at<
double>(0,0);
1106 double e4=E_2.at<
double>(0,0);
1107 double e3=E_2.at<
double>(0,1);
1108 double e2=E_2.at<
double>(0,2);
1109 double e1=E_2.at<
double>(0,3);
1110 double e0=E_2.at<
double>(0,4);
1115 double a3=(4*e4-2*e2);
1116 double a2=(-3*e1+3*e3);
1117 double a1=(-4*e0+2*e2);
1138 double zero_real[5];
1139 double zero_imag[5];
1141 memset(zero_real, 0,
sizeof(
double)*5);
1142 memset(zero_imag, 0,
sizeof(
double)*5);
1144 rpoly_ak1(coeffs, °rees, zero_real, zero_imag);
1156 for(
int i=0; i < 5; i++) {
1157 double _at = zero_real[i];
1159 double p1 = pow(1.0 + _at*_at, 3.0);
1161 if(fabs(p1) > 0.1 && zero_imag[i] == 0) {
1179 al.resize(at.size());
1181 vector <double> al2, at2;
1183 for(
unsigned int i=0; i < at.size(); i++) {
1184 double sa = (2.f*at[i]) / (1.
f +at[i]*at[i]);
1185 double ca = (1.f - at[i]*at[i]) / (1.
f + at[i]*at[i]);
1187 al[i] = atan2(sa,ca) * 180/M_PI;
1189 double tMaxMin = (4*a4*at[i]*at[i]*at[i] + 3*a3*at[i]*at[i] + 2*a2*at[i] + a1);
1192 al2.push_back(al[i]);
1193 at2.push_back(at[i]);
1200 tnew = Mat(3,al.size(),CV_64F);
1202 for(
unsigned int a=0; a < al.size(); a++) {
1203 Mat R = Rz*
RpyMat(Vec3d(0, (al[a]*M_PI/180), 0));
1204 Mat t_opt = Mat::zeros(3,1,CV_64F);
1206 Mat pcol(3,1,CV_64F);
1208 for(
int i=0; i < v.cols; i++) {
1209 pcol.at<
double>(0,0) = p.at<
double>(0,i);
1210 pcol.at<
double>(1,0) = p.at<
double>(1,i);
1211 pcol.at<
double>(2,0) = p.at<
double>(2,i);
1213 t_opt = t_opt + (V[i] - _eye)*R*pcol;
1218 tnew.at<
double>(0,a) = t_opt.at<
double>(0,0);
1219 tnew.at<
double>(1,a) = t_opt.at<
double>(1,0);
1220 tnew.at<
double>(2,a) = t_opt.at<
double>(2,0);
1226 for(
int i=0; i < m.rows; i++) {
1227 for(
int j=0; j < m.cols; j++) {
1228 printf(
"%4.6f ", m.at<
double>(i,j));
1239 printf(
"scalar = %f\n", q.
scalar);
bool Rpp(const Mat &model_3D, const Mat &iprts, Mat &Rlu, Mat &tlu, int &it1, double &obj_err1, double &img_err1, vector< Solution > &sol)
void ObjPose(const Mat _P, Mat Qp, Mat initR, Mat &R, Mat &t, int &it, double &obj_err, double &img_err)
Mat GetRotationbyVector(const Vec3d &v1, const Vec3d &v2)
bool RpyAng(const Mat &R, Vec3d &ret)
double Quaternion_Norm(const Quaternion &Q)
Mat Point2Mat(const vector< Point2d > &pts)
TFSIMD_FORCE_INLINE const tfScalar & y() const
void AbsKernel(Mat P, Mat Q, const vector< Mat > &F, const Mat &G, Mat &R, Mat &t, Mat &Qout, double &err2)
bool Get2ndPose_Exact(const Mat &v, const Mat &P, const Mat &R, const Mat &t, vector< Solution > &ret)
void GetRotationY_wrtT(const Mat &v, const Mat &p, const Mat &t, const Mat &Rz, vector< double > &al, Mat &tnew, vector< double > &at)
Quaternion Quaternion_byAngleAndVector(double q_angle, const Vec3d &q_vector)
bool GetRfor2ndPose_V_Exact(const Mat &v, const Mat &P, const Mat &R, const Mat &t, vector< Solution > &ret)
Mat NormRv(const Vec3d &V)
Mat Vec2Mat(const Vec3d &v)
void rpoly_ak1(double op[MDP1], int *Degree, double zeror[MAXDEGREE], double zeroi[MAXDEGREE])
bool DecomposeR(const Mat &R, Mat &Rz2, Mat &ret)
bool RpyAng_X(const Mat &R, Vec3d &ret)
TFSIMD_FORCE_INLINE const tfScalar & x() const
TFSIMD_FORCE_INLINE const tfScalar & z() const
Mat Mul(const Mat &a, const Mat &b)
Mat RpyMat(const Vec3d &angs)
cv::Mat quat2mat(const Quaternion &Q)
double Norm(const Mat &m)
Mat Xform(const Mat &P, const Mat &R, const Mat &t)
Mat EstimateT(const Mat &R, const Mat &G, const vector< Mat > &F, const Mat &P)
Mat Sum(const Mat &m, int dim)
Quaternion Quaternion_byVectorAndScalar(const cv::Vec3d &vector, double scalar)
Quaternion Quaternion_multiplyByScalar(const Quaternion &q, double scalar)