Measure OpenCV FPS

后端 未结 4 1255
别那么骄傲
别那么骄傲 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:13

    I would just measure the walltime and simply divide the frames by time elapsed. On linux:

    /*
    * compile with:
    *   g++ -ggdb webcam_fps_example2.cpp -o webcam_fps_example2 `pkg-config --cflags --libs opencv`
    */
    
    #include "opencv2/opencv.hpp"
    #include 
    #include 
    
    
    using namespace cv;
    using namespace std;
    
    double get_wall_time(){
        struct timeval time;
        if (gettimeofday(&time,NULL)){
            //  Handle error
            return 0;
        }
        return (double)time.tv_sec + (double)time.tv_usec * .000001;
    }
    
    
    int main(int argc, char** argv)
    {
        VideoCapture cap;
        // open the default camera, use something different from 0 otherwise;
        // Check VideoCapture documentation.
        if(!cap.open(0))
            return 0;
    
        cap.set(CV_CAP_PROP_FRAME_WIDTH,1920);
        cap.set(CV_CAP_PROP_FRAME_HEIGHT,1080);
    
        double wall0 = get_wall_time();
        for(int x = 0; x < 500; x++)
        {
              Mat frame;
              cap >> frame;
              if( frame.empty() ) break; // end of video stream
              //imshow("this is you, smile! :)", frame);
              if( waitKey(10) == 27 ) break; // stop capturing by pressing ESC 
        }
        double wall1 = get_wall_time();
        double fps = 500/(wall1 - wall0);
        cout << "Wall Time = " << wall1 - wall0 << endl;
        cout << "FPS = " << fps << endl;
        // the camera will be closed automatically upon exit
        // cap.close();
        return 0;
    }
    

    Wall Time = 43.9243 FPS = 11.3832

提交回复
热议问题