Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

opencv 视频转图像,图像转视频

$
0
0

由于经常需要用到图像序列进行图像处理,视频转图像,图像转视频方法保存备用

1. 确定所使用的视频格式opencv是否支持,网上有诸多软件可以进行转码。

2.上代码处理

#include <iostream>
#include <vector>
#include "opencv2\highgui\highgui.hpp"

using namespace std;
using namespace cv;

void video2image(string video,string path)
{
	VideoCapture capture(video);
	if(!capture.isOpened())
	{
		cerr<<"Failed to open a video"<<endl;
		return ;
	}

	Mat frame;
	int num=1;
	string filename;
	char   temp_file[5];

	for(;;)
	{
		capture>>frame;
		if(frame.empty())
			break;
		_itoa_s(num,temp_file,4,10); //4表示字符长度  10表示十进制  实现整型转字符串
		filename = temp_file;
		filename = path+filename+".jpg";  
		num++;
		imwrite(filename,frame);
	}
	capture.release();
}

//假定图像集已经按帧号顺序排列
void images2video(string _Imagespath,string &_videoTosave)
{
	int numframes = 120; //需要合成视频的帧数
	int fourcc    = CV_FOURCC('D','I','V','X'); //编解码类型
	double fps    = 30;
	bool iscolor  = true;
	int frameWidth = 160; //帧宽度
	int frameHeight= 120; //帧高度
	VideoWriter Writer;

	Writer = VideoWriter(_videoTosave,fourcc,fps,Size(frameWidth,frameHeight),iscolor);
	string filename;
	char   temp_file[5];
	Mat frame;
	for (int i=1;i<=numframes;i++)
	{
		_itoa_s(i,temp_file,4,10); //4表示字符长度  10表示十进制  实现整型转字符串
		filename = temp_file;
		filename = _Imagespath+filename+".jpg";
		frame    = imread(filename);
		if (frame.empty())
		{
			cout<<"could not load image"<<endl;
			exit(0);
		}
		Writer.write(frame);
	}
}
int main(int argc,char** argv)
{
	string videoFromfile = "E:\\opencv_demo\\vs2010\\video\\006_1.avi";  //读取视频
	string Imagespath    = "E:\\opencv_demo\\vs2010\\video\\Image\\";    //保存图片集路径
	string videoTosave   = "E:\\opencv_demo\\vs2010\\video\\testVideo.avi"; //保存视频路径
	video2image(videoFromfile,Imagespath);
	images2video(Imagespath,videoTosave);
	return 0;
}


作者:czm13616 发表于2013-7-21 0:39:55 原文链接
阅读:118 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>