Ask Your Question
0

opencv videowriter, whether the image had been write into vedio.

asked 2018-05-28 04:00:27 -0600

when I set the videowriter's fps is 10, but in for loop I get more than 10 images per second, but the vedio of output is 10fps, which image had been write into the vedio, or how the videowriter make the output vedio's fps is 10. thank you to give me some answer.

my program is: int main(int argc, char *argv) { VideoCapture cam(0);

cam.set(CV_CAP_PROP_FRAME_WIDTH, 1280);

cam.set(CV_CAP_PROP_FRAME_HEIGHT, 720);
if (!cam.isOpened()) {
    cout << "cam open failed!" << endl;
    return -1;
}

cout << "cam open success!" << endl;
namedWindow("cam");

Mat img;

VideoWriter vw;

vw.open("out.mp4",
    VideoWriter::fourcc('X', '2', '6', '4'),
    10,
    Size(cam.get(CAP_PROP_FRAME_WIDTH), cam.get(CAP_PROP_FRAME_HEIGHT))
    //Size(1280,720)
);
if (!vw.isOpened()) {
    cout << "VideoWriter open failed" << endl;
    return -1;
}

cout << "VideoWriter open Success" << endl;

for (;;) {
    cam.read(img);
    if (img.empty()) {
        break;
    }

    imshow("cam", img);
    vw.write(img);
    if (waitKey(5) == 'q') {
        break;
    }
}
waitKey(0);
return 0;

}

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-05-28 04:15:09 -0600

berak gravatar image

VideoCapture and VideoWriter are not synchronized at all. if you told the witer to have a final 10 fps, it will just do so, no matter how many you write per second in real life.

so, timing is everything here. you can either drop frames, or try to adjust the sleep time:

while(true)
{
    int64 start = getTickCount();

    cap >> frame;
    writer.write(frame);
    imshow("ocv",frame);

    int64 end = getTickCount();

    float sec = (end-start) / getTickFrequency();
    float t = (1.0 / fps) - sec;
    int ticks = t * 1000;
    cerr << sec << "  " << t << " " << ticks << endl;
    int k = waitKey(ticks>1 ? ticks : 10);
    if (k==27) break;  // esc. pressed
}
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-05-28 04:00:27 -0600

Seen: 155 times

Last updated: May 28 '18