当前位置:   article > 正文

【opencv】教程代码 —features2D(3)Homography—分解单应性矩阵

【opencv】教程代码 —features2D(3)Homography—分解单应性矩阵

decompose_homography.cpp 分解单应性矩阵

1f3cf8759d2a1979c1054e4d24bfe9c1.jpeg

left01.jpg  boardSize:9x6  squareSize:0.025

b5dda4984f7911928b3769da8026b2c1.jpeg

left02.jpg

92de7d4b88c7756e55cb3eb041aa944e.png

c31a1fe2f8c8c527266998e13bb16387.png

相机内参

1eaf211b2dced8899b341b6ae026820c.png

  1. #include <iostream> // 引入输入输出流库
  2. #include <opencv2/core.hpp> // 引入OpenCV的核心功能头文件
  3. #include <opencv2/highgui.hpp> // 引入OpenCV的GUI功能头文件
  4. #include <opencv2/calib3d.hpp> // 引入OpenCV的相机标定和三维重建功能头文件
  5. using namespace std; // 使用标准命名空间,例如std::vector可以省略std::
  6. using namespace cv; // 使用opencv命名空间,例如cv::Mat可以省略cv::
  7. namespace // 匿名命名空间
  8. {
  9. // 定义了几种标定板的模式的枚举类型
  10. enum Pattern { CHESSBOARD, CIRCLES_GRID, ASYMMETRIC_CIRCLES_GRID };
  11. // 函数用于计算棋盘格的角点
  12. void calcChessboardCorners(Size boardSize, float squareSize, vector<Point3f>& corners, Pattern patternType = CHESSBOARD)
  13. {
  14. corners.resize(0); // 清空角点向量
  15. // 根据不同的标定板类型计算角点
  16. switch (patternType) {
  17. case CHESSBOARD:
  18. case CIRCLES_GRID:
  19. // 对于棋盘格和圆点网格,遍历每行每列生成角点
  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. // 对于非对称圆点网格,遍历每行每列生成角点,同时考虑横向位置的偏移
  27. for( int i = 0; i < boardSize.height; i++ )
  28. for( int j = 0; j < boardSize.width; j++ )
  29. corners.push_back(Point3f(float((2*j + i % 2)*squareSize),
  30. float(i*squareSize), 0));
  31. break;
  32. // 如果模式类型未知则报错
  33. default:
  34. CV_Error(Error::StsBadArg, "Unknown pattern type\n");
  35. }
  36. }
  37. // 计算单应性矩阵的函数,基于旋转矩阵、平移向量、平面到相机的逆距离和平面的法向量
  38. Mat computeHomography(const Mat &R_1to2, const Mat &tvec_1to2, const double d_inv, const Mat &normal)
  39. {
  40. Mat homography = R_1to2 + d_inv * tvec_1to2*normal.t(); // 计算单应性矩阵
  41. return homography; // 返回计算出的单应性矩阵
  42. }
  43. // 根据两次相机位姿计算两个相机之间的位姿关系
  44. void computeC2MC1(const Mat &R1, const Mat &tvec1, const Mat &R2, const Mat &tvec2,
  45. Mat &R_1to2, Mat &tvec_1to2)
  46. {
  47. //c2Mc1 = c2Mo * oMc1 = c2Mo * c1Mo.inv()
  48. R_1to2 = R2 * R1.t(); // 计算旋转矩阵
  49. tvec_1to2 = R2 * (-R1.t()*tvec1) + tvec2; // 计算平移向量
  50. }
  51. // 主函数,使用两个图像、棋盘格大小、方块大小和内参路径进行标定板的单应性分解
  52. void decomposeHomography(const string &img1Path, const string &img2Path, const Size &patternSize,
  53. const float squareSize, const string &intrinsicsPath)
  54. {
  55. // 根据路径读取两张图像
  56. Mat img1 = imread( samples::findFile( img1Path) );
  57. Mat img2 = imread( samples::findFile( img2Path) );
  58. // 查找两幅图像中的角点
  59. vector<Point2f> corners1, corners2;
  60. bool found1 = findChessboardCorners(img1, patternSize, corners1);
  61. bool found2 = findChessboardCorners(img2, patternSize, corners2);
  62. // 如果任一图像无法找到角点则返回错误
  63. if (!found1 || !found2)
  64. {
  65. cout << "Error, cannot find the chessboard corners in both images." << endl;
  66. return;
  67. }
  68. //! [compute-poses]
  69. vector<Point3f> objectPoints; // 存储物体在世界坐标系中的点
  70. // 计算棋盘格角点的世界坐标
  71.     calcChessboardCorners(patternSize, squareSize, objectPoints); 
  72. // 读取相机的内参
  73. FileStorage fs( samples::findFile( intrinsicsPath ), FileStorage::READ);
  74. Mat cameraMatrix, distCoeffs;
  75. fs["camera_matrix"] >> cameraMatrix; // 相机矩阵
  76. fs["distortion_coefficients"] >> distCoeffs; // 畸变系数
  77. // 对两个图像使用solvePnP求解相机位姿
  78. // 使用solvePnP来估计两幅图片的相机外参即旋转向量和平移向量
  79. Mat rvec1, tvec1; // 旋转向量和平移向量
  80. solvePnP(objectPoints, corners1, cameraMatrix, distCoeffs, rvec1, tvec1);
  81. Mat rvec2, tvec2;
  82. solvePnP(objectPoints, corners2, cameraMatrix, distCoeffs, rvec2, tvec2);
  83. //! [compute-poses]
  84. //! [compute-camera-displacement]
  85. Mat R1, R2;
  86. Rodrigues(rvec1, R1); // 旋转向量转换成旋转矩阵
  87. Rodrigues(rvec2, R2);
  88. // 计算两个相机位姿之间的相对旋转和平移
  89. Mat R_1to2, t_1to2;
  90. computeC2MC1(R1, tvec1, R2, tvec2, R_1to2, t_1to2);
  91. Mat rvec_1to2;
  92. Rodrigues(R_1to2, rvec_1to2); // 计算得到的旋转矩阵再转换成旋转向量
  93. //! [compute-camera-displacement]
  94. //! [compute-plane-normal-at-camera-pose-1]
  95. Mat normal = (Mat_<double>(3,1) << 0, 0, 1); // 定义一个垂直于棋盘格表面的法向量
  96. Mat normal1 = R1*normal; //相机1坐标轴z在世界坐标系的表示// 使用相机1的旋转矩阵变换法向量到相机坐标系下
  97. //! [compute-plane-normal-at-camera-pose-1]
  98. //! [compute-plane-distance-to-the-camera-frame-1]
  99. // 计算棋盘格平面到相机坐标系原点的距离
  100. Mat origin(3, 1, CV_64F, Scalar(0)); // 定义原点
  101.     Mat origin1 = R1*origin + tvec1; // 相机坐标系1原点在世界坐标系下的表示
  102.     //相机1坐标系原点到标定板平面的距离的逆
  103.     double d_inv1 = 1.0 / normal1.dot(origin1); // 计算平面到相机1原点的逆距离
  104. //! [compute-plane-distance-to-the-camera-frame-1]
  105. //! [compute-homography-from-camera-displacement]
  106. // 根据相机位姿差异计算单应性矩阵
  107. Mat homography_euclidean = computeHomography(R_1to2, t_1to2, d_inv1, normal1);
  108. Mat homography =cameraMatrix * homography_euclidean * cameraMatrix.inv();
  109. // 根据欧几里得单应性矩阵计算实际的单应性矩阵
  110. // 内参解极约束
  111. homography /= homography.at<double>(2,2);
  112. homography_euclidean /= homography_euclidean.at<double>(2,2);
  113. //! [compute-homography-from-camera-displacement]
  114. //! [decompose-homography-from-camera-displacement]
  115. // 分解从相机位移计算得到的单应性矩阵
  116. vector<Mat> Rs_decomp, ts_decomp, normals_decomp;
  117. // 使用OpenCV函数decomposeHomographyMat进行分解
  118. int solutions = decomposeHomographyMat(homography, cameraMatrix, Rs_decomp, ts_decomp, normals_decomp);
  119. // 显示分解得到的解决方案数和每个解决方案
  120. cout << "Decompose homography matrix computed from the camera displacement:" << endl << endl;
  121. for (int i = 0; i < solutions; i++)
  122. {
  123. double factor_d1 = 1.0 / d_inv1;
  124. Mat rvec_decomp;
  125. Rodrigues(Rs_decomp[i], rvec_decomp); // 将旋转矩阵转换成旋转向量
  126. cout << "Solution " << i << ":" << endl;
  127. cout << "rvec from homography decomposition: " << rvec_decomp.t() << endl;
  128. cout << "rvec from camera displacement: " << rvec_1to2.t() << endl;
  129. cout << "tvec from homography decomposition: " << ts_decomp[i].t() << " and scaled by d: " << factor_d1 * ts_decomp[i].t() << endl;
  130. cout << "tvec from camera displacement: " << t_1to2.t() << endl;
  131. cout << "plane normal from homography decomposition: " << normals_decomp[i].t() << endl;
  132. cout << "plane normal at camera 1 pose: " << normal1.t() << endl << endl;
  133. }
  134. //! [decompose-homography-from-camera-displacement]
  135. //! [estimate homography]
  136. // 使用findHomography()估计两幅图像间点的单应性矩阵
  137. Mat H = findHomography(corners1, corners2);
  138. //! [estimate homography]
  139. //! [decompose-homography-estimated-by-findHomography]
  140. // 分解由findHomography()函数估计得到的单应性矩阵
  141. solutions = decomposeHomographyMat(H, cameraMatrix, Rs_decomp, ts_decomp, normals_decomp);
  142. cout << "Decompose homography matrix estimated by findHomography():" << endl << endl;
  143. for (int i = 0; i < solutions; i++)
  144. {
  145. double factor_d1 = 1.0 / d_inv1;
  146. Mat rvec_decomp;
  147. Rodrigues(Rs_decomp[i], rvec_decomp); // 将旋转矩阵转换成旋转向量
  148. cout << "Solution " << i << ":" << endl;
  149. cout << "rvec from homography decomposition: " << rvec_decomp.t() << endl;
  150. cout << "rvec from camera displacement: " << rvec_1to2.t() << endl;
  151. cout << "tvec from homography decomposition: " << ts_decomp[i].t() << " and scaled by d: " << factor_d1 * ts_decomp[i].t() << endl;
  152. cout << "tvec from camera displacement: " << t_1to2.t() << endl;
  153. cout << "plane normal from homography decomposition: " << normals_decomp[i].t() << endl;
  154. cout << "plane normal at camera 1 pose: " << normal1.t() << endl << endl;
  155. }
  156. //! [decompose-homography-estimated-by-findHomography]
  157. }
  158. // 命令行参数解析中用到的参数模板字符串
  159. const char* params
  160. = "{ help h | | print usage }"
  161. "{ image1 | left02.jpg | path to the source chessboard image }"
  162. "{ image2 | left01.jpg | path to the desired chessboard image }"
  163. "{ intrinsics | left_intrinsics.yml | path to camera intrinsics }"
  164. "{ width bw | 9 | chessboard width }"
  165. "{ height bh | 6 | chessboard height }"
  166. "{ square_size | 0.025 | chessboard square size }";
  167. }
  168. // main函数 - 程序入口点
  169. int main(int argc, char *argv[])
  170. {
  171. // 解析命令行输入的参数
  172. CommandLineParser parser(argc, argv, params);
  173. // 如果参数中包含help,则打印程序的帮助信息
  174. if ( parser.has("help") )
  175. {
  176. parser.about("Code for homography tutorial.\n"
  177. "Example 4: decompose the homography matrix.\n");
  178. parser.printMessage();
  179. return 0;
  180. }
  181. // 获取棋盘格尺寸参数、方块大小并调用decomposeHomography函数
  182. Size patternSize(parser.get<int>("width"), parser.get<int>("height"));
  183. float squareSize = (float) parser.get<double>("square_size");
  184. decomposeHomography(parser.get<String>("image1"),
  185. parser.get<String>("image2"),
  186. patternSize, squareSize,
  187. parser.get<String>("intrinsics"));
  188. return 0; // 程序正常结束
  189. }

代码的主要功能是通过两张棋盘格图像来计算它们之间的单应性矩阵,并对该矩阵进行分解以获得相对旋转向量、平移向量和正常向量。这个过程通常用于计算机视觉中相机的位姿估计和校准。代码使用了OpenCV的相关函数完成图像处理和矩阵运算。

decomposeHomography用于执行相机位移下单应性矩阵的分解与计算。整个代码流程包括:

1. 读取两幅图像并在棋盘格中找到角点。2. 从外部文件读取相机内参。3. 使用solvePnP函数估计两幅图像相对于世界坐标系的姿态。4. 计算相机1到相机2的旋转和平移。5. 计算参考平面的法线向量和到相机1的距离。6. 根据相机间的位移计算出单应性矩阵。7. 使用decomposeHomographyMat函数分解由相机位移计算出的单应性矩阵。8. 通过findHomography函数估计两个视图间的单应性矩阵,并分解之。 代码的最终目的是从两幅图像计算出相机的位移,并从位移中分解出单应性矩阵,这通常用于场景的三维重构和姿态估计。

终端输出:

  1. Decompose homography matrix computed from the camera displacement:
  2. Solution 0:
  3. rvec from homography decomposition: [-0.09198300601684746, -0.5372581107203847, 1.310868858823169]
  4. rvec from camera displacement: [-0.09198300601684731, -0.5372581107203847, 1.310868858823169]
  5. tvec from homography decomposition: [-0.7747960935447347, -0.02751122265362334, -0.6791979966698382] and scaled by d: [-0.1578091502009057, -0.005603439026252055, -0.1383378924669762]
  6. tvec from camera displacement: [0.1578091502009057, 0.005603439026252086, 0.1383378924669763]
  7. plane normal from homography decomposition: [-0.1973513031094161, 0.6283452092297845, -0.7524857215914424]
  8. plane normal at camera 1 pose: [0.1973513031094159, -0.6283452092297841, 0.7524857215914428]
  9. Solution 1:
  10. rvec from homography decomposition: [-0.09198300601684746, -0.5372581107203847, 1.310868858823169]
  11. rvec from camera displacement: [-0.09198300601684731, -0.5372581107203847, 1.310868858823169]
  12. tvec from homography decomposition: [0.7747960935447347, 0.02751122265362334, 0.6791979966698382] and scaled by d: [0.1578091502009057, 0.005603439026252055, 0.1383378924669762]
  13. tvec from camera displacement: [0.1578091502009057, 0.005603439026252086, 0.1383378924669763]
  14. plane normal from homography decomposition: [0.1973513031094161, -0.6283452092297845, 0.7524857215914424]
  15. plane normal at camera 1 pose: [0.1973513031094159, -0.6283452092297841, 0.7524857215914428]
  16. Solution 2:
  17. rvec from homography decomposition: [0.1053487844994422, -0.1561929302268701, 1.40135654775989]
  18. rvec from camera displacement: [-0.09198300601684731, -0.5372581107203847, 1.310868858823169]
  19. tvec from homography decomposition: [-0.4666552458868745, 0.105003306671971, -0.9130076449288979] and scaled by d: [-0.09504754657871854, 0.02138689486465782, -0.1859598438525694]
  20. tvec from camera displacement: [0.1578091502009057, 0.005603439026252086, 0.1383378924669763]
  21. plane normal from homography decomposition: [-0.3131715302799941, 0.8421206250806385, -0.4390403687998199]
  22. plane normal at camera 1 pose: [0.1973513031094159, -0.6283452092297841, 0.7524857215914428]
  23. Solution 3:
  24. rvec from homography decomposition: [0.1053487844994422, -0.1561929302268701, 1.40135654775989]
  25. rvec from camera displacement: [-0.09198300601684731, -0.5372581107203847, 1.310868858823169]
  26. tvec from homography decomposition: [0.4666552458868745, -0.105003306671971, 0.9130076449288979] and scaled by d: [0.09504754657871854, -0.02138689486465782, 0.1859598438525694]
  27. tvec from camera displacement: [0.1578091502009057, 0.005603439026252086, 0.1383378924669763]
  28. plane normal from homography decomposition: [0.3131715302799941, -0.8421206250806385, 0.4390403687998199]
  29. plane normal at camera 1 pose: [0.1973513031094159, -0.6283452092297841, 0.7524857215914428]
  30. Decompose homography matrix estimated by findHomography():
  31. Solution 0:
  32. rvec from homography decomposition: [0.1552207660862356, -0.1521327237748819, 1.323678685010914]
  33. rvec from camera displacement: [-0.09198300601684731, -0.5372581107203847, 1.310868858823169]
  34. tvec from homography decomposition: [-0.4482361641899351, 0.02485247517069353, -1.034409633494862] and scaled by d: [-0.09129597935439666, 0.005061909862158862, -0.2106867943469173]
  35. tvec from camera displacement: [0.1578091502009057, 0.005603439026252086, 0.1383378924669763]
  36. plane normal from homography decomposition: [-0.1384902519298367, 0.9063331412907092, -0.3992251083267753]
  37. plane normal at camera 1 pose: [0.1973513031094159, -0.6283452092297841, 0.7524857215914428]
  38. Solution 1:
  39. rvec from homography decomposition: [0.1552207660862356, -0.1521327237748819, 1.323678685010914]
  40. rvec from camera displacement: [-0.09198300601684731, -0.5372581107203847, 1.310868858823169]
  41. tvec from homography decomposition: [0.4482361641899351, -0.02485247517069353, 1.034409633494862] and scaled by d: [0.09129597935439666, -0.005061909862158862, 0.2106867943469173]
  42. tvec from camera displacement: [0.1578091502009057, 0.005603439026252086, 0.1383378924669763]
  43. plane normal from homography decomposition: [0.1384902519298367, -0.9063331412907092, 0.3992251083267753]
  44. plane normal at camera 1 pose: [0.1973513031094159, -0.6283452092297841, 0.7524857215914428]
  45. Solution 2:
  46. rvec from homography decomposition: [-0.2886605520980661, -0.5210498746612311, 1.381242036724107]
  47. rvec from camera displacement: [-0.09198300601684731, -0.5372581107203847, 1.310868858823169]
  48. tvec from homography decomposition: [-0.8705960844288273, 0.1353018264406045, -0.7037701811073137] and scaled by d: [-0.1773215293631533, 0.02755804582536839, -0.1433427131894416]
  49. tvec from camera displacement: [0.1578091502009057, 0.005603439026252086, 0.1383378924669763]
  50. plane normal from homography decomposition: [-0.2284582164830118, 0.6009247378618285, -0.7659610321335505]
  51. plane normal at camera 1 pose: [0.1973513031094159, -0.6283452092297841, 0.7524857215914428]
  52. Solution 3:
  53. rvec from homography decomposition: [-0.2886605520980661, -0.5210498746612311, 1.381242036724107]
  54. rvec from camera displacement: [-0.09198300601684731, -0.5372581107203847, 1.310868858823169]
  55. tvec from homography decomposition: [0.8705960844288273, -0.1353018264406045, 0.7037701811073137] and scaled by d: [0.1773215293631533, -0.02755804582536839, 0.1433427131894416]
  56. tvec from camera displacement: [0.1578091502009057, 0.005603439026252086, 0.1383378924669763]
  57. plane normal from homography decomposition: [0.2284582164830118, -0.6009247378618285, 0.7659610321335505]
  58. plane normal at camera 1 pose: [0.1973513031094159, -0.6283452092297841, 0.7524857215914428]

4e77715ade5f051f6e694f593e818fdd.png

a890d008417b11f5f935509f9f769e7f.png

如何计算相机间的转换矩阵?

88655865a194130991d82e04a535761e.png

如何使用solvePnP函数进行相机标定?

e638f60a6df69ab67916967293d4e732.png

Mat homography_euclidean = computeHomography(R_1to2, t_1to2, d_inv1, normal1);

880f95100bbc44eed34c436e47dd0c66.png

df6d8705862e2c4df59d00bf049f6f27.png

int solutions = decomposeHomographyMat(homography, cameraMatrix, Rs_decomp, ts_decomp, normals_decomp);

e6bc0e899b4837f031f0d251101cd2de.png

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

闽ICP备14008679号