Ask Your Question

Recording from imshow/matrix, not from webcam

asked 2019-12-09 02:49:39 -0500

Vizier87 gravatar image

updated 2019-12-10 00:46:07 -0500

Hi guys, hit a snag on this one. I'm trying to make the simplest recording from a video feed in imshow (Mat current_frame), not from a webcam.

Basically I'm trying to record what happens to a feed in an imshow.

So in my main function, I invoke the following to create the video folder:

VideoWriter cap1("C:\\Users\\Bla3\\Combined.avi", VideoWriter::fourcc('M', 'J', 'P', 'G'), 5, Size(CAP_PROP_FRAME_WIDTH, CAP_PROP_FRAME_HEIGHT), true);

And when I slide a trackbar in my while(1) routine to start recording like this:

if (RecState == 1) {

Should that already do the trick? Am I mising something here?


Edit: In short, what is the simplest code to just record an imshow without using VideoCapture?

edit retag flag offensive close merge delete


Did you try it? What is your problem?

kbarni gravatar imagekbarni ( 2019-12-09 08:24:21 -0500 )edit

Yeah. The video file does not play, which I believe is due to the write() not working as intended. Technically, those two lines should be enough, right? The current_frame shows nicely with imshow if it's relevant.

Vizier87 gravatar imageVizier87 ( 2019-12-09 21:00:56 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2019-12-10 02:05:27 -0500

berak gravatar image

updated 2019-12-10 02:10:15 -0500

Size(CAP_PROP_FRAME_WIDTH, CAP_PROP_FRAME_HEIGHT) -- that's for sure wrong, as it expands to Size(3,4) ;)

those constants are for the VideoCapture, and you don't even have one.

you need to choose the desired Video size, like:

Size frameSize(640, 480);
VideoWriter writer("C:\\Users\\Bla3\\Combined.avi", VideoWriter::fourcc('M', 'J', 'P', 'G'), 5, frameSize , true);

and in the loop, resize the images to that (since your images may have arbitrary sizes):

resize(img, img, frameSize);
edit flag offensive delete link more


Thanks berak. That did the job for me.

By the way I have a slightly related issue:

Right now my code, since the VideoWriter function has to be defined out of the while loop, means it will be generated even if I didn't want to record any.

I wanted it to be conditional recording, but an "if" function like follows:

if (RecState == 1) {

            // Create video for saving ///////////////////////////////////////////////////
            Size frameSize(horizontal * 0.3, vertical * 0.4);
            VideoWriter video("C:\\Users\\bla3.avi",
                cv::VideoWriter::fourcc('M', 'J', 'P', 'G'), 5, frameSize, true);

This snippet will just override the previous file. Any thoughts?

Vizier87 gravatar imageVizier87 ( 2019-12-12 01:37:55 -0500 )edit

generate a filename ;)

int counter = 0;
string name = cv::format("my_%d.avi", counter++);
berak gravatar imageberak ( 2019-12-12 01:54:23 -0500 )edit

Wouldn't that create a lot of files though? I just wanted it to be like, if I slide a trackbar to 1, it'll create one file on the desktop, and start recording. If I slide it back to 0, it stops, and voila the video can be released.

Vizier87 gravatar imageVizier87 ( 2019-12-12 01:58:18 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2019-12-09 02:49:39 -0500

Seen: 41 times

Last updated: Dec 10 '19