当前位置:   article > 正文

【opencv】教程代码 —features2D(4)利用两张摄像机拍摄的图片计算单应性矩阵...

【opencv】教程代码 —features2D(4)利用两张摄像机拍摄的图片计算单应性矩阵...

c011209281ae0e106040e01dc4bfab67.png

homography_from_camera_displacement.cpp

3c402bf19ec1bddb8a29a20d23b16c94.png

Chessboard poses 棋盘姿态

1641b7fc55e4c3e5ced790fe5ed1c22e.png

使用根据相机位移计算的单应性扭曲图像

a3f7f7f6404160a24bb86a6fca3efa10.png

使用根据绝对相机姿势计算的单应性扭曲图像

415bf793b7c344a67510d6e1b49678c0.png

Warped images comparison 扭曲图像比较

左侧-nfindHomography  右侧-使用根据相机位移计算的单应性扭曲图像

终端输出:

  1. Euclidean Homography:(根据相机位移计算单应性)
  2. [0.2215344870864446, -0.9949332344575121, 0.1140657984013492;
  3. 0.6776201343947539, 0.1839368762588706, -0.1530250021245755;
  4. 0.3300066201371719, -0.5683454599466965, 1]
  5. Euclidean Homography 2:(同上,但使用绝对相机姿势而不是相机位移 仅供检查)
  6. [0.2215344870864446, -0.9949332344575118, 0.1140657984013491;
  7. 0.6776201343947537, 0.1839368762588706, -0.1530250021245755;
  8. 0.3300066201371718, -0.5683454599466965, 1]
  9. findHomography H:
  10. [0.3290339333220099, -1.244138808862929, 536.4769088231476;
  11. 0.6969763913334047, -0.0893590907257152, -80.3406850408241;
  12. 0.0004051172959296097, -0.001079740100565012, 1]
  13. homography from camera displacement:(相机位移的单应性)
  14. [0.4160569974777896, -1.306889022263172, 553.7055455031656;
  15. 0.7917584238390836, -0.06341244817498765, -108.2770026444472;
  16. 0.0005926357279773245, -0.00102065172285972, 1]
  17. homography from absolute camera poses:(绝对相机姿势的单应性)
  18. [0.4160569974777895, -1.306889022263171, 553.7055455031656;
  19. 0.7917584238390833, -0.06341244817498759, -108.2770026444472;
  20. 0.0005926357279773244, -0.00102065172285972, 1]

d9fdeae07ea7e41f52e738ff039ed57f.png

  1. // 包含必要的库
  2. #include <iostream> // 用于基本输入输出
  3. #include <opencv2/core.hpp> // 包含OpenCV库的核心部分
  4. #include <opencv2/imgproc.hpp> // 包含OpenCV库的图像处理部分
  5. #include <opencv2/highgui.hpp> // 包含OpenCV库的高级GUI部分
  6. #include <opencv2/calib3d.hpp> // 包含OpenCV库的相机标定和3D重建部分
  7. using namespace std; // 使用标准命名空间
  8. using namespace cv; // 使用OpenCV命名空间
  9. namespace // 定义一个无名命名空间
  10. {
  11. enum Pattern { CHESSBOARD, CIRCLES_GRID, ASYMMETRIC_CIRCLES_GRID }; // 定义一个枚举类型Pattern,包含棋盘、圆形网格和非对称圆形网格三种模式
  12. void calcChessboardCorners(Size boardSize, float squareSize, vector<Point3f>& corners, Pattern patternType = CHESSBOARD)
  13. // 定义一个函数,用于计算棋盘角点
  14. {
  15. corners.resize(0); // 重置角点向量
  16. switch (patternType) // 根据模式类型进行不同的操作
  17. {
  18. case CHESSBOARD:
  19. case CIRCLES_GRID:
  20. for( int i = 0; i < boardSize.height; i++ ) // 遍历棋盘的每一行
  21. for( int j = 0; j < boardSize.width; j++ ) // 遍历棋盘的每一列
  22. corners.push_back(Point3f(float(j*squareSize), // 将计算得到的角点坐标添加到角点向量中
  23. float(i*squareSize), 0));
  24. break;
  25. case ASYMMETRIC_CIRCLES_GRID:
  26. for( int i = 0; i < boardSize.height; i++ ) // 遍历棋盘的每一行
  27. for( int j = 0; j < boardSize.width; j++ ) // 遍历棋盘的每一列
  28. corners.push_back(Point3f(float((2*j + i % 2)*squareSize), // 将计算得到的角点坐标添加到角点向量中
  29. float(i*squareSize), 0));
  30. break;
  31. default:
  32. CV_Error(Error::StsBadArg, "Unknown pattern type\n"); // 如果模式类型未知,则抛出错误
  33. }
  34. }
  35. //! [compute-homography]
  36. Mat computeHomography(const Mat &R_1to2, const Mat &tvec_1to2, const double d_inv, const Mat &normal)
  37. // 定义一个函数,用于计算单应性矩阵
  38. {
  39. Mat homography = R_1to2 + d_inv * tvec_1to2*normal.t(); // 计算单应性矩阵
  40. return homography; // 返回单应性矩阵
  41. }
  42. //! [compute-homography]
  43. Mat computeHomography(const Mat &R1, const Mat &tvec1, const Mat &R2, const Mat &tvec2,
  44. const double d_inv, const Mat &normal)
  45. // 定义一个函数,用于计算单应性矩阵
  46. {
  47. Mat homography = R2 * R1.t() + d_inv * (-R2 * R1.t() * tvec1 + tvec2) * normal.t(); // 计算单应性矩阵
  48. return homography; // 返回单应性矩阵
  49. }
  50. //! [compute-c2Mc1]
  51. void computeC2MC1(const Mat &R1, const Mat &tvec1, const Mat &R2, const Mat &tvec2,
  52. Mat &R_1to2, Mat &tvec_1to2)
  53. // 定义一个函数,用于计算相机位移
  54. {
  55. //c2Mc1 = c2Mo * oMc1 = c2Mo * c1Mo.inv()
  56. R_1to2 = R2 * R1.t(); // 计算旋转矩阵
  57. tvec_1to2 = R2 * (-R1.t()*tvec1) + tvec2; // 计算平移向量
  58. }
  59. //! [compute-c2Mc1]
  60. void homographyFromCameraDisplacement(const string &img1Path, const string &img2Path, const Size &patternSize,
  61. const float squareSize, const string &intrinsicsPath)
  62. // 定义一个函数,用于根据相机位移计算单应性矩阵
  63. {
  64. Mat img1 = imread( samples::findFile( img1Path ) ); // 读取第一张图片
  65. Mat img2 = imread( samples::findFile( img2Path ) ); // 读取第二张图片
  66. //! [compute-poses]
  67. vector<Point2f> corners1, corners2; // 定义两个角点向量
  68. bool found1 = findChessboardCorners(img1, patternSize, corners1); // 在第一张图片中寻找棋盘角点
  69. bool found2 = findChessboardCorners(img2, patternSize, corners2); // 在第二张图片中寻找棋盘角点
  70. if (!found1 || !found2) // 如果在任何一张图片中都找不到棋盘角点
  71. {
  72. cout << "Error, cannot find the chessboard corners in both images." << endl; // 输出错误信息
  73. return; // 返回
  74. }
  75. vector<Point3f> objectPoints; // 定义一个对象点向量
  76. calcChessboardCorners(patternSize, squareSize, objectPoints); // 计算棋盘角点
  77. FileStorage fs( samples::findFile( intrinsicsPath ), FileStorage::READ); // 打开内参文件
  78. Mat cameraMatrix, distCoeffs; // 定义相机矩阵和畸变系数
  79. fs["camera_matrix"] >> cameraMatrix; // 读取相机矩阵
  80. fs["distortion_coefficients"] >> distCoeffs; // 读取畸变系数
  81. Mat rvec1, tvec1; // 定义旋转向量和平移向量
  82. solvePnP(objectPoints, corners1, cameraMatrix, distCoeffs, rvec1, tvec1); // 求解PnP问题
  83. Mat rvec2, tvec2; // 定义旋转向量和平移向量
  84. solvePnP(objectPoints, corners2, cameraMatrix, distCoeffs, rvec2, tvec2); // 求解PnP问题
  85. //! [compute-poses]
  86. Mat img1_copy_pose = img1.clone(), img2_copy_pose = img2.clone(); // 复制两张图片
  87. Mat img_draw_poses; // 定义一个用于绘制姿态的图像
  88. drawFrameAxes(img1_copy_pose, cameraMatrix, distCoeffs, rvec1, tvec1, 2*squareSize); // 在第一张图片上绘制坐标轴
  89. drawFrameAxes(img2_copy_pose, cameraMatrix, distCoeffs, rvec2, tvec2, 2*squareSize); // 在第二张图片上绘制坐标轴
  90. hconcat(img1_copy_pose, img2_copy_pose, img_draw_poses); // 将两张图片水平拼接
  91. imshow("Chessboard poses", img_draw_poses); // 显示棋盘姿态
  92. //! [compute-camera-displacement]
  93. Mat R1, R2; // 定义两个旋转矩阵
  94. Rodrigues(rvec1, R1); // 将旋转向量转换为旋转矩阵
  95. Rodrigues(rvec2, R2); // 将旋转向量转换为旋转矩阵
  96. Mat R_1to2, t_1to2; // 定义旋转矩阵和平移向量
  97. computeC2MC1(R1, tvec1, R2, tvec2, R_1to2, t_1to2); // 计算相机位移
  98. Mat rvec_1to2; // 定义旋转向量
  99. Rodrigues(R_1to2, rvec_1to2); // 将旋转矩阵转换为旋转向量
  100. //! [compute-camera-displacement]
  101. //! [compute-plane-normal-at-camera-pose-1]
  102.     //平面法线在相机1坐标系下的坐标表示
  103. Mat normal = (Mat_<double>(3,1) << 0, 0, 1); // 定义法线向量
  104. Mat normal1 = R1*normal; // 计算第一帧的法线向量
  105. //! [compute-plane-normal-at-camera-pose-1]
  106. //! [compute-plane-distance-to-the-camera-frame-1]
  107.     //世界坐标系原点在相机1坐标系下的坐标表示origin1
  108. Mat origin(3, 1, CV_64F, Scalar(0)); // 定义原点
  109. Mat origin1 = R1*origin + tvec1; // 计算第一帧的原点
  110. double d_inv1 = 1.0 / normal1.dot(origin1);
  111. // 计算平面到第一帧相机坐标系原点的距离的倒数
  112. //! [compute-plane-distance-to-the-camera-frame-1]
  113. //!根据相机位移(位姿变化)计算单应性 [compute-homography-from-camera-displacement]
  114. Mat homography_euclidean = computeHomography(R_1to2, t_1to2, d_inv1, normal1); // 计算欧几里得单应性矩阵
  115. Mat homography = cameraMatrix * homography_euclidean * cameraMatrix.inv(); // 计算单应性矩阵
  116. homography /= homography.at<double>(2,2); // 归一化单应性矩阵
  117. homography_euclidean /= homography_euclidean.at<double>(2,2); // 归一化欧几里得单应性矩阵
  118. //! [compute-homography-from-camera-displacement]
  119. //Same but using absolute camera poses instead of camera displacement, just for check
  120. Mat homography_euclidean2 = computeHomography(R1, tvec1, R2, tvec2, d_inv1, normal1); // 计算欧几里得单应性矩阵
  121. Mat homography2 = cameraMatrix * homography_euclidean2 * cameraMatrix.inv(); // 计算单应性矩阵
  122. homography_euclidean2 /= homography_euclidean2.at<double>(2,2); // 归一化欧几里得单应性矩阵
  123. homography2 /= homography2.at<double>(2,2); // 归一化单应性矩阵
  124. cout << "\nEuclidean Homography:\n" << homography_euclidean << endl; // 输出欧几里得单应性矩阵
  125. cout << "Euclidean Homography 2:\n" << homography_euclidean2 << endl << endl; // 输出欧几里得单应性矩阵
  126.     //! 估计单应性矩阵H corners2 = H * corners1   [estimate-homography]
  127. Mat H = findHomography(corners1, corners2); // 估计单应性矩阵
  128. cout << "\nfindHomography H:\n" << H << endl; // 输出估计的单应性矩阵
  129. //! [estimate-homography]
  130. cout << "homography from camera displacement:\n" << homography << endl; // 输出由相机位移计算得到的单应性矩阵
  131. cout << "homography from absolute camera poses:\n" << homography2 << endl << endl; // 输出由绝对相机姿态计算得到的单应性矩阵
  132. //! [warp-chessboard]
  133. Mat img1_warp; // 定义一个变形后的图像
  134. warpPerspective(img1, img1_warp, H, img1.size()); // 对第一张图片进行透视变换
  135. //! [warp-chessboard]
  136. Mat img1_warp_custom; // 定义一个自定义的变形后的图像
  137. warpPerspective(img1, img1_warp_custom, homography, img1.size()); // 对第一张图片进行透视变换
  138. imshow("Warped image using homography computed from camera displacement", img1_warp_custom); // 显示由相机位移计算得到的单应性矩阵变形后的图像
  139. Mat img_draw_compare; // 定义一个用于比较的图像
  140. hconcat(img1_warp, img1_warp_custom, img_draw_compare); // 将两张变形后的图像水平拼接
  141. imshow("Warped images comparison", img_draw_compare); // 显示比较后的图像
  142. Mat img1_warp_custom2; // 定义一个自定义的变形后的图像
  143. warpPerspective(img1, img1_warp_custom2, homography2, img1.size()); // 对第一张图片进行透视变换
  144. imshow("Warped image using homography computed from absolute camera poses", img1_warp_custom2); // 显示由绝对相机姿态计算得到的单应性矩阵变形后的图像
  145. waitKey(); // 等待用户按键
  146. }
  147. const char* params
  148. = "{ help h | | print usage }"
  149. "{ image1 | left02.jpg | path to the source chessboard image }"
  150. "{ image2 | left01.jpg | path to the desired chessboard image }"
  151. "{ intrinsics | left_intrinsics.yml | path to camera intrinsics }"
  152. "{ width bw | 9 | chessboard width }"
  153. "{ height bh | 6 | chessboard height }"
  154. "{ square_size | 0.025 | chessboard square size }";
  155. // 定义一个参数列表,包含帮助信息、图像路径、内参路径、棋盘宽度、棋盘高度和棋盘方格大小
  156. }
  157. int main(int argc, char *argv[]) // 主函数
  158. {
  159. CommandLineParser parser(argc, argv, params); // 定义一个命令行解析器
  160. if (parser.has("help")) // 如果用户请求帮助
  161. {
  162. parser.about("Code for homography tutorial.\n"
  163. "Example 3: homography from the camera displacement.\n"); // 输出关于信息
  164. parser.printMessage(); // 打印消息
  165. return 0; // 返回0
  166. }
  167. Size patternSize(parser.get<int>("width"), parser.get<int>("height")); // 获取棋盘的尺寸
  168. float squareSize = (float) parser.get<double>("square_size"); // 获取棋盘方格的大小
  169. homographyFromCameraDisplacement(parser.get<String>("image1"), // 调用函数,计算单应性矩阵
  170. parser.get<String>("image2"),
  171. patternSize, squareSize,
  172. parser.get<String>("intrinsics"));
  173. return 0; // 返回0
  174. }

上面的C++代码是使用OpenCV库来实现基于棋盘格的摄像头标定、计算相机姿态、以及基于相机位移计算单应性矩阵。主要步骤如下:

  1. 读取两张包含棋盘格的图片。

  2. 检测棋盘格的角点。

  3. 计算角点在世界坐标系中的位置。

  4. 读取摄像头参数(内参和畸变参数)。

  5. 使用solvePnP方法分别计算两张图片中棋盘格的旋转向量和平移向量

  6. 计算棋盘格从第一张图片到第二张图片的相机位移(旋转矩阵和平移向量)。

  7. 计算单应性矩阵,将第一张图片中的棋盘格投影到第二张图片的视角

  8. 显示两种方式计算得到的单应性矩阵

  9. 使用单应性矩阵对第一张图片进行变换,以便与第二张图片对齐

该代码对于相机标定和三维重建的学习很有帮助,特别是在理解和应用单应性矩阵计算以实现图像配准和视角变换方面。

7e7d300af7f51d3fc4b6b51cefbe1658.png

863a35bbd1600c2f8e58d29961af2f11.png

18c452fb981015c06d61bb75ff3f49b9.png

单应性矩阵的物理意义

d4a741efd9b614d1e0743b182b8d2b94.png

963e2091af3523c6e2310c9c29318df8.png

如何计算单应性矩阵

564e3bcb997fcaf1ffca1b8b1e8964ce.png

Mat homography = R_1to2 + d_inv * tvec_1to2*normal.t();

46d4d4943b222f6542cbb2e5ea535312.png

solvePnP(objectPoints, corners1, cameraMatrix, distCoeffs, rvec1, tvec1);

cbaebc40ae37f48fbed5abe0c10a3cbd.png

drawFrameAxes(img1_copy_pose, cameraMatrix, distCoeffs, rvec1, tvec1, 2*squareSize);

8c36beeb2041b3ec977cb50a17a9669d.png

CV_EXPORTS_W void Rodrigues( InputArray src, OutputArray dst, OutputArray jacobian = noArray() );

0a466644d216d96fb5cb5f3d5c1aa0e2.png

Mat H = findHomography(corners1, corners2);

6c48881ef5143bbfcedde7ceffe11710.png

homography 与 homography_euclidean的区别

8824b76c66e4dfda2d17c70df8e9f2fb.png

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

闽ICP备14008679号