Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 30th, 2012  |  syntax: None  |  size: 5.60 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. OpenCV passed co-odrnates to OpenGL but OpenGL can`t draw it
  2. // this function for calculating rotation matrix and vector
  3. void calculateOpenCVPoints(IplImage* image)
  4.     {
  5.  
  6.     board_w = 4; // Board width in squares
  7.     board_h = 5; // Board height
  8.     n_boards =3; // Number of boards
  9.     board_n = board_w * board_h;
  10.     CvSize board_sz = cvSize( board_w, board_h );
  11.  
  12.     CvMat* warp_matrix = cvCreateMat(3,3,CV_32FC1);
  13.     CvPoint2D32f* corners = new CvPoint2D32f[ board_n ];
  14.     int corner_count;
  15.     char pressedChar;
  16.     //cvNamedWindow("Livevideo",CV_WINDOW_AUTOSIZE);
  17.     imgPoints = cvCreateMat(n_boards*board_n,2,CV_32FC1) ;
  18.     objectPoints=cvCreateMat(n_boards*board_n,3,CV_32FC1);
  19.     pointCounts = cvCreateMat(n_boards,1,CV_32SC1);
  20.     intrinsicMatrix = cvCreateMat(3,3,CV_32FC1);
  21.     distortionCoeffs = cvCreateMat(5,1,CV_32FC1);
  22.     corners = new CvPoint2D32f[ board_n ];
  23.  
  24.     IplImage *grayImage = cvCreateImage(cvGetSize(image),8,1);//subpixel
  25.     successes = 0;
  26.         while(successes < n_boards)
  27.         {
  28.             printf("nIn calculateOpenCVPoints function for calculating image points ==%d",successes);
  29.             //Skip every board_dt frames to allow user to move chessboard
  30.             if(frame++ % board_dt == 0)
  31.                 {
  32.                     //Find chessboard corners:
  33.                     int found = cvFindChessboardCorners(image, board_sz, corners, &cornerCount,
  34.                         CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FILTER_QUADS);    
  35.  
  36.                 cvCvtColor( image, grayImage, CV_BGR2GRAY );
  37.                 cvFindCornerSubPix( grayImage, corners, cornerCount, cvSize( 11, 11 ),
  38.                 cvSize( -1, -1 ), cvTermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 30, 0.1 ));
  39.                 cvDrawChessboardCorners(image, board_sz, corners,cornerCount, found);
  40.                 cvShowImage("Calibration", image );
  41.  
  42.                 if( cornerCount == board_n )
  43.                     {
  44.                     step = successes*board_n;
  45.                     for( int i=step, j=0; j < board_n; ++i, ++j )
  46.                         {
  47.                         CV_MAT_ELEM( *imgPoints, float, i, 0 ) = corners[j].x;
  48.                         CV_MAT_ELEM( *imgPoints, float, i, 1 ) = corners[j].y;
  49.                         CV_MAT_ELEM( *objectPoints, float, i, 0 ) = j/board_w;
  50.                         CV_MAT_ELEM( *objectPoints, float, i, 1 ) = j%board_w;
  51.                         CV_MAT_ELEM( *objectPoints, float, i, 2 ) = 0.0f;
  52.                         }
  53.                     CV_MAT_ELEM( *pointCounts, int, successes, 0 ) = board_n;
  54.                     successes++;
  55.                     }
  56.                 }//if(frame++ % board_dt == 0)
  57.             image = cvQueryFrame( cvCapture );
  58.             //cvShowImage("liveVideo",image);
  59.             presssedChar=cvWaitKey(5);
  60.         } //while(successes < n_boards)
  61.         cvFindExtrinsicCameraParams2(objectPoints,imgPoints,intrinsic,distortion,rotationVector,translationVector,0);
  62.         cvRodrigues2(rotationVector,rotationMatrix,NULL);
  63.         printf("nRotation Vector and Rotation Matrx are calculatedn");
  64.         float rv[]={rotationVector->data.fl[0],rotationVector->data.fl[1],rotationVector->data.fl[2]};
  65.         float tv[]={translationVector->data.fl[0],translationVector->data.fl[1],translationVector->data.fl[2]};
  66.         float rm[9];
  67.  
  68.  
  69.         for(int i=0;i<9;i++)
  70.             rm[i]=rotationMatrix->data.fl[i];
  71.  
  72.         for (int f=0; f<3; f++)
  73.             {
  74.             for (int c=0; c<3; c++)
  75.                 {
  76.                 toGLMatrix[c*4+f] = rm[f*3+c];  //transposed
  77.                 }
  78.             }  
  79.  
  80.         toGLMatrix[3] = 0.0;
  81.         toGLMatrix[7] = 0.0;    
  82.         toGLMatrix[11] = 0.0;
  83.         toGLMatrix[12] = -tv[0];
  84.         toGLMatrix[13] = -tv[1];
  85.         toGLMatrix[14] = tv[2];
  86.         toGLMatrix[15] = 1.0;
  87.  
  88.     }
  89.  
  90.  
  91. // openGL dispaly function
  92.        void display
  93.        {
  94.                       calculateOpenCVPoints("Image from camera");
  95.                        glMatrixMode(GL_MODELVIEW);
  96.             glLoadIdentity();
  97.  
  98.             //argDraw3dCamera( 0, 0 );
  99.             glViewport(0, 0, 640,480);
  100.             glMatrixMode(GL_PROJECTION);
  101.             glLoadMatrixd(toGLMatrix);
  102.  
  103.             glClearDepth( 1.0 );
  104.             glClear(GL_DEPTH_BUFFER_BIT);
  105.             glEnable(GL_DEPTH_TEST);
  106.             glDepthFunc(GL_LEQUAL);
  107.             glMatrixMode(GL_MODELVIEW);
  108.             // Loading the calculated matrix from OpenCV rotation matrix and vector
  109.             glLoadMatrixd(toGLMatrix);
  110.  
  111.             glEnable(GL_LIGHTING);
  112.             glEnable(GL_LIGHT0);
  113.             glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  114.             glLightfv(GL_LIGHT0, GL_AMBIENT, ambi);
  115.             glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor);
  116.             glMaterialfv(GL_FRONT, GL_SPECULAR, mat_flash);
  117.             glMaterialfv(GL_FRONT, GL_SHININESS, mat_flash_shiny);  
  118.             glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  119.             glMatrixMode(GL_MODELVIEW);
  120.             glColor3d(1.0f,1.0f,1.0f);
  121.             glTranslatef( 0.0, 0.0,0.0 );
  122.             glutSolidCube(140);
  123.             glDisable( GL_LIGHTING );
  124.             glDisable( GL_DEPTH_TEST );
  125.                         glutSwapBuffers();
  126.            }
  127.  
  128. // and remaining as initialize glut etc
  129.        
  130. glViewport(0, 0, 640,480);
  131.         glMatrixMode(GL_PROJECTION);
  132.         glLoadMatrixd(toGLMatrix);
  133.  
  134.         glMatrixMode(GL_MODELVIEW);
  135.         // Loading the calculated matrix from OpenCV rotation matrix and vector
  136.         glLoadMatrixd(toGLMatrix);
  137.        
  138. v' = M M v = M² v
  139.        
  140. C = P M
  141.  
  142. P.zx = 0
  143. P.zy = 0
  144. P.zz =/= 0
  145. P.zw =/= 0
  146.  
  147. M.x · M.y = 0
  148. M.y · M.z = 0
  149. M.z · M.x = 0
  150.  
  151. M.xw = 0
  152. M.yw = 0
  153. M.zw = 0
  154. M.ww = 1
  155.  
  156. | M.x | = 1
  157. | M.y | = 1
  158. | M.z | = 1