35 #include <visp3/core/vpMath.h>
36 #include <visp3/vision/vpHomography.h>
45 const double vpHomography::m_sing_threshold = 0.0001;
64 #ifndef DOXYGEN_SHOULD_SKIP_THIS
97 double distanceFictive;
98 double sinusTheta, cosinusTheta, signeSinus = 1;
99 double s, determinantU, determinantV;
100 unsigned int vOrdre[3];
105 #ifdef DEBUG_Homographie
106 printf(
"debut : Homographie_EstimationDeplacementCamera\n");
117 const unsigned int val_3 = 3;
118 for (
unsigned int i = 0; i < val_3; ++i) {
119 for (
unsigned int j = 0; j < val_3; ++j) {
120 mH[i][j] = aHb[i][j];
132 mTempU.svd(svTemp, mTempV);
137 if (svTemp[0] >= svTemp[1]) {
138 if (svTemp[0] >= svTemp[2]) {
139 if (svTemp[1] > svTemp[2]) {
157 if (svTemp[1] >= svTemp[2]) {
158 if (svTemp[0] > svTemp[2]) {
181 for (
unsigned int i = 0; i < val_3; ++i) {
182 sv[i] = svTemp[vOrdre[i]];
183 for (
unsigned int j = 0; j < val_3; ++j) {
184 mU[i][j] = mTempU[i][vOrdre[j]];
185 mV[i][j] = mTempV[i][vOrdre[j]];
189 #ifdef DEBUG_Homographie
191 std::cout << mU << std::endl;
193 std::cout << mV << std::endl;
194 printf(
"Valeurs singulieres : ");
199 determinantV = mV.det();
200 determinantU = mU.det();
202 s = determinantU * determinantV;
204 #ifdef DEBUG_Homographie
205 printf(
"s = det(U) * det(V) = %f * %f = %f\n", determinantU, determinantV, s);
212 distanceFictive = sv[1];
213 #ifdef DEBUG_Homographie
214 printf(
"d = %f\n", distanceFictive);
218 if (((sv[0] - sv[1]) < m_sing_threshold) && ((sv[0] - sv[2]) < m_sing_threshold)) {
228 n[0] = normaleDesiree[0];
229 n[1] = normaleDesiree[1];
230 n[2] = normaleDesiree[2];
233 #ifdef DEBUG_Homographie
234 printf(
"\nCas general\n");
248 mX[0][0] = sqrt(((sv[0] * sv[0]) - (sv[1] * sv[1])) / ((sv[0] * sv[0]) - (sv[2] * sv[2])));
250 mX[2][0] = sqrt(((sv[1] * sv[1]) - (sv[2] * sv[2])) / ((sv[0] * sv[0]) - (sv[2] * sv[2])));
252 mX[0][1] = -mX[0][0];
257 double cosinusAncien = 0.0;
258 for (
unsigned int w = 0; w < 2; ++w) {
259 for (
unsigned int k = 0; k < 2; ++k) {
262 for (
unsigned int i = 0; i < val_3; ++i) {
263 normaleEstimee[i] = 0.0;
264 for (
unsigned int j = 0; j < val_3; ++j) {
265 normaleEstimee[i] += ((2.0 * k) - 1.0) * mV[i][j] * mX[j][w];
270 double cosinusDesireeEstimee = 0.0;
271 for (
unsigned int i = 0; i < val_3; ++i) {
272 cosinusDesireeEstimee += normaleEstimee[i] * normaleDesiree[i];
279 if (cosinusDesireeEstimee > cosinusAncien) {
280 cosinusAncien = cosinusDesireeEstimee;
283 for (
unsigned int j = 0; j < val_3; ++j) {
284 n[j] = normaleEstimee[j];
288 aTbp[0] = ((2.0 * k) - 1.0) * (sv[0] - sv[2]) * mX[0][w];
289 aTbp[1] = ((2.0 * k) - 1.0) * (sv[0] - sv[2]) * mX[1][w];
290 aTbp[2] = -((2.0 * k) - 1.0) * (sv[0] - sv[2]) * mX[2][w];
305 for (
unsigned int i = 0; i < val_3; ++i) {
307 for (
unsigned int j = 0; j < val_3; ++j) {
308 atb[i] += mU[i][j] * aTbp[j];
310 atb[i] /= distanceFictive;
313 #ifdef DEBUG_Homographie
315 std::cout << aTbp.t();
317 std::cout << atb.
t();
330 (signeSinus * sqrt(((sv[0] * sv[0]) - (sv[1] * sv[1])) * ((sv[1] * sv[1]) - (sv[2] * sv[2])))) / ((sv[0] + sv[2]) * sv[1]);
332 cosinusTheta = ((sv[1] * sv[1]) + (sv[0] * sv[2])) / ((sv[0] + sv[2]) * sv[1]);
335 aRbp[0][0] = cosinusTheta;
337 aRbp[0][2] = -sinusTheta;
341 aRbp[2][0] = sinusTheta;
343 aRbp[2][2] = cosinusTheta;
346 for (
unsigned int i = 0; i < val_3; ++i) {
347 for (
unsigned int j = 0; j < val_3; ++j) {
349 for (
unsigned int k = 0; k < val_3; ++k) {
350 aRbint[i][j] += mU[i][k] * aRbp[k][j];
356 for (
unsigned int i = 0; i < val_3; ++i) {
357 for (
unsigned int j = 0; j < val_3; ++j) {
359 for (
unsigned int k = 0; k < val_3; ++k) {
360 aRb[i][j] += aRbint[i][k] * mV[j][k];
365 #ifdef DEBUG_Homographie
367 std::cout << aRb << std::endl;
398 double distanceFictive;
399 double sinusTheta, cosinusTheta, signeSinus = 1;
400 double s, determinantU, determinantV;
401 unsigned int vOrdre[3];
404 normaleDesiree[0] = 0;
405 normaleDesiree[1] = 0;
406 normaleDesiree[2] = 1;
409 #ifdef DEBUG_Homographie
410 printf(
"debut : Homographie_EstimationDeplacementCamera\n");
421 const unsigned int val_3 = 3;
422 for (
unsigned int i = 0; i < val_3; ++i) {
423 for (
unsigned int j = 0; j < val_3; ++j) {
424 mH[i][j] = aHb[i][j];
436 mTempU.svd(svTemp, mTempV);
441 if (svTemp[0] >= svTemp[1]) {
442 if (svTemp[0] >= svTemp[2]) {
443 if (svTemp[1] > svTemp[2]) {
461 if (svTemp[1] >= svTemp[2]) {
462 if (svTemp[0] > svTemp[2]) {
485 for (
unsigned int i = 0; i < val_3; ++i) {
486 sv[i] = svTemp[vOrdre[i]];
487 for (
unsigned int j = 0; j < val_3; ++j) {
488 mU[i][j] = mTempU[i][vOrdre[j]];
489 mV[i][j] = mTempV[i][vOrdre[j]];
493 #ifdef DEBUG_Homographie
495 std::cout << mU << std::endl;
497 std::cout << mV << std::endl;
498 printf(
"Valeurs singulieres : ");
503 determinantV = mV.det();
504 determinantU = mU.det();
506 s = determinantU * determinantV;
508 #ifdef DEBUG_Homographie
509 printf(
"s = det(U) * det(V) = %f * %f = %f\n", determinantU, determinantV, s);
516 distanceFictive = sv[1];
517 #ifdef DEBUG_Homographie
518 printf(
"d = %f\n", distanceFictive);
522 if (((sv[0] - sv[1]) < m_sing_threshold) && ((sv[0] - sv[2]) < m_sing_threshold)) {
532 n[0] = normaleDesiree[0];
533 n[1] = normaleDesiree[1];
534 n[2] = normaleDesiree[2];
537 #ifdef DEBUG_Homographie
538 printf(
"\nCas general\n");
552 mX[0][0] = sqrt(((sv[0] * sv[0]) - (sv[1] * sv[1])) / ((sv[0] * sv[0]) - (sv[2] * sv[2])));
554 mX[2][0] = sqrt(((sv[1] * sv[1]) - (sv[2] * sv[2])) / ((sv[0] * sv[0]) - (sv[2] * sv[2])));
556 mX[0][1] = -mX[0][0];
561 double cosinusAncien = 0.0;
562 for (
unsigned int w = 0; w < 2; ++w) {
563 for (
unsigned int k = 0; k < 2; ++k) {
566 for (
unsigned int i = 0; i < val_3; ++i) {
567 normaleEstimee[i] = 0.0;
568 for (
unsigned int j = 0; j < val_3; ++j) {
569 normaleEstimee[i] += ((2.0 * k)- 1.0) * mV[i][j] * mX[j][w];
574 double cosinusDesireeEstimee = 0.0;
575 for (
unsigned int i = 0; i < val_3; ++i) {
576 cosinusDesireeEstimee += normaleEstimee[i] * normaleDesiree[i];
583 if (cosinusDesireeEstimee > cosinusAncien) {
584 cosinusAncien = cosinusDesireeEstimee;
587 for (
unsigned int j = 0; j < val_3; ++j) {
588 n[j] = normaleEstimee[j];
592 aTbp[0] = ((2.0 * k) - 1.0) * (sv[0] - sv[2]) * mX[0][w];
593 aTbp[1] = ((2.0 * k) - 1.0) * (sv[0] - sv[2]) * mX[1][w];
594 aTbp[2] = -((2.0 * k) - 1.0) * (sv[0] - sv[2]) * mX[2][w];
609 for (
unsigned int i = 0; i < val_3; ++i) {
611 for (
unsigned int j = 0; j < val_3; ++j) {
612 atb[i] += mU[i][j] * aTbp[j];
614 atb[i] /= distanceFictive;
617 #ifdef DEBUG_Homographie
619 std::cout << aTbp.t();
621 std::cout << atb.
t();
634 (signeSinus * sqrt(((sv[0] * sv[0]) - (sv[1] * sv[1])) * ((sv[1] * sv[1]) - (sv[2] * sv[2])))) / ((sv[0] + sv[2]) * sv[1]);
636 cosinusTheta = ((sv[1] * sv[1]) + (sv[0] * sv[2])) / ((sv[0] + sv[2]) * sv[1]);
639 aRbp[0][0] = cosinusTheta;
641 aRbp[0][2] = -sinusTheta;
645 aRbp[2][0] = sinusTheta;
647 aRbp[2][2] = cosinusTheta;
650 for (
unsigned int i = 0; i < val_3; ++i) {
651 for (
unsigned int j = 0; j < val_3; ++j) {
653 for (
unsigned int k = 0; k < val_3; ++k) {
654 aRbint[i][j] += mU[i][k] * aRbp[k][j];
660 for (
unsigned int i = 0; i < val_3; ++i) {
661 for (
unsigned int j = 0; j < val_3; ++j) {
663 for (
unsigned int k = 0; k < val_3; ++k) {
664 aRb[i][j] += aRbint[i][k] * mV[j][k];
669 #ifdef DEBUG_Homographie
671 std::cout << aRb << std::endl;
676 std::list<vpTranslationVector> &vT, std::list<vpColVector> &vN)
679 #ifdef DEBUG_Homographie
680 printf(
"debut : Homographie_EstimationDeplacementCamera\n");
688 int cas1 = 1, cas2 = 2, cas3 = 3, cas4 = 4;
690 bool norm1ok =
false, norm2ok =
false, norm3ok =
false, norm4ok =
false;
692 double tmp, determinantU, determinantV, s, distanceFictive;
693 const unsigned int val_3 = 3;
694 vpMatrix mTempU(3, 3), mTempV(3, 3), U(3, 3), V(3, 3);
710 unsigned int vOrdre[3];
723 mTempU.
svd(svTemp, mTempV);
727 if (svTemp[0] >= svTemp[1]) {
728 if (svTemp[0] >= svTemp[2]) {
729 if (svTemp[1] > svTemp[2]) {
747 if (svTemp[1] >= svTemp[2]) {
748 if (svTemp[0] > svTemp[2]) {
770 for (
unsigned int i = 0; i < val_3; ++i) {
771 sv[i] = svTemp[vOrdre[i]];
772 for (
unsigned int j = 0; j < val_3; ++j) {
773 U[i][j] = mTempU[i][vOrdre[j]];
774 V[i][j] = mTempV[i][vOrdre[j]];
778 #ifdef DEBUG_Homographie
783 printf(
"Valeurs singulieres : ");
788 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]))) +
789 (U[0][2] * ((U[1][0] * U[2][1]) - (U[1][1] * U[2][0])));
791 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]))) +
792 (V[0][2] * ((V[1][0] * V[2][1]) - (V[1][1] * V[2][0])));
794 s = determinantU * determinantV;
796 #ifdef DEBUG_Homographie
797 printf(
"s = det(U) * det(V) = %f * %f = %f\n", determinantU, determinantV, s);
810 tmp = (H[2][0] * x) + (H[2][1] * y) + H[2][2];
812 if ((tmp / (sv[1] * s)) > 0) {
813 distanceFictive = sv[1];
816 distanceFictive = -sv[1];
825 if ((sv[0] - sv[1]) < m_sing_threshold) {
826 if ((sv[1] - sv[2]) < m_sing_threshold) {
834 if ((sv[1] - sv[2]) < m_sing_threshold) {
863 Nprim1[0] = sqrt(((sv[0] * sv[0]) - (sv[1] * sv[1])) / ((sv[0] * sv[0]) - (sv[2] * sv[2])));
865 Nprim1[2] = sqrt(((sv[1] * sv[1]) - (sv[2] * sv[2])) / ((sv[0] * sv[0]) - (sv[2] * sv[2])));
867 Nprim2[0] = Nprim1[0];
868 Nprim2[2] = -Nprim1[2];
869 Nprim3[0] = -Nprim1[0];
870 Nprim3[2] = Nprim1[2];
871 Nprim4[0] = -Nprim1[0];
872 Nprim4[2] = -Nprim1[2];
897 tmp = (N1[0] * x) + (N1[1] * y) + N1[2];
898 tmp /= (distanceFictive * s);
902 tmp = (N2[0] * x) + (N2[1] * y) + N2[2];
903 tmp /= (distanceFictive * s);
907 tmp = (N3[0] * x) + (N3[1] * y) + N3[2];
908 tmp /= (distanceFictive * s);
912 tmp = (N4[0] * x) + (N4[1] * y) + N4[2];
913 tmp /= (distanceFictive * s);
919 tmp = (N1[0] * x) + (N1[1] * y) + N1[2];
920 tmp /= (distanceFictive * s);
924 tmp = (N2[0] * x) + (N2[1] * y) + N2[2];
925 tmp /= (distanceFictive * s);
931 tmp = (N1[0] * x) + (N1[1] * y) + N1[2];
932 tmp /= (distanceFictive * s);
936 tmp = (N2[0] * x) + (N2[1] * y) + N2[2];
937 tmp /= (distanceFictive * s);
945 if (distanceFictive > 0) {
950 Rprim1[0][0] = (
vpMath::sqr(sv[1]) + (sv[0] * sv[2])) / ((sv[0] + sv[2]) * sv[1]);
952 Rprim1[2][2] = Rprim1[0][0];
956 ((sv[0] + sv[2]) * sv[1]);
958 Rprim1[0][2] = -Rprim1[2][0];
962 Tprim1[0] = Nprim1[0];
964 Tprim1[2] = -Nprim1[2];
966 Tprim1 *= (sv[0] - sv[2]);
972 Rprim2[0][0] = (
vpMath::sqr(sv[1]) + (sv[0] * sv[2])) / ((sv[0] + sv[2]) * sv[1]);
974 Rprim2[2][2] = Rprim2[0][0];
978 ((sv[0] + sv[2]) * sv[1]);
980 Rprim2[0][2] = -Rprim2[2][0];
984 Tprim2[0] = Nprim2[0];
986 Tprim2[2] = -Nprim2[2];
988 Tprim2 *= (sv[0] - sv[2]);
994 Rprim3[0][0] = (
vpMath::sqr(sv[1]) + (sv[0] * sv[2])) / ((sv[0] + sv[2]) * sv[1]);
996 Rprim3[2][2] = Rprim3[0][0];
1000 ((sv[0] + sv[2]) * sv[1]);
1002 Rprim3[0][2] = -Rprim3[2][0];
1006 Tprim3[0] = Nprim3[0];
1008 Tprim3[2] = -Nprim3[2];
1010 Tprim3 *= (sv[0] - sv[2]);
1016 Rprim4[0][0] = (
vpMath::sqr(sv[1]) + (sv[0] * sv[2])) / ((sv[0] + sv[2]) * sv[1]);
1018 Rprim4[2][2] = Rprim4[0][0];
1022 ((sv[0] + sv[2]) * sv[1]);
1024 Rprim4[0][2] = -Rprim4[2][0];
1028 Tprim4[0] = Nprim4[0];
1030 Tprim4[2] = -Nprim4[2];
1032 Tprim4 *= (sv[0] - sv[2]);
1043 Tprim1 *= (sv[2] - sv[0]);
1050 Tprim2 *= (sv[2] - sv[0]);
1058 Tprim1 *= (sv[0] - sv[1]);
1065 Tprim2 *= (sv[0] - sv[1]);
1075 if (distanceFictive < 0) {
1081 Rprim1[0][0] = ((sv[0] * sv[2]) -
vpMath::sqr(sv[1])) / ((sv[0] - sv[2]) * sv[1]);
1083 Rprim1[2][2] = -Rprim1[0][0];
1087 ((sv[0] - sv[2]) * sv[1]);
1089 Rprim1[0][2] = Rprim1[2][0];
1091 Rprim1[1][1] = -1.0;
1093 Tprim1[0] = Nprim1[0];
1095 Tprim1[2] = Nprim1[2];
1097 Tprim1 *= (sv[0] + sv[2]);
1103 Rprim2[0][0] = ((sv[0] * sv[2]) -
vpMath::sqr(sv[1])) / ((sv[0] - sv[2]) * sv[1]);
1105 Rprim2[2][2] = -Rprim2[0][0];
1109 ((sv[0] - sv[2]) * sv[1]);
1111 Rprim2[0][2] = Rprim2[2][0];
1113 Rprim2[1][1] = -1.0;
1115 Tprim2[0] = Nprim2[0];
1117 Tprim2[2] = Nprim2[2];
1119 Tprim2 *= (sv[0] + sv[2]);
1125 Rprim3[0][0] = ((sv[0] * sv[2]) -
vpMath::sqr(sv[1])) / ((sv[0] - sv[2]) * sv[1]);
1127 Rprim3[2][2] = -Rprim3[0][0];
1131 ((sv[0] - sv[2]) * sv[1]);
1133 Rprim3[0][2] = Rprim3[2][0];
1135 Rprim3[1][1] = -1.0;
1137 Tprim3[0] = Nprim3[0];
1139 Tprim3[2] = Nprim3[2];
1141 Tprim3 *= (sv[0] + sv[2]);
1146 Rprim4[0][0] = ((sv[0] * sv[2]) -
vpMath::sqr(sv[1])) / ((sv[0] - sv[2]) * sv[1]);
1148 Rprim4[2][2] = -Rprim4[0][0];
1152 ((sv[0] - sv[2]) * sv[1]);
1154 Rprim4[0][2] = Rprim4[2][0];
1156 Rprim4[1][1] = -1.0;
1158 Tprim4[0] = Nprim4[0];
1160 Tprim4[2] = Nprim4[2];
1162 Tprim4 *= (sv[0] + sv[2]);
1174 Tprim1 *= (sv[2] + sv[0]);
1183 Tprim2 *= (sv[2] + sv[0]);
1193 Tprim1 *= (sv[2] + sv[0]);
1202 Tprim2 *= (sv[0] + sv[2]);
1212 if ((distanceFictive > 0) || (cas != cas4)) {
1216 R1 = s * U * Rprim1 * V.
t();
1218 T1 /= (distanceFictive * s);
1227 R2 = s * U * Rprim2 * V.
t();
1229 T2 /= (distanceFictive * s);
1238 R3 = s * U * Rprim3 * V.
t();
1240 T3 /= (distanceFictive * s);
1248 R4 = s * U * Rprim4 * V.
t();
1250 T4 /= (distanceFictive * s);
1260 std::cout <<
"On tombe dans le cas particulier ou le mouvement n'est pas "
1265 #ifdef DEBUG_Homographie
1267 std::cout <<
"Analyse des resultats : "<< std::endl;
1269 std::cout <<
"On est dans le cas 1" << std::endl;
1272 std::cout <<
"On est dans le cas 2" << std::endl;
1275 std::cout <<
"On est dans le cas 3" << std::endl;
1278 std::cout <<
"On est dans le cas 4" << std::endl;
1281 if (distanceFictive < 0) {
1282 std::cout <<
"d'<0" << std::endl;
1285 std::cout <<
"d'>0" << std::endl;
1289 #ifdef DEBUG_Homographie
1290 printf(
"fin : Homographie_EstimationDeplacementCamera\n");
Implementation of column vector and the associated operations.
void resize(unsigned int i, bool flagNullify=true)
Implementation of an homography and operations on homographies.
void computeDisplacement(vpRotationMatrix &aRb, vpTranslationVector &atb, vpColVector &n)
static double sqr(double x)
Implementation of a matrix and operations on matrices.
void svd(vpColVector &w, vpMatrix &V)
Implementation of a rotation matrix and operations on such kind of matrices.
bool isARotationMatrix(double threshold=1e-6) const
vpRotationMatrix t() const
Class that consider the case of a translation vector.