简体   繁体   English

使用opencv显示视频

[英]Displaying a video using opencv

i have a little problem according "displaying a video with opencv". 根据“使用opencv显示视频”,我有一个小问题。 The code is written in c++ with visual studio 2008. 该代码是使用Visual Studio 2008的C ++编写的。

here is the code: 这是代码:

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" );
}

when debugging, the programm launches and i can see the command window and a grey window (wher the video should be displayed i suppose) for a few milliseconds. 调试时,程序启动,我可以看到命令窗口和灰色窗口(我想应该在此处显示视频)持续了几毫秒。 Then both windows close. 然后两个窗口都关闭。

the output from debug window in visual shows the following: 调试窗口中的输出以可视方式显示以下内容:

.. . .. (a lot of loaded and unloaded dlls) . (很多已加载和已卸载的dll)。 . .

The program '[3684] 2aufg4).exe: Native' has exited with code 0 (0x0). 程序“ [3684] 2aufg4).exe:本机”已退出,代码为0(0x0)。

i dont know what i am doing wrong... 我不知道我在做什么错...

i would appreciate your help a lot! 我将非常感谢您的帮助!

as allways thank you guys 一直谢谢你们

You need to check the return of cvCreateFileCapture() and make sure it loaded the file successfully: 您需要检查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");
}

Try this 尝试这个

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" );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM