Lktrack.cpp
上传用户:jsylhbnbhn
上传日期:2013-11-03
资源大小:119k
文件大小:9k
源码类别:

OpenCV

开发平台:

Visual C++

  1. #ifdef _CH_
  2. #pragma package <opencv>
  3. #endif
  4. #ifndef _EiC
  5. #include "cv.h"
  6. #include "highgui.h"
  7. #include <stdio.h>
  8. #include <ctype.h>
  9. #include "skelecton.h"
  10. #include "skeletons.h"
  11. #include "kalman.h"
  12. #include "OpenGL.h"
  13. #include <GL/gl.h>
  14. #include <GL/glu.h>
  15. #include <GL/glaux.h>
  16. #endif
  17. IplImage *image = 0, *grey = 0, *prev_grey = 0,
  18.  *pyramid = 0, *prev_pyramid = 0,*motion_track_img = 0, *swap_temp;
  19. int win_size = 10;
  20. const int MAX_COUNT = 13;  //骨架节点数
  21. CvPoint2D32f* points[2] = {0,0}, *swap_points;
  22. char* status = 0;
  23. int count = 0;
  24. int need_to_init = 0;
  25. int night_mode = 0;
  26. int flags = 0;
  27. int add_remove_pt = 0;
  28. int pause = 0;
  29. CvPoint pt;
  30. OpenGL *pgl;
  31. Vector3f  vt1,vt2;
  32. void CALLBACK display3()
  33. {
  34. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  35. glLoadIdentity();
  36. pgl->clear ();
  37. glColor3f(0.4f,0.5f,0.7f);
  38. //auxSolidCylinder(0.2,1.0);
  39. //pgl->draw2point (vt1,vt2);
  40. pgl->display ();
  41. //glFlush();
  42. }
  43. void opengl_test()
  44. {
  45. pgl = new OpenGL();
  46. pgl->Init();
  47.   vt1.x = 200;
  48.   vt1.y = 200;
  49.   vt1.z = 200;
  50.   vt2.x = 0;
  51.   vt2.y = 0;
  52.   vt2.z = 0;
  53. // auxMainLoop(display3);
  54. for(;;) display3();
  55. delete pgl;
  56. }
  57. void on_mouse( int event, int x, int y, int flags )
  58. {
  59.     
  60. if( !image )
  61.         return;
  62.     if( image->origin )
  63.         y = image->height - y;
  64.     if( event == CV_EVENT_LBUTTONDOWN )
  65.     {
  66.         pt = cvPoint(x,y);
  67.         add_remove_pt = 1;
  68.     }
  69. }
  70. int cvmain( int argc, char** argv )
  71. {
  72.     //opengl_test();
  73. //exit(0);
  74. skelecton *ske = new skelecton();
  75. skelectons *skes = new skelectons();
  76. kalman *bone_kalmans = new kalman[MAX_COUNT];
  77. CvCapture* capture = 0;
  78.     
  79.     if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
  80.         //capture = cvCaptureFromCAM( argc == 2 ? argv[1][0] - '0' : 0 );
  81. capture = cvCaptureFromFile("e:\MyFirstPoser.AVI");
  82.     else if( argc == 2 )
  83.         capture = cvCaptureFromAVI( argv[1] );
  84.     if( !capture )
  85.     {
  86.         fprintf(stderr,"Could not initialize capturing...n");
  87.         return -1;
  88.     }
  89.     printf( "Hot keys: n"
  90.             "tESC - quit the programn"
  91.             "tr - auto-initialize trackingn"
  92.             "tc - delete all the pointsn"
  93.             "tn - switch the "night" mode on/offn"
  94. "tp - pause on/offn"
  95.             "n" );
  96.     cvNamedWindow( "LkDemo", 1 );
  97. cvNamedWindow( "MotionTrackDemo", 0 );
  98.     cvSetMouseCallback( "LkDemo", on_mouse );
  99.     for(;;)
  100.     {
  101.         IplImage* frame = 0;
  102.         int i, k, c;
  103.         if(pause)
  104. frame = image ;
  105. else 
  106. frame = cvQueryFrame( capture );
  107.         if( !frame ) break;
  108.         if( !image )
  109.         {
  110.             /* allocate all the buffers */
  111.             image = cvCreateImage( cvGetSize(frame), 8, 3 );
  112.             image->origin = frame->origin;
  113. motion_track_img = cvCreateImage( cvGetSize(frame), 8, 3 );
  114. motion_track_img->origin = frame->origin;
  115.             grey = cvCreateImage( cvGetSize(frame), 8, 1 );
  116.             prev_grey = cvCreateImage( cvGetSize(frame), 8, 1 );
  117.             pyramid = cvCreateImage( cvGetSize(frame), 8, 1 );
  118.             prev_pyramid = cvCreateImage( cvGetSize(frame), 8, 1 );
  119.             points[0] = (CvPoint2D32f*)cvAlloc(MAX_COUNT*sizeof(points[0][0]));
  120.             points[1] = (CvPoint2D32f*)cvAlloc(MAX_COUNT*sizeof(points[0][0]));
  121.             status = (char*)cvAlloc(MAX_COUNT);
  122.             flags = 0;
  123.         }
  124.         cvCopy( frame, image, 0 );
  125.         cvCvtColor( image, grey, CV_BGR2GRAY );
  126. cvZero( motion_track_img );
  127.         if( night_mode )
  128.             cvZero( image );
  129.         
  130.         if( need_to_init )
  131.         {
  132.             /* automatic initialization */
  133.             IplImage* eig = cvCreateImage( cvGetSize(grey), 32, 1 );
  134.             IplImage* temp = cvCreateImage( cvGetSize(grey), 32, 1 );
  135.             double quality = 0.01;
  136.             double min_distance = 10;
  137.             count = MAX_COUNT;
  138.             cvGoodFeaturesToTrack( grey, eig, temp, points[1], &count,
  139.                                    quality, min_distance, 0 );
  140.             cvFindCornerSubPix( grey, points[1], count,
  141.                 cvSize(win_size,win_size), cvSize(-1,-1),
  142.                 cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,20,0.03));
  143.             cvReleaseImage( &eig );
  144.             cvReleaseImage( &temp );
  145.             add_remove_pt = 0;
  146.         }
  147.         else if( count > 0 )
  148.         {
  149.             cvCalcOpticalFlowPyrLK( prev_grey, grey, prev_pyramid, pyramid,
  150.                 points[0], points[1], count, cvSize(win_size,win_size), 3, status, 0,
  151.                 cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,20,0.03), flags );
  152.             flags |= CV_LKFLOW_PYR_A_READY;
  153.             for( i = k = 0; i < count; i++ )
  154.             {
  155.                 if( add_remove_pt )
  156.                 {
  157.                     double dx = pt.x - points[1][i].x;
  158.                     double dy = pt.y - points[1][i].y;
  159.                     if( dx*dx + dy*dy <= 25 )
  160.                     {
  161.                         add_remove_pt = 0;
  162.                         continue;
  163.                     }
  164.                 }
  165.                 
  166.                 if( !status[i] )
  167.                     continue;
  168.                 
  169.                 points[1][k++] = points[1][i];
  170. switch(i)
  171. {
  172. case 0:
  173. ske->CorrectPosition ("neck",points[1][i],bone_kalmans[i]);
  174. break;
  175. case 1:
  176. ske->CorrectPosition ("head",points[1][i],bone_kalmans[i]);
  177. break;
  178. case 2:
  179. ske->CorrectPosition ("rShoulder",points[1][i],bone_kalmans[i]);
  180. break;
  181. case 3:
  182. ske->CorrectPosition ("lShoulder",points[1][i],bone_kalmans[i]);
  183. break;
  184. case 4:
  185. ske->CorrectPosition ("rElbow",points[1][i],bone_kalmans[i]);
  186. break;
  187. case 5:
  188. ske->CorrectPosition ("lElbow",points[1][i],bone_kalmans[i]);
  189. break;
  190. case 6:
  191. ske->CorrectPosition ("rWrist",points[1][i],bone_kalmans[i]);
  192. break;
  193. case 7:
  194. ske->CorrectPosition ("lWrist",points[1][i],bone_kalmans[i]);
  195. break;
  196. case 8:
  197. ske->CorrectPosition ("Waist",points[1][i],bone_kalmans[i]);
  198. break;
  199. case 9:
  200. ske->CorrectPosition ("rKnee",points[1][i],bone_kalmans[i]);
  201. break;
  202. case 10:
  203. ske->CorrectPosition ("lKnee",points[1][i],bone_kalmans[i]);
  204. break;
  205. case 11:
  206. ske->CorrectPosition ("rAnkle",points[1][i],bone_kalmans[i]);
  207. break;
  208. case 12:
  209. ske->CorrectPosition ("lAnkle",points[1][i],bone_kalmans[i]);
  210. break;
  211. }
  212.                 cvCircle( image, cvPointFrom32f(points[1][i]), 3, CV_RGB(0,255,0), -1, 8,0);
  213.             }
  214.             count = k;
  215.         }
  216. //
  217. skelecton *pske;
  218. pske = skes->get_previous_skelecton ();
  219. if(pske!=NULL)  ske->set_previous_skelecton (pske);
  220. //ske->draw3D(motion_track_img); 
  221. ske->drawOpenGL3D(image); 
  222. skes->add (ske);  //不需要每次都保存,应该隔单位时间保存一次
  223. //skes->test ();
  224.         if( add_remove_pt && count < MAX_COUNT )
  225.         {
  226.             points[1][count++] = cvPointTo32f(pt);
  227.             cvFindCornerSubPix( grey, points[1] + count - 1, 1,
  228.                 cvSize(win_size,win_size), cvSize(-1,-1),
  229.                 cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,20,0.03));
  230.             add_remove_pt = 0;
  231. bone_kalmans[count-1].init_kalman (points[1][count-1].x,0,points[1][count-1].y,0);
  232. ske->CalcLengthRatio (count,points[1][count-1]);
  233.         }
  234.         CV_SWAP( prev_grey, grey, swap_temp );
  235.         CV_SWAP( prev_pyramid, pyramid, swap_temp );
  236.         CV_SWAP( points[0], points[1], swap_points );
  237.         need_to_init = 0;
  238.         cvShowImage( "LkDemo", image );
  239. cvShowImage( "MotionTrackDemo", motion_track_img );
  240.         c = cvWaitKey(10);
  241. //printf("%c,%d",c,c);
  242.         if( c == 'q' )
  243.             break;
  244.         switch( c )
  245.         {
  246.         case 'r':
  247.             need_to_init = 1;
  248.             break;
  249.         case 'c':
  250.             count = 0;
  251. ske->reset();
  252.             break;
  253.         case 'n':
  254.             night_mode ^= 1;
  255.             break;
  256. case 'p':
  257.             pause ^= 1;
  258.             break;
  259. case 'o':            
  260. if(ske->ske_pause)  ske->ske_pause= false;
  261. else ske->ske_pause= true;
  262.             break;
  263. case 'i':            
  264. ske->view_up();
  265. break;
  266. case 'k':            
  267. ske->view_down();
  268. break;
  269. case 'j':            
  270. ske->view_left();
  271. break;
  272. case 'l':            
  273. ske->view_right();
  274. break;
  275.         default:
  276.             ;
  277.         }
  278.     }
  279.     cvReleaseCapture( &capture );
  280.     cvDestroyWindow("LkDemo");
  281. delete ske;
  282. ske = NULL;
  283. delete skes;
  284. skes = NULL;
  285. delete []bone_kalmans;
  286.     return 0;
  287. }