之前做过双目相机的标定,需要同时开启两个摄像头同时采集图像,写过很麻烦每次只能采集一对图像的程序,后来发现waitKey()的使用可以很方便地通过键盘输入采集图像,写在博客里面希望可以给新手一些帮助吧。
#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;
}
需要注意的是摄像头插入的usb插口位置,在采集之前应该先打开摄像头通过遮挡一个摄像头的方式确定图像的左右和摄像头的左右是否对应