Ask Your Question
0

Getting the correct frame rate

asked 2016-08-10 15:39:22 -0600

LasOz gravatar image

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?

edit retag flag offensive close merge delete

Comments

I am maybe wrong on that but if you draw something or even show your frame it consumes time. So i think thats the reson why your framerate doesn't match. The speed also depends if your are running the code in "DEBUG" or "RELASE". From my knowledge "DEBUG" mode is approximately 2 to 2.5 times slower then running the code in "RELEASE" mode.

Ice_T02 gravatar imageIce_T02 ( 2016-08-12 11:43:16 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-08-10 16:31:55 -0600

could you try your video with the code below

#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <ctime>
#include <windows.h>

using namespace cv;

int main(int argc, char** argv)
{
    char* filename = argc >= 2 ? argv[1] : (char*)"c:/768x576.avi";

    VideoCapture capture(filename); // open video file
    if (!capture.isOpened())  // check if we succeeded
        return -1;

    int fps_of_video = (int)capture.get(CAP_PROP_FPS);
    int time_to_wait = 1000 / fps_of_video;

    int frameCounter = 0;
    int tick = 0;
    int fps = fps_of_video;
    std::time_t timeBegin = std::time(0);

    for (;;)
    {
        double time_start = (double)getTickCount();

        Mat frame;
        capture >> frame; // get a new frame

        if (frame.empty())
        {
            break;
        }

        frameCounter++;

        std::time_t timeNow = std::time(0) - timeBegin;

        if (timeNow - tick >= 1)
        {
            tick++;
            fps = frameCounter;
            frameCounter = 0;
        }

        cv::putText(frame, cv::format("Original FPS of Video=%d", fps_of_video), cv::Point(30, 50), cv::FONT_HERSHEY_SIMPLEX, 0.8, cv::Scalar(0, 0, 255));
        cv::putText(frame, cv::format("Average FPS=%d", fps), cv::Point(30, 80), cv::FONT_HERSHEY_SIMPLEX, 0.8, cv::Scalar(0, 0, 255));

        imshow("Video Player", frame);

        if (waitKey(1) == 27) break;

        // wait for some time to correct FPS
        while (time_to_wait > ((double)getTickCount() - time_start) / getTickFrequency() * 1000)
        {
            //http://stackoverflow.com/questions/1157209/is-there-an-alternative-sleep-function-in-c-to-milliseconds
            Sleep(1);
        }
    }
    return 0;
}
edit flag offensive delete link more

Comments

Hello, it makes the video run at roughly 3 times the video frame rate (64 FPS). I have tried changing a few things in your code but it had no affect on the result.

LasOz gravatar imageLasOz ( 2016-08-11 09:53:20 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-08-10 15:39:22 -0600

Seen: 7,044 times

Last updated: Aug 10 '16