当前位置:   article > 正文

vs2015 + opencv3 双目摄像头标定(C++实现)_#include #include

#include #include #include

    参考资料:张正友标定流程_张正友3d标定步骤_学之之博未若知之之要知之之要未若行之之实的博客-CSDN博客

    以前在本科的时候,做智能小车发现一个问题,当摄像头镜头视角越大,摄像头成像的照片出现变形的现象越严重,当时我们的解决办法是:使用小视角的镜头。今天学到摄像头标定之后,我发现,我们可以不用换小视角的镜头来解决这个问题,而是通过标定来解决这个问题。标定的目的:获得获取摄像机的内参和外参矩阵,通过这两个矩阵来矫正变形的图片,而内参矩阵指代:摄像头本身的畸变参数,外参矩阵指代:描述了如何把点从世界坐标系转换到摄像机坐标系。

    对于标定的方法,网上资料很多,有通过Matlab标定,有通过c++标定,我个人比较推荐c++标定。下面我将一一介绍“张正友标定”流程:

     1. 准备标定板

      本人因为是自学双目摄像头而且又比较穷,所以没有钱买标定板,也没有钱打印标定板,所以想了一个简单粗暴的办法,直接将电脑的显示器当标定板来用,

     2.保存样本图片

     在标定过程中,我们需要拍照一些图片,其内容必须得包含整幅标定板,用来求摄像机的内参和外参矩阵。由于我们标定的摄像头是双目,所以需要保存两份图片(左摄像头和右摄像头)

   3.对每一张标定图片,提取角点信息

     使用到的函数为:findChessboardCorners,但要注意一个问题,它提取的内角点,对于上面的标定板,每行9个角点数,每列6个角点数

  1. Size board_size = Size(9, 6); /* 标定板上每行、列的角点数 */
  2. Mat imageInput = imread(filename);
  3. vector<vector<Point2f>> image_points_seq; /* 保存检测到的所有角点 */
  4. findChessboardCorners(imageInput, board_size, image_points_buf)

  4.对粗提取的角点进行精确化

    使用到函数为:find4QuadCornerSubpix,其中Size(5,5)是角点搜索窗口的尺寸,相对于上面的结果,有些角点值被精确化了,可以对比上下两张图片的内容。

  1. Mat view_gray;
  2. cvtColor(imageInput, view_gray, CV_RGB2GRAY);
  3. /* 亚像素精确化 */
  4. find4QuadCornerSubpix(view_gray, image_points_buf, Size(5, 5)); //对粗提取的角点进行精确化

 

5.将内角点可视化

     使用到的函数为:drawChessboardCorners

drawChessboardCorners(view_gray, board_size, image_points_buf, false); //用于在图片中标记角点  

6.相机标定

   使用到函数为:calibrateCamera

  1. vector<vector<Point3f>> object_points; /* 保存标定板上角点的三维坐标 */
  2. vector<vector<Point2f>> image_points_seq; /* 保存检测到的所有角点 */
  3. Mat cameraMatrix = Mat(3, 3, CV_32FC1, Scalar::all(0)); /* 摄像机内参数矩阵 */
  4. Mat distCoeffs = Mat(1, 5, CV_32FC1, Scalar::all(0)); /* 摄像机的5个畸变系数:k1,k2,p1,p2,k3 */
  5. vector<Mat> tvecsMat; /* 每幅图像的旋转向量 */
  6. vector<Mat> rvecsMat; /* 每幅图像的平移向量 */
  7. calibrateCamera(object_points, image_points_seq, image_size, cameraMatrix, distCoeffs, rvecsMat, tvecsMat, 0);

  在相机标定之后,我们可以得到相机内参数矩阵与畸变系数

 

7.对标定结果进行评价

   对标定结果进行评价的方法是通过得到的摄像机内外参数,对空间的三维点进行重新投影计算,得到空间三维点在图像上新的投影点的坐标,计算投影坐标和亚像素角点坐标之间的偏差,偏差越小,标定结果越好,使用到的函数为:projectPoints

  1. vector<Point2f> image_points2; /* 保存重新计算得到的投影点 */
  2. /* 通过得到的摄像机内外参数,对空间的三维点进行重新投影计算,得到新的投影点 */
  3. projectPoints(tempPointSet, rvecsMat[i], tvecsMat[i], cameraMatrix, distCoeffs, image_points2);

亚像素角点坐标:

计算投影坐标:

经过循环计算,可以统计出所有的误差:

8.利用标定结果对棋盘图进行矫正

 左边为矫正后的图片,右边为原始图片,效果还是不错的。

以下完整的工程代码(代码重复运行两次分别得到左右摄像头的内参和外参

  1. #include "opencv2/core/core.hpp"
  2. #include "opencv2/imgproc/imgproc.hpp"
  3. #include "opencv2/calib3d/calib3d.hpp"
  4. #include "opencv2/highgui/highgui.hpp"
  5. #include <iostream>
  6. #include <fstream>
  7. using namespace cv;
  8. using namespace std;
  9. void main()
  10. {
  11. ifstream fin("calibdata_right.txt"); /* 标定所用图像文件的路径 */
  12. ofstream fout("caliberation_right_result.txt"); /* 保存标定结果的文件 */
  13. //读取每一幅图像,从中提取出角点,然后对角点进行亚像素精确化
  14. cout << "开始提取角点………………";
  15. int image_count = 0; /* 图像数量 */
  16. Size image_size; /* 图像的尺寸 */
  17. Size board_size = Size(9, 6); /* 标定板上每行、列的角点数 */
  18. vector<Point2f> image_points_buf; /* 缓存每幅图像上检测到的角点 */
  19. vector<vector<Point2f>> image_points_seq; /* 保存检测到的所有角点 */
  20. string filename;
  21. int count = -1;//用于存储角点个数。
  22. while (getline(fin, filename))
  23. {
  24. image_count++;
  25. // 用于观察检验输出
  26. cout << "image_count = " << image_count << endl;
  27. /* 输出检验*/
  28. cout << "-->count = " << count;
  29. Mat imageInput = imread(filename);
  30. if (image_count == 1) //读入第一张图片时获取图像宽高信息
  31. {
  32. image_size.width = imageInput.cols;
  33. image_size.height = imageInput.rows;
  34. cout << "image_size.width = " << image_size.width << endl;
  35. cout << "image_size.height = " << image_size.height << endl;
  36. }
  37. /* 提取角点 */
  38. if (0 == findChessboardCorners(imageInput, board_size, image_points_buf))
  39. {
  40. cout << "can not find chessboard corners!\n"; //找不到角点
  41. exit(1);
  42. }
  43. else
  44. {
  45. Mat view_gray;
  46. cvtColor(imageInput, view_gray, CV_RGB2GRAY);
  47. /* 亚像素精确化 */
  48. find4QuadCornerSubpix(view_gray, image_points_buf, Size(5, 5)); //对粗提取的角点进行精确化
  49. //cornerSubPix(view_gray,image_points_buf,Size(5,5),Size(-1,-1),TermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER,30,0.1));
  50. image_points_seq.push_back(image_points_buf); //保存亚像素角点
  51. /* 在图像上显示角点位置 */
  52. drawChessboardCorners(view_gray, board_size, image_points_buf, false); //用于在图片中标记角点
  53. imshow("Camera Calibration", view_gray);//显示图片
  54. waitKey(500);//暂停0.5S
  55. }
  56. }
  57. int total = image_points_seq.size();
  58. cout << "total = " << total << endl;
  59. int CornerNum = board_size.width*board_size.height; //每张图片上总的角点数
  60. for (int ii = 0; ii<total; ii++)
  61. {
  62. if (0 == ii%CornerNum)// 24 是每幅图片的角点个数。此判断语句是为了输出 图片号,便于控制台观看
  63. {
  64. int i = -1;
  65. i = ii / CornerNum;
  66. int j = i + 1;
  67. cout << "--> 第 " << j << "图片的数据 --> : " << endl;
  68. }
  69. if (0 == ii % 3) // 此判断语句,格式化输出,便于控制台查看
  70. {
  71. cout << endl;
  72. }
  73. else
  74. {
  75. cout.width(10);
  76. }
  77. //输出所有的角点
  78. cout << " -->" << image_points_seq[ii][0].x;
  79. cout << " -->" << image_points_seq[ii][0].y;
  80. }
  81. cout << "角点提取完成!\n";
  82. //以下是摄像机标定
  83. cout << "开始标定………………";
  84. /*棋盘三维信息*/
  85. Size square_size = Size(10, 10); /* 实际测量得到的标定板上每个棋盘格的大小 */
  86. vector<vector<Point3f>> object_points; /* 保存标定板上角点的三维坐标 */
  87. /*内外参数*/
  88. Mat cameraMatrix = Mat(3, 3, CV_32FC1, Scalar::all(0)); /* 摄像机内参数矩阵 */
  89. vector<int> point_counts; // 每幅图像中角点的数量
  90. Mat distCoeffs = Mat(1, 5, CV_32FC1, Scalar::all(0)); /* 摄像机的5个畸变系数:k1,k2,p1,p2,k3 */
  91. vector<Mat> tvecsMat; /* 每幅图像的旋转向量 */
  92. vector<Mat> rvecsMat; /* 每幅图像的平移向量 */
  93. /* 初始化标定板上角点的三维坐标 */
  94. int i, j, t;
  95. for (t = 0; t<image_count; t++)
  96. {
  97. vector<Point3f> tempPointSet;
  98. for (i = 0; i<board_size.height; i++)
  99. {
  100. for (j = 0; j<board_size.width; j++)
  101. {
  102. Point3f realPoint;
  103. /* 假设标定板放在世界坐标系中z=0的平面上 */
  104. realPoint.x = i*square_size.width;
  105. realPoint.y = j*square_size.height;
  106. realPoint.z = 0;
  107. tempPointSet.push_back(realPoint);
  108. }
  109. }
  110. object_points.push_back(tempPointSet);
  111. }
  112. /* 初始化每幅图像中的角点数量,假定每幅图像中都可以看到完整的标定板 */
  113. for (i = 0; i<image_count; i++)
  114. {
  115. point_counts.push_back(board_size.width*board_size.height);
  116. }
  117. /* 开始标定 */
  118. calibrateCamera(object_points, image_points_seq, image_size, cameraMatrix, distCoeffs, rvecsMat, tvecsMat, 0);
  119. cout << "标定完成!\n";
  120. //对标定结果进行评价
  121. cout << "开始评价标定结果………………\n";
  122. double total_err = 0.0; /* 所有图像的平均误差的总和 */
  123. double err = 0.0; /* 每幅图像的平均误差 */
  124. vector<Point2f> image_points2; /* 保存重新计算得到的投影点 */
  125. cout << "\t每幅图像的标定误差:\n";
  126. fout << "每幅图像的标定误差:\n";
  127. for (i = 0; i<image_count; i++)
  128. {
  129. vector<Point3f> tempPointSet = object_points[i];
  130. /* 通过得到的摄像机内外参数,对空间的三维点进行重新投影计算,得到新的投影点 */
  131. projectPoints(tempPointSet, rvecsMat[i], tvecsMat[i], cameraMatrix, distCoeffs, image_points2);
  132. /* 计算新的投影点和旧的投影点之间的误差*/
  133. vector<Point2f> tempImagePoint = image_points_seq[i];
  134. Mat tempImagePointMat = Mat(1, tempImagePoint.size(), CV_32FC2);
  135. Mat image_points2Mat = Mat(1, image_points2.size(), CV_32FC2);
  136. for (int j = 0; j < tempImagePoint.size(); j++)
  137. {
  138. image_points2Mat.at<Vec2f>(0, j) = Vec2f(image_points2[j].x, image_points2[j].y);
  139. tempImagePointMat.at<Vec2f>(0, j) = Vec2f(tempImagePoint[j].x, tempImagePoint[j].y);
  140. }
  141. err = norm(image_points2Mat, tempImagePointMat, NORM_L2);
  142. total_err += err /= point_counts[i];
  143. std::cout << "第" << i + 1 << "幅图像的平均误差:" << err << "像素" << endl;
  144. fout << "第" << i + 1 << "幅图像的平均误差:" << err << "像素" << endl;
  145. }
  146. std::cout << "总体平均误差:" << total_err / image_count << "像素" << endl;
  147. fout << "总体平均误差:" << total_err / image_count << "像素" << endl << endl;
  148. std::cout << "评价完成!" << endl;
  149. //保存定标结果
  150. std::cout << "开始保存定标结果………………" << endl;
  151. Mat rotation_matrix = Mat(3, 3, CV_32FC1, Scalar::all(0)); /* 保存每幅图像的旋转矩阵 */
  152. fout << "相机内参数矩阵:" << endl;
  153. fout << cameraMatrix << endl << endl;
  154. fout << "畸变系数:\n";
  155. fout << distCoeffs << endl << endl << endl;
  156. for (int i = 0; i<image_count; i++)
  157. {
  158. fout << "第" << i + 1 << "幅图像的旋转向量:" << endl;
  159. fout << tvecsMat[i] << endl;
  160. /* 将旋转向量转换为相对应的旋转矩阵 */
  161. Rodrigues(tvecsMat[i], rotation_matrix);
  162. fout << "第" << i + 1 << "幅图像的旋转矩阵:" << endl;
  163. fout << rotation_matrix << endl;
  164. fout << "第" << i + 1 << "幅图像的平移向量:" << endl;
  165. fout << rvecsMat[i] << endl << endl;
  166. }
  167. std::cout << "完成保存" << endl;
  168. fout << endl;
  169. /************************************************************************
  170. 显示定标结果
  171. *************************************************************************/
  172. Mat mapx = Mat(image_size, CV_32FC1);
  173. Mat mapy = Mat(image_size, CV_32FC1);
  174. Mat R = Mat::eye(3, 3, CV_32F);
  175. std::cout << "保存矫正图像" << endl;
  176. string imageFileName;
  177. std::stringstream StrStm;
  178. for (int i = 0; i != image_count; i++)
  179. {
  180. std::cout << "Frame #" << i + 1 << "..." << endl;
  181. initUndistortRectifyMap(cameraMatrix, distCoeffs, R, cameraMatrix, image_size, CV_32FC1, mapx, mapy);
  182. StrStm.clear();
  183. imageFileName.clear();
  184. string filePath = "image_right_";
  185. StrStm << i + 6;
  186. StrStm >> imageFileName;
  187. filePath += imageFileName;
  188. filePath += ".jpg";
  189. Mat imageSource = imread(filePath);
  190. Mat newimage = imageSource.clone();
  191. //另一种不需要转换矩阵的方式
  192. //undistort(imageSource,newimage,cameraMatrix,distCoeffs);
  193. remap(imageSource, newimage, mapx, mapy, INTER_LINEAR);
  194. StrStm.clear();
  195. filePath.clear();
  196. StrStm << i + 1;
  197. StrStm >> imageFileName;
  198. imageFileName += "_d.jpg";
  199. imwrite(imageFileName, newimage);
  200. }
  201. std::cout << "保存结束" << endl;
  202. return;
  203. }

9.利用左右摄像头的内参和外参进行双目标定

  代码如下(记得将第8步得到的左右摄像头的内参和外参分别填到cameraMatrixL、distCoeffL、cameraMatrixR和distCoeffR):

  1. #include <opencv2/core/core.hpp>
  2. #include <opencv2/imgproc/imgproc.hpp>
  3. #include <opencv2/calib3d/calib3d.hpp>
  4. #include <opencv2/highgui/highgui.hpp>
  5. #include <vector>
  6. #include <string>
  7. #include <algorithm>
  8. #include <iostream>
  9. #include <iterator>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <ctype.h>
  13. #include <opencv2/opencv.hpp>
  14. //#include <cv.h>
  15. //#include <cv.hpp>
  16. using namespace std;
  17. using namespace cv;
  18. //摄像头的分辨率
  19. const int imageWidth = 640;
  20. const int imageHeight = 360;
  21. //横向的角点数目
  22. const int boardWidth = 9;
  23. //纵向的角点数目
  24. const int boardHeight = 6;
  25. //总的角点数目
  26. const int boardCorner = boardWidth * boardHeight;
  27. //相机标定时需要采用的图像帧数
  28. const int frameNumber = 10;
  29. //标定板黑白格子的大小 单位是mm
  30. const int squareSize = 10;
  31. //标定板的总内角点
  32. const Size boardSize = Size(boardWidth, boardHeight);
  33. Size imageSize = Size(imageWidth, imageHeight);
  34. Mat R, T, E, F;
  35. //R旋转矢量 T平移矢量 E本征矩阵 F基础矩阵
  36. vector<Mat> rvecs; //R
  37. vector<Mat> tvecs; //T
  38. //左边摄像机所有照片角点的坐标集合
  39. vector<vector<Point2f>> imagePointL;
  40. //右边摄像机所有照片角点的坐标集合
  41. vector<vector<Point2f>> imagePointR;
  42. //各图像的角点的实际的物理坐标集合
  43. vector<vector<Point3f>> objRealPoint;
  44. //左边摄像机某一照片角点坐标集合
  45. vector<Point2f> cornerL;
  46. //右边摄像机某一照片角点坐标集合
  47. vector<Point2f> cornerR;
  48. Mat rgbImageL, grayImageL;
  49. Mat rgbImageR, grayImageR;
  50. Mat intrinsic;
  51. Mat distortion_coeff;
  52. //校正旋转矩阵R,投影矩阵P,重投影矩阵Q
  53. Mat Rl, Rr, Pl, Pr, Q;
  54. //映射表
  55. Mat mapLx, mapLy, mapRx, mapRy;
  56. Rect validROIL, validROIR;
  57. //图像校正之后,会对图像进行裁剪,其中,validROI裁剪之后的区域
  58. /*事先标定好的左相机的内参矩阵
  59. fx 0 cx
  60. 0 fy cy
  61. 0 0 1
  62. */
  63. Mat cameraMatrixL = (Mat_<double>(3, 3) << 940.4937685941497, 0, 227.3792711982141,
  64. 0, 949.0373173049467, 295.8358484611537,
  65. 0, 0, 1);
  66. //获得的畸变参数
  67. Mat distCoeffL = (Mat_<double>(5, 1) << -1.029423693166534, 1.058780876275055, -0.02758128682125558, 0.07367092471929151, -2.428716031795587);
  68. /*事先标定好的右相机的内参矩阵
  69. fx 0 cx
  70. 0 fy cy
  71. 0 0 1
  72. */
  73. Mat cameraMatrixR = (Mat_<double>(3, 3) << 1196.466071813248, 0, 386.9366034567681,
  74. 0, 1210.35406268507, 306.5246050041429,
  75. 0, 0, 1);
  76. Mat distCoeffR = (Mat_<double>(5, 1) << -1.58519463079371, 0.08628154897902529, -0.05737733624491874, -0.07915841018512512, 10.62054373440436);
  77. /*计算标定板上模块的实际物理坐标*/
  78. void calRealPoint(vector<vector<Point3f>>& obj, int boardWidth, int boardHeight, int imgNumber, int squareSize)
  79. {
  80. vector<Point3f> imgpoint;
  81. for (int rowIndex = 0; rowIndex < boardHeight; rowIndex++)
  82. {
  83. for (int colIndex = 0; colIndex < boardWidth; colIndex++)
  84. {
  85. imgpoint.push_back(Point3f(rowIndex * squareSize, colIndex * squareSize, 0));
  86. }
  87. }
  88. for (int imgIndex = 0; imgIndex < imgNumber; imgIndex++)
  89. {
  90. obj.push_back(imgpoint);
  91. }
  92. }
  93. void outputCameraParam(void)
  94. {
  95. /*保存数据*/
  96. /*输出数据*/
  97. FileStorage fs("intrisics.yml", FileStorage::WRITE);
  98. if (fs.isOpened())
  99. {
  100. fs << "cameraMatrixL" << cameraMatrixL << "cameraDistcoeffL" << distCoeffL << "cameraMatrixR" << cameraMatrixR << "cameraDistcoeffR" << distCoeffR;
  101. fs.release();
  102. cout << "cameraMatrixL=:" << cameraMatrixL << endl << "cameraDistcoeffL=:" << distCoeffL << endl << "cameraMatrixR=:" << cameraMatrixR << endl << "cameraDistcoeffR=:" << distCoeffR << endl;
  103. }
  104. else
  105. {
  106. cout << "Error: can not save the intrinsics!!!!" << endl;
  107. }
  108. fs.open("extrinsics.yml", FileStorage::WRITE);
  109. if (fs.isOpened())
  110. {
  111. fs << "R" << R << "T" << T << "Rl" << Rl << "Rr" << Rr << "Pl" << Pl << "Pr" << Pr << "Q" << Q;
  112. cout << "R=" << R << endl << "T=" << T << endl << "Rl=" << Rl << endl << "Rr" << Rr << endl << "Pl" << Pl << endl << "Pr" << Pr << endl << "Q" << Q << endl;
  113. fs.release();
  114. }
  115. else
  116. {
  117. cout << "Error: can not save the extrinsic parameters\n";
  118. }
  119. }
  120. int main(int argc, char* argv[])
  121. {
  122. Mat img;
  123. int goodFrameCount = 0;
  124. while (goodFrameCount < frameNumber)
  125. {
  126. char filename[100];
  127. /*读取左边的图像*/
  128. sprintf_s(filename, "E:\\c_pp_project\\opencv_test\\test\\test\\image_left_%d.jpg", goodFrameCount + 5);
  129. rgbImageL = imread(filename, CV_LOAD_IMAGE_COLOR);
  130. imshow("chessboardL", rgbImageL);
  131. cvtColor(rgbImageL, grayImageL, CV_BGR2GRAY);
  132. /*读取右边的图像*/
  133. sprintf_s(filename, "E:\\c_pp_project\\opencv_test\\test\\test\\image_right_%d.jpg", goodFrameCount + 5);
  134. rgbImageR = imread(filename, CV_LOAD_IMAGE_COLOR);
  135. cvtColor(rgbImageR, grayImageR, CV_BGR2GRAY);
  136. bool isFindL, isFindR;
  137. isFindL = findChessboardCorners(rgbImageL, boardSize, cornerL);
  138. isFindR = findChessboardCorners(rgbImageR, boardSize, cornerR);
  139. if (isFindL == true && isFindR == true)
  140. {
  141. cornerSubPix(grayImageL, cornerL, Size(5, 5), Size(-1, 1), TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 20, 0.1));
  142. drawChessboardCorners(rgbImageL, boardSize, cornerL, isFindL);
  143. imshow("chessboardL", rgbImageL);
  144. imagePointL.push_back(cornerL);
  145. cornerSubPix(grayImageR, cornerR, Size(5, 5), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 20, 0.1));
  146. drawChessboardCorners(rgbImageR, boardSize, cornerR, isFindR);
  147. imshow("chessboardR", rgbImageR);
  148. imagePointR.push_back(cornerR);
  149. goodFrameCount++;
  150. cout << "the image" << goodFrameCount << " is good" << endl;
  151. }
  152. else
  153. {
  154. cout << "the image is bad please try again" << endl;
  155. }
  156. if (waitKey(10) == 'q')
  157. {
  158. break;
  159. }
  160. }
  161. //计算实际的校正点的三维坐标,根据实际标定格子的大小来设置
  162. calRealPoint(objRealPoint, boardWidth, boardHeight, frameNumber, squareSize);
  163. cout << "cal real successful" << endl;
  164. //标定摄像头
  165. double rms = stereoCalibrate(objRealPoint, imagePointL, imagePointR,
  166. cameraMatrixL, distCoeffL,
  167. cameraMatrixR, distCoeffR,
  168. Size(imageWidth, imageHeight), R, T, E, F, CALIB_USE_INTRINSIC_GUESS,
  169. TermCriteria(TermCriteria::COUNT + TermCriteria::EPS, 100, 1e-5));
  170. cout << "Stereo Calibration done with RMS error = " << rms << endl;
  171. stereoRectify(cameraMatrixL, distCoeffL, cameraMatrixR, distCoeffR, imageSize, R, T, Rl,
  172. Rr, Pl, Pr, Q, CALIB_ZERO_DISPARITY, -1, imageSize, &validROIL, &validROIR);
  173. //摄像机校正映射
  174. initUndistortRectifyMap(cameraMatrixL, distCoeffL, Rl, Pl, imageSize, CV_32FC1, mapLx, mapLy);
  175. initUndistortRectifyMap(cameraMatrixR, distCoeffR, Rr, Pr, imageSize, CV_32FC1, mapRx, mapRy);
  176. Mat rectifyImageL, rectifyImageR;
  177. cvtColor(grayImageL, rectifyImageL, CV_GRAY2BGR);
  178. cvtColor(grayImageR, rectifyImageR, CV_GRAY2BGR);
  179. imshow("RecitifyL Before", rectifyImageL);
  180. imshow("RecitifyR Before", rectifyImageR);
  181. cout << "按Q1退出..." << endl;
  182. //经过remap之后,左右相机的图像已经共面并且行对准了
  183. Mat rectifyImageL2, rectifyImageR2;
  184. remap(rectifyImageL, rectifyImageL2, mapLx, mapLy, INTER_LINEAR);
  185. remap(rectifyImageR, rectifyImageR2, mapRx, mapRy, INTER_LINEAR);
  186. cout << "按Q2退出..." << endl;
  187. imshow("rectifyImageL", rectifyImageL2);
  188. imshow("rectifyImageR", rectifyImageR2);
  189. outputCameraParam();
  190. //显示校正结果
  191. Mat canvas;
  192. double sf;
  193. int w, h;
  194. sf = 600. / MAX(imageSize.width, imageSize.height);
  195. w = cvRound(imageSize.width * sf);
  196. h = cvRound(imageSize.height * sf);
  197. canvas.create(h, w * 2, CV_8UC3);
  198. //左图像画到画布上
  199. Mat canvasPart = canvas(Rect(0, 0, w, h));
  200. resize(rectifyImageL2, canvasPart, canvasPart.size(), 0, 0, INTER_AREA);
  201. Rect vroiL(cvRound(validROIL.x*sf), cvRound(validROIL.y*sf),
  202. cvRound(validROIL.width*sf), cvRound(validROIL.height*sf));
  203. rectangle(canvasPart, vroiL, Scalar(0, 0, 255), 3, 8);
  204. cout << "Painted ImageL" << endl;
  205. //右图像画到画布上
  206. canvasPart = canvas(Rect(w, 0, w, h));
  207. resize(rectifyImageR2, canvasPart, canvasPart.size(), 0, 0, INTER_LINEAR);
  208. Rect vroiR(cvRound(validROIR.x*sf), cvRound(validROIR.y*sf),
  209. cvRound(validROIR.width*sf), cvRound(validROIR.height*sf));
  210. rectangle(canvasPart, vroiR, Scalar(0, 255, 0), 3, 8);
  211. cout << "Painted ImageR" << endl;
  212. //画上对应的线条
  213. for (int i = 0; i < canvas.rows; i += 16)
  214. line(canvas, Point(0, i), Point(canvas.cols, i), Scalar(0, 255, 0), 1, 8);
  215. imshow("rectified", canvas);
  216. cout << "wait key" << endl;
  217. waitKey(0);
  218. return 0;
  219. }

     其效果见下图:

10.其中一些函数的说明:

1.findChessboardCorners:寻找棋盘图中棋盘角点

2.find4QuadCornerSubpix:对粗提取的角点进行精确化

3.cornerSubPix:在角点检测中精确化角点位置

4.drawChessboardCorners:将发现到的所有角点绘制到所提供的图像上

5.calibrateCamera:利用定标来计算摄像机的内参数和外参数

6.projectPoints:通过得到的摄像机内外参数,对空间的三维点进行重新投影计算,得到新的投影点

7.利用标定结果对棋盘图进行矫正

    1)initUndistortRectifyMap用来计算畸变映射,remap把求得的映射应用到图像上

    2)undistort一个函数搞定

8.stereoCalibrate:双目摄像机标定,计算了两个摄像头进行立体像对之间的转换关系,根据左右相机的参数矩阵,生成两个相机之间的关系矩阵,以及基本和本质矩阵

9.stereoRectify:作用是为每个摄像头计算立体校正的映射矩阵。所以其运行结果并不是直接将图片进行立体矫正,而是得出进行立体矫正所需要的映射矩阵

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/341161
推荐阅读
相关标签