Displaying a video using opencv

耗尽温柔 提交于 2020-01-03 03:01:07

问题


i have a little problem according "displaying a video with opencv". The code is written in c++ with visual studio 2008.

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) . . .

The program '[3684] 2aufg4).exe: Native' has exited with code 0 (0x0).

i dont know what i am doing wrong...

i would appreciate your help a lot!

as allways thank you guys


回答1:


You need to check the return of cvCreateFileCapture() and make sure it loaded the file successfully:

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



回答2:


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


来源:https://stackoverflow.com/questions/7639237/displaying-a-video-using-opencv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!