欢迎关注,定期更新
这一篇贴一下opencv如何播放视频,没什么大的技术性,只是单纯的增加一下学习乐趣。先看最原始的播放代码:
cvNamedWindow("play vedio");
CvCapture* capture=cvCreateFileCapture("D:\\KuGou\\NumberNine.mkv");
IplImage* frame;
while(1)
{
frame=cvQueryFrame(capture);
if(!frame) break;
cvShowImage("play vedio",frame);
if((char)cvWaitKey(33)==27) break;
}
cvReleaseCapture(&capture);
cvDestroyWindow("play vedio");
下边增加一个滚动条来控制播放,
int g_slider_position=0;
CvCapture* g_capture=NULL;
void onTrackbarSlider(int pos)
{
cvSetCaptureProperty(g_capture,CV_CAP_PROP_POS_FRAMES,pos);
}
cvNamedWindow("play vedio");
g_capture=cvCreateFileCapture("D:\\KuGou\\NumberNine.mkv");
int frames=(int)cvGetCaptureProperty(g_capture,CV_CAP_PROP_FRAME_COUNT);
if(frames!=0)
{
cvCreateTrackbar("进度","play vedio",&g_slider_position,frames,onTrackbarSlider);
}
IplImage* frame;
while(1)
{
frame=cvQueryFrame(g_capture);
if(!frame) break;
cvShowImage("play vedio",frame);
if((char)cvWaitKey(33)==27) break;
}
cvReleaseCapture(&g_capture);
cvDestroyWindow("play vedio");
CV_CAP_PROP_FRAME_COUNT是一个枚举类型,此参数配合cvGetCaptureProperty获取视频帧数;
CV_CAP_PROP_POS_FRAMES表示我们以帧数来设定读入位置;
当滚动条改变时,回调函数来实现相应的动作。上边的程序运行时会发现滚动条是不动的,这里我们加2条语句让滚动条随视频滚动。
g_slider_position=(int)cvGetCaptureProperty(g_capture,CV_CAP_PROP_POS_FRAMES);
setTrackbarPos("进度","play vedio",g_slider_position);
// opencv_test.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include
#include
using namespace std;
using namespace cv;
int g_slider_position=0;
CvCapture* g_capture=NULL;
void onTrackbarSlider(int,void*)
{
cvSetCaptureProperty(g_capture,CV_CAP_PROP_POS_FRAMES,g_slider_position);
}
int _tmain(int argc, _TCHAR* argv[])
{
//play vedio
cvNamedWindow("play vedio");
g_capture=cvCreateFileCapture("D:\\KuGou\\NumberNine.mkv");
int frames=(int)cvGetCaptureProperty(g_capture,CV_CAP_PROP_FRAME_COUNT);
if(frames!=0)
{
createTrackbar("进度","play vedio",&g_slider_position,frames,onTrackbarSlider);
}
IplImage* frame;
while(1)
{
frame=cvQueryFrame(g_capture);
g_slider_position=(int)cvGetCaptureProperty(g_capture,CV_CAP_PROP_POS_FRAMES);
setTrackbarPos("进度","play vedio",g_slider_position);
if(!frame) break;
cvShowImage("play vedio",frame);
if((char)cvWaitKey(33)==27) break;
}
cvReleaseCapture(&g_capture);
cvDestroyWindow("play vedio");
return 0;
}
这一篇就简单的介绍这些,