原则上获取双目只是单目的程序叠加,但需要注意的是,有时获取外部usb摄像头,需要将cap.set(CV_CAP_PROP_FPS, 30.0)注释掉
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
VideoCapture cap1(1);//开启外接摄像头,顺序要从大到小
VideoCapture cap0(0);
if (cap1.isOpened() && cap0.isOpened())
cout << "camera is opened"<< endl;
else
cout << "camera is not opened" << endl;
bool stop = false;
Mat frame;
//设置摄像头分辨率640*480 帧率30fps
cap0.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
cap0.set(CV_CAP_PROP_FRAME_WIDTH, 640);
cap1.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
cap1.set(CV_CAP_PROP_FRAME_WIDTH, 640);
//cap.set(CV_CAP_PROP_FPS, 30.0); //在连接有些外部usb摄像头时需要将此句注释掉
//保存视频格式为avi, 编码为MJPG
VideoWriter writer0("two_test0.avi",CV_FOURCC('M', 'J', 'P', 'G'),30, Size(640,480),true);
VideoWriter writer1("two_test1.avi",CV_FOURCC('M', 'J', 'P', 'G'),30, Size(640,480),true);
//通过总帧数控制拍摄时间,如果为5s的视频,循环5*30次;
Mat videoPlay0,videoPlay1;
int count = 150;
//namedWindow("videoplay", WINDOW_NORMAL);
namedWindow("videoplay0", WINDOW_AUTOSIZE);
namedWindow("videoplay1", WINDOW_AUTOSIZE);
while (count)
{
//cap >> videoPlay;
//writer << videoPlay;
cap0 >> videoPlay0; //
if(!cap0.read(videoPlay0)) // 读取下一帧图像
break;
writer0 << videoPlay0;
imshow("videoplay0", videoPlay0);
cap1 >> videoPlay1;
if(!cap1.read(videoPlay1)) // 读取下一帧图像
break;
writer1 << videoPlay1;
imshow("videoplay1", videoPlay1);
waitKey(30);
count --;
}
//读取获取到的视频,进行图像采集
VideoCapture cap00("two_test0.avi");
VideoCapture cap11("two_test1.avi");
if(cap00.isOpened() && cap11.isOpened())
cout << "ok ,have a video" << endl;
else
cout << "no video" << endl;
Mat frame00,frame11;
int num0 = 1, num1 = 1;
string filename0;
char temp_file0[5];
string path0 = "C:\\Users\\Administrator\\Desktop\\pic_save\\parral_x";//can change the path
for (int i = 1; ;i++)
{
cap00 >> frame00;
if(frame00.empty())
break;
if(i % 5 == 0)
{
_itoa_s(num0, temp_file0, 4, 10);
filename0 = temp_file0;
filename0 = path0 + filename0 + ".jpg";
num0 ++;
imwrite (filename0,frame00);
}
}
cap00.release();
//重复上一步骤
string filename1;
char temp_file1[5];
string path1 = "C:\\Users\\Administrator\\Desktop\\pic_save\\parral_y";//can change the path
for (int i = 1; ;i++)
{
cap11 >> frame11;
if(frame11.empty())
break;
if(i % 5 == 0)
{
_itoa_s(num1, temp_file1, 4, 10);
filename1 = temp_file1;
filename1 = path1 + filename1 + ".jpg";
num1 ++;
imwrite (filename1,frame11);
}
}
cap11.release();
//system("pause");
return 0;
}