本篇主要是介绍opencv下的视频播放,视频快进、快退和暂停的实现。
具体代码如下:
#include<cstring> #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <stdio.h> using namespace std; using namespace cv; double tmp; int main(int argc, char **argv){ Mat frame; bool stop(false); VideoCapture capture(argv[1]); if (!capture.isOpened()){ return 0; } while(!stop){ if (!capture.read(frame)){ break; } // show foreground imshow("Video", frame); if (waitKey(50) == ‘q‘){ stop = true; } } return 0; }
1、首先打开视频文件。
VideoCapture capture(argv[1]); if (!capture.isOpened()){ return 0; }
2、接着使用capture.read(frame)不断读取每帧视频文件数据,放入到frame中,之后imshow显示出当前帧数据,使用waitkey等待50ms,看用户是否 输入键值‘q’,如果有,则直接退出视频播放,若没有,则继续播放下一帧,直到视频文件播放完后程序退出。
while(!stop){ if (!capture.read(frame)){ break; } // show foreground imshow("Video", frame); if (waitKey(50) == ‘q‘){ stop = true; } }
具体效果如下:
在本例中实现了video播放简单的功能,如显示当前播放进度,快进、快退和暂停功能。
#include<cstring> #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <stdio.h> #include <stdlib.h> #include <opencv2/core/core.hpp> #include <math.h> #include <string.h> #include <opencv/cv.h> #define window_name "video" #define trackbar_type "progress" #define trackbar_speed "speed" using namespace std; using namespace cv; CvCapture *capture; int max_fps = 0, cur_fps = 0, tmp_fps = 0; int bar_num = 0, bar_max = 100; int video_speed = 10, video_maxspeed = 300; void fps_change(int, void* ){ cur_fps = bar_num * tmp_fps; cvSetCaptureProperty(capture, CV_CAP_PROP_POS_FRAMES, cur_fps); } void speed_change(int, void* ){ } int main(int argc, char **argv){ Mat frame; bool stop(false); VideoCapture capture; capture.open(argv[1]); if (!capture.isOpened()){ return 0; } while(!stop){ if (!capture.read(frame)){ break; } max_fps += 1; if (waitKey(1) == ‘q‘){ stop = true; } } capture.release(); stop = false; tmp_fps = max_fps / 100; capture.open(argv[1]); if (!capture.isOpened()){ return 0; } namedWindow(window_name); createTrackbar(trackbar_type, window_name, &bar_num, bar_max, fps_change); createTrackbar(trackbar_speed, window_name, &video_speed, video_maxspeed, speed_change); while(cur_fps <= max_fps){ if (!capture.read(frame)){ break; } cur_fps += 1; bar_num = cur_fps / tmp_fps; if(cur_fps % tmp_fps == 0){ cvSetTrackbarPos(trackbar_type, window_name, bar_num); } imshow(window_name,frame); if(cvWaitKey(video_speed) == ‘q‘) break; } <a href="http://www.opengroup.org/onlinepubs/%3Cspan%20class=" nu19"="" style="text-decoration: none; color: rgb(11, 0, 128); background-image: none; background-position: initial initial; background-repeat: initial initial;">009695399/functions/printf.html">printf("max_fps:%d, cur_fps:%d\n", max_fps, cur_fps); destroyWindow("video"); return 0; }
这部分主要是使用了两个Trackbar来实现,第一个Trackbar用来显示和拖动当前播放进度。第二个Trackbar用来设置视频播放的快进、慢放和暂停。 1、这里在播放之前需要获得当前视频文件的帧数。 opencv提供了如下函数,可以获得视频文件帧数,但是该函数并不一定准确。 int numFrames = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_COUNT); 所以最准确的办法,是将视频文件帧都取出来一遍,然后获得它们的帧总数,不过该方法耗时间很多,影响用户体验。
while(!stop){ if (!capture.read(frame)){ break; } max_fps += 1; if (waitKey(1) == ‘q‘){ stop = true; } } capture.release(); stop = false;
2、计算保存1%的总帧数,然后用这个为基准,计算当前视频播放的百分比,并在第一个Trackbar(progress)中显示出来。同时也可以通过拖动 第一个Trackbar(progress)来进行播放进度控制。
void fps_change(int, void* ){ cur_fps = bar_num * tmp_fps; cvSetCaptureProperty(capture, CV_CAP_PROP_POS_FRAMES, cur_fps); } tmp_fps = max_fps / 100; createTrackbar(trackbar_type, window_name, &bar_num, bar_max, fps_change); cur_fps += 1; bar_num = cur_fps / tmp_fps; if(cur_fps % tmp_fps == 0){ cvSetTrackbarPos(trackbar_type, window_name, bar_num); }
3、通过变量video_speed,在第二个Trackbar(speed)用来控制视频播放的速度,当video_speed数值越大,则waitkey等待时间越长,播放速度越慢; 同样的,当video_speed数值越小,则播放速度越快。但是需要注意:如果video_speed为0,则表示waitkey一直等待,则video播放暂停。
int video_speed = 10, video_maxspeed = 300; createTrackbar(trackbar_speed, window_name, &video_speed, video_maxspeed, speed_change); if(cvWaitKey(video_speed) == ‘q‘) break;
具体效果如下:
原文地址:http://blog.csdn.net/u011630458/article/details/45849363