Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.48 KB | None | 0 0
  1. #include "tracker/Camera.hpp"
  2. #include "tracker/ChessboardCameraTracker.hpp"
  3. #include "tracker/ChessboardCameraTrackerKLT.hpp"
  4. #include "tracker/utility.hpp"
  5.  
  6.  
  7. #include <opencv2/highgui/highgui.hpp>
  8. #include <opencv2/calib3d/calib3d.hpp>
  9. #include <opencv2/core/core.hpp>
  10. #include <opencv2/imgproc/imgproc.hpp>
  11.  
  12. #ifdef __APPLE__
  13. #include <OpenGL/gl.h>
  14. // #include <OpenGL/glu.h>
  15. #include <GLUT/glut.h>
  16. #else
  17. #ifdef _WIN32
  18. #include <windows.h>
  19. #endif
  20. #include <GL/gl.h>
  21. // #include <GL/glu.h>
  22. #include <GL/freeglut.h>
  23. #endif
  24.  
  25. #include <unistd.h>
  26.  
  27. #include <glm.h>
  28.  
  29. #include <cstdlib>
  30. #include <iostream>
  31. #include <cstdio>
  32.  
  33. using namespace std;
  34. using namespace cv;
  35.  
  36. // Display the help for the program
  37. void help( const char* programName );
  38.  
  39. // parse the input command line arguments
  40. bool parseArgs( int argc, char**argv, Size &boardSize, string &inputFilename, string &calibFile, string &objFile );
  41.  
  42.  
  43.  
  44. /*
  45. * Common globals
  46. */
  47.  
  48. int gFinished;
  49.  
  50. // the OpenGL reference for the texture to display
  51. GLuint gCameraTextureId;
  52. // the global array containg the explicit modelview matrix
  53. const float *gModelViewMatrix;
  54. // the global array containg the explicit projection matrix
  55. float gProjectionMatrix[16] = {0.f};
  56. // this will physically contain the current frame that is used as texture
  57. Mat gResultImage;
  58. bool stop = false;
  59.  
  60. // the size of the video frame
  61. Size singleSize;
  62.  
  63. void DrawAxis( float scale )
  64. {
  65. glPushMatrix( );
  66. glDisable( GL_LIGHTING );
  67. // glDisable(GL_TEXTURE_2D);
  68. glScalef( scale, scale, scale );
  69. glLineWidth( 4.0 );
  70.  
  71. glBegin( GL_LINES );
  72.  
  73. glColor3f( 1.0, 0.0, 0.0 );
  74. glVertex3f( .8f, 0.05f, 0.0 );
  75. glVertex3f( 1.0, 0.25f, 0.0 ); /* Letter X */
  76. glVertex3f( 0.8f, .25f, 0.0 );
  77. glVertex3f( 1.0, 0.05f, 0.0 );
  78. glVertex3f( 0.0, 0.0, 0.0 );
  79. glVertex3f( 1.0, 0.0, 0.0 ); /* X axis */
  80.  
  81. glColor3f( 0.0, 1.0, 0.0 );
  82. glVertex3f( 0.0, 0.0, 0.0 );
  83. glVertex3f( 0.0, 1.0, 0.0 ); /* Y axis */
  84.  
  85. glColor3f( 0.0, 0.0, 1.0 );
  86. glVertex3f( 0.0, 0.0, 0.0 );
  87. glVertex3f( 0.0, 0.0, 1.0 ); /* Z axis */
  88. glEnd( );
  89. // if (lighting)
  90. glEnable( GL_LIGHTING );
  91. // if (lighting)
  92. // glEnable(GL_TEXTURE_2D);
  93. glColor3f( 1.0, 1.0, 1.0 );
  94. glPopMatrix( );
  95. }
  96.  
  97. void DrawPoints( float scale )
  98. {
  99. glPushMatrix( );
  100. glDisable( GL_LIGHTING );
  101. // glDisable(GL_TEXTURE_2D);
  102. glScalef( scale, scale, scale );
  103. glPointSize( 5 );
  104. glBegin( GL_POINTS );
  105.  
  106. for( int i = 0; i < 6; i++ )
  107. {
  108.  
  109. glColor3f( 1.0, ( float ) 40.0f * i / 255.0f, ( float ) 40.0f * i / 255.0f );
  110. for( int j = 0; j < 9; j++ )
  111. glVertex3f( j, i, 0.0 );
  112. }
  113.  
  114. glEnd( );
  115. // if (lighting)
  116. glEnable( GL_LIGHTING );
  117. // if (lighting)
  118. // glEnable(GL_TEXTURE_2D);
  119. glColor3f( 1.0, 1.0, 1.0 );
  120. glPopMatrix( );
  121. }
  122.  
  123. /**
  124. * OpenGL initialization
  125. */
  126. void glInit( )
  127. {
  128.  
  129. //******************************************************************
  130. // enable the depth test
  131. //******************************************************************
  132. glEnable( GL_DEPTH_TEST );
  133. glDepthFunc( GL_LESS );
  134.  
  135. glEnable( GL_TEXTURE_2D );
  136. glGenTextures( 1, &gCameraTextureId );
  137.  
  138. //******************************************************************
  139. // set the Gouraud shading
  140. //******************************************************************
  141. glShadeModel( GL_SMOOTH );
  142.  
  143. //******************************************************************
  144. // set the LIGHT0 as a simple white, directional light with direction [1,2,-2]
  145. //******************************************************************
  146. GLfloat gLightPos[] = {100.0, 200.0, -200.0, 0.0};
  147. glLightfv( GL_LIGHT0, GL_POSITION, gLightPos );
  148.  
  149.  
  150. //******************************************************************
  151. // set the material properties for the teapot
  152. // choose the values for ambient, diffuse, specular and shininess
  153. // as you prefer. The teapot in the figure has is mainly gray with
  154. // ambient 0.7, diffuse 0.8, specular 1.0 and shininess 100
  155. //******************************************************************
  156. GLfloat mat_ambient[] = {0.7, 0.7, 0.7, 1.0};
  157. GLfloat mat_diffuse[] = {0.8, 0.8, 0.8, 1.0};
  158. GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0};
  159. GLfloat high_shininess[] = {100.0};
  160.  
  161. glMaterialfv( GL_FRONT, GL_AMBIENT, mat_ambient );
  162. glMaterialfv( GL_FRONT, GL_DIFFUSE, mat_diffuse );
  163. glMaterialfv( GL_FRONT, GL_SPECULAR, mat_specular );
  164. glMaterialfv( GL_FRONT, GL_SHININESS, high_shininess );
  165.  
  166. //******************************************************************
  167. // enable the lights
  168. //******************************************************************
  169. glEnable( GL_LIGHTING );
  170. glEnable( GL_LIGHT0 );
  171.  
  172. //******************************************************************
  173. // set the opengl projection matrix to gProjectionMatrix
  174. // load the identity and multiply it by gProjectionMatrix using glMultMatrixf
  175. //******************************************************************
  176. glMatrixMode( GL_PROJECTION );
  177. glLoadIdentity( );
  178. glMultMatrixf( gProjectionMatrix );
  179.  
  180. //******************************************************************
  181. // set back the modelview mode
  182. //******************************************************************
  183. glMatrixMode( GL_MODELVIEW );
  184.  
  185. }
  186.  
  187. // Updates texture handle gCameraTextureId with OpenCV image in cv::Mat from gResultImage
  188.  
  189. void updateTexture( )
  190. {
  191. glBindTexture( GL_TEXTURE_2D, gCameraTextureId );
  192.  
  193. // set texture filter to linear - we do not build mipmaps for speed
  194. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
  195. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
  196.  
  197. // create the texture from OpenCV image data
  198. glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB8, singleSize.width, singleSize.height, 0, GL_BGR,
  199. GL_UNSIGNED_BYTE, gResultImage.data );
  200. }
  201.  
  202. /**
  203. * Draw the background from the camera image
  204. */
  205. void drawBackground( )
  206. {
  207. // set up the modelview matrix so that the view is between [-1,-1] and [1,1]
  208. glMatrixMode( GL_PROJECTION );
  209. glPushMatrix( );
  210. glLoadIdentity( );
  211. glOrtho( -1, 1, -1, 1, 0, 1 );
  212. glMatrixMode( GL_MODELVIEW );
  213. glLoadIdentity( );
  214.  
  215. // draw the quad textured with the camera image
  216. glBindTexture( GL_TEXTURE_2D, gCameraTextureId );
  217. glBegin( GL_QUADS );
  218. glTexCoord2f( 0, 1 );
  219. glVertex2f( -1, -1 );
  220. glTexCoord2f( 0, 0 );
  221. glVertex2f( -1, 1 );
  222. glTexCoord2f( 1, 0 );
  223. glVertex2f( 1, 1 );
  224. glTexCoord2f( 1, 1 );
  225. glVertex2f( 1, -1 );
  226. glEnd( );
  227.  
  228. // reset the projection matrix
  229. glMatrixMode( GL_PROJECTION );
  230. glPopMatrix( );
  231. glMatrixMode( GL_MODELVIEW );
  232. }
  233.  
  234. /** OpenGL display callback */
  235. void displayFunc( )
  236. {
  237. glClear( GL_COLOR_BUFFER_BIT );
  238.  
  239. // render the background image from camera texture
  240. glEnable( GL_TEXTURE_2D );
  241. //******************************************************************
  242. // disable the lighting before drawing the background
  243. //******************************************************************
  244. glDisable( GL_LIGHTING );
  245.  
  246. drawBackground( );
  247.  
  248. // clear th depth buffer bit so that the background is overdrawn
  249. glClear( GL_DEPTH_BUFFER_BIT );
  250.  
  251. // everything will be white
  252. glColor3f( 1, 1, 1 );
  253.  
  254. // start with fresh modelview matrix and apply the transform of the plane
  255. glMatrixMode( GL_MODELVIEW );
  256. glLoadIdentity( );
  257. //******************************************************************
  258. // apply the modelview matrix gModelViewMatrix using glMultMatrixf
  259. //******************************************************************
  260. glMultMatrixf( gModelViewMatrix );
  261. // DrawPoints( 25 );
  262. glRotatef( -90, 1, 0, 0 );
  263.  
  264.  
  265. // enable the texture for a nice effect ;)
  266. glDisable( GL_TEXTURE_2D );
  267. //******************************************************************
  268. // enable the lighting before drawing the teapot/the object
  269. //******************************************************************
  270. glEnable( GL_LIGHTING );
  271.  
  272. // DrawAxis( 100 );
  273.  
  274. glTranslatef( 0, 50, 0 );
  275. //******************************************************************
  276. // draw the teapot (the solid version)
  277. //******************************************************************
  278. glutSolidTeapot( 45 );
  279.  
  280. glutSwapBuffers( );
  281. glutPostRedisplay( );
  282. }
  283.  
  284. // Windows resize callback
  285.  
  286. void reshape( GLint width, GLint height )
  287. {
  288. glViewport( 0, 0, width, height );
  289. }
  290.  
  291. // Keyboard callback
  292.  
  293. void keyFunc( unsigned char key, int x, int y )
  294. {
  295. cout << key << " pressed" << endl;
  296.  
  297. switch( key )
  298. {
  299. case 27:
  300. gFinished = true;
  301. break;
  302.  
  303. case 's':
  304. stop = !stop;
  305. break;
  306.  
  307. default:
  308. break;
  309. }
  310. }
  311.  
  312. int main( int argc, char** argv )
  313. {
  314. string videoFilename, calibFilename, objFile;
  315. int imgInType;
  316.  
  317. long frameNumber = 0;
  318.  
  319. /******************************************************************/
  320. /* VARIABLES TO USE */
  321. /******************************************************************/
  322.  
  323. // it will contain the size in terms of corners (width X height) of the chessboard
  324. Size boardSize;
  325.  
  326. // Default pattern is chessboard
  327. Pattern pattern = CHESSBOARD;
  328.  
  329. // Used to load the video and get the frames
  330. VideoCapture capture;
  331.  
  332. // Camera object containing the calibration parameters
  333. Camera cam;
  334.  
  335. // Camera Tracker object
  336. ChessboardCameraTrackerKLT tracker;
  337.  
  338. // 3x4 camera pose matrix [R t]
  339. Mat cameraPose;
  340.  
  341. // Mat dummyMatrix = Mat::eye( 4, 4, CV_32F );
  342. // dummyMatrix.at<float>(0, 3) = 102;
  343. // dummyMatrix.at<float>(1, 3) = 46;
  344. // dummyMatrix.at<float>(2, 3) = 217;
  345. // Mat dummyMatrix = (Mat_<float>(4,4) << -0.90750873, -0.0011025554, 0, 125.93854, 0.39205164, -0.0022058936, 0.00093782519, 43.355019, -0.15074302, 0.00085026468, 0.0024341263, 384.71075, 0,0,0,1);
  346. Mat dummyMatrix = ( Mat_<float>( 4, 4 ) << 0.4830, -0.8756, 0.0077, 125.93854, 0.8365, 0.4588, -0.2996, 43.355019, 0.2588, 0.1511, 0.9540, 384.71075, 0, 0, 0, 1 );
  347.  
  348.  
  349.  
  350. cout << dummyMatrix << endl;
  351.  
  352. /******************************************************************/
  353. /* READ THE INPUT PARAMETERS - DO NOT MODIFY */
  354. /******************************************************************/
  355.  
  356. if( !parseArgs( argc, argv, boardSize, videoFilename, calibFilename, objFile ) )
  357. {
  358. cerr << "Aborting..." << endl;
  359. return EXIT_FAILURE;
  360. }
  361.  
  362. //******************************************************************
  363. // init the Camera loading the calibration parameters
  364. //******************************************************************
  365. cam.init( calibFilename );
  366.  
  367. //******************************************************************
  368. // get the corresponding projection matrix in OGL format
  369. //******************************************************************
  370. cam.getOGLProjectionMatrix( gProjectionMatrix, 10.f, 10000.f );
  371.  
  372.  
  373. capture.open( videoFilename );
  374.  
  375. // check if capture has opened the video
  376. if( !capture.isOpened( ) )
  377. {
  378. cerr << "Could not open video file " << videoFilename << endl;
  379. return EXIT_FAILURE;
  380. }
  381. if( !getVideoSizeAndType(videoFilename, capture, singleSize, imgInType ) )
  382. {
  383. cerr << "Something wrong while checking the size and type of the video " << videoFilename << endl;
  384. return EXIT_FAILURE;
  385. }
  386.  
  387. gResultImage = Mat( singleSize, imgInType );
  388.  
  389.  
  390. // Setup GLUT rendering and callbacks
  391. glutInit( &argc, argv );
  392. glutCreateWindow( "Main" );
  393. glutKeyboardFunc( keyFunc );
  394. glutReshapeFunc( reshape );
  395. // reshape the window with the size of the image
  396. glutReshapeWindow( singleSize.width, singleSize.height );
  397. glutDisplayFunc( displayFunc );
  398.  
  399. glInit( );
  400.  
  401. gFinished = false;
  402.  
  403. while( !gFinished )
  404. {
  405.  
  406. if( !stop )
  407. {
  408. Mat view0;
  409. capture >> view0;
  410.  
  411. // get a copy of the frame
  412. if( view0.empty( ) )
  413. {
  414. cerr << "no more images available" << endl;
  415. gFinished = true;
  416. break;
  417. }
  418.  
  419. // undistort( view0, gResultImage, cam.matK, cam.distCoeff );
  420.  
  421. // process the image
  422. if( tracker.process( view0, cameraPose, cam, boardSize, pattern ) )
  423. {
  424. Mat temp;
  425. cameraPose.convertTo( temp, CV_32F );
  426. PRINTVAR( temp );
  427. #if CV_MINOR_VERSION < 4
  428. Mat fooMat = dummyMatrix.rowRange( 0, 3 );
  429. temp.copyTo( fooMat );
  430. #else
  431. temp.copyTo( dummyMatrix.rowRange( 0, 3 ) );
  432. #endif
  433. PRINTVAR( dummyMatrix );
  434. // gModelViewMatrix = (float*) Mat(temp.t()).data;
  435. gModelViewMatrix = ( float* ) Mat( dummyMatrix.t( ) ).data;
  436.  
  437. }
  438.  
  439. view0.copyTo( gResultImage );
  440. // gModelViewMatrix = (float*) Mat(dummyMatrix.t()).data;
  441.  
  442. // cout << endl << endl << "****************** frame " << frameNumber << " ******************" << endl;
  443.  
  444. ++frameNumber;
  445. }
  446.  
  447. // update the texture to be displayed in OPENGL
  448. updateTexture( );
  449.  
  450. // force Opengl to call the displayFunc
  451. #if __APPLE__
  452. glutCheckLoop( );
  453. #else
  454. glutMainLoopEvent( );
  455. #endif
  456.  
  457. // sleep for 35ms
  458. usleep( 35000 );
  459.  
  460. }
  461.  
  462. capture.release( );
  463.  
  464.  
  465. return EXIT_SUCCESS;
  466. }
  467.  
  468. // Display the help for the programm
  469.  
  470. void help( const char* programName )
  471. {
  472. cout << "Detect a chessboard in a given video and visualize a teapot on top of it" << endl
  473. << "Usage: " << programName << endl
  474. << " -w <board_width> # the number of inner corners per one of board dimension" << endl
  475. << " -h <board_height> # the number of inner corners per another board dimension" << endl
  476. << " -c <calib file> # the name of the calibration file" << endl
  477. << " -o <obj file> # the obj file containing the 3D model to display" << endl
  478. << " <video file> # the name of the video file" << endl
  479. << endl;
  480. }
  481.  
  482.  
  483. // parse the input command line arguments
  484.  
  485. bool parseArgs( int argc, char**argv, Size &boardSize, string &inputFilename, string &calibFile, string &objFile )
  486. {
  487. // check the minimum number of arguments
  488. if( argc < 3 )
  489. {
  490. help( argv[0] );
  491. return false;
  492. }
  493.  
  494.  
  495. // Read the input arguments
  496. for( int i = 1; i < argc; i++ )
  497. {
  498. const char* s = argv[i];
  499. if( strcmp( s, "-w" ) == 0 )
  500. {
  501. if( sscanf( argv[++i], "%u", &boardSize.width ) != 1 || boardSize.width <= 0 )
  502. {
  503. cerr << "Invalid board width" << endl;
  504. return false;
  505. }
  506. }
  507. else if( strcmp( s, "-h" ) == 0 )
  508. {
  509. if( sscanf( argv[++i], "%u", &boardSize.height ) != 1 || boardSize.height <= 0 )
  510. {
  511. cerr << "Invalid board height" << endl;
  512. return false;
  513. }
  514. }
  515. else if( s[0] != '-' )
  516. {
  517. inputFilename.assign( s );
  518. }
  519. else if( strcmp( s, "-c" ) == 0 )
  520. {
  521. if( i + 1 < argc )
  522. calibFile.assign( argv[++i] );
  523. else
  524. {
  525. cerr << "Missing argument for option " << s << endl;
  526. return false;
  527. }
  528. }
  529. else if( strcmp( s, "-o" ) == 0 )
  530. {
  531. if( i + 1 < argc )
  532. objFile.assign( argv[++i] );
  533. else
  534. {
  535. cerr << "Missing argument for the obj file " << s << endl;
  536. return false;
  537. }
  538. }
  539. else
  540. {
  541. cerr << "Unknown option " << s << endl;
  542. return false;
  543. }
  544. }
  545.  
  546. return true;
  547. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement