如何使用opencv计算视频的fps(带处理)

时间:2018-01-18 02:02:40

标签: c++ opencv frame-rate opencv3.1

我似乎无法弄清楚我所拥有的代码片段有什么问题......由于某种原因,它一直在输出超过200 fps。我使用OpenCV进行了测试,未处理的视频大约为30 fps。

int n_frame = 0;
Mat previous;
auto start = chrono::high_resolution_clock::now();
while (m_cap.isOpened()) {
    if (n_frame % 100 == 1) {
        auto end = chrono::high_resolution_clock::now();    
        auto time = chrono::duration_cast<chrono::seconds>(end - start);
        cout << "the fps is: " << 1.0 / (static_cast<double>(time.count()) / n_frame) << endl;
        //cin.ignore(1000, '\n');
    }
    n_frame++;
    cout << "frame number: " << n_frame << endl;
    Mat frame, original;

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


//do some video processing down here eg thresholding
}

1 个答案:

答案 0 :(得分:1)

我认为你忘了重新初始化开始时间,这里是修改后的代码,这提供了一致的fps值,我在笔记本电脑上测试并得到了大约33.2,我只计算了100帧并重新初始化所有内容,这似乎工作

int n_frame = 0;
Mat previous;
auto start = std::chrono::high_resolution_clock::now();
VideoCapture m_cap(0);

while (m_cap.isOpened()) {
    n_frame++;
    if (n_frame == 100) {
        auto end = std::chrono::high_resolution_clock::now();
        auto time = std::chrono::duration_cast<chrono::seconds>(end - start);
        cout << "the fps is: " << (n_frame / static_cast<double>(time.count())) << endl;
        start = end;
        n_frame = 0;
    }
    //cout << "frame number: " << n_frame << endl;
    Mat frame, original;
    m_cap >> frame;
    if (frame.empty())
        break;
}