Measure OpenCV FPS

后端 未结 4 1253
别那么骄傲
别那么骄傲 2021-02-08 19:32

I\'m looking for a correct way to measure openCV FPS. I\'ve found several ways to do it. but none of them looks right for me.

The first one I\'ve tested, uses ti

4条回答
  •  攒了一身酷
    2021-02-08 20:26

    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 
    #include 
    using namespace cv;
    
    #if defined(_MSC_VER) || defined(WIN32)  || defined(_WIN32) || defined(__WIN32__) \
        || defined(WIN64)    || defined(_WIN64) || defined(__WIN64__) 
    
    #include 
    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;
    }    
    

提交回复
热议问题