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

opencv2在视频帧序列中运用鼠标extract rectangle region

2018年10月24日 ⁄ 综合 ⁄ 共 2451字 ⁄ 字号 评论关闭

Refer from http://blog.csdn.net/qq_23880193/article/details/41974849

刚刚写好的在图像中运用鼠标事件,现在要写的是在视频序列中运用鼠标事件

  1. #include<opencv2/imgproc/imgproc.hpp>  
  2. #include<opencv2/imgproc/types_c.h>  
  3. #include<opencv2/highgui/highgui.hpp>  
  4. #include<opencv2/core/core.hpp>  
  5. #include<iostream>  
  6. #include<vector>  
  7. #include<opencv2/nonfree/features2d.hpp>  
  8. #include<opencv2/legacy/legacy.hpp>  
  9. #include<opencv2/features2d/features2d.hpp>  
  10. #include<opencv2/calib3d/calib3d.hpp>  
  11. #include<opencv2/video/tracking.hpp>  
  12.   
  13. using namespace cv;  
  14. using namespace std;  
  15.   
  16. Point coord;//储存初始坐标  
  17. Rect sqart;//储存矩形框的起始坐标以及长度和宽度  
  18. bool draw;  
  19. bool flag = 0;//这个标志位是用在如果要将矩形标定的部分单独显示在一个窗口时使用的  
  20. Mat frame;  
  21. Mat dst;//感兴趣区域图像  
  22.   
  23. void onMouse(int event, int x, int y, int flags, void *param)  
  24. {  
  25.     //显示鼠标的当前坐标  
  26.     cout << "Event:" << event << endl;  
  27.     cout << "x=" << x << "     " << "y=" << y << endl;  
  28.     cout << "flags:" << endl;  
  29.     cout << "param" << param << endl;  
  30.   
  31.     //这个if必须放在switch之前  
  32.     if (draw)  
  33.     {  
  34.         //用MIN得到左上点作为矩形框的其实坐标,如果不加这个,画矩形时只能向一个方向进行  
  35.         sqart.x = MIN(x, coord.x);  
  36.         sqart.y = MIN(y, coord.y);  
  37.         sqart.width = abs(coord.x - x);  
  38.         sqart.height = abs(coord.y - y);  
  39.         //防止矩形区域超出图像的范围  
  40.         sqart &= Rect(0, 0, frame.cols, frame.rows);  
  41.     }  
  42.   
  43.     switch (event)  
  44.     {  
  45.     case CV_EVENT_LBUTTONDOWN:  
  46.         coord = Point(x, y);  
  47.         sqart = Rect(x, y, 0, 0);  
  48.         draw = true;  
  49.         break;  
  50.   
  51.     case CV_EVENT_LBUTTONUP:  
  52.         draw = false;  
  53.         flag = 1;  
  54.         break;  
  55.     }  
  56. }  
  57.   
  58. int main()  
  59. {  
  60.     char c;  
  61.     VideoCapture capture(0);  
  62.     namedWindow("Mouse", 1);  
  63.   
  64.     setMouseCallback("Mouse", onMouse, 0);  
  65.   
  66.     //由于视频序列的每一帧都在跟新,所以不会出现连环嵌套的状况  
  67.     while (1)  
  68.     {  
  69.         capture >> frame;  
  70.         if (frame.empty())  
  71.             return -1;  
  72.   
  73.         //将矩形框得到矩形区域用另一个窗口显示  
  74.         if ((flag == 1) && sqart.height > 0 && sqart.width > 0)  
  75.         {  
  76.             dst = frame(Rect(sqart.x, sqart.y, sqart.width, sqart.height));  
  77.             namedWindow("dst");  
  78.             imshow("dst", dst);  
  79.             flag = 0;  
  80.         }  
  81.   
  82.         rectangle(frame, sqart, Scalar(0, 0, 255), 3);  
  83.   
  84.         imshow("Mouse", frame);  
  85.   
  86.         c = waitKey(20);  
  87.         if (c == 27)  
  88.             break;  
  89.     }  
  90.   
  91.     return 0;  
  92. }  
  1. 以上程序是以打开摄像头为例的,由于没有好的背景,下面的截图是用的一个视频段截出来的结果,程序中只要把capture.open(0)改为capture.open(视频路径)即可。  

抱歉!评论已关闭.