From e83db616a94e60609cc4424b4245fdf7d7d1d4d2 Mon Sep 17 00:00:00 2001 From: Paolo Cignoni cignoni Date: Tue, 16 Nov 2010 20:16:43 +0000 Subject: [PATCH] Tried to compute camera rotation and translation taking into account a calibration matrix. It seems a fail. --- .../filter_photosynth/filter_photosynth.cpp | 76 +++++++++++++++++++ src/fgt/filter_photosynth/filter_photosynth.h | 4 + 2 files changed, 80 insertions(+) diff --git a/src/fgt/filter_photosynth/filter_photosynth.cpp b/src/fgt/filter_photosynth/filter_photosynth.cpp index 4aa53af42..1cf92745d 100644 --- a/src/fgt/filter_photosynth/filter_photosynth.cpp +++ b/src/fgt/filter_photosynth/filter_photosynth.cpp @@ -17,6 +17,8 @@ #include #include "jhead/jhead.h" +#define CALIBRATED 1 + using namespace vcg; extern ImageInfo_t ImageInfo; @@ -114,12 +116,15 @@ bool FilterPhotosynthPlugin::applyFilter(QAction */*filter*/, MeshDocument &md, //Hangs on active wait until data are available from the server while(!synthData->_dataReady) + { //allows qt main loop to process the events relative to the response from the server, //triggering the signals that cause the invocation of the slots that process the response //and set the control variable that stops this active wait. //Note that a call to the function usleep() causes an infinite loop, because when the process awakes, //the control remains inside this loop and doesn't reach qt main loop that this way can't process events. QApplication::processEvents(); + cb(synthData->progressInfo(),synthData->_info.toStdString().data()); + } if(!synthData->isValid()) { @@ -178,6 +183,9 @@ bool FilterPhotosynthPlugin::applyFilter(QAction */*filter*/, MeshDocument &md, s.Intrinsics.PixelSizeMm = Point2f(cam._pixelSizeMm,cam._pixelSizeMm); s.Intrinsics.ViewportPx = Point2i(img._exifWidth,img._exifHeight); s.Intrinsics.CenterPx = Point2f(img._exifWidth/2,img._exifHeight/2); +#ifdef CALIBRATED + setShotCalibratedExtrinsics(s,computeProjectionMatrix(img._exifWidth,img._exifHeight,cam)); +#endif if(success) outputToFile(out, s, img, cam); //add a new raster @@ -207,6 +215,74 @@ QString FilterPhotosynthPlugin::filterScriptFunctionName(FilterIDType filterID) return QString(); } +void FilterPhotosynthPlugin::matrixScale(int m, int n, float *A, float s, float *R) +{ + int i; + int entries = m * n; + + for (i = 0; i < entries; i++) + { + R[i] = A[i] * s; + } +} + +void FilterPhotosynthPlugin::matrixProduct(int Am, int An, int Bm, int Bn, const float *A, const float *B, float *R) +{ + int r = Am; + int c = Bn; + int m = An; + + int i, j, k; + for (i = 0; i < r; i++) + { + for (j = 0; j < c; j++) + { + R[i * c + j] = 0.0; + for (k = 0; k < m; k++) + { + R[i * c + j] += A[i * An + k] * B[k * Bn + j]; + } + } + } +} + +float *FilterPhotosynthPlugin::computeProjectionMatrix(int width, int height, CameraParameters &cam) +{ + float focal = cam[CameraParameters::FOCAL_LENGTH] * qMax(width,height); + float R[9]; + float t[3]; + Matrix44f rot = cam.getRotation(); + Point3f tra = cam.getTranslation(); + for(int row = 0; row < 3; ++row) + for(int col = 0; col < 3; ++col) + R[row * 3 + col] = rot.ElementAt(row,col); + for(int i = 0; i < 3; ++i) + t[i] = tra[i]; + + float K[9] = { -focal, 0.0, 0.5 * width - 0.5, + 0.0, focal, 0.5 * height - 0.5, + 0.0, 0.0, 1.0 }; + float Ptmp[12] = { R[0], R[1], R[2], t[0], + R[3], R[4], R[5], t[1], + R[6], R[7], R[8], t[2] }; + float *P = (float*)calloc(12,sizeof(float)); + matrixProduct(3, 3, 3, 4, K, Ptmp, P); + matrixScale(3, 4, P, -1.0, P); + return P; +} + +void FilterPhotosynthPlugin::setShotCalibratedExtrinsics(Shotf &shot, float *projectionMatrix) +{ + float r[] = {projectionMatrix[0], projectionMatrix[1], projectionMatrix[2], 0, + projectionMatrix[4], projectionMatrix[5], projectionMatrix[6], 0, + projectionMatrix[8], projectionMatrix[9], projectionMatrix[10], 0, + 0, 0, 0, 1}; + Matrix44f rot(r); + shot.Extrinsics.SetRot(rot); + Point3f tra(projectionMatrix[3],projectionMatrix[7],projectionMatrix[11]); + shot.Extrinsics.SetTra(tra); +} + void FilterPhotosynthPlugin::readExifData(Image &img, CameraParameters &cam, QDir &root) { const char *FileName = root.filePath("IMG_%1.jpg").arg(img._ID).toStdString().data(); diff --git a/src/fgt/filter_photosynth/filter_photosynth.h b/src/fgt/filter_photosynth/filter_photosynth.h index e94f1d23f..414fac923 100644 --- a/src/fgt/filter_photosynth/filter_photosynth.h +++ b/src/fgt/filter_photosynth/filter_photosynth.h @@ -39,6 +39,10 @@ public: QString filterScriptFunctionName(FilterIDType filterID); void readExifData(Image &img, CameraParameters &cam, QDir &root); void outputToFile(QTextStream &out, Shotf &s, Image &img, CameraParameters &cam); + void matrixScale(int m, int n, float *A, float s, float *R); + void matrixProduct(int Am, int An, int Bm, int Bn, const float *A, const float *B, float *R); + float *computeProjectionMatrix(int width, int height, CameraParameters &cam); + void setShotCalibratedExtrinsics(Shotf &shot, float *projectionMatrix); }; #endif /* FILTER_PHOTOSYNTH_PLUGIN_H */