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



我似乎无法弄清楚我拥有的代码片段出了什么问题......由于某种原因,它不断输出超过 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
}

我想你忘了重新初始化开始时间,这是修改后的代码,这提供了一致的 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;
}

最新更新