Ask Your Question
0

How to write a sequence as images as a avi video ?

asked 2017-05-11 10:25:42 -0600

Laurentiu gravatar image

Hi, I want to create a video using a sequence as images, I have greayscale images with resolution 640x480,I tried to make this simple application fallowing the next steps: 1) I read each images with imread 2) I used VideoWriter for writing video, but my video after saving is the 0 second saved :(

This is my code:

#include "opencv2/opencv.hpp"
#include <iostream>

using namespace std;
using namespace cv;

int main(int argc, char* argv[])
{
    for(int i = 0; i < 30; i++)
    {
        char left[1024] = "";
        sprintf(left, "left640x480_%d.bmp", i);

        Mat image(640, 480, CV_8UC1);
        image = imread(left, 0);

        VideoWriter video("out.avi", CV_FOURCC('M','J','P','G'), 30, Size(640, 480), false);

        Mat frame;
        image.copyTo(frame);
        video.write(frame);
        imshow( "Frame", frame );
        char c = (char)waitKey(33);
        if( c == 27 )
            break;
    }

  return 0;
}

I use OpenCV 3.1 in C++. Thank you for your help!

edit retag flag offensive close merge delete

Comments

I changed what you said, but the result is the same @berak

Laurentiu gravatar imageLaurentiu ( 2017-05-11 10:40:34 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-05-11 10:48:02 -0600

berak gravatar image

updated 2017-05-11 10:56:54 -0600

if you open a new videowriter per image, it's no big wonder, it ends up with 0 seconds...

also, please make it a habit, to check resources and return values whenever possible !

 VideoWriter video("out.avi", CV_FOURCC('M','J','P','G'), 30, Size(640, 480), false);
 if (!video.isOpened())
 {
       .... fail
 }
 for(int i = 0; i < 30; i++)
 {
    char left[1024] = "";
    sprintf(left, "left640x480_%d.bmp", i);

    // please do not preallocate it, it will be overwritten anyway, and your 
    // original code had width & height in reverse !
    Mat image = imread(left, 0); 
   if (image.empty())
   {
          ... fail !
   }
   video.write(image); // no copy needed, don't vodoo !

    imshow( "Frame", image );
    char c = (char)waitKey(33);
    if( c == 27 )
        break;
edit flag offensive delete link more

Comments

And the code:

#include "opencv2/opencv.hpp"
#include <iostream>

using namespace std;
using namespace cv;

int main(int argc, char* argv[])
{

    VideoWriter video("out.avi", CV_FOURCC('M','J','P','G'), 30, Size(640, 480), false);

    if ( !video.isOpened() ) //if not initialize the VideoWriter successfully, exit the program
    {
        cout << "ERROR: Failed to write the video" << endl;
        return -1;
    }

    Mat image(480, 640, CV_8UC1);
    for(int i = 0; i < 100; i++)
    {
        char left[1024] = "";
        sprintf(left, "left640x480_%d.bmp", i);
        image = imread(left, 0);
        if (image.empty())
        {   
          cout << "ERROR! Failed to load image!" << endl;
          return -1;
        }

        video.write(image);
        imshow( "Frame", image );
        char c = (char)waitKey(33);
        ......
Laurentiu gravatar imageLaurentiu ( 2017-05-11 11:14:04 -0600 )edit

Is a bad result :(

Laurentiu gravatar imageLaurentiu ( 2017-05-11 11:14:35 -0600 )edit

i never look at youtube vids (waste of time), but 30 frames might not be enough for proper mjpg encoding.

berak gravatar imageberak ( 2017-05-11 11:17:28 -0600 )edit

ok, so the video is not good, I don't have the images from folder I have line of black gray and white

Laurentiu gravatar imageLaurentiu ( 2017-05-11 11:24:02 -0600 )edit

try another codec, then ?

use ffmpeg straight away ?

berak gravatar imageberak ( 2017-05-11 11:26:38 -0600 )edit
1

Thanks for help, I solve the problem, VideoWriter support just color, I had to do cvtColor for my image :D Thank you very much for help ;) @berak

Laurentiu gravatar imageLaurentiu ( 2017-05-12 09:06:56 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-05-11 10:25:42 -0600

Seen: 255 times

Last updated: May 11 '17