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