主体代码在visual studio中安装完opencv就可以跑了,可能会报错,根据报错内容改就行;
辅助代码中有设置图像分辨率的代码
主体代码:
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
cv::VideoCapture capl(0);
cv::VideoCapture capr(1);
int i = 0;
cv::Mat src_imgl;
cv::Mat src_imgr;
char filename_l[15];
char filename_r[15];
while(capl.read(src_imgl) && capr.read(src_imgr))
{
cv::imshow("src_imgl", src_imgl);
cv::imshow("src_imgr", src_imgr);
char c = cv::waitKey(1);
if(c==‘ ’) //按空格采集图像,这里必须是单引号,不能是双引号
{
sprintf(filename_l, "img%d.jpg",i);
imwrite(filename_l, src_imgl);
sprintf(filename_r, "right%d.jpg",i++);
imwrite(filename_r, src_imgr);
}
if(c==‘q’ || c=='Q') // 按q退出,这里必须是单引号,不能是双引号
{
break;
}
}
return 0;
}
辅助代码:
#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 camera0(1);
camera0.set(CAP_PROP_FRAME_WIDTH,320);//若无namespace cv,那得写成CV_CAP_PROP_FRAME_WIDTH
camera0.set(CAP_PROP_FRAME_HEIGHT,240);
VideoCapture camera1(0);
camera1.set(CAP_PROP_FRAME_WIDTH,320);
camera1.set(CAP_PROP_FRAME_HEIGHT,240);
if( !camera0.isOpened() ) return 1;
if( !camera1.isOpened() ) return 1;
while(true) {
//grab and retrieve each frames of the video sequentially
Mat3b frame0;
camera0 >> frame0;
Mat3b frame1;
camera1 >> frame1;
imshow("Video0", frame0);
imshow("Video1", frame1);
//std::cout << frame1.rows() << std::endl;
//wait for 40 milliseconds
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;
}