使用 OpenCV 和 C++ 从 mp4 视频中提取所有视频帧

Hir*_*ro 5 c++ opencv video-processing extraction

我正在按照教程提取视频帧。我已经阅读了这个问题,它不起作用,也是来自Open CV Answer 的问题,但解决方案是捕获当前帧。我有一个 120fps 的视频,想提取所有这些视频。这是我的代码

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <string>
#include <sstream>

using namespace cv;
using namespace std;
int c = 0;
string int2str(int &);

int main(int argc, char **argv) {
string s;

VideoCapture cap("test.mp4"); // video
if (!cap.isOpened())  
{
    cout << "Cannot open the video file" << endl;
    return -1;
}

double fps = cap.get(CV_CAP_PROP_FPS); //get the frames per seconds of the video

cout << "Frame per seconds : " << fps << endl;

namedWindow("MyVideo", CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"

while (1)
{
    Mat frame;
    Mat Gray_frame;
    bool bSuccess = cap.read(frame); // read a new frame from video

    if (!bSuccess) 
    {
        cout << "Cannot read the frame from video file" << endl;
        break;
    }
    s = int2str(c);
    //cout<<("%d\n",frame )<<endl;
    c++;
    imshow("MyVideo", frame); //show the frame in "MyVideo" window
    imwrite("ig" + s + ".jpg", frame);
    if (waitKey(30) == 27) //esc key
    {
        cout << "esc key is pressed by user" << endl;
        break;
    }
}

return 0;

}

//int to string function
string int2str(int &i) {
    string s;
    stringstream ss(s);
    ss << i;
    return ss.str();
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,我有一个 120fps 的分钟视频。我希望能够提取 120 帧。但是提取出错了,视频持续了 1 分钟,但我的文件夹中存储了 200 多帧。我的代码做错了吗?