calibrateCamera.C

Go to the documentation of this file.
00001 /*!@file Robots/Tools/calibrateCamera.C  */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00005 // University of Southern California (USC) and the iLab at USC.         //
00006 // See http://iLab.usc.edu for information about this project.          //
00007 // //////////////////////////////////////////////////////////////////// //
00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00010 // in Visual Environments, and Applications'' by Christof Koch and      //
00011 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00012 // pending; application number 09/912,225 filed July 23, 2001; see      //
00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00014 // //////////////////////////////////////////////////////////////////// //
00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00016 //                                                                      //
00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00018 // redistribute it and/or modify it under the terms of the GNU General  //
00019 // Public License as published by the Free Software Foundation; either  //
00020 // version 2 of the License, or (at your option) any later version.     //
00021 //                                                                      //
00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00025 // PURPOSE.  See the GNU General Public License for more details.       //
00026 //                                                                      //
00027 // You should have received a copy of the GNU General Public License    //
00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00030 // Boston, MA 02111-1307 USA.                                           //
00031 // //////////////////////////////////////////////////////////////////// //
00032 //
00033 // Primary maintainer for this file: Lior Elazary <elazary@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Robots/Tools/calibrateCamera.C $
00035 // $Id: calibrateCamera.C 12962 2010-03-06 02:13:53Z irock $
00036 //
00037 
00038 #include "Component/ModelManager.H"
00039 #include "Image/Image.H"
00040 #include "Image/DrawOps.H"
00041 #include "Image/ColorOps.H"
00042 #include "Image/FilterOps.H"
00043 #include "Image/VisualTracker.H"
00044 #include "GUI/ImageDisplayStream.H"
00045 #include "GUI/XWinManaged.H"
00046 #include "Image/MathOps.H"
00047 #include "Image/Transforms.H"
00048 #include "Image/Layout.H"
00049 #include "Media/FrameSeries.H"
00050 #include "Transport/FrameInfo.H"
00051 #include "Raster/GenericFrame.H"
00052 #include "Raster/Raster.H"
00053 #include "GUI/XWinManaged.H"
00054 #include "GUI/DebugWin.H"
00055 #include <pthread.h>
00056 
00057 #define KEY_UP 98
00058 #define KEY_DOWN 104
00059 #define KEY_LEFT 100
00060 #define KEY_RIGHT 102
00061 
00062 //Camera params
00063 CvMat* itsIntrinsicMatrix;
00064 CvMat* itsDistortionCoeffs;
00065 CvMat* itsCameraRotation;
00066 CvMat* itsCameraTranslation;
00067 
00068 //GRID param
00069 #define GRID_SIZE 51.5
00070 
00071 int getKey(nub::ref<OutputFrameSeries> &ofs)
00072 {
00073   const nub::soft_ref<ImageDisplayStream> ids =
00074     ofs->findFrameDestType<ImageDisplayStream>();
00075 
00076   const rutz::shared_ptr<XWinManaged> uiwin =
00077     ids.is_valid()
00078     ? ids->getWindow("Output")
00079     : rutz::shared_ptr<XWinManaged>();
00080 
00081   if (uiwin.is_valid())
00082     return uiwin->getLastKeyPress();
00083   else
00084     return -1;
00085 }
00086 
00087 Point2D<int> getMouseClick(nub::ref<OutputFrameSeries> &ofs, const char* wname)
00088 {
00089   const nub::soft_ref<ImageDisplayStream> ids =
00090     ofs->findFrameDestType<ImageDisplayStream>();
00091 
00092   const rutz::shared_ptr<XWinManaged> uiwin =
00093     ids.is_valid()
00094     ? ids->getWindow(wname)
00095     : rutz::shared_ptr<XWinManaged>();
00096 
00097   if (uiwin.is_valid())
00098     return uiwin->getLastMouseClick();
00099   else
00100     return Point2D<int>(-1,-1);
00101 }
00102 
00103 void projectGrid(Image<PixRGB<byte> > &img)
00104 {
00105 
00106   //draw center point
00107   drawCircle(img, Point2D<int>(img.getWidth()/2, img.getHeight()/2), 3, PixRGB<byte>(255,0,0));
00108 
00109   CvMat *rot_mat = cvCreateMat( 3, 3, CV_64FC1);
00110   cvRodrigues2( itsCameraRotation, rot_mat, 0);
00111 
00112   int  NUM_GRID         = 9; //21
00113   CvMat *my_3d_point = cvCreateMat( 3, NUM_GRID * NUM_GRID + 2, CV_64FC1);
00114         CvMat *my_image_point = cvCreateMat( 2, NUM_GRID * NUM_GRID + 2, CV_64FC1);
00115 
00116   for ( int i = 0; i < NUM_GRID; i++){
00117                 for ( int j = 0; j < NUM_GRID; j++){
00118                         cvSetReal2D( my_3d_point, 0, i * NUM_GRID + j,(i * GRID_SIZE));
00119                         cvSetReal2D( my_3d_point, 1, i * NUM_GRID + j,(j * GRID_SIZE));
00120                         cvSetReal2D( my_3d_point, 2, i * NUM_GRID + j, 0.0);
00121                 }
00122         }
00123 
00124   cvSetReal2D( my_3d_point, 0, NUM_GRID*NUM_GRID, 0);
00125         cvSetReal2D( my_3d_point, 1, NUM_GRID*NUM_GRID, 0);
00126         cvSetReal2D( my_3d_point, 2, NUM_GRID*NUM_GRID, 0);
00127 
00128   cvSetReal2D( my_3d_point, 0, NUM_GRID*NUM_GRID+1, 0);
00129         cvSetReal2D( my_3d_point, 1, NUM_GRID*NUM_GRID+1, 0);
00130         cvSetReal2D( my_3d_point, 2, NUM_GRID*NUM_GRID+1, 30);
00131 
00132   cvProjectPoints2( my_3d_point,
00133                                         itsCameraRotation,
00134                                         itsCameraTranslation,
00135                                         itsIntrinsicMatrix,
00136                                         itsDistortionCoeffs,
00137                                         my_image_point);
00138 
00139   for ( int i = 0; i < NUM_GRID; i++){
00140                 for ( int j = 0; j < NUM_GRID-1; j++){
00141                         int im_x1 = (int)cvGetReal2D( my_image_point, 0, i * NUM_GRID + j);
00142                         int im_y1 = (int)cvGetReal2D( my_image_point, 1, i * NUM_GRID + j);
00143                         int im_x2 = (int)cvGetReal2D( my_image_point, 0, i * NUM_GRID + j+1);
00144                         int im_y2 = (int)cvGetReal2D( my_image_point, 1, i * NUM_GRID + j+1);
00145 
00146                         cvLine( img2ipl(img), cvPoint( im_x1, im_y1), cvPoint( im_x2, im_y2), CV_RGB( 0, 255, 0), 1);
00147                 }
00148         }
00149         for ( int j = 0; j < NUM_GRID; j++){
00150                 for ( int i = 0; i < NUM_GRID-1; i++){
00151                         int im_x1 = (int)cvGetReal2D( my_image_point, 0, i * NUM_GRID + j);
00152                         int im_y1 = (int)cvGetReal2D( my_image_point, 1, i * NUM_GRID + j);
00153                         int im_x2 = (int)cvGetReal2D( my_image_point, 0, (i+1) * NUM_GRID + j);
00154                         int im_y2 = (int)cvGetReal2D( my_image_point, 1, (i+1) * NUM_GRID + j);
00155 
00156                         cvLine( img2ipl(img), cvPoint( im_x1, im_y1), cvPoint( im_x2, im_y2), CV_RGB( 0, 255, 0), 1);
00157                 }
00158         }
00159 
00160         int im_x0 = (int)cvGetReal2D( my_image_point, 0, NUM_GRID*NUM_GRID);
00161         int im_y0 = (int)cvGetReal2D( my_image_point, 1, NUM_GRID*NUM_GRID);
00162         int im_x = (int)cvGetReal2D( my_image_point, 0, NUM_GRID*NUM_GRID+1);
00163         int im_y = (int)cvGetReal2D( my_image_point, 1, NUM_GRID*NUM_GRID+1);
00164         cvLine( img2ipl(img), cvPoint( im_x0, im_y0), cvPoint( im_x, im_y), CV_RGB( 255, 0, 0), 2); //Z axis
00165 
00166 
00167         cvReleaseMat( &my_3d_point);
00168         cvReleaseMat( &my_image_point);
00169   cvReleaseMat( &rot_mat);
00170 
00171 }
00172 
00173 void projectRect(Image<PixRGB<byte> > &img, float width, float height)
00174 {
00175 
00176   //draw center point
00177   drawCircle(img, Point2D<int>(img.getWidth()/2, img.getHeight()/2), 3, PixRGB<byte>(255,0,0));
00178 
00179   CvMat *my_3d_point = cvCreateMat( 3, 5, CV_64FC1);
00180         CvMat *my_image_point = cvCreateMat( 2, 5, CV_64FC1);
00181 
00182   cvSetReal2D( my_3d_point, 0, 0, -width/2);
00183   cvSetReal2D( my_3d_point, 1, 0, -height/2);
00184   cvSetReal2D( my_3d_point, 2, 0, 0.0);
00185 
00186   cvSetReal2D( my_3d_point, 0, 1, width/2);
00187   cvSetReal2D( my_3d_point, 1, 1, -height/2);
00188   cvSetReal2D( my_3d_point, 2, 1, 0.0);
00189 
00190   cvSetReal2D( my_3d_point, 0, 2, width/2);
00191   cvSetReal2D( my_3d_point, 1, 2, height/2);
00192   cvSetReal2D( my_3d_point, 2, 2, 0.0);
00193 
00194   cvSetReal2D( my_3d_point, 0, 3, -width/2);
00195   cvSetReal2D( my_3d_point, 1, 3, height/2);
00196   cvSetReal2D( my_3d_point, 2, 3, 0.0);
00197 
00198   cvSetReal2D( my_3d_point, 0, 4, 0);
00199   cvSetReal2D( my_3d_point, 1, 4, 0);
00200   cvSetReal2D( my_3d_point, 2, 4, 0.0);
00201 
00202 
00203   cvProjectPoints2( my_3d_point,
00204                                         itsCameraRotation,
00205                                         itsCameraTranslation,
00206                                         itsIntrinsicMatrix,
00207                                         itsDistortionCoeffs,
00208                                         my_image_point);
00209 
00210   int x1 = (int)cvGetReal2D( my_image_point, 0, 0);
00211   int y1 = (int)cvGetReal2D( my_image_point, 1, 0);
00212   int x2 = (int)cvGetReal2D( my_image_point, 0, 1);
00213   int y2 = (int)cvGetReal2D( my_image_point, 1, 1);
00214   int x3 = (int)cvGetReal2D( my_image_point, 0, 2);
00215   int y3 = (int)cvGetReal2D( my_image_point, 1, 2);
00216   int x4 = (int)cvGetReal2D( my_image_point, 0, 3);
00217   int y4 = (int)cvGetReal2D( my_image_point, 1, 3);
00218 
00219   int cx = (int)cvGetReal2D( my_image_point, 0, 4);
00220   int cy = (int)cvGetReal2D( my_image_point, 1, 4);
00221 
00222   drawLine(img,  Point2D<int>(x1,y1),  Point2D<int>(x2,y2), PixRGB<byte>(0, 255,0));
00223   drawLine(img,  Point2D<int>(x2,y2),  Point2D<int>(x3,y3), PixRGB<byte>(0, 255,0));
00224   drawLine(img,  Point2D<int>(x3,y3),  Point2D<int>(x4,y4), PixRGB<byte>(0, 255,0));
00225   drawLine(img,  Point2D<int>(x4,y4),  Point2D<int>(x1,y1), PixRGB<byte>(0, 255,0));
00226 
00227   drawCircle(img,  Point2D<int>(cx,cy), 3, PixRGB<byte>(0,255,0));
00228 
00229 
00230         cvReleaseMat( &my_3d_point);
00231         cvReleaseMat( &my_image_point);
00232 
00233 }
00234 
00235 
00236 void calibrateViews(std::vector<CvPoint2D32f>& corners, int rows, int cols)
00237 {
00238 
00239   int nViews = corners.size()/(rows*cols);
00240   LINFO("Calibrate: %i views", nViews);
00241 
00242   if (nViews <= 0)
00243   {
00244     LINFO("No corners avl");
00245     return;
00246   }
00247 
00248   // Set up the object points matrix
00249   // Squares are size set in defines found in header file.
00250   CvMat* object_points = cvCreateMat(corners.size(), 3, CV_32FC1);
00251   for(uint k=0; k < corners.size()/(rows*cols); k++ )
00252   {
00253     for(int i=0; i < cols; i++ )
00254     {
00255       for(int j=0; j < rows; j++ )
00256       {
00257         cvmSet( object_points, k*(rows*cols) + i*rows + j, 0, GRID_SIZE*j );        // x coordinate
00258         cvmSet( object_points, k*(rows*cols) + i*rows + j, 1, GRID_SIZE*i ); // y coordinate
00259         cvmSet( object_points, k*(rows*cols) + i*rows + j, 2, 0 ); // z coordinate
00260       }
00261     }
00262   }
00263 
00264   //for (uint j = 0; j < corners.size(); j++){
00265   //  cvSetReal2D( object_points, j, 0, ( j % rows) * GRID_SIZE );
00266   //  cvSetReal2D( object_points, j, 1, ( j / rows) * GRID_SIZE );
00267   //  cvSetReal2D( object_points, j, 2, 0.0 );
00268         //}
00269 
00270         // Set up the matrix of points per image
00271         CvMat* point_counts = cvCreateMat(1, nViews, CV_32SC1);
00272         for(int i=0; i < nViews; i++ )
00273                 cvSetReal1D( point_counts, i, rows*cols );
00274 
00275         // Copy corners found to matrix
00276         CvMat image_points = cvMat(corners.size(), 2, CV_32FC1, &corners[0]);
00277 
00278 
00279 
00280   int flags = 0;
00281 
00282         // Initiliazie the intrinsic matrix such that the two focal lengths
00283         // have a ratio of 1.0
00284  cvmSet( itsIntrinsicMatrix, 0, 0, 1.0);
00285  cvmSet( itsIntrinsicMatrix, 1, 1, 1.0);
00286 
00287 
00288   //flags = CV_CALIB_FIX_PRINCIPAL_POINT; // | CV_CALIB_USE_INTRINSIC_GUESS;
00289   //flags =  CV_CALIB_USE_INTRINSIC_GUESS;
00290   flags =  CV_CALIB_FIX_ASPECT_RATIO;
00291 
00292   cvCalibrateCamera2( object_points, &image_points, point_counts, cvSize(320,240),
00293       itsIntrinsicMatrix, itsDistortionCoeffs,
00294       NULL, NULL,
00295       flags);
00296 
00297 
00298   //display results
00299  // Image<byte> in = itsPCameraCellsInput[0];
00300  // Image<byte> out(in.getDims(), ZEROS);
00301 
00302  // cvUndistort2( img2ipl(in), img2ipl(out), itsIntrinsicMatrix, itsDistortionCoeffs);
00303 
00304  // //itsDebugImg = out;
00305 
00306 
00307   cvReleaseMat( &object_points);
00308   cvReleaseMat( &point_counts);
00309 
00310 }
00311 
00312 void findExtrinsic(std::vector<CvPoint2D32f>& corners, int rows, int cols)
00313 {
00314 
00315   CvMat *image_points_ex = cvCreateMat( corners.size(), 2, CV_64FC1);
00316 
00317   for (uint j = 0; j < corners.size(); j++){
00318     cvSetReal2D( image_points_ex, j, 0, corners[j].x);
00319     cvSetReal2D( image_points_ex, j, 1, corners[j].y);
00320   }
00321 
00322   //int views = 1;
00323 
00324   CvMat *object_points_ex = cvCreateMat( corners.size(), 3, CV_64FC1);
00325   for (uint j = 0; j < corners.size(); j++){
00326                 cvSetReal2D( object_points_ex, j, 0, ( j % rows) * GRID_SIZE );
00327                 cvSetReal2D( object_points_ex, j, 1, ( j / rows) * GRID_SIZE );
00328                 cvSetReal2D( object_points_ex, j, 2, 0.0 );
00329         }
00330 
00331   //cvSetReal2D( itsCameraTranslation, 0, 2, 782.319961 );
00332   cvFindExtrinsicCameraParams2( object_points_ex,
00333       image_points_ex,
00334       itsIntrinsicMatrix,
00335       itsDistortionCoeffs,
00336       itsCameraRotation,
00337       itsCameraTranslation);
00338 
00339         cvReleaseMat( &image_points_ex);
00340   cvReleaseMat( &object_points_ex);
00341 
00342 }
00343 
00344 
00345 
00346 std::vector<CvPoint2D32f> findCorners(Image<PixRGB<byte> > &img, int rows, int cols)
00347 {
00348 
00349   int count = 0;
00350 
00351   std::vector<CvPoint2D32f> corners(rows*cols);
00352 
00353   Image<byte> in = luminance(img);
00354 
00355   int result = cvFindChessboardCorners(img2ipl(in), cvSize(rows,cols),
00356       &corners[0], &count,
00357       CV_CALIB_CB_ADAPTIVE_THRESH |
00358       CV_CALIB_CB_NORMALIZE_IMAGE |
00359       CV_CALIB_CB_FILTER_QUADS);
00360 
00361   // result = 0 if not all corners were found
00362   // Find corners to an accuracy of 0.1 pixel
00363         if(result != 0)
00364   {
00365                 cvFindCornerSubPix(img2ipl(in),
00366         &corners[0],
00367         count,
00368         cvSize(10,10), //win
00369         cvSize(-1,-1), //zero_zone
00370         cvTermCriteria(CV_TERMCRIT_ITER,1000,0.01) );
00371     return corners;
00372   } else {
00373     return std::vector<CvPoint2D32f>();
00374   }
00375 
00376 
00377 }
00378 
00379 
00380 
00381 void processUserInput(nub::ref<OutputFrameSeries> &ofs, bool &drawGrid, bool &saveCorners, bool &calibrate)
00382 {
00383   static bool moveCamera = false;
00384   static bool itsChangeRot = false;
00385 
00386   switch(getKey(ofs))
00387   {
00388     case KEY_UP:
00389       if (!moveCamera)
00390       {
00391         if (itsChangeRot)
00392           cvmSet(itsCameraRotation, 0, 0, cvGetReal2D(itsCameraRotation, 0, 0) + M_PI/180);
00393         else
00394           cvmSet(itsCameraTranslation, 0, 1, cvGetReal2D(itsCameraTranslation, 0, 1) - 1);
00395       } else {
00396         //itsCameraCtrl->movePanTilt(1, 0, true); //move relative
00397       }
00398 
00399       break;
00400     case KEY_DOWN:
00401       if (!moveCamera)
00402       {
00403         if (itsChangeRot)
00404           cvmSet(itsCameraRotation, 0, 0, cvGetReal2D(itsCameraRotation, 0, 0) - M_PI/180);
00405         else
00406           cvmSet(itsCameraTranslation, 0, 1, cvGetReal2D(itsCameraTranslation, 0, 1) + 1);
00407       } else {
00408         //itsCameraCtrl->movePanTilt(-1, 0, true); //move relative
00409       }
00410       break;
00411     case KEY_LEFT:
00412       if (!moveCamera)
00413       {
00414         if (itsChangeRot)
00415           cvmSet(itsCameraRotation, 0, 1, cvGetReal2D(itsCameraRotation, 0, 1) + M_PI/180);
00416         else
00417           cvmSet(itsCameraTranslation, 0, 0, cvGetReal2D(itsCameraTranslation, 0, 0) - 1);
00418       } else {
00419         //itsCameraCtrl->movePanTilt(0, 1, true); //move relative
00420       }
00421       break;
00422     case KEY_RIGHT:
00423       if (!moveCamera)
00424       {
00425         if (itsChangeRot)
00426           cvmSet(itsCameraRotation, 0, 1, cvGetReal2D(itsCameraRotation, 0, 1) - M_PI/180);
00427         else
00428           cvmSet(itsCameraTranslation, 0, 0, cvGetReal2D(itsCameraTranslation, 0, 0) + 1);
00429       } else {
00430         //itsCameraCtrl->movePanTilt(0, -1, true); //move relative
00431       }
00432       break;
00433     case 38: //a
00434       if (!moveCamera)
00435       {
00436         if (itsChangeRot)
00437           cvmSet(itsCameraRotation, 0, 2, cvGetReal2D(itsCameraRotation, 0, 2) + M_PI/180);
00438         else
00439           cvmSet(itsCameraTranslation, 0, 2, cvGetReal2D(itsCameraTranslation, 0, 2) + 1);
00440       } else {
00441         //itsCameraCtrl->zoom(1, true); //move relative
00442       }
00443       break;
00444     case 52: //z
00445       if (!moveCamera)
00446       {
00447         if (itsChangeRot)
00448           cvmSet(itsCameraRotation, 0, 2, cvGetReal2D(itsCameraRotation, 0, 2) - M_PI/180);
00449         else
00450           cvmSet(itsCameraTranslation, 0, 2, cvGetReal2D(itsCameraTranslation, 0, 2) - 1);
00451       } else {
00452         //itsCameraCtrl->zoom(-1, true); //move relative
00453       }
00454       break;
00455     case 39: //s
00456       saveCorners = true;
00457       break;
00458     case 54: //c
00459       calibrate = true;
00460       break;
00461     case 42: //g
00462       drawGrid = !drawGrid;
00463       {
00464         double d0 = cvGetReal2D( itsDistortionCoeffs, 0, 0);
00465         double d1 = cvGetReal2D( itsDistortionCoeffs, 1, 0);
00466         double d2 = cvGetReal2D( itsDistortionCoeffs, 2, 0);
00467         double d3 = cvGetReal2D( itsDistortionCoeffs, 3, 0);
00468         printf( "distortion_coeffs ( %7.7lf, %7.7lf, %7.7lf, %7.7lf)\n", d0, d1, d2, d3);
00469 
00470         double ir00 = cvGetReal2D( itsIntrinsicMatrix, 0, 0);
00471         double ir01 = cvGetReal2D( itsIntrinsicMatrix, 0, 1);
00472         double ir02 = cvGetReal2D( itsIntrinsicMatrix, 0, 2);
00473         double ir10 = cvGetReal2D( itsIntrinsicMatrix, 1, 0);
00474         double ir11 = cvGetReal2D( itsIntrinsicMatrix, 1, 1);
00475         double ir12 = cvGetReal2D( itsIntrinsicMatrix, 1, 2);
00476         double ir20 = cvGetReal2D( itsIntrinsicMatrix, 2, 0);
00477         double ir21 = cvGetReal2D( itsIntrinsicMatrix, 2, 1);
00478         double ir22 = cvGetReal2D( itsIntrinsicMatrix, 2, 2);
00479         printf( "intrinsics ( %7.5lf, %7.5lf, %7.5lf)\n", ir00, ir01, ir02);
00480         printf( "           ( %7.5lf, %7.5lf, %7.5lf)\n", ir10, ir11, ir12);
00481         printf( "           ( %7.5lf, %7.5lf, %7.5lf)\n", ir20, ir21, ir22);
00482 
00483         printf( "Rotation: %f(%0.2fDeg) %f(%0.2fDeg) %f(%0.2fDeg)\n",
00484             cvGetReal2D(itsCameraRotation, 0, 0),
00485             cvGetReal2D(itsCameraRotation, 0, 0)*180/M_PI,
00486             cvGetReal2D(itsCameraRotation, 0, 1),
00487             cvGetReal2D(itsCameraRotation, 0, 1)*180/M_PI,
00488             cvGetReal2D(itsCameraRotation, 0, 2),
00489             cvGetReal2D(itsCameraRotation, 0, 2)*180/M_PI);
00490         printf( "Translation: %f %f %f\n",
00491             cvGetReal2D(itsCameraTranslation, 0, 0),
00492             cvGetReal2D(itsCameraTranslation, 0, 1),
00493             cvGetReal2D(itsCameraTranslation, 0, 2));
00494 
00495         CvMat *rot_mat = cvCreateMat( 3, 3, CV_64FC1);
00496         cvRodrigues2( itsCameraRotation, rot_mat, 0);
00497 
00498         printf( "Rotation Rot: %0.2f %0.2f %0.2f\n",
00499             cvGetReal2D(rot_mat, 0, 0)*180/M_PI,
00500             cvGetReal2D(rot_mat, 0, 1)*180/M_PI,
00501             cvGetReal2D(rot_mat, 0, 2)*180/M_PI);
00502         printf( "              %0.2f %0.2f %0.2f\n",
00503             cvGetReal2D(rot_mat, 1, 0)*180/M_PI,
00504             cvGetReal2D(rot_mat, 1, 1)*180/M_PI,
00505             cvGetReal2D(rot_mat, 1, 2)*180/M_PI);
00506         printf( "              %0.2f %0.2f %0.2f\n",
00507             cvGetReal2D(rot_mat, 2, 0)*180/M_PI,
00508             cvGetReal2D(rot_mat, 2, 1)*180/M_PI,
00509             cvGetReal2D(rot_mat, 2, 2)*180/M_PI);
00510 
00511 
00512         cvReleaseMat( &rot_mat);
00513 
00514 
00515       }
00516       break;
00517     case 27: //r
00518       itsChangeRot = !itsChangeRot;
00519       break;
00520   }
00521 }
00522 
00523 int main(int argc, const char **argv)
00524 {
00525   // Instantiate a ModelManager:
00526   ModelManager manager("Test wiimote");
00527 
00528   nub::ref<OutputFrameSeries> ofs(new OutputFrameSeries(manager));
00529   manager.addSubComponent(ofs);
00530 
00531   nub::ref<InputFrameSeries> ifs(new InputFrameSeries(manager));
00532   manager.addSubComponent(ifs);
00533 
00534   // Parse command-line:
00535   if (manager.parseCommandLine(argc, argv, "", 0, 0) == false) return(1);
00536 
00537   manager.start();
00538 
00539 
00540 
00541   //Init camara params
00542         itsIntrinsicMatrix = cvCreateMat( 3, 3, CV_32FC1);
00543         itsDistortionCoeffs = cvCreateMat( 4, 1, CV_32FC1);
00544   itsCameraRotation = cvCreateMat( 1, 3, CV_64FC1);
00545   itsCameraTranslation = cvCreateMat( 1, 3, CV_64FC1);
00546 
00547   //cvmSet(itsDistortionCoeffs, 0, 0, -0.2403274);
00548   //cvmSet(itsDistortionCoeffs, 1, 0, 2.5312502);
00549   //cvmSet(itsDistortionCoeffs, 2, 0, -0.0439848);
00550   //cvmSet(itsDistortionCoeffs, 3, 0, -0.0106820);
00551   cvmSet(itsDistortionCoeffs, 0, 0, 0);
00552   cvmSet(itsDistortionCoeffs, 1, 0, 0);
00553   cvmSet(itsDistortionCoeffs, 2, 0, 0);
00554   cvmSet(itsDistortionCoeffs, 3, 0, 0);
00555 
00556   cvmSet(itsCameraRotation, 0, 0, 2.391102);
00557   cvmSet(itsCameraRotation, 0, 1, 0);
00558   cvmSet(itsCameraRotation, 0, 2, 0);
00559 
00560   cvmSet(itsCameraTranslation, 0, 0, 0);
00561   cvmSet(itsCameraTranslation, 0, 1, 0);
00562   cvmSet(itsCameraTranslation, 0, 2, 840.954432);
00563 
00564 
00565   //cvmSet(itsIntrinsicMatrix, 0, 0, 290.85342); cvmSet(itsIntrinsicMatrix, 0, 1, 0); cvmSet(itsIntrinsicMatrix, 0, 2, 320/2); //159.50000);
00566   //cvmSet(itsIntrinsicMatrix, 1, 0, 0); cvmSet(itsIntrinsicMatrix, 1, 1, 290.85342 ); cvmSet(itsIntrinsicMatrix, 1, 2, 240/2); // 119.5);
00567   //cvmSet(itsIntrinsicMatrix, 2, 0, 0); cvmSet(itsIntrinsicMatrix, 2, 1, 0); cvmSet(itsIntrinsicMatrix, 2, 2, 1);
00568 
00569   cvmSet(itsIntrinsicMatrix, 0, 0, 415.5); cvmSet(itsIntrinsicMatrix, 0, 1, 0); cvmSet(itsIntrinsicMatrix, 0, 2, 320/2); //159.50000);
00570   cvmSet(itsIntrinsicMatrix, 1, 0, 0); cvmSet(itsIntrinsicMatrix, 1, 1, 436 ); cvmSet(itsIntrinsicMatrix, 1, 2, 240/2); // 119.5);
00571   cvmSet(itsIntrinsicMatrix, 2, 0, 0); cvmSet(itsIntrinsicMatrix, 2, 1, 0); cvmSet(itsIntrinsicMatrix, 2, 2, 1);
00572 
00573   bool drawGrid = true;
00574   bool saveCorners = false;
00575   bool calibrate = false;
00576 
00577   std::vector<CvPoint2D32f> allCorners;
00578 
00579   while(1)
00580   {
00581 
00582     GenericFrame input = ifs->readFrame();
00583     Image<PixRGB<byte> > img = input.asRgb();
00584 
00585 
00586     int rows = 4, cols = 3;
00587 
00588     std::vector<CvPoint2D32f> corners = findCorners(img, rows, cols);
00589 
00590     if (corners.size() == (uint)(rows*cols))
00591     {
00592       if (saveCorners)
00593         for(uint i=0; i<corners.size(); i++)
00594           allCorners.push_back(corners[i]);
00595       saveCorners = false;
00596 
00597       cvDrawChessboardCorners(img2ipl(img), cvSize(rows,cols), &corners[0], corners.size(), 1);
00598     }
00599 
00600     if (calibrate)
00601     {
00602       calibrateViews(allCorners, rows, cols);
00603       if (corners.size() == (uint)(rows*cols))
00604         findExtrinsic(corners, rows, cols);
00605       calibrate = false;
00606     }
00607 
00608     if (drawGrid)
00609       projectGrid(img);
00610 
00611     projectRect(img, 216.5, 279.5);
00612 
00613 
00614     processUserInput(ofs, drawGrid, saveCorners, calibrate);
00615 
00616     ofs->writeRGB(img, "Output", FrameInfo("Output", SRC_POS));
00617 
00618     ofs->updateNext();
00619   }
00620 
00621   // stop all our ModelComponents
00622   manager.stop();
00623 
00624   // all done!
00625   return 0;
00626 }
00627 
00628 // ######################################################################
00629 /* So things look consistent in everyone's emacs... */
00630 /* Local Variables: */
00631 /* indent-tabs-mode: nil */
00632 /* End: */
Generated on Sun May 8 08:06:38 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3