1

Here is my sample program for creating Video from images with OpenCV. But my output video is not working and An error occurred ans stating that "Could not demultiplex 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;
}
AruniRC
  • 5,070
  • 7
  • 43
  • 73
  • This seems to be a very general error in Ubuntu. I don't think that it's specific to `opencv`. For example: http://ubuntuforums.org/archive/index.php/t-763441.html – eboix Jun 21 '12 at 09:14

3 Answers3

6

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

What you can try: Use VLC (in case you don't alreay use it). It is one of the most robust players out there.

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

The 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
}
Sam
  • 19,708
  • 4
  • 59
  • 82
  • Hi I have tried many CODEC such as DIVX, PIM1, MJPG, DIB , CV_FOURCC_PROMPT and also VLC player. But none is working. What do you mean by "cap >> frame;" in loop? My objective is to use few stored images to create a video. – Still Learning Boy Jun 21 '12 at 09:26
  • The example shows how to read, not to write images, just in case you were planning to make a video to play it back with opencv. But another fix for your problem would be to install some more codecs. IF you're on Windows, try K-Lite codec pack. It is a chance that the video will play then – Sam Jun 21 '12 at 10:46
  • Noo need for thanks. Upvote and/or accept answer. This is the way you say thanks on stackoverflow. – Sam Jun 22 '12 at 05:13
  • That's a pretty nice function of VideoCapture. And too bad the opencv doc is so bad. – dynamic May 22 '13 at 20:13
5

Usually this is not a FOURCC problem. The problem here is that the size of the img and the size used to open the VideoWriter are different.

In that way you should be sure that IPLImages or Mat images, and the VideoWriter have the same size, otherwise the video output will be wrong.

Javi Carnero
  • 409
  • 4
  • 9
1

I'm also trying to create a video from multiple images. I didn't understand but are you trying to load a single image?

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

I was having some trouble with the video because I wasn't getting the width and height of the image(s) I was trying to load to create the video. So I first got those proprieties:

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

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

Then created the video writer 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 image found wrote to the video's frame and then released 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!

xatz
  • 425
  • 1
  • 6
  • 14