`

opencv和C++将连续的图片合成视频

 
阅读更多
#include<opencv2\opencv.hpp>
#include<iostream>
#include<Windows.h>
//由图片生成视频

using namespace std;
using namespace cv;

void main()
{
	//输入一张图片
	Mat src = imread("D:\\wen1\\1.jpg");
	int isColor = 1;
	//每秒钟的帧率,即一秒放多少帧图片
	int fps = 10;
	//获取图片的宽和高
	int frameWidth = src.cols;
	int frameHeight = src.rows;
	//声明VideoWriter类,写入视频
	VideoWriter writer("D:\\test.avi", VideoWriter::fourcc('M', 'J', 'P', 'G'), fps,
		Size(frameWidth, frameHeight), isColor);

	cout << "info:" << endl
		<< "ff.avi" << endl
		<< "Size:" << frameWidth << "*" << frameHeight << endl
		<< "fps:" << fps << endl;

	int i = 1;//从第一张开始
	while (1)
	{
		//找到存放图片的文件夹
  		string strDir = "D:\\wen1\\";
  		//将整型的i转换成字符串ss
  		stringstream ss;
  		ss << i;
  		//声明指针
  		string str = ss.str();
  		//查找图片(名字.jpg)		
		strDir += str;
		strDir += ".jpg";
		//输入图片
		src = imread(strDir, 1);
		if (src.empty())
		{
			MessageBox(NULL, L"加载完成!", L"温馨提示", MB_OK | MB_ICONINFORMATION);
			break;
		}
		imshow("src", src);
		waitKey(30);
		//调用writer类,传入图片
		writer.write(src);
		i++;
	}
}

        小编今天的任务就是把图片序列转换成视频, 代码如上,已经经过了详细的注释。

 

分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

Global site tag (gtag.js) - Google Analytics