一、鼠标截图
1 #include<opencv2/opencv.hpp>
2 #include<iostream>
3 using namespace cv;
4 using namespace std;
5
6 Mat img=imread("E://1.jpg");
7 Mat temp=img.clone();
8 Mat ROI;
9 Point pt;
10 bool flag=false;
11
12 void onMouse(int event,int x,int y,int flag,void* param){
13 switch (event)
14 {
15 case CV_EVENT_LBUTTONDOWN://鼠标左键按下
16 //cout<<"鼠标左键按下"<<endl;
17 flag=true;
18 pt.x=x;
19 pt.y=y;
20 break;
21 case CV_EVENT_MOUSEMOVE://鼠标移动
22 //cout<<"鼠标移动"<<endl;
23 if(flag){
24 temp.copyTo(img);//temp复制给img(相当于恢复成原图),以便永远只有一个矩形(否则许多矩形会重叠在一起)
25 rectangle(img,pt,Point(x,y),Scalar(0,255,0),2,8);
26 }
27 break;
28 case CV_EVENT_LBUTTONUP://鼠标左键抬起
29 //cout<<"鼠标抬起"<<endl;
30 flag=false;
31 //ROI=img(Rect(pt.x,pt.y,x-pt.x,y-pt.y));//确定ROI区域,有矩形边框
32 ROI=temp(Rect(pt.x,pt.y,x-pt.x,y-pt.y));//无矩形边框
33 imshow("ROI",ROI);
34 imwrite("E://ROI.jpg",ROI);
35 break;
36 default:
37 break;
38 }
39 }
40
41 void main(){
42 namedWindow("mouse",CV_WINDOW_AUTOSIZE);
43 setMouseCallback("mouse",onMouse,0);
44 while(1){
45 imshow("mouse",img);
46 if(27==waitKey(10))//esc跳出循环
47 break;
48 }
49 }
二、滑动条视频播放
1 #include<opencv2/opencv.hpp>
2 #include<iostream>
3 using namespace cv;
4 using namespace std;
5
6 int value;
7 void onChange(int,void* param){
8 VideoCapture cap=*(VideoCapture*)param;
9 cap.set(CV_CAP_PROP_POS_FRAMES,value);//设置视频帧位置
10 }
11
12 void main(){
13 Mat frame;
14 char strFps[20];
15 VideoCapture cap("E://2.avi");
16 namedWindow("video",CV_WINDOW_AUTOSIZE);
17 int frameCount=cap.get(CV_CAP_PROP_FRAME_COUNT);//获取视频总帧数
18 createTrackbar("Frame","video",&value,frameCount,onChange,&cap);
19
20 if(cap.isOpened()){//如果视频成功打开
21 while(1){
22 double Fps=cap.get(CV_CAP_PROP_FPS);//获得视频帧率
23 sprintf(strFps,"Fps%0.1f/s",Fps);//格式化字符串
24 int framePos=cap.get(CV_CAP_PROP_POS_FRAMES);//获取视频帧位置
25 setTrackbarPos("Frame","video",framePos);//设置滑动条位置
26 cap>>frame;
27 if(!frame.empty()){//如果该帧不为空
28 putText(frame,strFps,Point(5,30),CV_FONT_HERSHEY_COMPLEX_SMALL,1,Scalar(0,255,0),2,8);
29 imshow("video",frame);
30 if(27==waitKey(1000/Fps))//esc退出,1000/Fps毫秒刷新
31 break;
32 }
33 }
34 }
35 }