opencv学习之路(6)、鼠标截图,滑动条播放视频

一、鼠标截图

 #include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std; Mat img=imread("E://1.jpg");
Mat temp=img.clone();
Mat ROI;
Point pt;
bool flag=false; void onMouse(int event,int x,int y,int flag,void* param){
switch (event)
{
case CV_EVENT_LBUTTONDOWN://鼠标左键按下
//cout<<"鼠标左键按下"<<endl;
flag=true;
pt.x=x;
pt.y=y;
break;
case CV_EVENT_MOUSEMOVE://鼠标移动
//cout<<"鼠标移动"<<endl;
if(flag){
temp.copyTo(img);//temp复制给img(相当于恢复成原图),以便永远只有一个矩形(否则许多矩形会重叠在一起)
rectangle(img,pt,Point(x,y),Scalar(,,),,);
}
break;
case CV_EVENT_LBUTTONUP://鼠标左键抬起
//cout<<"鼠标抬起"<<endl;
flag=false;
//ROI=img(Rect(pt.x,pt.y,x-pt.x,y-pt.y));//确定ROI区域,有矩形边框
ROI=temp(Rect(pt.x,pt.y,x-pt.x,y-pt.y));//无矩形边框
imshow("ROI",ROI);
imwrite("E://ROI.jpg",ROI);
break;
default:
break;
}
} void main(){
namedWindow("mouse",CV_WINDOW_AUTOSIZE);
setMouseCallback("mouse",onMouse,);
while(){
imshow("mouse",img);
if(==waitKey())//esc跳出循环
break;
}
}

opencv学习之路(6)、鼠标截图,滑动条播放视频

二、滑动条视频播放

 #include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std; int value;
void onChange(int,void* param){
VideoCapture cap=*(VideoCapture*)param;
cap.set(CV_CAP_PROP_POS_FRAMES,value);//设置视频帧位置
} void main(){
Mat frame;
char strFps[];
VideoCapture cap("E://2.avi");
namedWindow("video",CV_WINDOW_AUTOSIZE);
int frameCount=cap.get(CV_CAP_PROP_FRAME_COUNT);//获取视频总帧数
createTrackbar("Frame","video",&value,frameCount,onChange,&cap); if(cap.isOpened()){//如果视频成功打开
while(){
double Fps=cap.get(CV_CAP_PROP_FPS);//获得视频帧率
sprintf(strFps,"Fps%0.1f/s",Fps);//格式化字符串
int framePos=cap.get(CV_CAP_PROP_POS_FRAMES);//获取视频帧位置
setTrackbarPos("Frame","video",framePos);//设置滑动条位置
cap>>frame;
if(!frame.empty()){//如果该帧不为空
putText(frame,strFps,Point(,),CV_FONT_HERSHEY_COMPLEX_SMALL,,Scalar(,,),,);
imshow("video",frame);
if(==waitKey(/Fps))//esc退出,1000/Fps毫秒刷新
break;
}
}
}
}

opencv学习之路(6)、鼠标截图,滑动条播放视频

上一篇:【LOJ】#2186. 「SDOI2015」道路修建


下一篇:《Effect Java》 归纳总结