分享

L-K光流法

 昵称6290398 2011-10-27
 

[learning opencv]第十章 跟踪与运动:金字塔Lucas-kanade(cvCalcOpticalFlowPyrLK)检测光流

分类: OpenCV 535人阅读 评论(0) 收藏 举报

函数解析

cvGoodFeaturesToTrack,http://blog.csdn.net/moc062066/article/details/6634120,

cvFindCornerSubPix ,http://blog.csdn.net/moc062066/article/details/6634961

两个函数的基础,接下来就是在视频中检测光流(optical flow),经常用的函数是cvCalcOpticalFlowPyrLK,函数说明如下;

  1. <span style="font-size:18px;">Calculates the optical flow for a sparse feature set using the iterative Lucas-Kanade method with  
  2. pyramids.  
  3.   
  4. //通过<a target="_blank" href="http://en./wiki/Lucas%E2%80%93Kanade_method">Lucas-Kanade</a>方法与图像金字塔的结合,计算稀疏特征集合的光流</span>  

  1. <span style="font-size:18px;">void cvCalcOpticalFlowPyrLK(  
  2.                             const CvArr* prev,  
  3.                             const CvArr* curr,  
  4.                             CvArr* prevPyr,  
  5.                             CvArr* currPyr,  
  6.                             const CvPoint2D32f* prevFeatures,  
  7.                             CvPoint2D32f* currFeatures,  
  8.                             int count,  
  9.                             CvSize winSize,  
  10.                             int level,  
  11.                             char* status,  
  12.                             float* track error,  
  13.                             CvTermCriteria criteria,  
  14.                             int flags );</span>  

prev First frame, at time t  //取t时刻为第一帧

curr Second frame, at time t + dt //第二帧出现在 t + dt时刻

prevPyr Buffer for the pyramid for the first frame. If the pointer is not NULL , the buffer must
have a sufficient size to store the pyramid from level 1 to level level ; the total size of
(image width+8)*image height/3 bytes is sufficient

//第一帧的图像金字塔的缓存之处。如果该指针不为空,该buffer必须有足够的空间来存储从第1层到第level 层的图像金字塔;prevPyr 指针所指的图像/矩阵的大小为(image width+8) * (image height/3) 就足够了。

currPyr Similar to prevPyr, used for the second frame //同上

prevFeatures Array of points for which the flow needs to be found //在数组中定义(当前帧中的)那些点是要在(下一帧)检测的

currFeatures Array of 2D points containing the calculated new positions of the input features
in the second image

//一个二维的点数组,用于存放输入的特征(就是prevFeatures )在第二帧中的新位置

count Number of feature points//特征点的数目

winSize Size of the search window of each pyramid level //每一层金字塔所有的搜索窗口的大小

level Maximal pyramid level number. If 0 , pyramids are not used (single level), if 1 , two levels
are used, etc

//最多有多少层金字塔。如果是0,就不用图像金字塔,如果是1,就有两层,以此类推。

status Array. Every element of the array is set to 1 if the flow for the corresponding feature has
been found, 0 otherwise

//是一个数组,对应点在第二帧中找到,那该位置就值为1,找不到就值为0. 

track error Array of double numbers containing the difference between patches around the
original and moved points. Optional parameter; can be NULL
criteria Specifies when the iteration process of finding the flow for each point on each pyramid
level should be stopped 

flags Miscellaneous flags:

CV LKFLOWPyr A READY pyramid for the first frame is precalculated before the call
CV LKFLOWPyr B READY pyramid for the second frame is precalculated before the call
CV LKFLOW INITIAL GUESSES array B contains initial coordinates of features before the
function call

demo:

  1. //cvCaclOpticalFlowPyrLk_demo  
  2. //mochen  
  3. //2011年7月26日20:23:42  
  4.   
  5. #include <stdio.h>  
  6. #include "cv.h"  
  7. #include "cxcore.h"  
  8. #include "highgui.h"  
  9.   
  10. #pragma comment(lib, "opencv_core220d.lib")  
  11. #pragma comment(lib, "opencv_highgui220d.lib")  
  12. #pragma comment(lib, "opencv_imgproc220d.lib")  
  13. #pragma comment(lib, "opencv_calib3d220d.lib")  
  14. #pragma comment(lib, "opencv_features2d220d.lib")  
  15. #pragma comment(lib, "opencv_contrib220d.lib")  
  16. #pragma comment(lib, "opencv_ml220d.lib")   
  17. #pragma comment(lib, "opencv_video220d.lib")   
  18.   
  19.   
  20. #if 0  
  21. void cvCalcOpticalFlowPyrLK(  
  22.                             const CvArr* prev,  
  23.                             const CvArr* curr,  
  24.                             CvArr* prevPyr,  
  25.                             CvArr* currPyr,  
  26.                             const CvPoint2D32f* prevFeatures,  
  27.                             CvPoint2D32f* currFeatures,  
  28.                             int count,  
  29.                             CvSize winSize,  
  30.                             int level,  
  31.                             char* status,  
  32.                             float* track error,  
  33.                             CvTermCriteria criteria,  
  34.                             int flags );  
  35. #endif  
  36.   
  37. const int MAX_CORNERS = 1000 ;  
  38.   
  39. int main(int argc,char** argv)  
  40. {  
  41.     while ( 1 )  
  42.     {  
  43.         //use webcam   
  44.         CvCapture* cam = cvCaptureFromCAM( CV_CAP_ANY ) ;  
  45.         assert( NULL != cam ) ;  
  46.   
  47.         //get a color image   
  48.         IplImage* frame = cvQueryFrame(cam) ;  
  49.   
  50.         CvSize img_sz = cvGetSize(frame);  
  51.         const int win_size = 10 ;  
  52.   
  53.         //convert the image to grey image  
  54.         IplImage* frame_prev = cvQueryFrame(cam) ;  
  55.         IplImage* img_prev = cvCreateImage(img_sz,IPL_DEPTH_8U,1) ;  
  56.         cvCvtColor( frame_prev,img_prev ,CV_BGR2GRAY);  
  57.   
  58.         //convert the image to grey image  
  59.         IplImage* frame_cur = cvQueryFrame(cam) ;  
  60.         IplImage* img_curr = cvCreateImage(img_sz,IPL_DEPTH_8U,1) ;  
  61.         cvCvtColor( frame_cur,img_curr ,CV_BGR2GRAY);  
  62.   
  63.         //create a imge to display result  
  64.         IplImage* img_res = cvCreateImage(img_sz,IPL_DEPTH_8U,1) ;  
  65.         for ( int y = 0 ; y < img_sz.height ; ++y )  
  66.         {  
  67.             uchar* ptr = (uchar*)( img_res->imageData + y * img_res->widthStep ) ;  
  68.             for ( int x = 0 ; x <img_res->width; ++x )  
  69.             {  
  70.                 ptr[x] = 255 ;  
  71.             }  
  72.         }  
  73.   
  74.         //get good features   
  75.         IplImage* img_eig = cvCreateImage(img_sz,IPL_DEPTH_32F,1) ;  
  76.         IplImage* img_temp = cvCreateImage(img_sz,IPL_DEPTH_32F,1) ;  
  77.         int corner_count = MAX_CORNERS ;  
  78.         CvPoint2D32f*  features_prev = new CvPoint2D32f[MAX_CORNERS] ;  
  79.   
  80.         cvGoodFeaturesToTrack(  
  81.             img_prev,  
  82.             img_eig,  
  83.             img_temp,  
  84.             features_prev,  
  85.             &corner_count,  
  86.             0.01,  
  87.             5.0,  
  88.             0,  
  89.             3,  
  90.             0,  
  91.             0.4  
  92.             );  
  93.   
  94.         cvFindCornerSubPix(  
  95.             img_prev,  
  96.             features_prev,  
  97.             corner_count,  
  98.             cvSize(win_size,win_size),  
  99.             cvSize(-1,-1),  
  100.             cvTermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER,20,0.03)  
  101.             );  
  102.   
  103.         // L-K   
  104.         char feature_found[ MAX_CORNERS ] ;  
  105.         float feature_errors[ MAX_CORNERS ] ;  
  106.   
  107.         CvSize pyr_sz = cvSize( frame->width + 8 ,frame->height / 3 ) ;  
  108.   
  109.         IplImage* pyr_prev = cvCreateImage(img_sz,IPL_DEPTH_32F,1) ;  
  110.         IplImage* pyr_cur = cvCreateImage(img_sz,IPL_DEPTH_32F,1) ;  
  111.         CvPoint2D32f*  features_cur = new CvPoint2D32f[ MAX_CORNERS ] ;  
  112.   
  113.         cvCalcOpticalFlowPyrLK(  
  114.             img_prev,  
  115.             img_curr,  
  116.             pyr_prev,  
  117.             pyr_cur,  
  118.             features_prev,  
  119.             features_cur,  
  120.             corner_count,  
  121.             cvSize(win_size,win_size),  
  122.             5,  
  123.             feature_found,  
  124.             feature_errors,  
  125.             cvTermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER,20,0.3),  
  126.             0  
  127.             );  
  128.   
  129.         for ( int i = 0 ; i < corner_count ; i++)  
  130.         {  
  131.             if ( 0 == feature_found[i] || feature_errors[i] > 550 )  
  132.             {  
  133.                 printf("error is %f \n" , feature_errors[i] ) ;  
  134.                 continue ;  
  135.             }  
  136.   
  137.             printf("find it !\n") ;  
  138.   
  139.             CvPoint pt_prev = cvPoint( features_prev[i].x , features_prev[i].y ) ;  
  140.             CvPoint pt_cur = cvPoint( features_cur[i].x , features_cur[i].y ) ;  
  141.   
  142.             cvLine( img_res,pt_prev,pt_cur,CV_RGB( 255,0,0),2 );  
  143.         }  
  144.   
  145.         const char* window_prev = "img_prev" ;  
  146.         const char* window_curr = "img_curr" ;  
  147.         const char* window_res = "result" ;  
  148.         cvNamedWindow(window_prev,CV_WINDOW_AUTOSIZE);  
  149.         cvNamedWindow(window_curr,CV_WINDOW_AUTOSIZE);  
  150.         cvNamedWindow(window_res,CV_WINDOW_AUTOSIZE);  
  151.   
  152.         cvShowImage( window_prev,img_prev );  
  153.         cvShowImage( window_curr,img_curr );  
  154.         cvShowImage( window_res,img_res );  
  155.   
  156.         char opt = cvWaitKey( 10000 ) ;  
  157.         if ( 27 == opt )  
  158.         {  
  159.             break ;  
  160.         }  
  161.   
  162.         cvReleaseCapture( &cam );  
  163.         cvReleaseImage( &img_curr );  
  164.         cvReleaseImage( &img_eig );  
  165.         cvReleaseImage( &img_prev );  
  166.         cvReleaseImage( &img_res );  
  167.         cvReleaseImage( &img_temp );  
  168.         cvDestroyAllWindows() ;  
  169.     }  
  170.   
  171.   
  172.     return 0 ;  
  173. }  

结果:



    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多