当前位置:   article > 正文

OpenCV——点云投影到图像中_点云投影至图像

点云投影至图像

读取一张照片和一张 pcd, 根据标定的内参和外参,将点云投影到图像中,用于判断雷达相机外参标定是否准确。

  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 <pcl/point_types.h>
  6. #include <pcl/point_cloud.h>
  7. #include <pcl/io/pcd_io.h>
  8. #include <pcl/filters/filter.h>
  9. #include <iostream>
  10. int main(int argc, char** argv)
  11. {
  12. // read a image and a pcd
  13. cv::Mat image_origin = cv::imread("/media/data/temp/image/0.jpeg");
  14. pcl::PointCloud<pcl::PointXYZI>::Ptr cloud_origin(new pcl::PointCloud<pcl::PointXYZI>);
  15. pcl::PointCloud<pcl::PointXYZI>::Ptr cloud_withoutNAN(new pcl::PointCloud<pcl::PointXYZI>);
  16. pcl::io::loadPCDFile<pcl::PointXYZI> ("/media/liuzhiyang/data/temp/pcd/0.pcd", *cloud_origin);
  17. std::vector<int> indices;
  18. pcl::removeNaNFromPointCloud(*cloud_origin, *cloud_withoutNAN, indices);
  19. std::vector<cv::Point3f> pts_3d;
  20. for (size_t i = 0; i < cloud_withoutNAN->size(); ++i)
  21. {
  22. pcl::PointXYZI point_3d = cloud_withoutNAN->points[i];
  23. if (point_3d.x > 2 && point_3d.x < 3 && point_3d.y > -10 && point_3d.y < 10)
  24. {
  25. pts_3d.emplace_back(cv::Point3f(point_3d.x, point_3d.y, point_3d.z));
  26. }
  27. }
  28. // using iterator
  29. // read calibration parameter
  30. double fx = 1.0757955405501191e+03, fy = 1.0762345733674481e+03;
  31. double cx = 9.6249394948422218e+02, cy = 6.1957628038839391e+02;
  32. double k1 = -1.1995613777994101e-01, k2 = 8.6245969435724004e-02, k3 = -2.6778267188218002e-02;
  33. double p1 = 1.0621717082800000e-03, p2 = 5.4033385896265832e-04;
  34. cv::Mat camera_matrix = (cv::Mat_<double>(3, 3) << fx, 0.0, cx, 0.0, fy, cy, 0.0, 0.0, 1.0);
  35. cv::Mat distortion_coeff = (cv::Mat_<double>(1, 5) << k1, k2, p1, p2, k3);
  36. cv::Mat r_vec = (cv::Mat_<double>(3, 1) << 1.29949179254383, -1.113823535227475, 1.108412921650477);
  37. cv::Mat t_vec = (cv::Mat_<double>(3, 1) << -0.370740907093656, -0.2397403632299851, -0.0407927826288379);
  38. // project 3d-points into image view
  39. std::vector<cv::Point2f> pts_2d;
  40. cv::projectPoints(pts_3d, r_vec, t_vec, camera_matrix, distortion_coeff, pts_2d);
  41. cv::Mat image_project = image_origin.clone();
  42. int image_rows = image_origin.rows;
  43. int image_cols = image_origin.cols;
  44. for (size_t i = 0; i < pts_2d.size(); ++i)
  45. {
  46. cv::Point2f point_2d = pts_2d[i];
  47. if (point_2d.x < 0 || point_2d.x > image_cols || point_2d.y < 0 || point_2d.y > image_rows)
  48. {
  49. continue;
  50. }
  51. else
  52. {
  53. image_project.at<cv::Vec3b>(point_2d.y, point_2d.x)[0] = 0;
  54. image_project.at<cv::Vec3b>(point_2d.y, point_2d.x)[1] = 0;
  55. image_project.at<cv::Vec3b>(point_2d.y, point_2d.x)[2] = 255;
  56. }
  57. if (point_2d.x > 0 && point_2d.x < image_cols && point_2d.y > 0 && point_2d.y < image_rows)
  58. {
  59. image_project.at<cv::Vec3b>(point_2d.y, point_2d.x)[0] = 0;
  60. image_project.at<cv::Vec3b>(point_2d.y, point_2d.x)[1] = 0;
  61. image_project.at<cv::Vec3b>(point_2d.y, point_2d.x)[2] = 255;
  62. }
  63. else
  64. {
  65. continue;
  66. }
  67. }
  68. cv::imshow("origin image", image_origin);
  69. cv::imshow("project image", image_project);
  70. cv::imwrite("/media/data/temp/image_origin.jpg", image_origin);
  71. cv::imwrite("/media/data/temp/image_project.jpg", image_project);
  72. cv::waitKey(10000);
  73. return 0;
  74. }

后记:投影部分区域的点云到图像中,不要全部都投。(一般选取标定板所处位置的点云)

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

闽ICP备14008679号