从文件夹中的序列图像制作视频

时间:2014-04-24 19:09:08

标签: image opencv video-capture

我有一系列灰度图像,如名为fr1.jpg, fr2.jpg,… 我使用此代码从此序列中编写视频。

#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);
    cout<<path<<endl;
    VideoCapture sequence("path/fr%1d.jpg");
    if (!sequence.isOpened())
       {
           cerr << "Failed to open Image Sequence!\n" << endl;
           return -1;
       }
    //Write video
    VideoWriter oVideoWriter ("path/MyVideo.avi",CV_FOURCC('8','B','P','S'), 30, frameSize);
    Mat imageGrey;
    if(!oVideoWriter.isOpened())
    {
      cout<<"ERROR: Failed to write the video"<<endl;
      return -1;
    }
    Mat Image;
    do
    {
    sequence>>Image;
    printf("Write image to video \n");
    Mat imageArr[] = {Image, Image, Image};
    merge(imageArr, 3, imageGrey);
    //cvtColor(Image,imageGrey,CV_GRAY2BGR);
    oVideoWriter.write(imageGrey);
    }while(!Image.empty());
    return 1;
}

我收到了这个错误

OpenCV Error: Bad argument (Unknown array type) in cvarrToMat, file /path/modules/core/src/matrix.cpp, line 697
libc++abi.dylib: terminate called throwing an exception
The program has unexpectedly finished.

1 个答案:

答案 0 :(得分:0)

#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);
    cout<<path<<endl;
    VideoCapture sequence("path/fr%1d.jpg");
    if (!sequence.isOpened())
    {
       cerr << "Failed to open Image Sequence!\n" << endl;
       return -1;
    }
    //Write video
    VideoWriter oVideoWriter
    ("path/MyVideo.avi",CV_FOURCC('8','B','P','S'), 30, frameSize);
    Mat imageGrey;
    if(!oVideoWriter.isOpened())
    {
      cout<<"ERROR: Failed to write the video"<<endl;
      return -1;
    }
    Mat Image;
    do
    {
      sequence>>Image;
      //You added this test to avoid the empty image
      if(Image.empty())
         break;
      printf("Write image to video \n");
      Mat imageArr[] = {Image, Image, Image};
      merge(imageArr, 3, imageGrey);
      //cvtColor(Image,imageGrey,CV_GRAY2BGR);
      oVideoWriter.write(imageGrey);
    }
    while(!Image.empty());

    return 1;
}