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

时间:2019-01-20 22:43:28

标签: c++ opencv mp4

我试图从这样的电影中分离出帧序列:

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”中存储了多少帧,但是没有图像写入我的文件目录中。我想念什么? (或者还有更好的选择来分隔序列吗?)

1 个答案:

答案 0 :(得分:0)

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