如何使用 OpenCV (c++) 存储序列中的所有帧?

How to store all frames from a sequence with OpenCV (c++)?

我试过像这样从电影中分离帧序列:

void storeSequence() {
Mat frame;

VideoCapture vid("myVideo.mp4");

if (!vid.isOpened()) {
    return;
}

int fps = 30;
int count = 0;
bool cut = false;

vector<Mat> frames;

bool capture = true;

while (capture) {

    vid >> frame;
    imshow("video", frame);

    char character = waitKey(1000 / fps);

    Mat outMat;

    switch (character) {
            case 'c':
        cut = !cut;
        break;
    case 27:
        return;
    case 'x':
        capture = false;
    }
    if (cut) {
        frame.copyTo(outMat);
        frames.push_back(outMat);
        count++;
    }
    if (!capture) break;
}

cout << count << endl;

for (int i = 0; i < count; i++) {
    stringstream fn;
    Mat img = frames[i];
    fn << "seq1_" << i << ".jpg" << endl;
    imwrite(fn.str(), img);
}

}

显示视频,在我按 c 两次后按 x,控制台告诉我向量中存储了多少帧 "frames",但没有图像写入我的文件目录。我错过了什么? (或者是否有更好的方法来分隔序列?)

fn << "seq1_" << i << ".jpg" << endl;更改为fn << "seq1_" << i << ".jpg"; 当您在末尾添加 endl 时,您的字符串看起来像这样 "seq1_0.jpg\n"ant 这是错误的路径。