Opencv打开摄像头VideoCapture 以及鼠标画图事件

来源:互联网 发布:sja1000编程 编辑:程序博客网 时间:2024/05/16 17:32

Opencv打开摄像头VideoCapture 以及鼠标画图事件

#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>#include "opencv2/opencv.hpp"   #include <iostream>using namespace cv;using namespace std;Rect box; bool gotBox = false;    Mat image;char temp[16];  //event鼠标事件代号,x,y鼠标坐标,flags拖拽和键盘操作的代号  void mouseHandler(int event, int x, int y, int flags, void *param){    static Point pre_pt = (-1,-1);//初始坐标      static Point cur_pt = (-1,-1);//实时坐标      if (event == CV_EVENT_LBUTTONDOWN)//左键按下,读取初始坐标,并在图像上该点处划圆      {            sprintf(temp,"(%d,%d)",x,y);          pre_pt = Point(x,y);          putText(image,temp,pre_pt,FONT_HERSHEY_SIMPLEX,0.5,Scalar(0,0,0,255),1,8);//在窗口上显示坐标          circle(image,pre_pt,2,Scalar(255,0,0,0),CV_FILLED,CV_AA,0);//划圆          imshow("Test",image);      }      else if (event == CV_EVENT_MOUSEMOVE && !(flags & CV_EVENT_FLAG_LBUTTON))//左键没有按下的情况下鼠标移动的处理函数      {            sprintf(temp,"(%d,%d)",x,y);          cur_pt = Point(x,y);          putText(image,temp,cur_pt,FONT_HERSHEY_SIMPLEX,0.5,Scalar(0,0,0,255));//只是实时显示鼠标移动的坐标          imshow("Test",image);      }      else if (event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON))//左键按下时,鼠标移动,则在图像上划矩形      {          sprintf(temp,"(%d,%d)",x,y);          cur_pt = Point(x,y);          putText(image,temp,cur_pt,FONT_HERSHEY_SIMPLEX,0.5,Scalar(0,0,0,255));          rectangle(image,pre_pt,cur_pt,Scalar(0,255,0,0),1,8,0);//在临时图像上实时显示鼠标拖动时形成的矩形          imshow("Test",image);      }      else if (event == CV_EVENT_LBUTTONUP)//左键松开,将在图像上划矩形      {          sprintf(temp,"(%d,%d)",x,y);          cur_pt = Point(x,y);          putText(image,temp,cur_pt,FONT_HERSHEY_SIMPLEX,0.5,Scalar(0,0,0,255));          circle(image,pre_pt,2,Scalar(255,0,0,0),CV_FILLED,CV_AA,0);          rectangle(image,pre_pt,cur_pt,Scalar(0,255,0,0),1,8,0);//根据初始点和结束点,将矩形画到img上          //截取矩形包围的图像,并保存到dst中          int width = abs(pre_pt.x - cur_pt.x);          int height = abs(pre_pt.y - cur_pt.y);          if (width == 0 || height == 0)          {              printf("width == 0 || height == 0");              return;          }          box=Rect(pre_pt.x,pre_pt.y,width,height);        gotBox=true;        imshow("Test",image);     }}int main(int argc, char * argv[]){    VideoCapture capture;    capture.open(0);    capture.set(CV_CAP_PROP_FRAME_WIDTH, 340);    capture.set(CV_CAP_PROP_FRAME_HEIGHT, 240);    // 判断摄像头是否打开    if (!capture.isOpened())    {        cout << "capture device failed to open!" << endl;        return 1;    }    namedWindow("Test", CV_WINDOW_AUTOSIZE);    // 注册鼠标画图事件    setMouseCallback("Test", mouseHandler, NULL);    Mat frame;    Mat first_gray;    while(!gotBox)    {        capture >> frame;        frame.copyTo(image);        if (cvWaitKey(33) == 27) {  return 0; }    }    // 取消回调事件    setMouseCallback("Test", NULL, NULL);    printf("Initial Tracking Box = x:%d y:%d h:%d w:%d\n", box.x, box.y, box.width, box.height);    // Run-time    Mat current_gray;    while(capture.read(frame))    {        frame.copyTo(image);        cvtColor(frame, current_gray, CV_RGB2GRAY);        //processFrame(current_gray, box);//图像处理过程        // 画矩形框        rectangle(frame, box, Scalar(0,0,255));        imshow("Test", frame);        if(cvWaitKey(20)==27){break;}    }      return 0;}
0 0
原创粉丝点击