使用 opencv 显示视频
Posted
技术标签:
【中文标题】使用 opencv 显示视频【英文标题】:Displaying a video using opencv 【发布时间】:2011-10-03 18:43:59 【问题描述】:根据“”,我遇到了一点问题。代码是用Visual Studio 2008用C++编写的。
代码如下:
int main( int argc, char** argv )
cvNamedWindow( "xample2", CV_WINDOW_AUTOSIZE );
CvCapture* capture = cvCreateFileCapture( "Micro-dance_2_.avi" );
IplImage* frame;
while(1)
frame = cvQueryFrame( capture );
if( !frame ) break;
cvShowImage( "xample2", frame );
char c = cvWaitKey(33);
if( c == 27 ) break;
cvReleaseCapture( &capture );
cvDestroyWindow( "xample2" );
调试时,程序启动,我可以看到命令窗口和灰色窗口(我想应该在哪里显示视频)几毫秒。然后两个窗口都关闭。
调试窗口的输出显示如下:
.. . (很多加载和卸载的dll) . . .
程序“[3684] 2aufg4).exe: Native”已退出,代码为 0 (0x0)。
我不知道我做错了什么......
非常感谢您的帮助!
谢谢你们
【问题讨论】:
程序看起来不错,但可能找不到 avi 文件。使用绝对路径作为 cvCreateFileCapture 的参数,看看是不是这样。 您可能缺少正确的编解码器。我已经很久没有看到它给出最新的建议了,但我似乎记得搜索“ffmpeg 和 opencv”找到了有用的答案。 【参考方案1】:您需要检查cvCreateFileCapture()
的返回并确保它成功加载文件:
#include <cv.h>
#include <highgui.h>
int main(int argc, char** argv)
cvNamedWindow("xample2", CV_WINDOW_AUTOSIZE);
CvCapture* capture = cvCreateFileCapture( "Micro-dance_2_.avi" );
if (!capture)
std::cout << "!!! cvCreateFileCapture didn't found the file !!!\n";
return -1;
IplImage* frame;
while (1)
frame = cvQueryFrame(capture);
if(!frame)
break;
cvShowImage("xample2", frame);
char c = cvWaitKey(33);
if (c == 27)
break;
cvReleaseCapture(&capture);
cvDestroyWindow("xample2");
【讨论】:
嗨。确实没有加载文件 尝试将完整路径传递给文件。 嗨,确实无法加载文件...我检查了路径,但视频仍然无法加载。但是谢谢你,我会检查我是否犯了愚蠢的错误 您遇到以下两种情况之一:1) 路径不正确或被误解或 2) 您没有正确的编解码器来打开文件。要查看是否为#1,请将AVI文件与exe文件放在同一文件夹中(而不是在项目文件夹中),并使用不带路径的文件名。 嘿,我已经添加了检查代码,我可以看到“!!! cvCreateFileCapture 没有找到文件!!!”在命令窗口中输出。是的,我尝试加载其他视频。我现在不知道该怎么办,但我想我在某个地方犯了一个非常愚蠢的错误。我明天再试一次,因为现在我还有其他事情要做:)。感谢您对小新手的帮助,谢谢【参考方案2】:试试这个
int main( int argc, char** argv )
cvNamedWindow( "xample2", CV_WINDOW_AUTOSIZE );
CvCapture* capture = cvCreateFileCapture( "Micro-dance_2_.avi" );
IplImage* frame;
if(!cvQueryFrame( capture ))
std::cout << "Could not open file\n";
return -1;
while(1)
frame = cvQueryFrame( capture );
if( !frame ) break;
cvShowImage( "xample2", frame );
char c = cvWaitKey(33);
if( c == 27 ) break;
cvReleaseCapture( &capture );
cvDestroyWindow( "xample2" );
【讨论】:
以上是关于使用 opencv 显示视频的主要内容,如果未能解决你的问题,请参考以下文章