Archive for the ‘Computer Vision’ Category.

Getting the number of frames of an AVI video file for OpenCV

I’ve been using the OpenCV (ver. 1.0.0) lately and I noticed that under Linux, using the “cvCaptureFromAVI” does not detect the correct number of frames from an AVI video file. So this code returns 0 as the number of frames:

  1. CvCapture  *capture = cvCaptureFromAVI( path2Video );
  2. cvQueryFrame( capture );
  3. int nFrames = (int) cvGetCaptureProperty( capture , CV_CAP_PROP_FRAME_COUNT );

So I wrote a small piece of code which reads the number of frames directly from the AVI file. Please use it and let me know if you had any problem.

  1. int  nFrames;
  2. char tempSize[4];
  3.  
  4. // Trying to open the video file
  5. ifstream  videoFile( path2Video , ios::in | ios::binary );
  6. // Checking the availablity of the file
  7. if ( !videoFile ) {
  8. cout << “Couldn’t open the input file “ << path2Video << endl;
  9. exit( 1 );
  10. }
  11.  
  12. // get the number of frames
  13. videoFile.seekg( 0×30 , ios::beg );
  14. videoFile.read( tempSize , 4 );
  15. nFrames = (unsigned char ) tempSize[0] + 0×100*(unsigned char ) tempSize[1] + 0×10000*(unsigned char ) tempSize[2] +    0×1000000*(unsigned char ) tempSize[3];
  16.  
  17. videoFile.close(  );