在opencv中从.jpg图像创建.avi视频

时间:2014-04-13 05:54:00

标签: visual-studio-2010 opencv

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv[])
{
    Mat image;
    image = imread("C:/Users/Abhilash/Desktop/myFrames/frame1.jpg",1);


     if(! image.data )                              // Check for invalid input
    {
        cout <<  "Could not open or find the image" << endl ;
        return -1;
    }

获取框架的大小

   Size s = image.size();
   double dWidth = s.height;
   double dHeight = s.width;

    cout << "Frame Size = " << dWidth << "x" << dHeight << endl;

    Size frameSize(static_cast<int>(dWidth), static_cast<int>(dHeight));

初始化视频任务

        VideoWriter oVideoWriter ("D:/MyVideo.avi", CV_FOURCC('P','I','M','1'), 25,        
        frameSize, true);  


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



    namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"

        int i;
        char buffer[50];

阅读图像并将其添加到视频文件

      for(i=1;i<=250;i++) //putting 250 frames to MyVideo
      {

         Mat frame;

         sprintf(buffer,"C:/Users/Abhilash/Desktop/myFrames/frame%d.jpg",i);

         frame = imread(buffer,1);

          if(! frame.data )                              // Check for invalid input
    {
        cout <<  "Could not open or find the image" << endl ;
        return -1;
    }

          oVideoWriter << (frame); //writer the frame into the file

          imshow("MyVideo", frame); //show the frame in "MyVideo" window




 if (waitKey(10) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
       {
            cout << "esc key is pressed by user" << endl;
            break; 
       }
    }

    return 0;

   }

此代码读取250个.jpg帧并正确显示。创建的视频文件大小为5.54KB。但我无法播放视频文件(在KMPlayer中)。

0 个答案:

没有答案