Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Getting the correct frame rate

I am using OpenCV 3.1 on VS2015.

I have a video that, according to the file properties, runs at 26 FPS.

I am trying to set the waitKey in such a way that it will allow the video to play at the correct frame rate.

Here is a snippet of my code:

clock_t begin = clock(), end = clock();
unsigned int count = 0;
float FPS = 0;
int wait = (int)(1000.0/cap.get(CV_CAP_PROP_FPS));
std::cout << "Video show a frame every " << wait << " milliseconds (" << cap.get(CV_CAP_PROP_FPS) << " FPS)" << std::endl;

for (unsigned int i = 0; i < (frames-1); i++)
{
    if ((end - begin) / CLOCKS_PER_SEC >= 1)
    {
        FPS = (float)count;
        count = 0;
        begin = clock();
    }

    cv::putText(window_frame, std::to_string(FPS), cv::Point(10, window_frame.rows - 40), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar::all(255), 2, 8);
    cv::imshow("Video", window_frame.clone());

    //Need to get frame rate correct, involving the waitkey
    if (cv::waitKey(wait) >= 0) break;
    end = clock();
    count++;
}

When the code is run the output is as follows:

Video show a frame every 37 milliseconds (26.9043 FPS)

However the variable FPS is reporting back 18 to 22. What is the reason for this? I would very much like the video frame rate and the program frame rate to match. I understand waitKey waits for a MINIMUM of the delay supplied to it, so it may not be the best "frame rate setting" device. Should I try to limit or force the framerate I want through other means?