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

How to calculate the fps of a video (with processing) using opencv

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