市面上能买到的双目相机传输图像的方式无非两种:一种是两个相机用两根数据线,分别向主机传输图像,这种模式无法保证相机图像的同步性。另一种方式是两个相机模组通过xx芯片同步控制,保证双目相机的同步触发,并通过xx芯片,将左右目的图像拼接起来,再通过一根数据线,将组合后的图像传输至电脑中。这种方式可以保证双目采集到的图像具有同步性。
那么如何用C++来读取一组双目图像呢:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
int main()
{
VideoCapture cap;
cap.open(0);
cap.set(CAP_PROP_FRAME_WIDTH, 2560);
cap.set(CAP_PROP_FRAME_HEIGHT, 720);
while (1) {
Mat frame;
cap >> frame;
imshow("a", frame);
waitKey(10);
}
}
如果我们想拆分图像,我们可以这样:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
int main()
{
VideoCapture cap;
cap.open(1);
cap.set(CAP_PROP_FRAME_WIDTH, 2560);
cap.set(CAP_PROP_FRAME_HEIGHT, 720);
int n = 0;
while (1) {
Mat frame;
cap >> frame;
Mat leftImage, rightImage;
leftImage = frame(Rect(0, 0, img.size().width / 2, img.size().height));//split left image
rightImage = frame(Rect(img.size().width / 2, 0, img.size().width / 2, img.size().height));//split right image
imshow("leftImage", leftImage);//left image
imshow("rightImage", rightImage);//right image
waitKey(10);
}
}
如果我们需要标定双目相机,那么可以用上述代码进行图像采集并保存。如果标定选用的棋盘格标定吧,那么我们可以直接用MATLAB双目标定的组件进行相机标定。这个流程比较简单,笔者不进行赘述。
考虑到用圆形标定板标定的结果可能更加精确,笔者在下一章向各位介绍如何用C++编写圆形标定板的标定程序。