OpenCV例子三:将文字生成视频文件

#include <iostream>
#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;//必须加入,否则无法检找到OPENCV的各个函数
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{

	Size s(320, 240);
	//VideoWriter wr("dd.avi", CV_FOURCC(M, J, P, G), 25, s);//无法使用该参数
	VideoWriter wr("dd.avi", -1, 25, s);

	if (!wr.isOpened())
	{
		cout << "open faild 2.." << endl;
		return -1;
	}

	char text[10] = { 0 };
	Mat fm(s, CV_8SC3);
	for (int i = 0; i < 100; i++)
	{
		fm = Scalar::all(255);
		sprintf_s(text, "%d", i);
		putText(fm, text, Point(s.width / 3, s.height / 3), FONT_HERSHEY_COMPLEX, 3, Scalar(0, 0, 255), 3, 8);
		wr << fm;
	}

	waitKey();
	return 0;
}

注意:

//VideoWriter wr("dd.avi", CV_FOURCC(M, J, P, G), 25, s);必须有编码器才支持。

因此使用VideoWriter wr("dd.avi", -1, 25, s);中,传入-1选择系统支持的编码器。

生成完毕后,就可以用播放器播放dd.avi文件。

经验分享 程序员 微信小程序 职场和发展