1

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

user967493
  • 143
  • 2
  • 8
  • The program looks fine but perhaps it can't find the avi file. Use an absolute path as the parameter of cvCreateFileCapture to find out if this is the case. – Jong Bor Lee Oct 03 '11 at 18:48
  • You're probably missing the proper codec. It's been too long since I looked at it to give up to date advice, but I seem to recall searching for 'ffmpeg and opencv' turned up useful answers. – user786653 Oct 03 '11 at 19:24

2 Answers2

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");
}
karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • hi. indeed the file isnt loaded – user967493 Oct 03 '11 at 19:07
  • Try passing the full path to the file. – karlphillip Oct 03 '11 at 19:10
  • hi, indeed the file couldnt be loaded... i checked the path but the video still wont load. but thank u, i will check whether i made a stupid misstake – user967493 Oct 03 '11 at 19:11
  • hi, i checked the path and everything seems to be right. (the .avi is in the project folder) but still the file is not found – user967493 Oct 03 '11 at 19:17
  • The problem is that when you run your application from within VStudio, it doesn't understand the relative location of the file. Your best choice is to pass the full path to file instead, i.e., `C:\\path_to\\Micro-dance_2_.avi`. I'm not sure if double slashes are really necessary. – karlphillip Oct 03 '11 at 19:23
  • i passed the full path, but still no loading – user967493 Oct 03 '11 at 19:54
  • 1
    You are experiencing one of two things: 1) the path is incorrect or is being misinterpreted or 2) you don't have the correct codec to open the file. To see if it's #1, put the AVI file in the same folder as the exe file (not in the project folder) and use the file name without a path. – SSteve Oct 03 '11 at 20:21
  • Hi, i put the file in the folder where my .exe is, but still doesnt work... what about those codecs? – user967493 Oct 03 '11 at 20:59
  • You might have to install some codecs so OpenCV knows how to properly read your video file. Check K-Lite Codec Pack. – karlphillip Oct 03 '11 at 21:17
  • i have quicktime installed, shouldnt that work? or do i have to "include those codecs"? sorry for my stupid questions :) – user967493 Oct 03 '11 at 21:22
  • No, it shouldn't since there are a multitude of video codecs out there and quicktime can only understand a few. From now on I think your codec related questions are better suited to your sister site, superuser.com. That leaves you with two options, the first, follow the instruction I gave you or seek help at superuser.com. – karlphillip Oct 04 '11 at 01:17
  • hi, i just installed the k-lite pack, but it still doesnt work. – user967493 Oct 04 '11 at 13:19
  • You didn't mention which OpenCV version you are using. I assumed you were using the latest 2.3.x – karlphillip Oct 04 '11 at 13:52
  • You might want to update to version 2.3.1 since that were made video changes: http://opencv.willowgarage.com/wiki/OpenCV%20Change%20Logs Download and install the superpack: http://sourceforge.net/projects/opencvlibrary/files/opencv-win/2.3.1/ and if you have any problems check our installing guide at http://stackoverflow.com/questions/7011238/opencv-2-3-c-visual-studio-2010/7014918#7014918 – karlphillip Oct 04 '11 at 14:08
  • i installed the opencv 2.3.1, linked everything and included and stuff... now the programm wont compile because of the following: fatal error C1083: Cannot open include file: 'highgui.h': No such file or directory – user967493 Oct 04 '11 at 15:47
  • ok solved this problem, but now i am where i started before installing opencv2.3.1 .... after compiling, i can see the grey window for a few millisecs, but no video – user967493 Oct 04 '11 at 15:56
  • I imagine you added the code to check the return of `cvCreateFileCapture()`, right? Have you tried to load other videos? – karlphillip Oct 04 '11 at 16:28
  • 1
    hey, y i have added the code for the check, and i can see the "!!! cvCreateFileCapture didn't found the file !!!" output in the commandwindow. and yes i have tried loading other videos. i dont know what to do now, but i suppose i made a lil very stupid misstake somewhere down the line. i will try again tomorrow cause for now i got other stuff to do :). i appreciate your help for a little newbie, thank you – user967493 Oct 04 '11 at 16:45
  • Copy the video to every single folder of your project and see if that solves the problem. If it does, you need to adjust the path of the filename when calling `cvCreateFileCapture()` because it is not finding the file. – karlphillip Oct 04 '11 at 17:00
  • allready did the first, but still no viedo. now i will try to adjust the path. Tahnk you! – user967493 Oct 05 '11 at 12:17
  • i tried the last hour to adjust the path, but still dont work... i noticed a line in the command window that says: "Warning: Error opening file (../../modules/highgui/src/cap_ffmpeg_impl.hpp:526>" it stands before the line saying "!!! cvCreateFileCapture didnt found the file !!!" i dont know what to do with this warning, is the highgui library broken or something?^^ – user967493 Oct 05 '11 at 16:55
  • Looking at *cap_ffmpeg_impl.hpp* line 526 will tell you that `av_open_input_file()` could not open the file, which still means 1 of 2 things: either fmmpeg thinks you don't have the codec, or it can't really find the file at the specified location. Since stackoveflow is not a chat I'm saying goodbye to this thread. If you have any more questions I suggest you ask them in other threads. But this question has been answered already. The reason why you saw a grey window is because you wasn't checking the return of the function that was failing, as I stated on my answer. – karlphillip Oct 05 '11 at 17:18
0

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" );
}
Wazy
  • 8,822
  • 10
  • 53
  • 98