I have posted a way to do that @ Getting current FPS of OpenCV. It is necessary to do a bit of averaging otherwise the fps will be too jumpy.

edit

I have put a Sleep inside process() and it gives correct fps and duration(+/- 1ms).

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <opencv/cv.h>
#include <sys/timeb.h>
using namespace cv;

#if defined(_MSC_VER) || defined(WIN32)  || defined(_WIN32) || defined(__WIN32__) \
    || defined(WIN64)    || defined(_WIN64) || defined(__WIN64__) 

#include <windows.h>
bool _qpcInited=false;
double PCFreq = 0.0;
__int64 CounterStart = 0;
void InitCounter()
{
    LARGE_INTEGER li;
    if(!QueryPerformanceFrequency(&li))
    {
        std::cout << "QueryPerformanceFrequency failed!\n";
    }
    PCFreq = double(li.QuadPart)/1000.0f;
    _qpcInited=true;
}
double CLOCK()
{
    if(!_qpcInited) InitCounter();
    LARGE_INTEGER li;
    QueryPerformanceCounter(&li);
    return double(li.QuadPart)/PCFreq;
}

#endif

#if defined(unix)        || defined(__unix)      || defined(__unix__) \
    || defined(linux)       || defined(__linux)     || defined(__linux__) \
    || defined(sun)         || defined(__sun) \
    || defined(BSD)         || defined(__OpenBSD__) || defined(__NetBSD__) \
    || defined(__FreeBSD__) || defined __DragonFly__ \
    || defined(sgi)         || defined(__sgi) \
    || defined(__MACOSX__)  || defined(__APPLE__) \
    || defined(__CYGWIN__) 
double CLOCK()
{
    struct timespec t;
    clock_gettime(CLOCK_MONOTONIC,  &t);
    return (t.tv_sec * 1000)+(t.tv_nsec*1e-6);
}
#endif

double _avgdur=0;
double _fpsstart=0;
double _avgfps=0;
double _fps1sec=0;

double avgdur(double newdur)
{
    _avgdur=0.98*_avgdur+0.02*newdur;
    return _avgdur;
}

double avgfps()
{
    if(CLOCK()-_fpsstart>1000)      
    {
        _fpsstart=CLOCK();
        _avgfps=0.7*_avgfps+0.3*_fps1sec;
        _fps1sec=0;
    }
    _fps1sec++;
    return _avgfps;
}

void process(Mat& frame)
{
    Sleep(3);
}
int main(int argc, char** argv)
{
    int frameno=0;
    cv::Mat frame;
    cv::VideoCapture cap(0);
    for(;;)
    {
        //cap>>frame;
        double start=CLOCK();
        process(frame);
        double dur = CLOCK()-start;
        printf("avg time per frame %f ms. fps %f. frameno = %d\n",avgdur(dur),avgfps(),frameno++ );
        if(waitKey(1)==27)
            exit(0);
    }
    return 0;
}    
Answer from Zaw Lin on Stack Overflow
🌐
LearnOpenCV
learnopencv.com › home › find frame rate (frames per second-fps) in opencv (python/c++)
Find frame rate (frames per second-fps) in OpenCV (Python/C++)
July 31, 2026 - Tutorial on how to find frame rate or frames per second (fps) in a video using OpenCV. Python and C++ code is provided for practice and study.
Top answer
1 of 4
10

I have posted a way to do that @ Getting current FPS of OpenCV. It is necessary to do a bit of averaging otherwise the fps will be too jumpy.

edit

I have put a Sleep inside process() and it gives correct fps and duration(+/- 1ms).

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <opencv/cv.h>
#include <sys/timeb.h>
using namespace cv;

#if defined(_MSC_VER) || defined(WIN32)  || defined(_WIN32) || defined(__WIN32__) \
    || defined(WIN64)    || defined(_WIN64) || defined(__WIN64__) 

#include <windows.h>
bool _qpcInited=false;
double PCFreq = 0.0;
__int64 CounterStart = 0;
void InitCounter()
{
    LARGE_INTEGER li;
    if(!QueryPerformanceFrequency(&li))
    {
        std::cout << "QueryPerformanceFrequency failed!\n";
    }
    PCFreq = double(li.QuadPart)/1000.0f;
    _qpcInited=true;
}
double CLOCK()
{
    if(!_qpcInited) InitCounter();
    LARGE_INTEGER li;
    QueryPerformanceCounter(&li);
    return double(li.QuadPart)/PCFreq;
}

#endif

#if defined(unix)        || defined(__unix)      || defined(__unix__) \
    || defined(linux)       || defined(__linux)     || defined(__linux__) \
    || defined(sun)         || defined(__sun) \
    || defined(BSD)         || defined(__OpenBSD__) || defined(__NetBSD__) \
    || defined(__FreeBSD__) || defined __DragonFly__ \
    || defined(sgi)         || defined(__sgi) \
    || defined(__MACOSX__)  || defined(__APPLE__) \
    || defined(__CYGWIN__) 
double CLOCK()
{
    struct timespec t;
    clock_gettime(CLOCK_MONOTONIC,  &t);
    return (t.tv_sec * 1000)+(t.tv_nsec*1e-6);
}
#endif

double _avgdur=0;
double _fpsstart=0;
double _avgfps=0;
double _fps1sec=0;

double avgdur(double newdur)
{
    _avgdur=0.98*_avgdur+0.02*newdur;
    return _avgdur;
}

double avgfps()
{
    if(CLOCK()-_fpsstart>1000)      
    {
        _fpsstart=CLOCK();
        _avgfps=0.7*_avgfps+0.3*_fps1sec;
        _fps1sec=0;
    }
    _fps1sec++;
    return _avgfps;
}

void process(Mat& frame)
{
    Sleep(3);
}
int main(int argc, char** argv)
{
    int frameno=0;
    cv::Mat frame;
    cv::VideoCapture cap(0);
    for(;;)
    {
        //cap>>frame;
        double start=CLOCK();
        process(frame);
        double dur = CLOCK()-start;
        printf("avg time per frame %f ms. fps %f. frameno = %d\n",avgdur(dur),avgfps(),frameno++ );
        if(waitKey(1)==27)
            exit(0);
    }
    return 0;
}    
2 of 4
4

You can use opencv helper cv::getTickCount()

#include <iostream>
#include <string>

#include "opencv2/core.hpp"
#include "opencv2/core/utility.hpp"
#include "opencv2/video.hpp"
#include "opencv2/highgui.hpp"

using namespace cv;


int main(int ac, char** av) {

    VideoCapture capture(0);
    Mat frame;

    for (;;) {

        int64 start = cv::getTickCount();

        capture >> frame;
        if (frame.empty())
            break;

        /* do some image processing here */

        char key = (char)waitKey(1);

        double fps = cv::getTickFrequency() / (cv::getTickCount() - start);
        std::cout << "FPS : " << fps << std::endl;
    }
    return 0;
}
Discussions

c++ - How to calculate Frame Per Second in opencv? - Stack Overflow
Here is my code it display video but at high fps. I want original fps here but don't know how to do it. Watching some tutorials , they are using VideoCapture , I tried to use it but this is giving me More on stackoverflow.com
🌐 stackoverflow.com
November 7, 2016
c++ - How to calculate FPS of a live video stream using Chrono and OpenCV? - Stack Overflow
For a while I've been trying to calculate the FPS of a video stream coming through my OpenCV program, and was wondering if there's any way to do it using Chrono rather than time.h or any other API that measures with CPU ticks. More on stackoverflow.com
🌐 stackoverflow.com
c++ - How to calculate the fps of a video (with processing) using opencv - Stack Overflow
I can't seem to figure out what is wrong with the code fragment I have...it keeps on outputting over 200 fps for some reason. I tested with OpenCV and the unprocessed video is about 30 fps. More on stackoverflow.com
🌐 stackoverflow.com
algorithm - How to find framerate of video using c++ opencv 2.4.10? - Stack Overflow
Actually,I am trying detect and track the vehicles from a video using C++ opencv 2.4.10.I did so.Now,I want to find the frame rate of the output video.I want to know if there is any way to find out... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Top Tech Boy
toptechboy.com › calculating-frames-per-second-fps-in-opencv-and-overlay-on-frame
Calculating Frames Per Second (FPS) In OpenCV and Overlay on Frame | Technology Tutorials
January 26, 2023 - In this lesson we show how to instrument our code to calculate the Frames Per Second (FPS) we are achieving with our camera and OpenCV.
🌐
GeeksforGeeks
geeksforgeeks.org › python-displaying-real-time-fps-at-which-webcam-video-file-is-processed-using-opencv
Python - Displaying real time FPS at which webcam/video file is processed using OpenCV - GeeksforGeeks
January 4, 2023 - For calculating FPS, we will be keeping the record of the time when last frame processed and the end time when current frame processed.So, the processing time for one frame will be time difference between current time and previous frame time .
🌐
TutorialsPoint
tutorialspoint.com › how-to-get-the-fps-value-in-opencv-using-cplusplus
How to get the FPS value in OpenCV using C++?
The following program gets the FPS of a video and shows it in the console window. #include<opencv2/opencv.hpp>//OpenCV header to use VideoCapture class// #include<iostream> using namespace std; using namespace cv; int main() { int FPS;//Declaring an integer variable to store the number of total frames// VideoCapture cap("video1.mp4");//Declaring an object to capture stream of frames from default camera// FPS = cap.get(CAP_PROP_FPS);//Getting the total number of frames// cout << "Total Number of frames are:" << FPS << endl;//Showing the number in console window// system("pause");//Pausing the system to see the result cap.release();//Releasing the buffer memory// return 0; } After launching this program, we will get the FPS value in the console window.
🌐
Learning About Electronics
learningaboutelectronics.com › Articles › How-to-find-the-frames-per-second-of-a-video-Python-OpenCV.php
How to Find the Frames Per Second in a Video in Python using OpenCV
One of the features we can get is the frames per second (FPS) there are in a video. Frames are the images that make up a video. A video is a series of images shown in sequence. The frames per second tells us how many images there are per second of video. To find the frames per second of a video using OpenCV, we use the following statement, cv2.CAP_PROP_FPS
Find elsewhere
🌐
YouTube
youtube.com › watch
Calculate FPS and Average FPS from live video using OpenCV python - YouTube
This is a shorter video of mine on how to calculate the fps of a video using math and opencv and later show it on the frame.
Published: January 4, 2023
🌐
YouTube
youtube.com › watch
Mastering Computer Vision with Live FPS Count: Take Your Skills to the Next Level with OpenCV - YouTube
Inside my school and program, I teach you my system to become an AI engineer or freelancer. Life-time access, personal help by me and I will show you exactly...
Published: February 16, 2021
🌐
GitHub
github.com › kfatehi › opencv-webcam-fps › blob › master › src › main.c
opencv-webcam-fps/src/main.c at master · kfatehi/opencv-webcam-fps
// 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; ·
🌐
YouTube
youtube.com › data magic (by sunny kusawa)
How to check FPS using OpenCV | Frames Per Seconds | Video FPS | Machine Learning | Data Magic - YouTube
Hello Friends, In this vide, I will quickly show, How to check Frames Per Second(FPS) of any Video or Live stream using OpenCV.GitHub: https://github.com/dat...
Published: June 3, 2022
Views: 3K
🌐
OpenCV Q&A Forum
answers.opencv.org › question › 72386 › how-to-get-fps-in-opencv-live-capture
How to get FPS in OpenCV live capture - OpenCV Q&A Forum
#include <iostream> #include <ctime> #include <opencv2/imgproc.hpp> #include <opencv2/highgui.hpp> int main(int argc, char**argv) { cv::VideoCapture capture; // Read the video stream or open the web cam if( argc > 1) capture.open( argv[1] ); else capture.open( 0 ); if (!capture.isOpened()) { std::cout << "Problem connecting to cam " << std::endl; return -1; } else if( argc == 1 ) { std::cout << "Successfuly connected to camera " << std::endl; //capture.set(cv::CAP_PROP_FRAME_WIDTH,320); //capture.set(cv::CAP_PROP_FRAME_HEIGHT,240); } int frameCounter = 0; int tick = 0; int fps; std::time_t tim
🌐
YouTube
youtube.com › zubayer hossain
Calculate FPS from live video. Python OpenCV - YouTube
In this video, I show you a simple script to calculate the frames per second of your video feed. Also, I show you how to connect with your webcam if it is a ...
Published: September 2, 2021
Views: 6K
🌐
PyImageSearch
pyimagesearch.com › home › blog › faster video file fps with cv2.videocapture and opencv
Faster video file FPS with cv2.VideoCapture and OpenCV - PyImageSearch
April 17, 2021 - The goal of this section is to obtain a baseline on our video frame processing throughput rate using OpenCV and Python. To start, open up a new file, name it read_frames_slow.py , and insert the following code: # import the necessary packages from imutils.video import FPS import numpy as np import argparse import imutils import cv2 # construct the argument parse and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-v", "--video", required=True, help="path to input video file") args = vars(ap.parse_args()) # open a pointer to the video stream and start the FPS timer stream = cv2.VideoCapture(args["video"]) fps = FPS().start()
🌐
Reddit
reddit.com › r/computervision › how to find frame rate or frames per second (fps) in opencv ( python / c++ ) ?
r/computervision on Reddit: How to find frame rate or frames per second (fps) in OpenCV ( Python / C++ ) ?
November 7, 2020 - In this post, we will go over a useful trick in OpenCV where we find how to use OpenCV to find a common property - the frame per second for any…
🌐
OpenCV Q&A Forum
answers.opencv.org › question › 228564 › how-to-use-fps-module
How to use FPS module - OpenCV Q&A Forum
FPS: {:.2f}".format(fps.fps())) cv2.destroyAllWindows() ... Also the length of original video is 70 seconds. Why I see different results? ... the numbers you see all make sense. the FPS module is unfortunately not part of the opencv codebase, but assuming it "just works" --