tsaimethods.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2008 by Luca Baronti                                    *
00003  *   lbaronti@gmail.com                                                    *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 /***************************************************************************
00022   History
00023 Modified by sottile on July 2009
00024 ****************************************************************************/
00025 
00026 /****************************************************************************
00027 TSAI METHODS
00028   Interface class between the tsai.lib and the project structure.
00029   It is sufficient calling <alignImage> with correct parameters, to
00030   get a calibrated shot.
00031   -
00032   Comment:
00033   Sometimes the Tsai Lib returns a irregular shifted translation values.
00034   Since now no solution found.
00035 *****************************************************************************/
00036 extern "C" {
00037 #include <../meshlab/src/external/tsai-30b3/cal_main.h>
00038 }
00039 
00040 #include "tsaimethods.h"                //base header
00041 
00042 #include <vcg/math/camera.h>
00043 
00044 /*********************************************
00045 ...
00046 *********************************************/
00047 bool TsaiMethods::calibrate( vcg::Shot<double>* shot,std::list<TsaiCorrelation>* corr, bool p_foc)
00048 {
00049   bool my_ret_val=false;
00050 
00051   if(corr->size() >= MIN_POINTS_FOR_CALIBRATE){
00052 
00053     //initialize_photometrics_parms ();
00054     Shot2Tsai(shot);
00055 
00056     if(createDataSet(corr,shot))
00057     {
00058       if (p_foc)
00059         //noncoplanar_calibration();
00060         noncoplanar_calibration_with_full_optimization ();
00061       else
00062         noncoplanar_extrinsic_parameter_estimation ();
00063 
00064       Tsai2Shot(shot);
00065       my_ret_val = true;
00066     }
00067     else my_ret_val = false;
00068   }
00069   return my_ret_val;
00070 }
00071 
00072 /*********************************************
00073 CREATE DATA SET
00074 *********************************************/
00075 //TOGLIERE SHOT DAI PARAMETRI!
00076 bool TsaiMethods::createDataSet(std::list<TsaiCorrelation>* corr,vcg::Shot<double>* s)
00077 {
00078   bool my_ret_val=false;
00079 
00080   vcg::Point3d *p1;
00081   vcg::Point2d *p2;
00082         int count=0;
00083 
00084         std::list<TsaiCorrelation>::iterator it_c;
00085   TsaiCorrelation* c;
00086 
00087         double ratio = s->Intrinsics.ViewportPx.X()/(double) s->Intrinsics.ViewportPx.Y();
00088 
00089         for ( it_c= corr->begin() ; it_c !=corr->end(); it_c++ ){
00090                 c=&*it_c;
00091                 p1=&(c->point3d);
00092     p2=&(c->point2d);
00093 
00094     if(p1!=NULL && p2!=NULL)
00095     {
00096                         cd.xw[count] = p1->X();
00097                         cd.yw[count] = p1->Y();
00098                         cd.zw[count] = p1->Z();
00099 //                        cd.Xf[count] = (p2->X()+1)/2.0 * cp.Cx*2.0;
00100 //                        cd.Yf[count] = ((-p2->Y())+1)/2.0 * cp.Cy*2.0;
00101 
00102                         cd.Xf[count] = ((p2->X()/ratio) +1)/2.0 * cp.Cx*2.0;
00103                         cd.Yf[count] = ((-p2->Y())+1)/2.0 * cp.Cy*2.0;
00104       count++;
00105     }
00106     if(count>=MAX_POINTS) break;
00107 
00108   }//all corrs
00109         assert(count==corr->size());
00110         cd.point_count = count;
00111         //qDebug("all points: %i",cd.point_count);
00112         if(count>0 && count<MAX_POINTS && count>10) my_ret_val = true;
00113 
00114         return my_ret_val;
00115 }
00116 
00117 
00118 /*********************************************
00119 SHOT 2 TSAI
00120 Transformation of the camera data between tsai structure and vcg structure
00121 *********************************************/
00122 void TsaiMethods::Shot2Tsai(vcg::Shot<double>* shot){
00123   vcg::Matrix44d mat = shot->Extrinsics.Rot();
00124 
00125   vcg::Point3d view_p = shot->Extrinsics.Tra();
00126 
00127   cc.r1 = mat[0][0];  cc.r2 = mat[0][1]; cc.r3 = mat[0][2];
00128   cc.r4 = -mat[1][0];  cc.r5 = -mat[1][1]; cc.r6 = -mat[1][2];
00129   cc.r7 = -mat[2][0];  cc.r8 = -mat[2][1]; cc.r9 = -mat[2][2];
00130 
00131 
00132   vcg::Point3<vcg::Shot<double>::ScalarType> tl;//(-cc.Tx,cc.Ty,cc.Tz);
00133   tl = mat* shot->Extrinsics.Tra();
00134   cc.Tx = -tl[0];
00135   cc.Ty = tl[1];
00136   cc.Tz = tl[2];
00137 
00138   //cc.Tx = view_p.X();
00139   //cc.Ty = view_p.Y();
00140   //cc.Tz = view_p.Z();
00141 
00142   Cam2Tsai(shot);
00143 }
00144 
00145 /*********************************************
00146 TSAI 2 SHOT
00147 Transformation of the camera data between tsai structure and vcg structure
00148 *********************************************/
00149 void TsaiMethods::Tsai2Shot(vcg::Shot<double>* shot, bool p_foc ) {
00150 
00151   if(p_foc)
00152     shot->Intrinsics.FocalMm =  cc.f;//*cp.sx;// *SCALE_FACTOR;
00153   /* old ones
00154   shot->Intrinsics.DistorCenterPx[0] = cc.p1;
00155   shot->Intrinsics.DistorCenterPx[1] = cc.p2;
00156 
00157   shot->Intrinsics.DistorCenterPx[0] = shot->Intrinsics.CenterPx.X()+(cc.p1/shot->Intrinsics.PixelSizeMm.X());
00158   shot->Intrinsics.DistorCenterPx[1] = shot->Intrinsics.CenterPx.Y()+(cc.p2/shot->Intrinsics.PixelSizeMm.Y());
00159   */
00160   shot->Intrinsics.DistorCenterPx[0] = cp.Cx;
00161   shot->Intrinsics.DistorCenterPx[1] = cp.Cy;
00162 
00163   shot->Intrinsics.k[0]=cc.kappa1;
00164 
00165   /* ROTATION */
00166   vcg::Matrix44<vcg::Shot<double>::ScalarType> mat;
00167   vcg::Matrix44<vcg::Shot<double>::ScalarType> s_mat=shot->Extrinsics.Rot();
00168   mat.SetIdentity();
00169   mat[0][0]=cc.r1;      mat[0][1]=cc.r2;        mat[0][2]=cc.r3;
00170   mat[1][0]=-cc.r4;     mat[1][1]=-cc.r5;       mat[1][2]=-cc.r6;
00171   mat[2][0]=-cc.r7;     mat[2][1]=-cc.r8;       mat[2][2]=-cc.r9;
00172 
00173   shot->Extrinsics.SetRot(mat);
00174 
00175   /* TRANSLATION */
00176   vcg::Point3d tl = shot->Extrinsics.Tra();
00177 
00178   tl = vcg::Inverse(shot->Extrinsics.Rot())* vcg::Point3d(-cc.Tx,cc.Ty,cc.Tz);
00179 
00180         shot->Extrinsics.SetTra(tl);
00181 }
00182 
00183 void TsaiMethods::Cam2Tsai(vcg::Shot<double> *s){
00184 
00185   cp.Ncx = s->Intrinsics.ViewportPx.X();        // [sel]     Number of sensor elements in camera's x direction //
00186   cp.Nfx = s->Intrinsics.ViewportPx.X();        // [pix]     Number of pixels in frame grabber's x direction   //
00187   cp.dx  = s->Intrinsics.PixelSizeMm.X();//*SCALE_FACTOR;       // [mm/sel]  X dimension of camera's sensor element (in mm)    //
00188   cp.dy  = s->Intrinsics.PixelSizeMm.Y();//*SCALE_FACTOR;       // [mm/sel]  Y dimension of camera's sensor element (in mm)    //
00189 
00190   cp.dpx = cp.dx * cp.Nfx/cp.Ncx;       // [mm/pix]  Effective X dimension of pixel in frame grabber   //
00191   cp.dpy = cp.dy;       // [mm/pix]  Effective Y dimension of pixel in frame grabber   //
00192 
00193   cp.Cx  = s->Intrinsics.CenterPx.X();  // [pix]     Z axis intercept of camera coordinate system      //
00194   cp.Cy  = s->Intrinsics.CenterPx.Y();  // [pix]     Z axis intercept of camera coordinate system      //
00195 
00196   cp.sx  = 1.0; // []        Scale factor to compensate for any error in dpx   //
00197 
00198   cc.f = s->Intrinsics.FocalMm;// *SCALE_FACTOR;
00199   cc.kappa1 = s->Intrinsics.k[0];
00200 }


shape_reconstruction
Author(s): Roberto Martín-Martín
autogenerated on Sat Jun 8 2019 18:38:27