一.读取双目摄像头图像
我们在淘宝上买的双目摄像头:
分辨率参数:
这里我在程序中设置3040 * 1080分辨率(设置3040 * 1520时,实际上只有2176 * 1520,不知道是什么原因)
这种摄像头输出的是左右摄像头拼接在一起的图像,可以用ROI将其分割:
#include <opencv2/highgui/highgui.hpp>#include <opencv2/core/core.hpp>#include <opencv2/opencv.hpp>#define FRAME_WIDTH 3040#define FRAME_HEIGHT 1080using namespace cv;using namespace std;int main(){VideoCapture cap(1);cap.set(CV_CAP_PROP_FRAME_WIDTH,FRAME_WIDTH);cap.set(CV_CAP_PROP_FRAME_HEIGHT,FRAME_HEIGHT);// cout<<"Resolution:<"<<cap.get(CV_CAP_PROP_FRAME_WIDTH)<<","<<cap.get(CV_CAP_PROP_FRAME_HEIGHT)<<">\n";namedWindow("camera",CV_WINDOW_NORMAL);namedWindow("left_img",CV_WINDOW_NORMAL);namedWindow("right_img",CV_WINDOW_NORMAL);resizeWindow("camera",1020,760);while(1){Mat img;Mat left_img,right_img;//左右摄像图图像cap >> img;left_img = img(Rect(0,0,FRAME_WIDTH>>1,FRAME_HEIGHT));right_img = img(Rect(FRAME_WIDTH>>1,0,FRAME_WIDTH>>1,FRAME_HEIGHT));imshow("camera",img);imshow("left_img",left_img);imshow("right_img",right_img);//按下Esc退出if(waitKey(30)==27)return 0;}}
效果:
二.单目摄像头标定与矫正
分别对双目摄像头的左右摄像头做标定。
利用以下代码实现按下空格键时保存左右摄像头的图像:
(在可执行文件的目录下新建left_camera和right_camera两个文件夹,图像会保存到这两个文件夹里)
#include <opencv2/highgui/highgui.hpp>#include <opencv2/core/core.hpp>#include <opencv2/opencv.hpp>#define FRAME_WIDTH 3040#define FRAME_HEIGHT 1080using namespace cv;using namespace std;int main(){VideoCapture cap(1);cap.set(CV_CAP_PROP_FRAME_WIDTH,FRAME_WIDTH);cap.set(CV_CAP_PROP_FRAME_HEIGHT,FRAME_HEIGHT);cout<<"Resolution:<"<<cap.get(CV_CAP_PROP_FRAME_WIDTH)<<","<<cap.get(CV_CAP_PROP_FRAME_HEIGHT)<<">\n";namedWindow("camera",CV_WINDOW_NORMAL);namedWindow("left_img",CV_WINDOW_NORMAL);namedWindow("right_img",CV_WINDOW_NORMAL);resizeWindow("camera",1020,540);while(1){Mat img;Mat left_img,right_img;//左右摄像图图像cap >> img;left_img = img(Rect(0,0,FRAME_WIDTH>>1,FRAME_HEIGHT));right_img = img(Rect(FRAME_WIDTH>>1,0,FRAME_WIDTH>>1,FRAME_HEIGHT));imshow("camera",img);imshow("left_img",left_img);imshow("right_img",right_img);int key=waitKey(30);if(key==27)//按下Esc退出return 0;else if(key==32)//按下空格键保存图片{static int num=1;String left_img_name,right_img_name;left_img_name = "left_" + to_string(num) + ".jpg";right_img_name = "right_" + to_string(num) + ".jpg";cout<<left_img_name<<" "<<right_img_name<<endl;num++;imwrite("left_camera/" + left_img_name,left_img);imwrite("right_camera/" + right_img_name,right_img);}}}
Ubuntu上安装MATLAB参考:
/qq_32892383/article/details/79670871
MATLAB进行摄像头标定参考:
/Loser__Wang/article/details/51811347
标定前我们需要制作标准棋盘。
MATLAB中输入cameraCalibrator
进入标定工具
< ADD Images > 添加图片,输入棋盘方格的边长,确实后matlab会自动识别图中的棋盘。
< Radial Distortion> 的参数个数视摄像头的畸变程度而定,< Tangential Distortion >是切向畸变,< skew >是扭曲,按照实际情况选择。
我的摄像头畸变很小,所以我只选择了< Radial Distortion >的< 2 Coefficients>.
点击< Calibration > 进行校准,效果如下:
校准前:
校准后:
点击< Export Camera Parameters >,点击<确定>,即可导出相机参数。
命令行窗口会打印以下信息:
具体数值可在<工作区>查看
具体参数意义:
IntrinsicMatrix:
| Fx 0 0 |
| 0 Fy 0 |
| Cx Cy 0 |
(Cx,Cy)为成像中心点在图像中的坐标
(Fx,Fy)为焦距F分别除于dx,dy的值,其单位是像素。
(这个矩阵在opencv中是转置的,这点要注意)
dx,dy含义:
FocalLength,PrincipalPoint:其实就是 (Fx,Fy)与(Cx,Cy)
RadialDistortion:径向畸变参数(k1,k2)
TangentialDistortion:切向畸变参数(p1,p2)
获得摄像头内参后,我们可以在opencv中通过initUndistortRectifyMap()
和remap()
两个函数获得矫正过的图像。
代码:
#include <opencv2/highgui/highgui.hpp>#include <opencv2/core/core.hpp>#include <opencv2/opencv.hpp>#define FRAME_WIDTH 3040#define FRAME_HEIGHT 1080using namespace cv;using namespace std;int main(){VideoCapture cap(1);cap.set(CV_CAP_PROP_FRAME_WIDTH,FRAME_WIDTH);cap.set(CV_CAP_PROP_FRAME_HEIGHT,FRAME_HEIGHT);cout<<"Resolution:<"<<cap.get(CV_CAP_PROP_FRAME_WIDTH)<<","<<cap.get(CV_CAP_PROP_FRAME_HEIGHT)<<">\n";namedWindow("camera",CV_WINDOW_NORMAL);namedWindow("left_img",CV_WINDOW_NORMAL);namedWindow("right_img",CV_WINDOW_NORMAL);namedWindow("leftCalibration_img",CV_WINDOW_NORMAL);namedWindow("rightCalibration_img",CV_WINDOW_NORMAL);resizeWindow("camera",1020,540);Mat left_cameraMatrix = Mat::eye(3, 3, CV_64F);//左相机内参矩阵left_cameraMatrix.at<double>(0, 0) = 1.918336153824488e+03;//Fxleft_cameraMatrix.at<double>(0, 1) = 0;left_cameraMatrix.at<double>(0, 2) = 6.907303693220415e+02;//Cxleft_cameraMatrix.at<double>(1, 1) = 1.929292488197410e+03;//Fyleft_cameraMatrix.at<double>(1, 2) = 6.284631474799424e+02;//CyMat left_distCoeffs = Mat::zeros(5, 1, CV_64F);//畸变系数left_distCoeffs.at<double>(0, 0) = -0.035177753004162;//k1left_distCoeffs.at<double>(1, 0) = 0.529741452707019;//k2left_distCoeffs.at<double>(2, 0) = 0;//p1left_distCoeffs.at<double>(3, 0) = 0;//p2left_distCoeffs.at<double>(4, 0) = 0;Mat right_cameraMatrix = Mat::eye(3, 3, CV_64F);//右相机内参矩阵right_cameraMatrix.at<double>(0, 0) = 1.915849735633710e+03;//Fxright_cameraMatrix.at<double>(0, 1) = 0;right_cameraMatrix.at<double>(0, 2) = 6.147645852519341e+02;//Cxright_cameraMatrix.at<double>(1, 1) = 1.925977816564622e+03;//Fyright_cameraMatrix.at<double>(1, 2) = 6.307232887078274e+02;//CyMat right_distCoeffs = Mat::zeros(5, 1, CV_64F);//畸变系数right_distCoeffs.at<double>(0, 0) = -0.005231683363986;//k1right_distCoeffs.at<double>(1, 0) = 0.060025441793370;//k2right_distCoeffs.at<double>(2, 0) = 0;//p1right_distCoeffs.at<double>(3, 0) = 0;//p2right_distCoeffs.at<double>(4, 0) = 0;Mat left_map1,left_map2;Mat right_map1,right_map2;Size imageSize;imageSize = Size(FRAME_WIDTH>>1,FRAME_HEIGHT);initUndistortRectifyMap(left_cameraMatrix, left_distCoeffs, Mat(), getOptimalNewCameraMatrix(left_cameraMatrix, left_distCoeffs, imageSize, 1, imageSize, 0), imageSize, CV_16SC2, left_map1, left_map2);initUndistortRectifyMap(right_cameraMatrix, right_distCoeffs, Mat(), getOptimalNewCameraMatrix(right_cameraMatrix, right_distCoeffs, imageSize, 1, imageSize, 0), imageSize, CV_16SC2, right_map1, right_map2);while(1){Mat img;Mat left_img,right_img;//左右摄像头原始图像Mat leftCalibration_img,rightCalibration_img;//左右摄像头校准后图像cap >> img;left_img = img(Rect(0,0,FRAME_WIDTH>>1,FRAME_HEIGHT));right_img = img(Rect(FRAME_WIDTH>>1,0,FRAME_WIDTH>>1,FRAME_HEIGHT));remap(left_img, leftCalibration_img, left_map1, left_map2, INTER_LINEAR);remap(right_img, rightCalibration_img, right_map1, right_map2, INTER_LINEAR);imshow("camera",img);imshow("left_img",left_img);imshow("right_img",right_img);imshow("leftCalibration_img",leftCalibration_img);imshow("rightCalibration_img",rightCalibration_img);int key=waitKey(30);if(key==27)//按下Esc退出return 0;}}
效果:
因为我的摄像头本身畸变小,矫正后并没有太大变化。
三.双目摄像头标定
我利用MATLAB做双目标定
在< APP > 中 < 图像处理与计算机视觉 > 中打开 < Stereo Camera Calibration >
跟单目校准一样的使用方式,导入图片,输入方格大小等。
效果:
导出参数:
这里的Parameters of Two Cameras 的内容跟单目校准的参数是一样的。
TranslationOfCamera2 :平移向量
RotationOfCamera2是相机2相对于相机1的旋转矩阵,这里要注意opencv里面用的是旋转向量,转换
旋转矩阵和旋转向量概念:
/hongbin_xu/article/details/78929006
在opencv中我们用Rodrigues()
将旋转矩阵转化为旋转向量,利用stereoRectify()
,initUndistortRectifyMap()
,remap()
来得到双目校准和对齐后的图像。
#include <opencv2/highgui/highgui.hpp>#include <opencv2/core/core.hpp>#include <opencv2/opencv.hpp>#define FRAME_WIDTH 3040#define FRAME_HEIGHT 1080using namespace cv;using namespace std;int main(){VideoCapture cap(1);cap.set(CV_CAP_PROP_FRAME_WIDTH,FRAME_WIDTH);cap.set(CV_CAP_PROP_FRAME_HEIGHT,FRAME_HEIGHT);cout<<"Resolution:<"<<cap.get(CV_CAP_PROP_FRAME_WIDTH)<<","<<cap.get(CV_CAP_PROP_FRAME_HEIGHT)<<">\n";namedWindow("camera",CV_WINDOW_NORMAL);// namedWindow("left_img",CV_WINDOW_NORMAL);// namedWindow("right_img",CV_WINDOW_NORMAL);// namedWindow("left_rectified_img",CV_WINDOW_NORMAL);// namedWindow("right_rectified_img",CV_WINDOW_NORMAL);// namedWindow("rectified_img",CV_WINDOW_NORMAL);namedWindow("aligned_rectified_img",CV_WINDOW_NORMAL);resizeWindow("camera",1020,540);resizeWindow("aligned_rectified_img",1020,540);Mat left_cameraMatrix = Mat::eye(3, 3, CV_64F);//左相机内参矩阵left_cameraMatrix.at<double>(0, 0) = 1.918336153824488e+03;//Fxleft_cameraMatrix.at<double>(0, 1) = 0;left_cameraMatrix.at<double>(0, 2) = 6.907303693220415e+02;//Cxleft_cameraMatrix.at<double>(1, 1) = 1.929292488197410e+03;//Fyleft_cameraMatrix.at<double>(1, 2) = 6.284631474799424e+02;//CyMat left_distCoeffs = Mat::zeros(5, 1, CV_64F);//畸变系数left_distCoeffs.at<double>(0, 0) = -0.035177753004162;//k1left_distCoeffs.at<double>(1, 0) = 0.529741452707019;//k2left_distCoeffs.at<double>(2, 0) = 0;//p1left_distCoeffs.at<double>(3, 0) = 0;//p2left_distCoeffs.at<double>(4, 0) = 0;Mat right_cameraMatrix = Mat::eye(3, 3, CV_64F);//右相机内参矩阵right_cameraMatrix.at<double>(0, 0) = 1.915849735633710e+03;//Fxright_cameraMatrix.at<double>(0, 1) = 0;right_cameraMatrix.at<double>(0, 2) = 6.147645852519341e+02;//Cxright_cameraMatrix.at<double>(1, 1) = 1.925977816564622e+03;//Fyright_cameraMatrix.at<double>(1, 2) = 6.307232887078274e+02;//CyMat right_distCoeffs = Mat::zeros(5, 1, CV_64F);//畸变系数right_distCoeffs.at<double>(0, 0) = -0.005231683363986;//k1right_distCoeffs.at<double>(1, 0) = 0.060025441793370;//k2right_distCoeffs.at<double>(2, 0) = 0;//p1right_distCoeffs.at<double>(3, 0) = 0;//p2right_distCoeffs.at<double>(4, 0) = 0;Mat rotation_matrix = Mat::zeros(3,3,CV_64F);//旋转矩阵rotation_matrix.at<double>(0,0)=0.999945124306786;rotation_matrix.at<double>(1,0)=0.008043320446283;rotation_matrix.at<double>(2,0)=0.006712180814433;rotation_matrix.at<double>(1,0)=0.008030133665074;rotation_matrix.at<double>(1,1)=0.999965779337229;rotation_matrix.at<double>(1,2)=0.001989248051137;rotation_matrix.at<double>(2,0)=0.006727951278679;rotation_matrix.at<double>(2,1)=0.001935239180647;rotation_matrix.at<double>(2,2)=0.999975494460192;Mat rotation_vector;//旋转矩阵Rodrigues(rotation_matrix,rotation_vector);//旋转矩阵转化为旋转向量Mat translation_vector = Mat::zeros(3,1,CV_64F);//平移向量translation_vector.at<double>(0,0) = 51.157553416555460;translation_vector.at<double>(1,0) = 0.176996310367285;translation_vector.at<double>(2,0) = 1.00136980156;Mat R1,R2;//左右相机的3x3矫正变换(旋转矩阵)Mat P1,P2;//左右相机新的坐标系统(矫正过的)输出 3x4 的投影矩阵Mat Q;//深度视差映射矩阵Rect left_valid_roi,right_valid_roi;//图像校正之后,会对图像进行裁剪,这里的validRoi就是指裁剪之后的区域Mat rmap[2][2];//映射表 必须为:CV_16SC2/CV_32FC1/CV_32FC2Size imageSize;imageSize = Size(FRAME_WIDTH>>1,FRAME_HEIGHT);/*立体校正的时候需要两幅图像共面并且行对准 以使得立体匹配更加的可靠使得两幅图像共面的方法就是把两个摄像头的图像投影到一个公共成像面上,这样每幅图像从本图像平面投影到公共图像平面都需要一个旋转矩阵RstereoRectify 这个函数计算的就是从图像平面投影都公共成像平面的旋转矩阵R1,R2。 R1,R2即为左右相机平面行对准的校正旋转矩阵。左相机经过R1旋转,右相机经过R2旋转之后,两幅图像就已经共面并且行对准了。其中P1,P2为两个相机的投影矩阵,其作用是将3D点的坐标转换到图像的2D点的坐标:P*[X Y Z 1]' =[x y w]Q矩阵为重投影矩阵,即矩阵Q可以把2维平面(图像平面)上的点投影到3维空间的点:Q*[x y d 1] = [X Y Z W]。其中d为左右两幅图像的时差*///双目校准stereoRectify(left_cameraMatrix,left_distCoeffs,//左摄像头内参和畸变系数right_cameraMatrix,right_distCoeffs,//右摄像头内参和畸变系数imageSize,rotation_vector,translation_vector,//图像大小,右摄像头相对于左摄像头旋转矩阵,平移向量R1,R2,P1,P2,Q,//输出的参数CALIB_ZERO_DISPARITY,-1,imageSize, &left_valid_roi, &right_valid_roi);//Precompute maps for cv::remap()initUndistortRectifyMap(left_cameraMatrix,left_distCoeffs,R1,P1,imageSize,CV_16SC2,rmap[0][0],rmap[0][1]);initUndistortRectifyMap(right_cameraMatrix,right_distCoeffs,R2,P2,imageSize,CV_16SC2,rmap[1][0],rmap[1][1]);while(1){Mat img;Mat left_img,right_img;//左右摄像头原始图像Mat left_rectified_img,right_rectified_img;//左右摄像头校准后图像Mat aligned_rectified_img(FRAME_HEIGHT,FRAME_WIDTH ,CV_8UC3);//校准+对齐后的图像cap >> img;left_img = img(Rect(0,0,FRAME_WIDTH>>1,FRAME_HEIGHT));right_img = img(Rect(FRAME_WIDTH>>1,0,FRAME_WIDTH>>1,FRAME_HEIGHT));//经过remap之后,左右相机的图像已经共面并且行对准了remap(left_img, left_rectified_img, rmap[0][0], rmap[0][1], INTER_LINEAR);remap(right_img, right_rectified_img, rmap[1][0], rmap[1][1], INTER_LINEAR);//复制左相机校准图像到总图像上for(int i=0; i<left_rectified_img.rows ;i++){for(int j=0 ; j<left_rectified_img.cols ;j++){aligned_rectified_img.at<Vec3b>(i,j)[0] = left_rectified_img.at<Vec3b>(i,j)[0];aligned_rectified_img.at<Vec3b>(i,j)[1] = left_rectified_img.at<Vec3b>(i,j)[1];aligned_rectified_img.at<Vec3b>(i,j)[2] = left_rectified_img.at<Vec3b>(i,j)[2];}}//复制右相机校准图像到总图像上for(int i=0; i<right_rectified_img.rows ;i++){for(int j=0 ; j<right_rectified_img.cols ;j++){aligned_rectified_img.at<Vec3b>(i,j+(FRAME_WIDTH>>1))[0] = right_rectified_img.at<Vec3b>(i,j)[0];aligned_rectified_img.at<Vec3b>(i,j+(FRAME_WIDTH>>1))[1] = right_rectified_img.at<Vec3b>(i,j)[1];aligned_rectified_img.at<Vec3b>(i,j+(FRAME_WIDTH>>1))[2] = right_rectified_img.at<Vec3b>(i,j)[2];}}rectangle(aligned_rectified_img,left_valid_roi,Scalar(0,0,255),5,8);rectangle(aligned_rectified_img,Rect(right_valid_roi.x+(FRAME_WIDTH>>1),right_valid_roi.y,right_valid_roi.width,right_valid_roi.height),Scalar(0,0,255),5,8);for(int i=0 ; i<aligned_rectified_img.rows ; i+=32){line(aligned_rectified_img,Point(0,i),Point(aligned_rectified_img.cols,i),Scalar(0,255,0),1,LINE_8);}imshow("camera",img);// imshow("left_img",left_img);// imshow("right_img",right_img);// imshow("left_rectified_img",left_rectified_img);// imshow("right_rectified_img",right_rectified_img);imshow("aligned_rectified_img",aligned_rectified_img);int key=waitKey(30);if(key==27)//按下Esc退出return 0;}}
效果:
这里红色的框内为valid roi。
四
五.总结
1.此demo主要是为了测试用的,用到了opencv和MATLAB,操作麻烦。下一步我们准备把标定也用opencv来实现。
2.此demo跑的速度只有4Fps,太慢了,需要做优化。
3.考虑到后面我们主要是用作测距和物体检测,可能需要做GUI界面,考虑用Qt来做。