StraightenPanorama.cpp

Go to the documentation of this file.
00001 // -*- c-basic-offset: 4 -*-
00026 #include "StraightenPanorama.h"
00027 
00028 #include <hugin_math/eig_jacobi.h>
00029 
00030 namespace HuginBase {
00031 
00032 Matrix3 StraightenPanorama::calcStraighteningRotation(const PanoramaData& panorama)
00033 {
00034     // landscape/non rotated portrait detection is not working correctly
00035     // should use the exif rotation tag but thats not stored anywhere currently...
00036     // 1: use y axis (image x axis), for normal image
00037     // 0: use z axis (image y axis), for non rotated portrait images
00038     //    (usually rotation is just stored in EXIF tag)
00039     std::vector<int> coord_idx;
00040 
00041     for (unsigned int i = 0; i < panorama.getNrOfImages(); i++) {
00042         SrcPanoImage img = panorama.getSrcImage(i);
00043         if (img.getExifMake().empty() || img.getExifModel().empty())
00044         {
00045             img.readEXIF();
00046         };
00047         double roll = img.getExifOrientation();
00048         if (roll == 90 || roll == 270 ) {
00049             coord_idx.push_back(2);
00050         } else {
00051             coord_idx.push_back(1);
00052         }
00053     }
00054 
00055     // build covariance matrix of X
00056     Matrix3 cov;
00057     unsigned int nrOfVariableImages=0;
00058 
00059     for (unsigned int i = 0; i < panorama.getNrOfImages(); i++) 
00060     {
00061         const SrcPanoImage & img=panorama.getImage(i);
00062         if(img.YawisLinked())
00063         {
00064             //only consider images which are not linked with the previous ones
00065             bool consider=true;
00066             for(unsigned int j=0; j<i; j++)
00067             {
00068                 if(img.YawisLinkedWith(panorama.getImage(j)))
00069                 {
00070                     consider=false;
00071                     break;
00072                 };
00073             };
00074             if(!consider)
00075                 continue;
00076         };
00077         double y = const_map_get(panorama.getImageVariables(i), "y").getValue();
00078         double p = const_map_get(panorama.getImageVariables(i), "p").getValue();
00079         double r = const_map_get(panorama.getImageVariables(i), "r").getValue();
00080         Matrix3 mat;
00081         mat.SetRotationPT(DEG_TO_RAD(y), DEG_TO_RAD(p), DEG_TO_RAD(r));
00082         nrOfVariableImages++;
00083         DEBUG_DEBUG("mat = " << mat);
00084         for (int j=0; j<3; j++) {
00085             for (int k=0; k<3; k++) {
00086                 cov.m[j][k] += mat.m[j][coord_idx[i]] * mat.m[k][coord_idx[i]];
00087             }
00088         }
00089     }
00090     cov /= nrOfVariableImages;
00091     DEBUG_DEBUG("cov = " << cov);
00092 
00093     // calculate eigenvalues and vectors
00094     Matrix3 eigvectors;
00095     double eigval[3];
00096     int eigvalIdx[3];
00097     int maxsweep = 100;
00098     int maxannil = 0;
00099     double eps = 1e-16;
00100     
00101     hugin_utils::eig_jacobi(3, cov.m, eigvectors.m, eigval, eigvalIdx, &maxsweep, &maxannil, &eps);
00102     
00103     DEBUG_DEBUG("Eigenvectors & eigenvalues:" << std::endl
00104                 << "V = " << eigvectors << std::endl
00105                 << "D = [" << eigval[0] << ", " << eigval[1] << ", " << eigval[2] << " ]"
00106                 << "idx = [" << eigvalIdx[0] << ", " << eigvalIdx[1] << ", " << eigvalIdx[2] << " ]");
00107     
00108     // get up vector, eigenvector with smallest eigenvalue
00109     Vector3 up;
00110     up.x = eigvectors.m[eigvalIdx[2]][0];
00111     up.y = eigvectors.m[eigvalIdx[2]][1];
00112     up.z = eigvectors.m[eigvalIdx[2]][2];
00113     
00114     // normalize vector
00115     up.Normalize();
00116     DEBUG_DEBUG("Up vector: up = " << up );
00117     
00118     double rotAngle = acos(up.Dot(Vector3(0,0,1)));
00119     if (rotAngle > M_PI/2) {
00120         // turn in shorter direction
00121         up *= -1;
00122         rotAngle = acos(up.Dot(Vector3(0,0,1)));
00123     }
00124     DEBUG_DEBUG("rotation Angle: " << rotAngle);
00125     
00126     // get rotation axis
00127     Vector3 rotAxis = up.Cross(Vector3(0,0,1));
00128     DEBUG_DEBUG("rotAxis = " << rotAngle);
00129     
00130     // calculate rotation matrix
00131     Matrix3 rotMat = GetRotationAroundU(rotAxis, -rotAngle);
00132     DEBUG_DEBUG("rotMat = " << rotMat);
00133 
00134     return rotMat;
00135 }
00136 
00137 } // namespace

Generated on 2 Aug 2015 for Hugintrunk by  doxygen 1.4.7