1. 程式人生 > >光流法-運動目標的檢測(opencv學習)

光流法-運動目標的檢測(opencv學習)

        cvCalcOpticalFlowPyrLK 函式在使用時,首先要確定特徵點,也就是目標舊的位置。

        本程式通過使用cvGoodFeaturesToTrack 函式選擇角點作為特徵點。

        本程式只是一個簡單的運動檢測,在具體應用過程中,可以根據自己的需要修正

#include <cv.h>
#include <highgui.h>

int main (int argc, char **argv)

 CvCapture* capture = 0;
 capture = cvCaptureFromCAM(  CV_CAP_ANY ); 

 
 int i;

 int corner_count = 1000; 

 CvTermCriteria criteria; 
 criteria = cvTermCriteria (CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 64, 0.01);

 IplImage *src_img1;
 IplImage *src_img2;

 IplImage *dst_img;
 IplImage *pre_img;
 

 IplImage *eig_img;
 IplImage *temp_img;

 IplImage *prev_pyramid;
 IplImage *curr_pyramid;

 CvPoint2D32f *corners1;
 CvPoint2D32f *corners2; 
 corners1 = (CvPoint2D32f *) cvAlloc (corner_count * sizeof (CvPoint2D32f)); 
 corners2 = (CvPoint2D32f *) cvAlloc (corner_count * sizeof (CvPoint2D32f));
 cvNamedWindow ("Image", 1);
 char *status;
 status = (char *) cvAlloc (corner_count); 
 while (1)
 {
  
  pre_img = cvQueryFrame(capture);

  CvSize img_sz = cvGetSize(pre_img);
  src_img1 = cvCreateImage(img_sz, IPL_DEPTH_8U, 1);
  cvCvtColor(pre_img, src_img1, CV_RGB2GRAY);

  dst_img = cvQueryFrame(capture);
  src_img2 = cvCreateImage(img_sz, IPL_DEPTH_8U, 1);
  cvCvtColor(dst_img, src_img2, CV_RGB2GRAY);
  
  eig_img = cvCreateImage (img_sz, IPL_DEPTH_32F, 1);
  temp_img = cvCreateImage (img_sz, IPL_DEPTH_32F, 1);

  prev_pyramid = cvCreateImage (cvSize (src_img1->width + 8, src_img1->height / 3), IPL_DEPTH_8U, 1); 
  curr_pyramid = cvCreateImage (cvSize (src_img1->width + 8, src_img1->height / 3), IPL_DEPTH_8U, 1);


   
  cvGoodFeaturesToTrack (src_img1, eig_img, temp_img, corners1, &corner_count, 0.001, 5, NULL);

  cvCalcOpticalFlowPyrLK (src_img1, src_img2, prev_pyramid, curr_pyramid,                      
   corners1, corners2, corner_count, cvSize (10, 10), 4, status, NULL, criteria, 0); 

  for (i = 0; i < corner_count; i++)
  {   
   if (status[i])     
    cvLine (dst_img, cvPointFrom32f (corners1[i]), cvPointFrom32f (corners2[i]), CV_RGB (255, 0, 0), 1, CV_AA, 0); 
  } 
  
  cvShowImage ("Image", dst_img); 
  cvWaitKey (1);
  cvReleaseImage (&src_img1); 
  cvReleaseImage (&src_img2);
  cvReleaseImage (&eig_img);
  cvReleaseImage (&temp_img); 
  cvReleaseImage (&prev_pyramid); 
  cvReleaseImage (&curr_pyramid);

 }

 cvDestroyWindow ("Image");
 cvReleaseImage (&dst_img);
 cvReleaseImage(&pre_img);
 return 0;
}