测量OpenCV FPS [英] Measure OpenCV FPS

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

问题描述

我在寻找一个正确的方法来衡量openCV FPS。我发现了几种方法。但它们都不适合我。



我测试的第一个,使用 time_t start和time_t end 。我认为一个是错误的,一旦它返回一个转储函数作为fps X时间图(我真的不能想象一个fps图可以是一个转储函数)。



这里是这个情节的图片。





第二个测试使用 t =(double)cvGetTickCount()来测量fps。这种方式是错误的,一旦返回120 fps的结果,但是,对于30秒长度的视频采用120 fps不应该超过1分钟被处理。因此这是一个错误的测量FPS的方法。



有人知道另一种方法来测量openCV的FPS吗?



Ps。我正在尝试在视频的每个帧中查找圈子。视频帧大小为320x240像素。



更新2
我要测量FPS的代码。



for(;;)
{

  clock_t start = CLOCK (); 

Mat frame,finalFrame;
capture>>帧;

finalFrame = frame;

cvtColor(frame,frame,CV_BGR2GRAY);

GaussianBlur(frame,frame,Size(7,7),1.5,1.5);
threshold(frame,frame,20,255,CV_THRESH_BINARY);

dilate(frame,frame,Mat(),Point(-1,-1),2,1,1);
erode(frame,frame,Mat(),Point(-1,-1),2,1,1);

Canny(frame,frame,20,20 * 2,3);

vector< Vec3f>界;

findContours(frame,_contours,_storage,CV_RETR_CCOMP,CV_CHAIN_APPROX_SIMPLE);

vector< vector< Point> > contours_poly(_contours.size());
vector< Rect> boundRect(_contours.size());
vector< Point2f> center(_contours.size());
vector< float> radius(_contours.size());


int temp = 0;

for(int i = 0; i <_contours.size(); i ++)
{
if(_contours [i] .size()> 100)
{
approxPolyDP(Mat(_contours [i]),contours_poly [i],3,true);
boundRect [i] = boundingRect(Mat(_contours [i]));
minEnclosingCircle((Mat)_contours [i],center [i],radius [i]);
temp = i;
break;
}
}



double dur = CLOCK() - start;
printf(avg time per frame%f ms。fps%f。frameno =%d\\\
,avgdur(dur),avgfps(),frameno ++);

frameCounter ++;

if(frameCounter == 3600)
break;

if(waitKey(1000/120)> = 0)break;
}

更新

使用Zaw Lin方法执行程序!

解决方案

我已经发布了一种方法,获取当前的FPS的OpenCV 。有必要做一点平均,否则fps会太跳跃。



编辑



我已经把sleep里面的process - 1ms)。

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

#if defined(_MSC_VER)||定义(WIN32)|| defined(_WIN32)||定义(__ WIN32__)\
||定义(WIN64)||定义(_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!\\\
;
}
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)\
|| (BSD)||定义(__ OpenBSD__)|| defined(__ NetBSD__)\
||定义(__ FreeBSD__)||定义__DragonFly__ \
|| defined(sgi)|| defined(__ sgi)\
|| defined(__ MACOSX__)|| defined(__ APPLE__)\
||定义(__ 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\\\
,avgdur(dur),avgfps(),frameno ++);
if(waitKey(1)== 27)
exit(0);
}
return 0;
}


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 time_t start and time_t end. I think that one is wrong once it returns me a dumped function as fps x time plot (I really can't imagine how a fps plot could be a dumped function).

Here the image of this plot.

The second I've tested uses t = (double)cvGetTickCount() to measure fps. This way is wrong once it returns 120 fps as result, but, for a 30 seconds length video captured with 120 fps shouldn't take more than 1 minute to be processed. so this is a wrong way to measure FPS.

Someone knows another way to measure FPS in openCV?

Ps. I'm trying to find circles in each frame of the video. The video frame size is 320x240 pixels.

Update 2 The code that I'm trying to measure FPS.

for(;;) {

    clock_t start=CLOCK();

    Mat frame, finalFrame;
    capture >> frame; 

    finalFrame = frame;

    cvtColor(frame, frame, CV_BGR2GRAY);

    GaussianBlur(frame, frame, Size(7,7), 1.5, 1.5);
    threshold(frame, frame, 20, 255, CV_THRESH_BINARY);

    dilate(frame, frame, Mat(), Point(-1, -1), 2, 1, 1);
    erode(frame, frame, Mat(), Point(-1, -1), 2, 1, 1);

    Canny(frame, frame, 20, 20*2, 3 );

    vector<Vec3f> circles;

    findContours(frame,_contours,_storage,CV_RETR_CCOMP,CV_CHAIN_APPROX_SIMPLE );

    vector<vector<Point> > contours_poly( _contours.size() );
    vector<Rect> boundRect( _contours.size() );
    vector<Point2f>center( _contours.size() );
    vector<float>radius( _contours.size() );


    int temp = 0;

    for( int i = 0; i < _contours.size(); i++ )
    { 
        if( _contours[i].size() > 100 )
        {
           approxPolyDP( Mat(_contours[i]), contours_poly[i], 3, true );
           boundRect[i] = boundingRect( Mat(_contours[i]) );
           minEnclosingCircle( (Mat)_contours[i], center[i], radius[i] );
           temp = i;
           break;
        }
    }



    double dur = CLOCK()-start;
        printf("avg time per frame %f ms. fps %f. frameno = %d\n",avgdur(dur),avgfps(),frameno++ );

    frameCounter++;

    if(frameCounter == 3600)
    break;

    if(waitKey(1000/120) >= 0) break;
}

Update

Program execution using the Zaw Lin method!

解决方案

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;
}    

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

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