opencv进度条控制视频进度
2020-01-21 14:53阅读:
君和
博主很神秘,什么也没有留下~
关注
//Example 2-4. Adding a trackbar slider to the basic viewer window
for moving around
//within the video file
#include 'opencv2/highgui/highgui.hpp'
#include 'opencv2/imgproc/imgproc.hpp'
#include
#include
using namespace std;
int g_slider_position = 0;//定义一个全局变量存储滑动条位置
int g_run = 1, g_dontset = 0; //g_run
在新的跳转触发之后置0。它为正的时候,指示在停止之前程序要播放多少张图片;当它为负的时候,表示系统处于连续视频播放模式
//定义g_dontset避免调整进度条位置时候触发单步模式
cv::VideoCapture g_cap;
void onTrackbarSlide(int pos, void *)
{//定义一个用户调整滑动条的时候执行的回调程序
g_cap.set(cv::CAP_PROP_POS_FRAMES, pos);//移动到想要的位置
if (!g_dontset) g_run =
1;//是否进入单步模式,用户触发滑动条事件的时候,这个设置才会生效,系统自动调用该回调函数的时候,单步模式不会生效
g_dontset = 0;
}
int main(int argc, char** argv) {
cv::namedWindow('Example 2-4', cv::WINDOW_AUTOSIZE);
g_cap.open('D:\\123.mp4');//文件路径
int frames = (int)g_cap.get(cv::CAP_PROP_FRAME_COUNT);
int tmpw = (int)g_cap.get(cv::CAP_PROP_FRAME_WIDTH);
int tmph = (int)g_cap.get(cv::CAP_PROP_FRAME_HEIGHT);//.get()来
设置总的长宽高
cout << 'Video has ' << frames << ' frames of
dimensions('
<< tmpw << ', ' << tmph << ').' <<
endl;
cv::createTrackbar(//创建一个滑动条
'Position',
'Example 2-4',//,这个函数允许我们给滑动条一个标签
&g_slider_position,//,指明在哪一个窗口放置滑动条
frames,//滑动条能够达到的最大值
onTrackbarSlide//一个在滑动条移动时候的会调函数(不需要的时候也可以输入NULL)
);
cv::Mat frame;
for (;;) {
if (g_run != 0) {
g_cap >> frame;
if (frame.empty()) break;
int current_pos =
(int)g_cap.get(cv::CAP_PROP_POS_FRAMES);//.get()读取播放进度
g_dontset = 1;
cv::setTrackbarPos('Position', 'Example 2-4',
current_pos);//设置播放进度
cv::imshow('Example 2-4', frame);
g_run -= 1;
}
char c = (char)cv::waitKey(10);
if (c == 's') { // single step
g_run = 1;
cout << 'Single step, run = ' << g_run <<
endl;
}
if (c == 'r') { // run mode
g_run = -1;
cout << 'Run mode, run = ' << g_run <<
endl;
}
if (c == 27) break;
}
system('pause');
return(0);
}