Create video from images with OpenCV 2.4.1 on Ubuntu

Here is my example program for creating video from images with OpenCV. But my output video is not working and there was an error saying "Unable to demux the stream" Please help.

  #include<cv.h>
  #include<highgui.h>
  #include<cvaux.h>
  #include<cxcore.h>

  int main()
{
    //CvVideoWriter *writer = 0;
    int isColor = 1;
    int fps     = 25;  // or 30
    int frameW  = 320; // 744 for firewire cameras
    int frameH  = 240; // 480 for firewire cameras
    CvSize size;

    size.width = frameW;
    size.height = frameH;
    CvVideoWriter *writer = cvCreateVideoWriter(
            "data3.avi",
            CV_FOURCC('M','J','P','G'),
            fps,
            size);
    IplImage* img = 0; 
    img=cvLoadImage("IMG_0157.JPG");
    for(int counter=0;counter < 3000;counter++)
    {
    cvWriteFrame(writer,img);      // add the frame to the file
    }
    cvReleaseVideoWriter(&writer);
    return 0;
}

      

+1


source to share


3 answers


You can try another FOURCC code. Some of them are not supported correctly by OpenCV, some by multimedia applications. Having one that works with both OpenCV and your favorite video player is a matter of trial and error.

What you can try: Use VLC (in case you are not using it). This is one of the most reliable players.

If all you want to do is display / process a sequence of images in OpenCV as video, you can use the undocumented VideoCapture function: Load a sequence of images.



An example is in C ++, but you can easily convert it to C.

// pics are a sequence of Pictures001.jpg, PicturesS002.jpg, etc
cv::VideoCapture cap("path/to/my/Pictures%03d.jpg");

cv::Mat frame;

for(;;)
{
    cap >> frame;
    if(frame.empty())
       break;

    // do some processing
}

      

+6


source


This is usually not a FOURCC problem. The problem here is that the img size and the size used to open the VideoWriter are different.



Thus, you must be sure that the IPLImages or Mat and VideoWriter are the same size, otherwise the video output will be wrong.

+5


source


I am also trying to create a video from multiple images. I didn't get it, but are you trying to upload one image?

IplImage * img = 0; IMG = cvLoadImage ("IMG_0157.JPG");

I was having some problems with the video because I was not getting the width and height of the images I was trying to upload in order to create the video. So I got this propriety for the first time:

IplImage *img = cvLoadImage("<folder>\\<image_name>.jpg");

size.width = img->width;
size.height = img->height;

      

Then I created a video and checked if it existed:

CvVideoWriter *writer = cvCreateVideoWriter(
    "<video_name>.avi",
    -1,//CV_FOURCC('I','Y','U','V'), // VIDEO CODEC
    fps,
    size);

if(writer == NULL)
    std::cout << "No videowrite here!" << '\nl';

      

And for each found image recorded in a video frame, and then release it.

while(img!=NULL)
{
    sprintf( filename, "<folder>\\<image_name>_%d.jpg", i );

    img = cvLoadImage(filename); //imagem b&w
    cvWriteFrame(writer,img);

    i++;
}

cvReleaseVideoWriter(&writer);
cvReleaseImage(&img);

      

And it worked!

Don't forget to initialize the variables filename

and int i

.

Hope it helped!

+1


source







All Articles