最近在做 opencv 处理视频,在博客上找到许多有用的资料,此篇博客做个总结(介绍两种播放视频的方式):
编译环境:VS2015 + opencv3.3
- 转自:博客
稍微完善了一下(进度条可动态变化、空格暂停、esc 退出)
结果预览:
#include <iostream>
#include <opencv2\opencv.hpp>
#include "opencv\highgui.h"
using namespace std;
using namespace cv;
CvCapture* g_capture = NULL;
int g_trackbar_pos = 0; // global trackbar position recorder
void onTrackbarSlide(int pos)
{
cvSetCaptureProperty(
g_capture,
CV_CAP_PROP_POS_FRAMES,
pos
);
g_trackbar_pos = pos;
}
int main()
{
cvNamedWindow("VideoSlider", CV_WINDOW_AUTOSIZE);
g_capture = cvCreateFileCapture("D:\\FFOutput\\V000.mp4");
int frames = (int)cvGetCaptureProperty(
g_capture,
CV_CAP_PROP_FRAME_COUNT);
IplImage* frame;
while (1)
{
frame = cvQueryFrame(g_capture);
if (!frame) break;
cvCreateTrackbar(
"Position",
"VideoSlider",
&g_trackbar_pos,
frames,
onTrackbarSlide);
cvShowImage("VideoSlider", frame);
setTrackbarPos("Position", "VideoSlider", g_trackbar_pos); // set trackbar position
char c = waitKey(33);
g_trackbar_pos++; // let trackbar moving
if (c == 27) break;//"Esc"退出
if (c == 32) waitKey(0);//空格实现暂停
}
cvReleaseCapture(&g_capture);
cvDestroyWindow("VideoSlider");
return 0;
}
- 简单一点的,转自 博客
稍微完善了一下(空格暂停、esc 退出)
#include <iostream>
#include <opencv2\opencv.hpp>
#include "opencv\highgui.h"
using namespace std;
using namespace cv;
void main()
{
VideoCapture capture;
capture.open("D:\\FFOutput\\V000.mp4");
if (!capture.isOpened())
{
cout<<"can not open ...\n";
return;
}
cvNamedWindow("play vedio");
long totalFrameNumber = capture.get(CV_CAP_PROP_FRAME_COUNT);//获取视频的总帧数
long i = 1;
while (true)
{
Mat frame;
capture >> frame;
if (i > totalFrameNumber) break;i++;
imshow("play vedio", frame);
char c = waitKey(33);
if (c == 27) break; //按“Esc”退出
if (c == 32) waitKey(0);//空格实现暂停
}
capture.release();
cvDestroyWindow("play vedio");
}
- 遇到的几个问题及其解决方案:
3.1 !capture.isOpened() 为false 导致播放不了视频
解决方案:把 opencv_ffmpeg330_64.dll 文件贴到工程同.cpp文件目录下