如何使用OpenCV从磁盘播放视频文件? [英] How to play a video file from my disk using OpenCV.?
问题描述
请帮助我完成上述任务.我是openCV的新手.我的系统中安装了OpenCV 2.2,并使用VC ++ 2010 Express作为IDE.我的笔记本电脑中没有内置摄像头... 只是我学会了如何加载图像.我非常渴望从磁盘上加载视频文件(最好是mp4,flv格式),并希望使用openCV播放它.
Please help me in achieving above task.I'm newbie to openCV. I have OpenCV 2.2 installed in my system and using VC++ 2010 Express as IDE. I don't have inbuilt webcam in my laptop... just i learnt how to load image. I'm very eager to load a video file from my disk(preferably mp4 , flv format) and wish to play it using openCV.
推荐答案
使用OpenCV的 C 接口(在Windows机器上对我来说效果更好),加载视频文件的功能是cvCaptureFromAVI()
.之后,您需要使用传统的循环来通过cvQueryFrame()
检索帧,然后通过cvShowImage()
将其显示在使用cvNamedWindow()
创建的窗口中.
Using the C interface of OpenCV (which have worked better for me on Windows boxes), the function to load the video file is cvCaptureFromAVI()
. After that, you need to use the traditional loop to retrieve frames throughcvQueryFrame()
and then cvShowImage()
to display them on a window created with cvNamedWindow()
.
CvCapture *capture = cvCaptureFromAVI("video.avi");
if(!capture)
{
printf("!!! cvCaptureFromAVI failed (file not found?)\n");
return -1;
}
int fps = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
printf("* FPS: %d\n", fps);
cvNamedWindow("display_video", CV_WINDOW_AUTOSIZE);
IplImage* frame = NULL;
char key = 0;
while (key != 'q')
{
frame = cvQueryFrame(capture);
if (!frame)
{
printf("!!! cvQueryFrame failed: no frame\n");
break;
}
cvShowImage("display_video", frame);
key = cvWaitKey(1000 / fps);
}
cvReleaseCapture(&capture);
cvDestroyWindow("display_video");
此博客文章带来了一些有关您要完成的任务的额外信息.
This blog post brings a little extra info on the task you are trying to accomplish.
这篇关于如何使用OpenCV从磁盘播放视频文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!