opencv视频的读取和创建

转自http://blog.csdn.net/lst227405/article/details/27566167

#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/legacy/legacy.hpp>
#include <iostream>
#include <vector>

//#include <iostream> // for standard I/O
#include <string>   // for strings
#include <iomanip>  // for controlling float print precision
#include <sstream>  // string to number conversion

#include <opencv2/imgproc/imgproc.hpp>  // Gaussian Blur
//#include <opencv2/core/core.hpp>        // Basic OpenCV structures (cv::Mat, Scalar)
//#include <opencv2/highgui/highgui.hpp>  // OpenCV window I/O

using namespace cv;
using namespace std;

int main()
{
    //读取视频,生成图像
    const string name_file="C:\\Users\\Administrator\\Desktop\\code\\video\\video\\video";
    const string name_save_file="C:\\Users\\Administrator\\Desktop\\code\\video\\video\\video\\photo\\";
    const string name1="1.avi";
    cv::VideoCapture src(name1);
    cv::VideoCapture captst;
    if(!src.isOpened())
    {
        cout<<"视频文件打开失败"<<endl;
        return -1;
    }
    cv::Size size_frame=cv::Size((int)src.get(CV_CAP_PROP_FRAME_WIDTH),
                                 (int)src.get(CV_CAP_PROP_FRAME_HEIGHT));
    const string name_win="test";
    //cv::namedWindow(name_win);
    cv::Mat frame_src;
    int frameNum=0;
    int k=1;
    std::stringstream stream;

    string name_write,name_tmp;
    while(true)
    {
        src>>frame_src;
        if(frame_src.empty())
        {
            cout<<"game over!"<<endl;
            break;    
        }
        ++frameNum;
        stream<<k;
        name_tmp=stream.str();
        name_write=name_save_file+name_tmp+".bmp";
        imwrite(name_write,frame_src);
        k++;
        stream.str("");  

    }
    //////////////////////////////////////
    
    //读取图像,生成视频
    //生成一个倒序的视频
    cv::Mat frame_tmp;
    VideoWriter outputVideo;
    outputVideo.open("2.avi" , src.get(CV_CAP_PROP_FOURCC), src.get(CV_CAP_PROP_FPS),size_frame, true);
    //                 文件名        编码器                        帧率                     大小
    if(!outputVideo.isOpened())
    {
        cout<<"输出文件打开失败"<<endl;
        return -1;
    }
    cout<<k<<endl;
    int m=k-1;
    while(true)
    {
        stream<<m;
        name_tmp=stream.str();
        name_write=name_save_file+name_tmp+".bmp";
        //cout<<name_write<<endl;
        frame_src=imread(name_write);
        
        outputVideo << frame_src;
        //cv::VideoCapture qwe(name_write);
        //qwe >> frame_tmp;
        //cv::imshow("11", frame_tmp);    

        m--;
        if(m==0)
            break;
        stream.str("");  
    }
    cout << "Finished writing" << endl;

    return 0;

}


你可能感兴趣的:(opencv视频的读取和创建)