Benchmarking frames per second when using OpenCV’s cvCaptureFromCAM

by Carson Reynolds

I find myself benchmarking a lot of cameras these days. Oddly, the OpenCV stuff (e.g. CV_CAP_PROP_POS_MSEC) which you’d tend to use seems to work well for recorded video files but not live capture. But no matter, an FPS meter is a trivial undertaking:

#include <cv.h>    
#include <highgui.h>

#include <time.h>
#include <stdio.h>

using namespace std;

int main(int argc, char** argv)
{
  // OpenCV Capture object to grab frames
  CvCapture *capture = cvCaptureFromCAM(0);

  // start and end times
  time_t start, end;

  // fps calculated using number of frames / seconds
  double fps;

  // frame counter
  int counter = 0;

  // floating point seconds elapsed since start
  double sec;

  // start the clock
  time(&start);
  
  while(cvGrabFrame(capture))
    {
      // grab a frame
      IplImage *frame = cvRetrieveFrame(capture);

      // see how much time has elapsed
      time(&end);

      // calculate current FPS
      ++counter;		
      sec = difftime (end, start);      
      
      fps = counter / sec;

      // will print out Inf until sec is greater than 0
      printf("FPS = %.2f\n", fps);
    }
	
  cvReleaseCapture(&capture);
  return 0;
}

Maybe Connsynn will find this an interesting alternative to her FPS calculation approach?

Advertisements