Ask Your Question
0

What is the most efficient way to read the frame rate from a camera ?

asked 2017-01-18 12:39:00 -0600

simozz gravatar image

updated 2017-01-18 12:39:31 -0600

I need to process real time images using my laptop camera and a Logitech C270 USB, and I need to determine the frame rate. For video files the solution is very trivial, just use

VideoCapture capture;
capture.get(CV_CAP_PROP_FPS)

for cameras, seems to be not as easy. The program I am writing will run on a Linux platform and the solution I wrote and testing is this one (resumed):

bool initTimeout;
bool fpsTimeout;

void AlarmHandler(int sig)
{
    if (initTimeout)
        initTimeout = false;
    else
        fpsTimeout = false;
}

int main(int argc, char *argv[])
{    
    // frame rate variable
    uint32_t fps = 0;

    VideoCapture capture;
    capture.open(argv[1]);

    if (inputFilename.find("/dev/video") != std::string::npos) 
    {
        fps = 0;
        initTimeout = true;
        fpsTimeout = true;

        cout << "Data input from a camera. Initialisation timeout" << endl;
        alarm(5);
        while (initTimeout)
            capture >> currentFrame;

        cout << "Calculating framerate ... " << endl;
        alarm(1);
        while (fpsTimeout)
        {
            capture >> currentFrame;
            if(! currentFrame.empty())
                fps++;
        }
    }
}

I implemented the initialisation timeout becuase I noticed that the camera need a frames fflush before the timeout used for frame rate.

Comparing the results with VLC player, it gives 30 frames per second for the laptop camera and 25 for the USB camera.

My program sometimes agrees with VLC, sometimes it doesn't, returning different results (16 for the USB camera, 31 for the laptop camera etc.).

Do you know a more efficient way to read the frame rate from a video camera ?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2017-01-19 05:14:47 -0600

pi-null-mezon gravatar image

First, note that when you open cv::VideoCapture it could open the target video device not in the same mode as VLC player. So, if you have got different fps values in this case - it is normal.

Secondly, why you can not use the simplest one solution:

...
VideoCapture capture;
if(capture.open(argv[1])) {
   cv::Mat _framemat;
   double _frequency = cv::getTickFrequency(), _tick = cv::getTickCount();
   while(true) {
      if(capture.read(_framemat)) {

         // DO WHAT YOU NEED WITH THE IMAGE

         cout << "FPS: " << cv::getTickFrequency() / (cv::getTickCount() - _tick) << endl;
         _tick = cv::getTickCount(); 
      }
   }
}
...
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-01-18 12:39:00 -0600

Seen: 1,079 times

Last updated: Jan 19 '17