Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

There are several problems with your code:

  • Don't release the capture interface inside the loop!!! Do it only at the end of the application;
  • Code defensively: Always initialize your variables and check the return of the functions;
  • Add a call to cvWaitKey() after displaying the image from the camera;

You code would look something like this:

#include <highgui.h>
#include <stdio.h>

int main( int argc, char** argv ) 
{ 
    cvNamedWindow("Example2", CV_WINDOW_AUTOSIZE); 
    CvCapture *capture = cvCreateFileCapture(argv[1]); 
    if (!capture)
    {
         printf("!!! Failed cvCreateFileCapture\n");
         return -1;
    }

    IplImage* frame = NULL;

    while(1) 
    { 
        frame = cvQueryFrame(capture); 
        if (!frame) break; 

        cvShowImage("Example2" , frame); 
        cvWaitKey(33);
    }

    cvReleaseCapture(&capture); 
    cvDestroyWindow("Example2"); 
    return 0;
}