Write a video from a list of grey scale images using opencv

asked 2014-09-29 09:09:16 -0500

developer2000 gravatar image

Hello, I have a list of images named like this img_001,img_002,...,img_154. I want to write a video using this list of images. I code using Qt on mac os 10.9.5 Here is my code

  #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/opencv.hpp>
#include<string.h>
using namespace cv;
using namespace std;

int main()
{
    //Read sequence of images
    Size frameSize(1360,1024);
    Mat dst;//dst image
    Size size(800,600);
    //cout<<path<<endl;
    VideoCapture sequence("/Users/xxxxx/Documents/QtProject/xxxxx/build-getVideo-Desktop_Qt_5_3_clang_64bit-Debug/images/img_%03d.png");
    if (!sequence.isOpened())
       {
           cerr << "Failed to open Image Sequence!\n" << endl;
           return -1;
       }
    //Write video
    VideoWriter oVideoWriter ("/Users/xxxxx/Documents/QtProject/xxxxx/build-getVideo-Desktop_Qt_5_3_clang_64bit-Debug/renameimages/MyVideo.avi",CV_FOURCC('8','B','P','S'), 4
                              , frameSize);
    Mat imageGrey;
    if(!oVideoWriter.isOpened())
    {
      cout<<"ERROR: Failed to write the video"<<endl;
      return -1;
    }
    Mat Image;
    int i=0;
    while(true)
    {
    sequence>>Image;
    namedWindow( "Display frame",CV_WINDOW_AUTOSIZE);
    resize(Image,Image,Size(Image.cols/2, Image.rows/2));//resize image
    imshow("Display frame", Image);
    waitKey(0);
    if(Image.empty())
        break;
    cout<<i<<endl;
    Mat imageArr[] = {Image, Image, Image};
    merge(imageArr, 3, imageGrey);
    //cvtColor(Image,imageGrey,CV_GRAY2BGR);
    oVideoWriter.write(imageGrey);
    i++;
    }
    cout<<"video written!"<<endl;
    return 1;
}

I am getting this warning WARNING: Couldn't read movie file /Users/xxxxx/Documents/QtProject/xxxxx/build-getVideo-Desktop_Qt_5_3_clang_64bit-Debug/images/img_%03d.png and the program is freezed.

edit retag flag offensive close merge delete