打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
OPENCV3.0 双目立体标定

转自:http://blog.csdn.net/zc850463390zc/article/details/48975263

这里是在上一篇单目标定的基础上拓展来的进行双目标定的程序。

在这个程序里面,默认是先对两个摄像头分别进行了单目标定的,也就是说相机的内参数和畸变向量是知道了的。

所以在进行标定的时候,参数选择的是CALIB_USE_INTRINSIC_GUESS。

此程序依然是使用系统自带的标定的图片,其路径在OpenCV的安装目录下:opencv\sources\samples\data。

本程序最终的输出图像为


上图就是左右相机的图像经过行对准之后的结果。

具体的代码如下:

  1. // stereoCalibration.cpp : 定义控制台应用程序的入口点。  
  2. //  
  3. //在进行双目摄像头的标定之前,最好事先分别对两个摄像头进行单目视觉的标定   
  4. //分别确定两个摄像头的内参矩阵,然后再开始进行双目摄像头的标定  
  5. //在此例程中是先对两个摄像头进行单独标定(见上一篇单目标定文章),然后在进行立体标定  
  6.   
  7. #include "stdafx.h"  
  8. #include <opencv2/opencv.hpp>  
  9. #include <highgui.hpp>  
  10. #include "cv.h"  
  11. #include <cv.hpp>  
  12. #include <iostream>  
  13.   
  14. using namespace std;  
  15. using namespace cv;  
  16.   
  17. const int imageWidth = 640;                             //摄像头的分辨率  
  18. const int imageHeight = 480;  
  19. const int boardWidth = 9;                               //横向的角点数目  
  20. const int boardHeight = 6;                              //纵向的角点数据  
  21. const int boardCorner = boardWidth * boardHeight;       //总的角点数据  
  22. const int frameNumber = 13;                             //相机标定时需要采用的图像帧数  
  23. const int squareSize = 20;                              //标定板黑白格子的大小 单位mm  
  24. const Size boardSize = Size(boardWidth, boardHeight);   //  
  25. Size imageSize = Size(imageWidth, imageHeight);  
  26.   
  27. Mat R, T, E, F;                                         //R 旋转矢量 T平移矢量 E本征矩阵 F基础矩阵  
  28. vector<Mat> rvecs;                                        //旋转向量  
  29. vector<Mat> tvecs;                                        //平移向量  
  30. vector<vector<Point2f>> imagePointL;                    //左边摄像机所有照片角点的坐标集合  
  31. vector<vector<Point2f>> imagePointR;                    //右边摄像机所有照片角点的坐标集合  
  32. vector<vector<Point3f>> objRealPoint;                   //各副图像的角点的实际物理坐标集合  
  33.   
  34.   
  35. vector<Point2f> cornerL;                              //左边摄像机某一照片角点坐标集合  
  36. vector<Point2f> cornerR;                              //右边摄像机某一照片角点坐标集合  
  37.   
  38. Mat rgbImageL, grayImageL;  
  39. Mat rgbImageR, grayImageR;  
  40.   
  41. Mat Rl, Rr, Pl, Pr, Q;                                  //校正旋转矩阵R,投影矩阵P 重投影矩阵Q (下面有具体的含义解释)   
  42. Mat mapLx, mapLy, mapRx, mapRy;                         //映射表  
  43. Rect validROIL, validROIR;                              //图像校正之后,会对图像进行裁剪,这里的validROI就是指裁剪之后的区域  
  44.   
  45. /* 
  46. 事先标定好的左相机的内参矩阵 
  47. fx 0 cx 
  48. 0 fy cy 
  49. 0 0  1 
  50. */  
  51. Mat cameraMatrixL = (Mat_<double>(3, 3) << 532.782, 0,       532.904,  
  52.                                           0,       342.505, 233.876,  
  53.                                           0,       0,       1);  
  54. Mat distCoeffL = (Mat_<double>(5, 1) << -0.28095, 0.0255745, 0.00122226, -0.000137736, 0.162946);  
  55. /* 
  56. 事先标定好的右相机的内参矩阵 
  57. fx 0 cx 
  58. 0 fy cy 
  59. 0 0  1 
  60. */  
  61. Mat cameraMatrixR = (Mat_<double>(3, 3) << 532.782, 0,       532.904,  
  62.                                             0,      342.505, 233.876,  
  63.                                             0,      0,       1);  
  64. Mat distCoeffR = (Mat_<double>(5, 1) << -0.28095, 0.0255745, 0.00122226, -0.000137736, 0.162946);  
  65.   
  66.   
  67. /*计算标定板上模块的实际物理坐标*/  
  68. void calRealPoint(vector<vector<Point3f>>& obj, int boardwidth, int boardheight, int imgNumber, int squaresize)  
  69. {  
  70.     //  Mat imgpoint(boardheight, boardwidth, CV_32FC3,Scalar(0,0,0));  
  71.     vector<Point3f> imgpoint;  
  72.     for (int rowIndex = 0; rowIndex < boardheight; rowIndex++)  
  73.     {  
  74.         for (int colIndex = 0; colIndex < boardwidth; colIndex++)  
  75.         {  
  76.             //  imgpoint.at<Vec3f>(rowIndex, colIndex) = Vec3f(rowIndex * squaresize, colIndex*squaresize, 0);  
  77.             imgpoint.push_back(Point3f(rowIndex * squaresize, colIndex * squaresize, 0));  
  78.         }  
  79.     }  
  80.     for (int imgIndex = 0; imgIndex < imgNumber; imgIndex++)  
  81.     {  
  82.         obj.push_back(imgpoint);  
  83.     }  
  84. }  
  85.   
  86. void outputCameraParam(void)  
  87. {  
  88.     /*保存数据*/  
  89.     /*输出数据*/  
  90.     FileStorage fs("intrinsics.yml", FileStorage::WRITE);  
  91.     if (fs.isOpened())  
  92.     {  
  93.         fs << "cameraMatrixL" << cameraMatrixL << "cameraDistcoeffL" << distCoeffL <<"cameraMatrixR" << cameraMatrixR << "cameraDistcoeffR" << distCoeffR;  
  94.         fs.release();  
  95.         cout << "cameraMatrixL=:" << cameraMatrixL <<endl<< "cameraDistcoeffL=:" << distCoeffL <<endl<<"cameraMatrixR=:" << cameraMatrixR <<endl<< "cameraDistcoeffR=:" << distCoeffR<<endl;  
  96.     }  
  97.     else  
  98.     {  
  99.         cout << "Error: can not save the intrinsics!!!!!" << endl;  
  100.     }  
  101.   
  102.     fs.open("extrinsics.yml", FileStorage::WRITE);  
  103.     if (fs.isOpened())  
  104.     {  
  105.         fs << "R" << R << "T" << T << "Rl" << Rl << "Rr" << Rr << "Pl" << Pl << "Pr" << Pr << "Q" << Q;  
  106.         cout << "R=" << R << endl << "T=" << T << endl << "Rl=" << Rl << endl << "Rr=" << Rr << endl << "Pl=" << Pl << endl << "Pr=" << Pr << endl << "Q=" << Q << endl;  
  107.         fs.release();  
  108.     }  
  109.     else  
  110.         cout << "Error: can not save the extrinsic parameters\n";  
  111. }  
  112.   
  113.   
  114. int _tmain(int argc, _TCHAR* argv[])  
  115. {  
  116.     Mat img;  
  117.     int goodFrameCount = 0;  
  118.     namedWindow("ImageL");  
  119.     namedWindow("ImageR");  
  120.     cout << "按Q退出 ..." << endl;  
  121.     while (goodFrameCount < frameNumber)  
  122.     {  
  123.         char filename[100];  
  124.         /*读取左边的图像*/  
  125.         sprintf_s(filename, "image\\left%02d.jpg", goodFrameCount + 1);  
  126.         rgbImageL = imread(filename, CV_LOAD_IMAGE_COLOR);  
  127.         cvtColor(rgbImageL, grayImageL, CV_BGR2GRAY);  
  128.   
  129.         /*读取右边的图像*/  
  130.         sprintf_s(filename, "image\\right%02d.jpg", goodFrameCount + 1);  
  131.         rgbImageR = imread(filename, CV_LOAD_IMAGE_COLOR);  
  132.         cvtColor(rgbImageR, grayImageR, CV_BGR2GRAY);  
  133.   
  134.         bool isFindL, isFindR;  
  135.   
  136.         isFindL = findChessboardCorners(rgbImageL, boardSize, cornerL);  
  137.         isFindR = findChessboardCorners(rgbImageR, boardSize, cornerR);  
  138.         if (isFindL == true && isFindR == true)  //如果两幅图像都找到了所有的角点 则说明这两幅图像是可行的  
  139.         {  
  140.             /* 
  141.             Size(5,5) 搜索窗口的一半大小 
  142.             Size(-1,-1) 死区的一半尺寸 
  143.             TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 20, 0.1)迭代终止条件 
  144.             */  
  145.             cornerSubPix(grayImageL, cornerL, Size(5, 5), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 20, 0.1));  
  146.             drawChessboardCorners(rgbImageL, boardSize, cornerL, isFindL);  
  147.             imshow("chessboardL", rgbImageL);  
  148.             imagePointL.push_back(cornerL);  
  149.   
  150.   
  151.             cornerSubPix(grayImageR, cornerR, Size(5, 5), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 20, 0.1));  
  152.             drawChessboardCorners(rgbImageR, boardSize, cornerR, isFindR);  
  153.             imshow("chessboardR", rgbImageR);  
  154.             imagePointR.push_back(cornerR);  
  155.   
  156.             /* 
  157.             本来应该判断这两幅图像是不是好的,如果可以匹配的话才可以用来标定 
  158.             但是在这个例程当中,用的图像是系统自带的图像,都是可以匹配成功的。 
  159.             所以这里就没有判断 
  160.             */  
  161.             //string filename = "res\\image\\calibration";  
  162.             //filename += goodFrameCount + ".jpg";  
  163.             //cvSaveImage(filename.c_str(), &IplImage(rgbImage));       //把合格的图片保存起来  
  164.             goodFrameCount++;  
  165.             cout << "The image is good" << endl;  
  166.         }  
  167.         else  
  168.         {  
  169.             cout << "The image is bad please try again" << endl;  
  170.         }  
  171.   
  172.         if (waitKey(10) == 'q')  
  173.         {  
  174.             break;  
  175.         }  
  176.     }  
  177.   
  178.     /* 
  179.     计算实际的校正点的三维坐标 
  180.     根据实际标定格子的大小来设置 
  181.     */  
  182.     calRealPoint(objRealPoint, boardWidth, boardHeight, frameNumber, squareSize);  
  183.     cout << "cal real successful" << endl;  
  184.   
  185.     /* 
  186.         标定摄像头 
  187.         由于左右摄像机分别都经过了单目标定 
  188.         所以在此处选择flag = CALIB_USE_INTRINSIC_GUESS 
  189.     */  
  190.     double rms = stereoCalibrate(objRealPoint, imagePointL, imagePointR,  
  191.         cameraMatrixL, distCoeffL,  
  192.         cameraMatrixR, distCoeffR,  
  193.         Size(imageWidth, imageHeight), R, T, E, F,  
  194.         CALIB_USE_INTRINSIC_GUESS,  
  195.         TermCriteria(TermCriteria::COUNT + TermCriteria::EPS, 100, 1e-5));  
  196.   
  197.     cout << "Stereo Calibration done with RMS error = " << rms << endl;  
  198.   
  199.     /* 
  200.     立体校正的时候需要两幅图像共面并且行对准 以使得立体匹配更加的可靠 
  201.     使得两幅图像共面的方法就是把两个摄像头的图像投影到一个公共成像面上,这样每幅图像从本图像平面投影到公共图像平面都需要一个旋转矩阵R 
  202.     stereoRectify 这个函数计算的就是从图像平面投影都公共成像平面的旋转矩阵Rl,Rr。 Rl,Rr即为左右相机平面行对准的校正旋转矩阵。 
  203.     左相机经过Rl旋转,右相机经过Rr旋转之后,两幅图像就已经共面并且行对准了。 
  204.     其中Pl,Pr为两个相机的投影矩阵,其作用是将3D点的坐标转换到图像的2D点的坐标:P*[X Y Z 1]' =[x y w]  
  205.     Q矩阵为重投影矩阵,即矩阵Q可以把2维平面(图像平面)上的点投影到3维空间的点:Q*[x y d 1] = [X Y Z W]。其中d为左右两幅图像的时差 
  206.     */  
  207.     stereoRectify(cameraMatrixL, distCoeffL, cameraMatrixR, distCoeffR, imageSize, R, T, Rl, Rr, Pl, Pr, Q,  
  208.                   CALIB_ZERO_DISPARITY,-1,imageSize,&validROIL,&validROIR);  
  209.     /* 
  210.     根据stereoRectify 计算出来的R 和 P 来计算图像的映射表 mapx,mapy 
  211.     mapx,mapy这两个映射表接下来可以给remap()函数调用,来校正图像,使得两幅图像共面并且行对准 
  212.     ininUndistortRectifyMap()的参数newCameraMatrix就是校正后的摄像机矩阵。在openCV里面,校正后的计算机矩阵Mrect是跟投影矩阵P一起返回的。 
  213.     所以我们在这里传入投影矩阵P,此函数可以从投影矩阵P中读出校正后的摄像机矩阵 
  214.     */  
  215.     initUndistortRectifyMap(cameraMatrixL, distCoeffL, Rl, Pr, imageSize, CV_32FC1, mapLx, mapLy);  
  216.     initUndistortRectifyMap(cameraMatrixR, distCoeffR, Rr, Pr, imageSize, CV_32FC1, mapRx, mapRy);  
  217.   
  218.   
  219.     Mat rectifyImageL, rectifyImageR;  
  220.     cvtColor(grayImageL, rectifyImageL, CV_GRAY2BGR);  
  221.     cvtColor(grayImageR, rectifyImageR, CV_GRAY2BGR);  
  222.   
  223.     imshow("Rectify Before", rectifyImageL);  
  224.   
  225.     /* 
  226.     经过remap之后,左右相机的图像已经共面并且行对准了 
  227.     */  
  228.     remap(rectifyImageL, rectifyImageL, mapLx, mapLy, INTER_LINEAR);  
  229.     remap(rectifyImageR, rectifyImageR, mapRx, mapRy, INTER_LINEAR);  
  230.   
  231.     imshow("ImageL", rectifyImageL);  
  232.     imshow("ImageR", rectifyImageR);  
  233.   
  234.     /*保存并输出数据*/  
  235.     outputCameraParam();  
  236.   
  237.     /* 
  238.         把校正结果显示出来 
  239.         把左右两幅图像显示到同一个画面上 
  240.         这里只显示了最后一副图像的校正结果。并没有把所有的图像都显示出来 
  241.     */  
  242.     Mat canvas;  
  243.     double sf;  
  244.     int w, h;  
  245.     sf = 600. / MAX(imageSize.width, imageSize.height);  
  246.     w = cvRound(imageSize.width * sf);  
  247.     h = cvRound(imageSize.height * sf);  
  248.     canvas.create(h, w * 2, CV_8UC3);  
  249.   
  250.     /*左图像画到画布上*/  
  251.     Mat canvasPart = canvas(Rect(w*0, 0, w, h));                                //得到画布的一部分  
  252.     resize(rectifyImageL, canvasPart, canvasPart.size(), 0, 0, INTER_AREA);     //把图像缩放到跟canvasPart一样大小  
  253.     Rect vroiL(cvRound(validROIL.x*sf), cvRound(validROIL.y*sf),                //获得被截取的区域    
  254.               cvRound(validROIL.width*sf), cvRound(validROIL.height*sf));  
  255.     rectangle(canvasPart, vroiL, Scalar(0, 0, 255), 3, 8);                      //画上一个矩形  
  256.   
  257.     cout << "Painted ImageL" << endl;  
  258.   
  259.     /*右图像画到画布上*/  
  260.     canvasPart = canvas(Rect(w, 0, w, h));                                      //获得画布的另一部分  
  261.     resize(rectifyImageR, canvasPart, canvasPart.size(), 0, 0, INTER_LINEAR);  
  262.     Rect vroiR(cvRound(validROIR.x * sf), cvRound(validROIR.y*sf),            
  263.               cvRound(validROIR.width * sf), cvRound(validROIR.height * sf));  
  264.     rectangle(canvasPart, vroiR, Scalar(0, 255, 0), 3, 8);  
  265.   
  266.     cout << "Painted ImageR" << endl;  
  267.   
  268.     /*画上对应的线条*/  
  269.     for (int i = 0; i < canvas.rows;i+=16)  
  270.         line(canvas, Point(0, i), Point(canvas.cols, i), Scalar(0, 255, 0), 1, 8);  
  271.   
  272.     imshow("rectified", canvas);  
  273.   
  274.     cout << "wait key" << endl;  
  275.     waitKey(0);  
  276.     system("pause");  
  277.     return 0;  
  278. }  


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
镜头畸变现象及其校正方法
张正友相机标定Opencv实现以及标定流程&&标定结果评价&&图像矫正流程解析
高翔Slambook第七讲代码解读(2d-2d位姿估计)
手撕ORB_SLAM2系列--跟踪--单目初始化1
实战 | 巧用位姿解算实现单目相机测距
VS2017+OpenCV3.3基于SGBM算法的双目立体视觉、双目测距(双目校正和立体匹配)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服