OpenCV 录制视频

使用OpenCV打开摄像头,并进行录制。 

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv) {
	VideoCapture cam(0);
	if (!cam.isOpened())
	{
		cout << "cam open failed!" << endl;
		getchar();
		return -1;
	}
	cout << "cam open success!" << endl;
	namedWindow("cam");
	Mat img;
	VideoWriter vw;
	int fps = cam.get(CAP_PROP_FPS);  //获取摄像机帧率
	if (fps <= 0)fps = 25;
	//创建视频文件
	vw.open("out.avi", //路径
		VideoWriter::fourcc('X', '2', '6', '4'), //编码格式
		fps, //帧率
		Size(cam.get(CAP_PROP_FRAME_WIDTH),
			cam.get(CAP_PROP_FRAME_HEIGHT))  //尺寸
		);
	if (!vw.isOpened())
	{
		cout << "VideoWriter open failed!" << endl;
		getchar();
		return -1;
	}
	cout << "VideoWriter open success!" << endl;

	for (;;)
	{
		cam.read(img);
		if (img.empty())break;
		imshow("cam", img);
		//写入视频文件
		vw.write(img);
		if (waitKey(5) == 'q') break;
	}

	waitKey(0);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/huhuandk/article/details/85268518
今日推荐