main.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //
  2. // Created by zx on 2023/9/21.
  3. //
  4. #include <dirent.h>
  5. #include <vector>
  6. #include <iostream>
  7. #include <opencv2/opencv.hpp>
  8. #include <pcl/point_types.h>
  9. #include <pcl/point_cloud.h>
  10. #include <pcl/filters/crop_hull.h>
  11. #include <pcl/surface/convex_hull.h>
  12. #include <pcl/filters/passthrough.h>
  13. #include <pcl/filters/statistical_outlier_removal.h>
  14. #include "defines.hpp"
  15. #include "error_code/error_code.hpp"
  16. #include "tool/proto_tool.h"
  17. #include "velodyne_config.pb.h"
  18. #include "tool/static_tool.hpp"
  19. #include "tool/point3D_tool.h"
  20. const cv::Size MatSmallSize(640, 480);
  21. /**
  22. * @brief GetFiles: 获取文件夹内的所有文件名字
  23. * @param sdir
  24. * @param bsubdir: true 包含子目录下的文件
  25. * @return
  26. */
  27. std::vector<std::string> GetFiles(const std::string& sdir = ".",
  28. bool bsubdir = true) {
  29. LOG(INFO) << sdir;
  30. DIR* dp;
  31. struct dirent* dirp;
  32. std::vector<std::string> filenames;
  33. if ((dp = opendir(sdir.c_str())) != NULL) {
  34. while ((dirp = readdir(dp)) != NULL) {
  35. if (strcmp(".", dirp->d_name) == 0 || strcmp("..", dirp->d_name) == 0)
  36. continue;
  37. if (dirp->d_type != DT_DIR)
  38. filenames.push_back(sdir + "/" + dirp->d_name);
  39. if (bsubdir && dirp->d_type == DT_DIR) {
  40. std::vector<std::string> names = GetFiles(sdir + "/" + dirp->d_name);
  41. filenames.insert(filenames.begin(), names.begin(), names.end());
  42. }
  43. }
  44. }
  45. closedir(dp);
  46. return filenames;
  47. }
  48. int Points2MatSegmentWheels(pcl::PointCloud<pcl::PointXYZ>::Ptr &clouds, cv::Mat &mat) {
  49. mat.setTo(0);
  50. cv::imwrite(ETC_PATH"/etc/data/mat.jpg", mat);
  51. }
  52. int Points2Mat(pcl::PointCloud<pcl::PointXYZ>::Ptr &clouds, cv::Mat &mat) {
  53. //离群点过滤
  54. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered(new pcl::PointCloud<pcl::PointXYZ>);
  55. pcl::StatisticalOutlierRemoval<pcl::PointXYZ> sor;
  56. sor.setInputCloud(clouds);
  57. sor.setMeanK(50); //K近邻搜索点个数
  58. sor.setStddevMulThresh(0.7); //标准差倍数
  59. sor.setNegative(false); //保留未滤波点(内点)
  60. sor.filter(*cloud_filtered); //保存滤波结果到cloud_filter
  61. // 获取点云外接矩形
  62. cv::RotatedRect car_body_rect;
  63. Point3D_tool::getMinRect(car_body_rect, cloud_filtered);
  64. // 初始化裁剪参数
  65. pcl::PointCloud<pcl::PointXYZ>::Ptr left_clouds(new pcl::PointCloud<pcl::PointXYZ>);
  66. pcl::PointCloud<pcl::PointXYZ>::Ptr right_clouds(new pcl::PointCloud<pcl::PointXYZ>);
  67. cv::Point2f car_body_rect_vertex[4];
  68. car_body_rect.points(car_body_rect_vertex);
  69. float minx = 1000000, maxx = 0;
  70. for (auto &point:car_body_rect_vertex) {
  71. minx = MIN(minx, point.x);
  72. maxx = MAX(maxx, point.x);
  73. }
  74. // 裁出左侧点云
  75. pcl::PassThrough<pcl::PointXYZ> cut_pass;
  76. cut_pass.setFilterFieldName("x");
  77. cut_pass.setFilterLimits(minx, car_body_rect.center.x);
  78. cut_pass.setFilterLimitsNegative(false);
  79. cut_pass.setInputCloud(clouds);
  80. cut_pass.filter(*left_clouds);
  81. for (auto &point: left_clouds->points) {
  82. int mat_x = -(point.y - 3.2) * 100;
  83. int mat_y = (point.z + 2.4) * 100;
  84. int mat_depth = (car_body_rect.center.x - point.x) * 100;
  85. mat_x = MAX(mat_x, 0);
  86. mat_x = MIN(mat_x, 640);
  87. mat_y = MAX(mat_y, 241);
  88. mat_y = MIN(mat_y, 480);
  89. mat_depth = MAX(mat_depth, 0);
  90. mat_depth = MIN(mat_depth, 255);
  91. mat.at<uchar>(480 - mat_y, mat_x) = MAX(mat.at<uchar>(mat_y, mat_x), mat_depth);
  92. }
  93. // 裁出右侧点云
  94. cut_pass.setFilterFieldName("x");
  95. cut_pass.setFilterLimits(car_body_rect.center.x, maxx);
  96. cut_pass.setFilterLimitsNegative(false);
  97. cut_pass.setInputCloud(clouds);
  98. cut_pass.filter(*right_clouds);
  99. for (auto &point: right_clouds->points) {
  100. int mat_x = -(point.y - 3.2) * 100;
  101. int mat_y = point.z * 100;
  102. int mat_depth = (point.x - car_body_rect.center.x) * 100;
  103. mat_x = MAX(mat_x, 0);
  104. mat_x = MIN(mat_x, 640);
  105. mat_y = MAX(mat_y, 0);
  106. mat_y = MIN(mat_y, 240);
  107. mat_depth = MAX(mat_depth, 0);
  108. mat_depth = MIN(mat_depth, 255);
  109. mat.at<uchar>(480 - mat_y, mat_x) = MAX(mat.at<uchar>(mat_y, mat_x), mat_depth);
  110. }
  111. return EXIT_SUCCESS;
  112. }
  113. int main(int argc, char* argv[]) {
  114. if (argc == 1) {
  115. return EXIT_FAILURE;
  116. }
  117. ZX::InitGlog("Cloud2Mat", ETC_PATH"/etc/MCloud2MatLog/");
  118. std::string prototxt_path = ETC_PATH"/etc/velodyne_manager.prototxt";
  119. velodyne::velodyneManagerParams t_velo_params;
  120. if (!proto_tool::read_proto_param(prototxt_path, t_velo_params)) {
  121. LOG(ERROR) << "read_proto_param failed.";
  122. return EXIT_FAILURE;
  123. }
  124. for (auto &m_region: t_velo_params.region()) {
  125. // 裁剪块
  126. pcl::PointCloud<pcl::PointXYZ>::Ptr boundingbox_ptr(new pcl::PointCloud<pcl::PointXYZ>);
  127. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);
  128. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_cut = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);
  129. //创建多边形区域指针
  130. for (auto &point: m_region.cut_box().cut_points()) {
  131. LOG(INFO) << point.DebugString();
  132. boundingbox_ptr->push_back(pcl::PointXYZ(point.x(), point.y(), point.z()));
  133. }
  134. std::vector<pcl::Vertices> m_polygons; //设置动态数组用于保存凸包顶点
  135. pcl::PointCloud<pcl::PointXYZ>::Ptr m_surface_hull = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);
  136. pcl::ConvexHull<pcl::PointXYZ> hull; //建立一个凸包对象
  137. hull.setInputCloud(boundingbox_ptr); //设置凸包的维度
  138. hull.reconstruct(*m_surface_hull, m_polygons); //计算凸包结果
  139. // 找到该区域目录下所有点云文件
  140. auto file_list = GetFiles(ETC_PATH"/etc/data/" + std::to_string(m_region.region_id()));
  141. for (auto &file: file_list) {
  142. LOG(INFO) << file;
  143. auto mat_file = file.substr(0, file.length() - 4) + ".jpg";
  144. if (file.substr(file.find_last_of('.') + 1) != "txt") {
  145. continue;
  146. } else {
  147. remove(mat_file.c_str());
  148. }
  149. cv::Mat mat = cv::Mat(MatSmallSize, CV_8U, cv::Scalar(0));// 创建单通道黑色图像。
  150. Points2MatSegmentWheels(cloud, mat);
  151. exit(EXIT_SUCCESS);
  152. cloud->clear();
  153. cloud_cut->clear();
  154. ReadTxtCloud(file, cloud);
  155. pcl::CropHull<pcl::PointXYZ> bb_filter; //创建CropHull对象
  156. bb_filter.setInputCloud(cloud); //设置输入点云
  157. bb_filter.setDim(2); //设置维度
  158. bb_filter.setHullIndices(m_polygons); //输入封闭多边形的顶点
  159. bb_filter.setHullCloud(m_surface_hull); //输入封闭多边形的形状
  160. bb_filter.filter(*cloud_cut);
  161. pcl::PassThrough<pcl::PointXYZ> cut_pass;
  162. cut_pass.setInputCloud(cloud_cut);
  163. cut_pass.setFilterFieldName("z");
  164. cut_pass.setFilterLimits(m_region.minz(), m_region.minz() + 2);
  165. cut_pass.setFilterLimitsNegative(false);
  166. cut_pass.filter(*cloud_cut);
  167. // WriteTxtCloud(lll, cloud_cut);
  168. Points2Mat(cloud_cut, mat);
  169. cv::imwrite(mat_file, mat);
  170. }
  171. }
  172. return EXIT_SUCCESS;
  173. }