OpenCV同时打开3个摄像头并保存为视频

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

using namespace cv;
using namespace std;

int main()
{

	//initialize and allocate memory to load the video stream from camera
	VideoCapture camera1(1 + cv::CAP_DSHOW);
	/*camera1.set(CAP_PROP_FRAME_WIDTH, 320);
	camera1.set(CAP_PROP_FRAME_HEIGHT, 240);*/

	VideoCapture camera2(0 + cv::CAP_DSHOW);
	//camera2.set(CAP_PROP_FRAME_WIDTH, 320);
	//camera2.set(CAP_PROP_FRAME_HEIGHT, 240);

	VideoCapture camera3(3 + cv::CAP_DSHOW);
	if (!camera1.isOpened())
	{
		cout << "ERROR!camera 1 open failed" << endl;
		return 1;
	}
	if (!camera2.isOpened())
	{
		cout << "ERROR!camera 3 open failed" << endl;
		return 1;
	}
	if (!camera3.isOpened())
	{
		cout << "ERROR!camera 3 open failed" << endl;
		return 1;
	}
	//grab and retrieve each frames of the video sequentially
	Mat frame1;
	camera1 >> frame1;
	if (frame1.empty()) {
		cerr << "ERROR! blank frame1 grabbed\n";
		return -1;
	}
	Mat frame2;
	camera2 >> frame2;
	if (frame2.empty()) {
		cerr << "ERROR! blank frame2 grabbed\n";
		return -1;
	}
	Mat frame3;
	camera3 >> frame3;
	if (frame3.empty()) {
		cerr << "ERROR! blank frame2 grabbed\n";
		return -1;
	}
	bool isColor = (frame1.type() == CV_8UC3);
	//--- INITIALIZE VIDEOWRITER
	VideoWriter writer1;
	int codec = VideoWriter::fourcc('M', 'J', 'P', 'G');  // select desired codec (must be available at runtime)
	double fps = 25.0;                          // framerate of the created video stream
	string filename1 = "./live1.avi";             // name of the output video file
	writer1.open(filename1, codec, fps, frame1.size(), isColor);
	// check if we succeeded
	if (!writer1.isOpened()) {
		cerr << "Could not open the output video file for write\n";
		return -1;
	}
	//--- INITIALIZE VIDEOWRITER
	VideoWriter writer2;
	string filename2 = "./live2.avi";             // name of the output video file
	writer2.open(filename2, codec, fps, frame1.size(), isColor);
	// check if we succeeded
	if (!writer2.isOpened()) {
		cerr << "Could not open the output video file for write\n";
		return -1;
	}
	//--- INITIALIZE VIDEOWRITER
	VideoWriter writer3;
	string filename3 = "./live3.avi";             // name of the output video file
	writer3.open(filename3, codec, fps, frame1.size(), isColor);
	// check if we succeeded
	if (!writer3.isOpened()) {
		cerr << "Could not open the output video file for write\n";
		return -1;
	}
	while (true) {

		// check if we succeeded
		if (!camera1.read(frame1)) {
			cerr << "ERROR! blank frame1 grabbed\n";
			break;
		}
		if (!camera2.read(frame2)) {
			cerr << "ERROR! blank frame1 grabbed\n";
			break;
		}
		if (!camera3.read(frame3)) {
			cerr << "ERROR! blank frame1 grabbed\n";
			break;
		}
		imshow("Video1", frame1);
		imshow("Video2", frame2);
		imshow("Video3", frame3);
		//std::cout << frame1.rows() << std::endl;
		//wait for 40 milliseconds

	
		// encode the frame into the videofile stream
		writer1.write(frame1);
		writer2.write(frame2);
		writer3.write(frame3);
		int c = waitKey(20);
		//exit the loop if user press "Esc" key  (ASCII value of "Esc" is 27)
		if (27 == char(c)) break;

	}
	return 0;
}

 

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值