获取OpenCV的当前FPS [英] Getting current FPS of OpenCV

查看:722
本文介绍了获取OpenCV的当前FPS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个OpenCV应用程序,FPS非常重要.如何计时主循环的处理时间以获得当前和平均FPS? 这样,我可以知道我的应用程序可以运行的速度.顺便说一句,我使用的是固态硬盘,因此处理器是这​​里的瓶颈!

I am writing an OpenCV application and the FPS is very important. How do I time the processing time of the main loop to get the current and Average FPS? This way, I can know how fast my application can operate. By the way, I'm using imread off an SSD so the processor is the bottleneck here!

推荐答案

您可以执行以下操作.关于fps,我发现与其从平均持续时间中得出,不如将其实际计入1秒段并进行平均.您可以通过更改_avgfps=0.7*_avgfps+0.3*_fps1sec;来控制平均(或间隔窗口)的稳定性.例如,_avgfps=0.9*_avgfps+0.1*_fps1sec;将使收敛到实际速度变慢,但更能抵抗暂时的波动.比率必须精确地等于1.

you can do something like this. regarding fps, i find that instead of deriving from average duration, it's slightly better to actually count in 1 second segments and average. you can control the stability of the averaging(or the interval window) by changing the _avgfps=0.7*_avgfps+0.3*_fps1sec;. for example _avgfps=0.9*_avgfps+0.1*_fps1sec; will make the convergence to actual slower but will be more resistant to temporary fluctuations. the ratios must sum to exactly 1.

#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__) 
int CLOCK()
{
    return clock();
}
#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__) 
int CLOCK()
{
    struct timespec t;
    clock_gettime(CLOCK_MONOTONIC,  &t);
    return (t.tv_sec * 1000)+(t.tv_nsec*1e-6);
}
#endif

double _avgdur=0;
int _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)
{
    imshow("frame",frame);
}

int main(int argc, char** argv)
{
    int frameno=0;
    cv::Mat frame;
    cv::VideoCapture cap(0);
    for(;;)
    {
        cap>>frame;

        clock_t start=CLOCK();

        if(frame.data)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;
}     

这篇关于获取OpenCV的当前FPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆