40 #include <visp3/core/vpMath.h> 41 #include <visp3/vision/vpHomography.h> 48 const double vpHomography::sing_threshold = 0.0001;
98 #ifndef DOXYGEN_SHOULD_SKIP_THIS 131 double distanceFictive;
132 double sinusTheta, cosinusTheta, signeSinus = 1;
133 double s, determinantU, determinantV;
134 unsigned int vOrdre[3];
141 #ifdef DEBUG_Homographie 142 printf(
"debut : Homographie_EstimationDeplacementCamera\n");
153 for (
unsigned int i = 0; i < 3; i++)
154 for (
unsigned int j = 0; j < 3; j++)
155 mH[i][j] = aHb[i][j];
165 mTempU.
svd(svTemp, mTempV);
170 if (svTemp[0] >= svTemp[1]) {
171 if (svTemp[0] >= svTemp[2]) {
172 if (svTemp[1] > svTemp[2]) {
187 if (svTemp[1] >= svTemp[2]) {
188 if (svTemp[0] > svTemp[2]) {
209 for (
unsigned int i = 0; i < 3; i++) {
210 sv[i] = svTemp[vOrdre[i]];
211 for (
unsigned int j = 0; j < 3; j++) {
212 mU[i][j] = mTempU[i][vOrdre[j]];
213 mV[i][j] = mTempV[i][vOrdre[j]];
217 #ifdef DEBUG_Homographie 219 std::cout << mU << std::endl;
221 std::cout << mV << std::endl;
222 printf(
"Valeurs singulieres : ");
227 determinantV = mV.
det();
228 determinantU = mU.
det();
230 s = determinantU * determinantV;
232 #ifdef DEBUG_Homographie 233 printf(
"s = det(U) * det(V) = %f * %f = %f\n", determinantU, determinantV, s);
239 distanceFictive = sv[1];
240 #ifdef DEBUG_Homographie 241 printf(
"d = %f\n", distanceFictive);
245 if (((sv[0] - sv[1]) < sing_threshold) && (sv[0] - sv[2]) < sing_threshold) {
258 n[0] = normaleDesiree[0];
259 n[1] = normaleDesiree[1];
260 n[2] = normaleDesiree[2];
262 #ifdef DEBUG_Homographie 263 printf(
"\nCas general\n");
277 mX[0][0] = sqrt((sv[0] * sv[0] - sv[1] * sv[1]) / (sv[0] * sv[0] - sv[2] * sv[2]));
279 mX[2][0] = sqrt((sv[1] * sv[1] - sv[2] * sv[2]) / (sv[0] * sv[0] - sv[2] * sv[2]));
281 mX[0][1] = -mX[0][0];
286 double cosinusAncien = 0.0;
287 for (
unsigned int w = 0; w < 2; w++) {
288 for (
unsigned int k = 0; k < 2; k++) {
291 for (
unsigned int i = 0; i < 3; i++) {
292 normaleEstimee[i] = 0.0;
293 for (
unsigned int j = 0; j < 3; j++) {
294 normaleEstimee[i] += (2.0 * k - 1.0) * mV[i][j] * mX[j][w];
299 double cosinusDesireeEstimee = 0.0;
300 for (
unsigned int i = 0; i < 3; i++)
301 cosinusDesireeEstimee += normaleEstimee[i] * normaleDesiree[i];
307 if (cosinusDesireeEstimee > cosinusAncien) {
308 cosinusAncien = cosinusDesireeEstimee;
311 for (
unsigned int j = 0; j < 3; j++)
312 n[j] = normaleEstimee[j];
315 aTbp[0] = (2.0 * k - 1.0) * (sv[0] - sv[2]) * mX[0][w];
316 aTbp[1] = (2.0 * k - 1.0) * (sv[0] - sv[2]) * mX[1][w];
317 aTbp[2] = -(2.0 * k - 1.0) * (sv[0] - sv[2]) * mX[2][w];
330 for (
unsigned int i = 0; i < 3; i++) {
332 for (
unsigned int j = 0; j < 3; j++) {
333 atb[i] += mU[i][j] * aTbp[j];
335 atb[i] /= distanceFictive;
338 #ifdef DEBUG_Homographie 340 std::cout << aTbp.t();
342 std::cout << atb.
t();
355 signeSinus * sqrt((sv[0] * sv[0] - sv[1] * sv[1]) * (sv[1] * sv[1] - sv[2] * sv[2])) / ((sv[0] + sv[2]) * sv[1]);
357 cosinusTheta = (sv[1] * sv[1] + sv[0] * sv[2]) / ((sv[0] + sv[2]) * sv[1]);
360 aRbp[0][0] = cosinusTheta;
362 aRbp[0][2] = -sinusTheta;
366 aRbp[2][0] = sinusTheta;
368 aRbp[2][2] = cosinusTheta;
371 for (
unsigned int i = 0; i < 3; i++) {
372 for (
unsigned int j = 0; j < 3; j++) {
374 for (
unsigned int k = 0; k < 3; k++) {
375 aRbint[i][j] += mU[i][k] * aRbp[k][j];
381 for (
unsigned int i = 0; i < 3; i++) {
382 for (
unsigned int j = 0; j < 3; j++) {
384 for (
unsigned int k = 0; k < 3; k++) {
385 aRb[i][j] += aRbint[i][k] * mV[j][k];
390 #ifdef DEBUG_Homographie 392 std::cout << aRb << std::endl;
423 double distanceFictive;
424 double sinusTheta, cosinusTheta, signeSinus = 1;
425 double s, determinantU, determinantV;
426 unsigned int vOrdre[3];
429 normaleDesiree[0] = 0;
430 normaleDesiree[1] = 0;
431 normaleDesiree[2] = 1;
434 #ifdef DEBUG_Homographie 435 printf(
"debut : Homographie_EstimationDeplacementCamera\n");
446 for (
unsigned int i = 0; i < 3; i++)
447 for (
unsigned int j = 0; j < 3; j++)
448 mH[i][j] = aHb[i][j];
458 mTempU.
svd(svTemp, mTempV);
463 if (svTemp[0] >= svTemp[1]) {
464 if (svTemp[0] >= svTemp[2]) {
465 if (svTemp[1] > svTemp[2]) {
480 if (svTemp[1] >= svTemp[2]) {
481 if (svTemp[0] > svTemp[2]) {
502 for (
unsigned int i = 0; i < 3; i++) {
503 sv[i] = svTemp[vOrdre[i]];
504 for (
unsigned int j = 0; j < 3; j++) {
505 mU[i][j] = mTempU[i][vOrdre[j]];
506 mV[i][j] = mTempV[i][vOrdre[j]];
510 #ifdef DEBUG_Homographie 512 std::cout << mU << std::endl;
514 std::cout << mV << std::endl;
515 printf(
"Valeurs singulieres : ");
520 determinantV = mV.
det();
521 determinantU = mU.
det();
523 s = determinantU * determinantV;
525 #ifdef DEBUG_Homographie 526 printf(
"s = det(U) * det(V) = %f * %f = %f\n", determinantU, determinantV, s);
532 distanceFictive = sv[1];
533 #ifdef DEBUG_Homographie 534 printf(
"d = %f\n", distanceFictive);
538 if (((sv[0] - sv[1]) < sing_threshold) && (sv[0] - sv[2]) < sing_threshold) {
551 n[0] = normaleDesiree[0];
552 n[1] = normaleDesiree[1];
553 n[2] = normaleDesiree[2];
555 #ifdef DEBUG_Homographie 556 printf(
"\nCas general\n");
570 mX[0][0] = sqrt((sv[0] * sv[0] - sv[1] * sv[1]) / (sv[0] * sv[0] - sv[2] * sv[2]));
572 mX[2][0] = sqrt((sv[1] * sv[1] - sv[2] * sv[2]) / (sv[0] * sv[0] - sv[2] * sv[2]));
574 mX[0][1] = -mX[0][0];
579 double cosinusAncien = 0.0;
580 for (
unsigned int w = 0; w < 2; w++) {
581 for (
unsigned int k = 0; k < 2; k++) {
584 for (
unsigned int i = 0; i < 3; i++) {
585 normaleEstimee[i] = 0.0;
586 for (
unsigned int j = 0; j < 3; j++) {
587 normaleEstimee[i] += (2.0 * k - 1.0) * mV[i][j] * mX[j][w];
592 double cosinusDesireeEstimee = 0.0;
593 for (
unsigned int i = 0; i < 3; i++)
594 cosinusDesireeEstimee += normaleEstimee[i] * normaleDesiree[i];
600 if (cosinusDesireeEstimee > cosinusAncien) {
601 cosinusAncien = cosinusDesireeEstimee;
604 for (
unsigned int j = 0; j < 3; j++)
605 n[j] = normaleEstimee[j];
608 aTbp[0] = (2.0 * k - 1.0) * (sv[0] - sv[2]) * mX[0][w];
609 aTbp[1] = (2.0 * k - 1.0) * (sv[0] - sv[2]) * mX[1][w];
610 aTbp[2] = -(2.0 * k - 1.0) * (sv[0] - sv[2]) * mX[2][w];
623 for (
unsigned int i = 0; i < 3; i++) {
625 for (
unsigned int j = 0; j < 3; j++) {
626 atb[i] += mU[i][j] * aTbp[j];
628 atb[i] /= distanceFictive;
631 #ifdef DEBUG_Homographie 633 std::cout << aTbp.t();
635 std::cout << atb.
t();
648 signeSinus * sqrt((sv[0] * sv[0] - sv[1] * sv[1]) * (sv[1] * sv[1] - sv[2] * sv[2])) / ((sv[0] + sv[2]) * sv[1]);
650 cosinusTheta = (sv[1] * sv[1] + sv[0] * sv[2]) / ((sv[0] + sv[2]) * sv[1]);
653 aRbp[0][0] = cosinusTheta;
655 aRbp[0][2] = -sinusTheta;
659 aRbp[2][0] = sinusTheta;
661 aRbp[2][2] = cosinusTheta;
664 for (
unsigned int i = 0; i < 3; i++) {
665 for (
unsigned int j = 0; j < 3; j++) {
667 for (
unsigned int k = 0; k < 3; k++) {
668 aRbint[i][j] += mU[i][k] * aRbp[k][j];
674 for (
unsigned int i = 0; i < 3; i++) {
675 for (
unsigned int j = 0; j < 3; j++) {
677 for (
unsigned int k = 0; k < 3; k++) {
678 aRb[i][j] += aRbint[i][k] * mV[j][k];
683 #ifdef DEBUG_Homographie 685 std::cout << aRb << std::endl;
690 std::list<vpRotationMatrix> &vR, std::list<vpTranslationVector> &vT,
691 std::list<vpColVector> &vN)
694 #ifdef DEBUG_Homographie 695 printf(
"debut : Homographie_EstimationDeplacementCamera\n");
703 int cas1 = 1, cas2 = 2, cas3 = 3, cas4 = 4;
705 bool norm1ok =
false, norm2ok =
false, norm3ok =
false, norm4ok =
false;
707 double tmp, determinantU, determinantV, s, distanceFictive;
708 vpMatrix mTempU(3, 3), mTempV(3, 3), U(3, 3), V(3, 3);
724 unsigned int vOrdre[3];
737 mTempU.
svd(svTemp, mTempV);
741 if (svTemp[0] >= svTemp[1]) {
742 if (svTemp[0] >= svTemp[2]) {
743 if (svTemp[1] > svTemp[2]) {
758 if (svTemp[1] >= svTemp[2]) {
759 if (svTemp[0] > svTemp[2]) {
779 for (
unsigned int i = 0; i < 3; i++) {
780 sv[i] = svTemp[vOrdre[i]];
781 for (
unsigned int j = 0; j < 3; j++) {
782 U[i][j] = mTempU[i][vOrdre[j]];
783 V[i][j] = mTempV[i][vOrdre[j]];
787 #ifdef DEBUG_Homographie 792 printf(
"Valeurs singulieres : ");
797 determinantU = U[0][0] * (U[1][1] * U[2][2] - U[1][2] * U[2][1]) + U[0][1] * (U[1][2] * U[2][0] - U[1][0] * U[2][2]) +
798 U[0][2] * (U[1][0] * U[2][1] - U[1][1] * U[2][0]);
800 determinantV = V[0][0] * (V[1][1] * V[2][2] - V[1][2] * V[2][1]) + V[0][1] * (V[1][2] * V[2][0] - V[1][0] * V[2][2]) +
801 V[0][2] * (V[1][0] * V[2][1] - V[1][1] * V[2][0]);
803 s = determinantU * determinantV;
805 #ifdef DEBUG_Homographie 806 printf(
"s = det(U) * det(V) = %f * %f = %f\n", determinantU, determinantV, s);
819 tmp = H[2][0] * x + H[2][1] * y + H[2][2];
821 if ((tmp / (sv[1] * s)) > 0)
822 distanceFictive = sv[1];
824 distanceFictive = -sv[1];
832 if ((sv[0] - sv[1]) < sing_threshold) {
833 if ((sv[1] - sv[2]) < sing_threshold)
838 if ((sv[1] - sv[2]) < sing_threshold)
865 Nprim1[0] = sqrt((sv[0] * sv[0] - sv[1] * sv[1]) / (sv[0] * sv[0] - sv[2] * sv[2]));
867 Nprim1[2] = sqrt((sv[1] * sv[1] - sv[2] * sv[2]) / (sv[0] * sv[0] - sv[2] * sv[2]));
869 Nprim2[0] = Nprim1[0];
870 Nprim2[2] = -Nprim1[2];
871 Nprim3[0] = -Nprim1[0];
872 Nprim3[2] = Nprim1[2];
873 Nprim4[0] = -Nprim1[0];
874 Nprim4[2] = -Nprim1[2];
899 tmp = N1[0] * x + N1[1] * y + N1[2];
900 tmp /= (distanceFictive * s);
904 tmp = N2[0] * x + N2[1] * y + N2[2];
905 tmp /= (distanceFictive * s);
909 tmp = N3[0] * x + N3[1] * y + N3[2];
910 tmp /= (distanceFictive * s);
914 tmp = N4[0] * x + N4[1] * y + N4[2];
915 tmp /= (distanceFictive * s);
921 tmp = N1[0] * x + N1[1] * y + N1[2];
922 tmp /= (distanceFictive * s);
926 tmp = N2[0] * x + N2[1] * y + N2[2];
927 tmp /= (distanceFictive * s);
933 tmp = N1[0] * x + N1[1] * y + N1[2];
934 tmp /= (distanceFictive * s);
938 tmp = N2[0] * x + N2[1] * y + N2[2];
939 tmp /= (distanceFictive * s);
947 if (distanceFictive > 0) {
952 Rprim1[0][0] = (
vpMath::sqr(sv[1]) + sv[0] * sv[2]) / ((sv[0] + sv[2]) * sv[1]);
954 Rprim1[2][2] = Rprim1[0][0];
958 ((sv[0] + sv[2]) * sv[1]);
960 Rprim1[0][2] = -Rprim1[2][0];
964 Tprim1[0] = Nprim1[0];
966 Tprim1[2] = -Nprim1[2];
968 Tprim1 *= (sv[0] - sv[2]);
974 Rprim2[0][0] = (
vpMath::sqr(sv[1]) + sv[0] * sv[2]) / ((sv[0] + sv[2]) * sv[1]);
976 Rprim2[2][2] = Rprim2[0][0];
980 ((sv[0] + sv[2]) * sv[1]);
982 Rprim2[0][2] = -Rprim2[2][0];
986 Tprim2[0] = Nprim2[0];
988 Tprim2[2] = -Nprim2[2];
990 Tprim2 *= (sv[0] - sv[2]);
996 Rprim3[0][0] = (
vpMath::sqr(sv[1]) + sv[0] * sv[2]) / ((sv[0] + sv[2]) * sv[1]);
998 Rprim3[2][2] = Rprim3[0][0];
1002 ((sv[0] + sv[2]) * sv[1]);
1004 Rprim3[0][2] = -Rprim3[2][0];
1008 Tprim3[0] = Nprim3[0];
1010 Tprim3[2] = -Nprim3[2];
1012 Tprim3 *= (sv[0] - sv[2]);
1018 Rprim4[0][0] = (
vpMath::sqr(sv[1]) + sv[0] * sv[2]) / ((sv[0] + sv[2]) * sv[1]);
1020 Rprim4[2][2] = Rprim4[0][0];
1024 ((sv[0] + sv[2]) * sv[1]);
1026 Rprim4[0][2] = -Rprim4[2][0];
1030 Tprim4[0] = Nprim4[0];
1032 Tprim4[2] = -Nprim4[2];
1034 Tprim4 *= (sv[0] - sv[2]);
1045 Tprim1 *= (sv[2] - sv[0]);
1052 Tprim2 *= (sv[2] - sv[0]);
1060 Tprim1 *= (sv[0] - sv[1]);
1067 Tprim2 *= (sv[0] - sv[1]);
1077 if (distanceFictive < 0) {
1083 Rprim1[0][0] = (sv[0] * sv[2] -
vpMath::sqr(sv[1])) / ((sv[0] - sv[2]) * sv[1]);
1085 Rprim1[2][2] = -Rprim1[0][0];
1089 ((sv[0] - sv[2]) * sv[1]);
1091 Rprim1[0][2] = Rprim1[2][0];
1093 Rprim1[1][1] = -1.0;
1095 Tprim1[0] = Nprim1[0];
1097 Tprim1[2] = Nprim1[2];
1099 Tprim1 *= (sv[0] + sv[2]);
1105 Rprim2[0][0] = (sv[0] * sv[2] -
vpMath::sqr(sv[1])) / ((sv[0] - sv[2]) * sv[1]);
1107 Rprim2[2][2] = -Rprim2[0][0];
1111 ((sv[0] - sv[2]) * sv[1]);
1113 Rprim2[0][2] = Rprim2[2][0];
1115 Rprim2[1][1] = -1.0;
1117 Tprim2[0] = Nprim2[0];
1119 Tprim2[2] = Nprim2[2];
1121 Tprim2 *= (sv[0] + sv[2]);
1127 Rprim3[0][0] = (sv[0] * sv[2] -
vpMath::sqr(sv[1])) / ((sv[0] - sv[2]) * sv[1]);
1129 Rprim3[2][2] = -Rprim3[0][0];
1133 ((sv[0] - sv[2]) * sv[1]);
1135 Rprim3[0][2] = Rprim3[2][0];
1137 Rprim3[1][1] = -1.0;
1139 Tprim3[0] = Nprim3[0];
1141 Tprim3[2] = Nprim3[2];
1143 Tprim3 *= (sv[0] + sv[2]);
1148 Rprim4[0][0] = (sv[0] * sv[2] -
vpMath::sqr(sv[1])) / ((sv[0] - sv[2]) * sv[1]);
1150 Rprim4[2][2] = -Rprim4[0][0];
1154 ((sv[0] - sv[2]) * sv[1]);
1156 Rprim4[0][2] = Rprim4[2][0];
1158 Rprim4[1][1] = -1.0;
1160 Tprim4[0] = Nprim4[0];
1162 Tprim4[2] = Nprim4[2];
1164 Tprim4 *= (sv[0] + sv[2]);
1176 Tprim1 *= (sv[2] + sv[0]);
1185 Tprim2 *= (sv[2] + sv[0]);
1195 Tprim1 *= (sv[2] + sv[0]);
1204 Tprim2 *= (sv[0] + sv[2]);
1214 if ((distanceFictive > 0) || (cas != cas4)) {
1218 R1 = s * U * Rprim1 * V.
t();
1220 T1 /= (distanceFictive * s);
1229 R2 = s * U * Rprim2 * V.
t();
1231 T2 /= (distanceFictive * s);
1240 R3 = s * U * Rprim3 * V.
t();
1242 T3 /= (distanceFictive * s);
1250 R4 = s * U * Rprim4 * V.
t();
1252 T4 /= (distanceFictive * s);
1261 std::cout <<
"On tombe dans le cas particulier ou le mouvement n'est pas " 1282 #ifdef DEBUG_Homographie 1283 printf(
"fin : Homographie_EstimationDeplacementCamera\n");
1286 #endif //#ifndef DOXYGEN_SHOULD_SKIP_THIS void svd(vpColVector &w, vpMatrix &V)
Implementation of a matrix and operations on matrices.
vpRotationMatrix t() const
void computeDisplacement(vpRotationMatrix &aRb, vpTranslationVector &atb, vpColVector &n)
bool isARotationMatrix() const
Implementation of a rotation matrix and operations on such kind of matrices.
Implementation of an homography and operations on homographies.
static double sqr(double x)
double det(vpDetMethod method=LU_DECOMPOSITION) const
void resize(unsigned int i, bool flagNullify=true)
Implementation of column vector and the associated operations.
Class that consider the case of a translation vector.