整合了一下网上代码,可成功获得左右相机的图片,获取完成后会自动退出
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include<iostream>
#include<string>
#include<sstream>
#include<stdio.h>
using namespace cv;
int main()
{
VideoCapture cap(0);
Mat frame;
cap >> frame;
imshow("whole",frame);
Mat leftImage, rightImage;
//split right image
int i = 0, j = 0;
char key;
char image1[200];
char image2[200];
while (i <= 15) {//数字可以更改
key = waitKey(500);
cap >> frame;//捕获双目摄像头下的当前帧图片
resize(frame, frame, Size(1280, 480));//调整图像大小
leftImage = frame(Rect(0, 0, frame.size().width / 2, frame.size().height));//分离得左图像
rightImage = frame(Rect(frame.size().width / 2, 0, frame.size().width / 2, frame.size().height));//分离得右图像
imshow("leftImage", leftImage);//left image
imshow("rightImage", rightImage);//right image
//输入ESC,退出循环
if (key == 27)
break;
//输入空格键,获取并保存图像
if (key == 32) {
sprintf_s(image1, "C:\\image_left_%d.bmp", ++i);//sprintf的安全格式
imwrite(image1, leftImage);
sprintf_s(image2, "C:\\image_right_%d.bmp", ++j);
imwrite(image2, rightImage);
}
waitKey(33);
}
return 0;
}