码迷,mamicode.com
首页 > 其他好文 > 详细

OpenCV Tutorials —— Creating a video with OpenCV

时间:2014-11-24 22:25:43      阅读:383      评论:0      收藏:0      [点我收藏+]

标签:style   http   io   ar   color   os   sp   for   文件   

写video 需要用到 VideoWriter 

视频文件可看作一个容器

视频的类型由视频文件的后缀名来指定

bubuko.com,布布扣

 

Due to this OpenCV for video containers supports only the avi extension, its first version. A direct limitation of this is that you cannot save a video file larger than 2 GB. Furthermore you can only create and expand a single video track inside the container. No audio or other track editing support here.

 

Code

#include "stdafx.h"

#include <iostream> // for standard I/O
#include <string>   // for strings

#include <opencv2/core/core.hpp>        // Basic OpenCV structures (cv::Mat)
#include <opencv2/highgui/highgui.hpp>  // Video write

using namespace std;
using namespace cv;

static void help()
{
	cout
		<< "------------------------------------------------------------------------------" << endl
		<< "This program shows how to write video files."                                   << endl
		<< "You can extract the R or G or B color channel of the input video."              << endl
		<< "Usage:"                                                                         << endl
		<< "./video-write inputvideoName [ R | G | B] [Y | N]"                              << endl
		<< "------------------------------------------------------------------------------" << endl
		<< endl;
}

int main(int argc, char *argv[])
{
	help();

	if (argc != 4)
	{
		cout << "Not enough parameters" << endl;
		return -1;
	}

	const string source      = argv[1];           // the source file name
	const bool askOutputType = argv[3][0] ==‘Y‘;  // If false it will use the inputs codec type

	VideoCapture inputVideo(source);              // Open input
	if (!inputVideo.isOpened())
	{
		cout  << "Could not open the input video: " << source << endl;
		return -1;
	}

	string::size_type pAt = source.find_last_of(‘.‘);                  // Find extension point
	const string NAME = source.substr(0, pAt) + argv[2][0] + ".avi";   // Form the new name with container
	int ex = static_cast<int>(inputVideo.get(CV_CAP_PROP_FOURCC));     // Get Codec Type- Int form

	// Transform from int to char via Bitwise operators
	char EXT[] = {(char)(ex & 0XFF) , (char)((ex & 0XFF00) >> 8),(char)((ex & 0XFF0000) >> 16),(char)((ex & 0XFF000000) >> 24), 0};

	Size S = Size((int) inputVideo.get(CV_CAP_PROP_FRAME_WIDTH),    // Acquire input size
		(int) inputVideo.get(CV_CAP_PROP_FRAME_HEIGHT));

	VideoWriter outputVideo;                                        // Open the output
	if (askOutputType)
		outputVideo.open(NAME, ex=-1, inputVideo.get(CV_CAP_PROP_FPS), S, true);
	else
		outputVideo.open(NAME, ex, inputVideo.get(CV_CAP_PROP_FPS), S, true);

	if (!outputVideo.isOpened())
	{
		cout  << "Could not open the output video for write: " << source << endl;
		return -1;
	}

	cout << "Input frame resolution: Width=" << S.width << "  Height=" << S.height
		<< " of nr#: " << inputVideo.get(CV_CAP_PROP_FRAME_COUNT) << endl;
	cout << "Input codec type: " << EXT << endl;

	int channel = 2; // Select the channel to save
	switch(argv[2][0])
	{
	case ‘R‘ : channel = 2; break;
	case ‘G‘ : channel = 1; break;
	case ‘B‘ : channel = 0; break;
	}
	Mat src, res;
	vector<Mat> spl;

	for(;;) //Show the image captured in the window and repeat
	{
		inputVideo >> src;              // read
		if (src.empty()) break;         // check if at end

		split(src, spl);                // process - extract only the correct channel
		for (int i =0; i < 3; ++i)
			if (i != channel)
				spl[i] = Mat::zeros(S, spl[0].type());
		merge(spl, res);	// 仅单通道不能构成图像,需要将其他通道置零,merge

		//outputVideo.write(res); //save or
		outputVideo << res;
	}

	cout << "Finished writing" << endl;
	return 0;
}

OpenCV Tutorials —— Creating a video with OpenCV

标签:style   http   io   ar   color   os   sp   for   文件   

原文地址:http://www.cnblogs.com/sprint1989/p/4119634.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!