Ask Your Question

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;

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;
        fpsTimeout = false;

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

    VideoCapture capture;[1]);

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

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

        cout << "Calculating framerate ... " << endl;
        while (fpsTimeout)
            capture >> currentFrame;
            if(! currentFrame.empty())

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

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([1])) {
   cv::Mat _framemat;
   double _frequency = cv::getTickFrequency(), _tick = cv::getTickCount();
   while(true) {
      if( {


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

Question Tools

1 follower


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

Seen: 930 times

Last updated: Jan 19 '17