#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;
}