I'm trying my hand at OpenCV and have run into a problem that Dr. Google hasn't been able to help me out with. According to O'Reilly, the following code should play an AVI file in a window the program creates.

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

int main( int argc, char** argv ){
   cvNamedWindow( "Example2", CV_WINDOW_AUTOSIZE );
   CvCapture* capture = cvCreateFileCapture( argv[1] );
   IplImage* frame;

   printf("Starting loop\n");
      frame = cvQueryFrame( capture );

      if( !frame ){
         printf("Breaking: No frame\n");

      cvShowImage( "Example2", frame );
      char c = cvWaitKey(33);
      if( c == 27 ) break;
   cvReleaseCapture( &capture );
   cvDestroyWindow( "Example2" );

But, when I compile using the latest OpenCV libraries I always fall into the !frame. The references for OpenCV say that cvQueryFrame will return NULL on error - but they're not really specific as to what conditions will cause an error. I'm passing in the name of 19-sec AVI clip as the target file, but I never seem to grab a frame.
(running a 'file' on my AVI gives this as the file type: RIFF (little-endian) data, AVI, 640 x 480, 30.00 fps, video:, audio: uncompressed PCM (mono, 44100 Hz)). I don't think my AVI file is not messed up because totem can play it. I just can't find any information as to what causes cvQueryFrame to error and quit.

Can anybody shed some light on to why my code is failing? Or point me to any information that gives more details about reading AVI data?

P.S. - The code snippet is from "Learning OpenCV Computer Vision with the OpenCV Library", 2008. I've also read through the capture methods from opencv.willowgarage.com - but there's just not a lot of information.
The program is built on an Ubuntu 10.04 system with GCC 4.4.3, linked against the OpenCV 2.3.1a libraries.

I was able to at least continue with my original project by installing the OpenCV-2.0.0 libraries on a vanilla Ubuntu 10.04 VM using VirtualBox. I can see that the libraries have changed pretty significantly between 2.0.0 and 2.3.1a (not surprising), but I still can't find any documentation that even hints at how/why the HighGUI functions changed.
I've found some sources that suggest that it might be codec issue, but nothing that on opencv.willowgarage.com explains how to tell if it really is a codec issue.

Also, I was able to determine that cvQueryFrame() is failing because cvCreateFileCapture() is returning a NULL pointer - but I'm lost as to why cvCreateFileCapture is failing.

I had the same problem, I eventually resolved this by putting the opencv_ffmpeg.dll into the same location as the .exe of my program


Nathan Hadley

sorry ignore that, that was to solve a problem with capture

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.