Ask Your Question
3

Exporting video from image sequence

asked 2016-10-25 13:33:06 -0600

Locke gravatar image

updated 2016-10-27 09:52:23 -0600

Hey guys,

So I'm working on a project that involves the user outputting a series of images that I would like to combine into a single video. Right now, these images are exported as HDR files, but I have tried this with a png sequence as well and run into the same issue. I'm running into an issue with combining these into a video. The images are read in fine, but when I try to open the VideoWriter, something isn't working.

I'm not sure if it has to do with the codec that I'm trying to use (I'll admit to a slight bit of ignorance when it comes to those) or something else entirely. The program does output an empty .AVI video of 0kb, which is obviously wrong.

Any help would be appreciated, so thanks in advance. Let me know if there's anything I can clarify on.

int main()
{

    //Passes through this sequence without any issues
    VideoCapture sequence("data/00001.HDR");
    if (!sequence.isOpened())
    {
        cerr << "Failed to open Image Sequence!\n" << endl;
        return -6;
    }

    double FPS = 60.0;
    int wid = sequence.get(CV_CAP_PROP_FRAME_WIDTH);
    int het = sequence.get(CV_CAP_PROP_FRAME_HEIGHT);

    VideoWriter oVideoWriter;
    oVideoWriter.open("data/MyVideo.avi", CV_FOURCC('F', 'M', 'P', '4'), FPS, Size(wid, het), true);

    //This is where something goes wrong and the program returns a -1
    if (!oVideoWriter.isOpened())
    {
        cout << "ERROR: Failed to write the video" << endl;
        return -1;
    }

    //Larger body of the remainder of the function
    ...

    return 1;
}

Final Solution

This was my final solution, based off of the comment string below.

int main()
{   
    VideoCapture sequence("data/00001.HDR", CAP_IMAGES);

    if (!sequence.isOpened())
    {
        cerr << "Failed to open Image Sequence!\n" << endl;
        return -6;
    }

    double FPS = 60.0;
    int wid = sequence.get(CV_CAP_PROP_FRAME_WIDTH);
    int het = sequence.get(CV_CAP_PROP_FRAME_HEIGHT);

    VideoWriter oVideoWriter;
    oVideoWriter.open("data/MyVideo.avi", CV_FOURCC_DEFAULT, FPS, Size(wid, het));

    if (!oVideoWriter.isOpened())
    {
        cerr << "ERROR: Failed to write the video" << endl;
        return -1;
    }

    Mat frame8;
    Mat frame32;

    try {
        do
        {
            sequence >> frame32;
            if (frame32.empty())
                break;

            frame32.convertTo(frame8, CV_8UC3, 550, 25);

            oVideoWriter.write(frame8);

        } while (!frame32.empty());
    }
    catch (Exception ex) {
        cout << ex.code << " " << ex.msg << endl;
        cin.get();
    }

    return 1;
}
edit retag flag offensive close merge delete

Comments

could you check the type() of the images you load there ? (you can't write float type images to video atm)

berak gravatar imageberak ( 2016-10-25 23:57:49 -0600 )edit
1

I'm finding that its type 21 (Float CV_32FC3). Is there a way to convert this to a usable format? I'm now getting an error that says "Assertion failed (image->depth == 8)" as well, if that helps

Locke gravatar imageLocke ( 2016-10-26 09:17:50 -0600 )edit

yes, perfect, thanks !

berak gravatar imageberak ( 2016-10-26 09:43:56 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2016-10-26 09:45:35 -0600

berak gravatar image

since you can only write 8bits (1 or 3 chan) images to video, you have to convert them:

Mat bgr32f = ... // from your hdr sequence
Mat bgr8u;
bgr32f.convertTo( bgr8u, CV_8U, /*maybe-some-scale-factor-here*/);

oVideoWriter << bgr8u;
edit flag offensive delete link more

Comments

Awesome, its finally exporting video, which is a huge step. My last problem is that the footage is now exporting grey-scale, with only the whitest whites showing up in the final video. Is there a way to convert with the color remaining intact?

I've been able to export the footage with color using -1 in place of the CV_FOURCC input, but I want the user to be able to run the program without having to use any extra steps on their part

Locke gravatar imageLocke ( 2016-10-26 11:22:10 -0600 )edit

unfortunately, the existence of specific codecs depends heavily on os / user, the only codec guaranteed to be there (in opencv3) is MJPG and avi container.

berak gravatar imageberak ( 2016-10-27 02:06:15 -0600 )edit

ohh, btw, the grayscale is due to your last arg in the VideoWriter constructor !

berak gravatar imageberak ( 2016-10-27 02:07:57 -0600 )edit
2

You sir, are a gentleman and a scholar. I finally was able to get everything working. I found that my image wasn't actually being converted to gray-scale, it was just very dark for some reason. In order to get everything working I had to change the convertTo function to this:

frame32.convertTo(frame8, CV_8UC3, 550, 25);

This brightened my image and leveled everything out. Thank you for all the help.

I'll include my final solution in the Edited section above

Locke gravatar imageLocke ( 2016-10-27 09:48:45 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2016-10-25 13:33:06 -0600

Seen: 841 times

Last updated: Oct 27 '16