现在的位置: 首页 > 综合 > 正文

学习OpenCV——初始化视频第一帧 .

2013年09月05日 ⁄ 综合 ⁄ 共 5569字 ⁄ 字号 评论关闭

本文转自http://blog.csdn.net/sangni007/article/details/8112486

在很多视频跟踪或分割中,总是需要初始化第一帧,即在第一帧上画一个框,或者标注前景与背景啊等等,今天就把初始化第一帧,在第一帧上画一个框的代码实现了一下,顺便复习一下OpenCV里面的鼠标召回事件,在此贴上代码,与大家分享,也方便以后查找。

  1. #include "opencv2/opencv.hpp"
      
  2. #include "opencv2/highgui/highgui.hpp"  
  3. #include <string>
      
  4. #include <iostream>   
  5. #include <stdio.h>
      
  6.   
  7. using namespace cv;  
  8. using namespace std;  
  9.   
  10. //最多对一个对象进行跟踪;   
  11. #define MAX_OBJECTS 1
      
  12.   
  13. //传递参数的class   
  14. class params  
  15. {  
  16. public:  
  17.       
  18.     Point loc1,loc2;  
  19.     string win_name;  
  20.     Mat src;  
  21.     Mat cur;  
  22.     int n;                  //记录对象个数  
  23.     params() : n(0) {}  
  24.     //  ~params();   
  25. };  
  26.   
  27. //鼠标事件   
  28. void mouseEvent(int event, int x, int y, int flags, void* param)    
  29. {  
  30.     static bool check_line_state = FALSE;  
  31.     Mat tmp;  
  32.     params* p = (params*)param;  
  33.     //按下鼠标左键,存储初始位置;   
  34.     if( event == CV_EVENT_LBUTTONDOWN )  
  35.     {  
  36.          if (p->n == MAX_OBJECTS)//判断是否已经有了跟踪对象;  
  37.         {  
  38.             cout<<"Fail! \n Only Can Tracking One Object!"<<endl;  
  39.             return ;  
  40.         }  
  41.         p->loc1.x = x;  
  42.         p->loc1.y = y;  
  43.         check_line_state = TRUE;  
  44.     }  
  45.   
  46.     //按下鼠标左键且鼠标移动,画矩形框;   
  47.     else if (check_line_state && event == CV_EVENT_MOUSEMOVE)  
  48.     {  
  49.         if (p->n == MAX_OBJECTS)//判断是否已经有了跟踪对象;  
  50.         {  
  51.             cout<<"Fail! \n Only Can Tracking One Object!"<<endl;  
  52.             return ;  
  53.         }  
  54.         p->src.copyTo(tmp);  
  55.         rectangle(tmp, p->loc1, Point(x,y), CV_RGB(255,0,0), 2, 8 );  
  56.         imshow(p->win_name,tmp);  
  57.     }  
  58.   
  59.     //左键弹起,画框结束;   
  60.     else if(event == CV_EVENT_LBUTTONUP)  
  61.     {  
  62.         if (p->n == MAX_OBJECTS)//判断是否已经有了跟踪对象;  
  63.         {  
  64.             cout<<"Fail! \n Only Can Tracking One Object!"<<endl;  
  65.             return ;  
  66.         }  
  67.         p->loc2.x = x;  
  68.         p->loc2.y = y;  
  69.         rectangle(p->src, p->loc1, Point(x,y), CV_RGB(255,0,0), 2, 8 );  
  70.         imshow(p->win_name,p->src);  
  71.         p->n++;  
  72.         check_line_state = FALSE;  
  73.     }  
  74. }  
  75.   
  76. //初始化第一帧,画出要跟踪的对象   
  77. void InitialVideo(Mat& src, Rect& rect)  
  78. {  
  79.     params p;  
  80.     src.copyTo(p.src);  
  81.     p.win_name = "Initial Window";  
  82.     imshow(p.win_name, src);  
  83.       
  84.     //鼠标召回事件   
  85.     setMouseCallback(p.win_name, &mouseEvent, &p);   
  86.     cout<<"draw rect & press any key to end"<<endl;  
  87.       
  88.     waitKey();  
  89.   
  90.     //把得到的位置赋给rect   
  91.     rect.x = min(p.loc1.x, p.loc2.x);  
  92.     rect.y = min(p.loc1.y, p.loc2.y);  
  93.     rect.width = abs(p.loc1.x - p.loc2.x);  
  94.     rect.height = abs(p.loc1.y - p.loc2.y);  
  95.   
  96.     destroyWindow("Initial Window");  
  97. }  
  98.   
  99. int main()  
  100. {  
  101.     //从默认摄像头输入   
  102.     VideoCapture cap(0);  
  103.     //从视频输入   
  104.     //string filename = "D:/soccer.avi";
      
  105.     //VideoCapture cap(filename);   
  106.   
  107.     if(!cap.isOpened())  // check if we succeeded  
  108.     {  
  109.         cout<<"couldn't open video file"<<endl;  
  110.         return -1;  
  111.     }  
  112.   
  113.     Mat frame;  
  114.     bool isFirstFrame = TRUE;  
  115.     Rect rect;  
  116.         cap>>frame;//可能是因为电脑问题,导致第一帧显示不出来,所以就把第一帧先读一次  
  117.     for (;;)  
  118.     {  
  119.         cap>>frame;  
  120.         //初始化第一帧   
  121.         if ( isFirstFrame)  
  122.         {  
  123.             InitialVideo(frame,rect);  
  124.              isFirstFrame = FALSE;  
  125.         }  
  126.         //其他操作   
  127.         rectangle(frame, rect, CV_RGB(255,0,0), 2, 8 );  
  128.         imshow("showSrc",frame);  
  129.         char key = (char)waitKey(5);  
  130.         switch (key)  
  131.         {  
  132.         case 27:  
  133.             return 0;  
  134.         case  ' ' :  
  135.             cout<<"save process"<<endl;  
  136.             break;  
  137.         default:  
  138.             break;  
  139.         }  
  140.     }  
  141. }  

 

 

抱歉!评论已关闭.