Ask Your Question
1

Saving gray images to .avi with VideoWriter and Lagarith codec

asked 2015-04-21 10:52:20 -0600

lb gravatar image

updated 2015-04-23 03:56:16 -0600

I wish to save grayscale images to a (small) avi file with the Lagarith codec under Windows in a 32 bit application compiled with Visual C++ 2010 and OpenCV 2.4.10. I'm using the VideoWriter object from highgui. However, the function VideoWriter::open returns false --see the following code snippet:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;

int main(int argc, char* argv[]){
    VideoWriter outputVideo;
    if (!outputVideo.open("test.avi", CV_FOURCC('L', 'A', 'G', 'S'), 25.0, Size(800, 600), false))
        std::cout << "Couldn't open VideoWriter" << std::endl;
}

Is this the expected behaviour? I could convert my gray image to a color image but as far as I know the Lagarith codec compresses each channel separately so I think it would increase the size of the resulting video. How can I get the smallest possible video file?

Edit:

  • Encoding color images works.
  • I compiled OpenCV in debug mode and it seems Video For Windows is being used for compression.
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
0

answered 2015-04-22 11:22:22 -0600

Eduardo gravatar image

updated 2015-04-22 11:25:48 -0600

"How can I get the smallest possible video file?"

You can use a lossy codec (Lagarith is a lossless codec) like CV_FOURCC('M','J','P','G').

OpenCV use ffmpeg for reading / writing videos. I think there is some kind of problem between OpenCV and the ffmpeg library used when the OpenCV dll was built, that's why it returns 0 ? Or maybe ffmpeg can decode Lagarith but not encode in Lagarith ?

If you want to keep a lossless video, one option could be to save the images in a directory (img_1.png, img_2.png, etc.) using a lossless format like png and after use a tool to create a video from images like ffmpeg.

edit flag offensive delete link more

Comments

Thanks for your answer! From executing OpenCV in Debug mode it seems Video For Windows is used internally. Encoding color images is OK. Saving the images as png could be a good workaround but I leave the question open to see if there are other options.

lb gravatar imagelb ( 2015-04-23 04:01:01 -0600 )edit
0

answered 2016-09-18 14:47:09 -0600

Hi!

I was also unable to make the Lagarith codec only work with grayscale images (maybe it's prepared for color images only?).

I use the following workaround: for every three grayscale frames in the video, I stack them creating a false color image. Then I save this image using the Lagaryth codec.

Of course, this solution is not good if you want a standard video player to reproduce your video. In my case I just want to archive the images in compressed format, and read them from another script.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-04-21 10:52:20 -0600

Seen: 3,608 times

Last updated: Apr 23 '15