main.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. bool resetColor(cv::Mat &mat) {
  22. if (mat.empty()) {
  23. return false;
  24. }
  25. for (int len_x = 0; len_x < mat.cols; len_x++) {
  26. for (int len_y = 0; len_y < mat.rows; len_y++) {
  27. if (mat.at<cv::Vec3b>(len_y, len_x)[0] == 0 &&
  28. mat.at<cv::Vec3b>(len_y, len_x)[1] == 255 &&
  29. mat.at<cv::Vec3b>(len_y, len_x)[2] == 0 ) {
  30. mat.at<cv::Vec3b>(len_y, len_x)[0] = 0;
  31. mat.at<cv::Vec3b>(len_y, len_x)[1] = 0;
  32. mat.at<cv::Vec3b>(len_y, len_x)[2] = 0;
  33. }
  34. }
  35. }
  36. return true;
  37. }
  38. /**
  39. * @brief GetFiles: 获取文件夹内的所有文件名字
  40. * @param sdir
  41. * @param bsubdir: true 包含子目录下的文件
  42. * @return
  43. */
  44. std::vector<std::string> GetFiles(const std::string& sdir = ".",
  45. bool bsubdir = true) {
  46. LOG(INFO) << sdir;
  47. DIR* dp;
  48. struct dirent* dirp;
  49. std::vector<std::string> filenames;
  50. if ((dp = opendir(sdir.c_str())) != NULL) {
  51. while ((dirp = readdir(dp)) != NULL) {
  52. if (strcmp(".", dirp->d_name) == 0 || strcmp("..", dirp->d_name) == 0)
  53. continue;
  54. if (dirp->d_type != DT_DIR)
  55. filenames.push_back(sdir + "/" + dirp->d_name);
  56. if (bsubdir && dirp->d_type == DT_DIR) {
  57. std::vector<std::string> names = GetFiles(sdir + "/" + dirp->d_name);
  58. filenames.insert(filenames.begin(), names.begin(), names.end());
  59. }
  60. }
  61. }
  62. closedir(dp);
  63. return filenames;
  64. }
  65. int Points2MatSegmentWheels(pcl::PointCloud<pcl::PointXYZ>::Ptr &clouds, cv::Mat &mat) {
  66. //离群点过滤
  67. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered(new pcl::PointCloud<pcl::PointXYZ>);
  68. pcl::StatisticalOutlierRemoval<pcl::PointXYZ> sor;
  69. sor.setInputCloud(clouds);
  70. sor.setMeanK(50); //K近邻搜索点个数
  71. sor.setStddevMulThresh(0.7); //标准差倍数
  72. sor.setNegative(false); //保留未滤波点(内点)
  73. sor.filter(*cloud_filtered); //保存滤波结果到cloud_filter
  74. cv::RotatedRect car_body_rect;
  75. Point3D_tool::getMinRect(car_body_rect, cloud_filtered);
  76. cv::Point2f car_body_rect_vertex[4];
  77. car_body_rect.points(car_body_rect_vertex);
  78. float minx = 1e8, maxx = -1e8;
  79. for (auto &point:car_body_rect_vertex) {
  80. minx = MIN(minx, point.x);
  81. maxx = MAX(maxx, point.x);
  82. }
  83. pcl::PointCloud<pcl::PointXYZ>::Ptr a(new pcl::PointCloud<pcl::PointXYZ>);
  84. pcl::PointCloud<pcl::PointXYZ>::Ptr b(new pcl::PointCloud<pcl::PointXYZ>);
  85. pcl::PointCloud<pcl::PointXYZ>::Ptr c(new pcl::PointCloud<pcl::PointXYZ>);
  86. pcl::PointCloud<pcl::PointXYZ>::Ptr d(new pcl::PointCloud<pcl::PointXYZ>);
  87. car_body_rect.center.y = car_body_rect.center.y + 3.2;
  88. for (auto &point: clouds->points) {
  89. point.y = point.y + 3.2;
  90. if (point.x < car_body_rect.center.x && point.y > car_body_rect.center.y) {
  91. a->emplace_back(point);
  92. } else if (point.x > car_body_rect.center.x && point.y > car_body_rect.center.y) {
  93. b->emplace_back(point);
  94. } else if (point.x < car_body_rect.center.x && point.y < car_body_rect.center.y) {
  95. c->emplace_back(point);
  96. } else {
  97. d->emplace_back(point);
  98. }
  99. }
  100. // 左前
  101. for (auto &point: a->points) {
  102. int mat_x = (point.z + 0.1) * 161;
  103. int mat_y = (point.y - 3.2) * 161;
  104. int mat_depth = fabs(car_body_rect.center.x - point.x) * 100 + 0;
  105. mat_x = MAX(mat_x, 0);
  106. mat_x = MIN(mat_x, 159);
  107. mat_y = MAX(mat_y, 0);
  108. mat_y = MIN(mat_y, 479);
  109. mat_depth = MAX(mat_depth, 0);
  110. mat_depth = MIN(mat_depth, 255);
  111. mat.at<cv::Vec3b>(479 - mat_y, mat_x)[0] = MAX(mat.at<cv::Vec3b>(479 - mat_y, mat_x)[0], mat_depth);
  112. mat.at<cv::Vec3b>(479 - mat_y, mat_x)[1] = MIN(mat.at<cv::Vec3b>(479 - mat_y, mat_x)[1], mat_depth);
  113. mat.at<cv::Vec3b>(479 - mat_y, mat_x)[2] = mat.at<cv::Vec3b>(479 - mat_y, mat_x)[0] - mat.at<cv::Vec3b>(479 - mat_y, mat_x)[1];
  114. }
  115. // WriteTxtCloud(ETC_PATH"Cloud2Mat/data/a.txt", a);
  116. // 右前
  117. for (auto &point: b->points) {
  118. int mat_x = (point.z + 0.1) * 161 + 160;
  119. int mat_y = (point.y - 3.2) * 161;
  120. int mat_depth = fabs(car_body_rect.center.x - point.x) * 100 + 0;
  121. mat_x = MAX(mat_x, 160);
  122. mat_x = MIN(mat_x, 319);
  123. mat_y = MAX(mat_y, 0);
  124. mat_y = MIN(mat_y, 479);
  125. mat_depth = MAX(mat_depth, 0);
  126. mat_depth = MIN(mat_depth, 255);
  127. mat.at<cv::Vec3b>(479 - mat_y, mat_x)[0] = MAX(mat.at<cv::Vec3b>(479 - mat_y, mat_x)[0], mat_depth);
  128. mat.at<cv::Vec3b>(479 - mat_y, mat_x)[1] = MIN(mat.at<cv::Vec3b>(479 - mat_y, mat_x)[1], mat_depth);
  129. mat.at<cv::Vec3b>(479 - mat_y, mat_x)[2] = mat.at<cv::Vec3b>(479 - mat_y, mat_x)[0] - mat.at<cv::Vec3b>(479 - mat_y, mat_x)[1];
  130. }
  131. // WriteTxtCloud(ETC_PATH"Cloud2Mat/data/b.txt", b);
  132. // 左后
  133. for (auto &point: c->points) {
  134. int mat_x = (point.z + 0.1) * 161 + 320;
  135. int mat_y = (point.y) * 161;
  136. int mat_depth = fabs(car_body_rect.center.x - point.x) * 100 + 0;
  137. mat_x = MAX(mat_x, 320);
  138. mat_x = MIN(mat_x, 479);
  139. mat_y = MAX(mat_y, 0);
  140. mat_y = MIN(mat_y, 479);
  141. mat_depth = MAX(mat_depth, 0);
  142. mat_depth = MIN(mat_depth, 255);
  143. mat.at<cv::Vec3b>(479 - mat_y, mat_x)[0] = MAX(mat.at<cv::Vec3b>(479 - mat_y, mat_x)[0], mat_depth);
  144. mat.at<cv::Vec3b>(479 - mat_y, mat_x)[1] = MIN(mat.at<cv::Vec3b>(479 - mat_y, mat_x)[1], mat_depth);
  145. mat.at<cv::Vec3b>(479 - mat_y, mat_x)[2] = mat.at<cv::Vec3b>(479 - mat_y, mat_x)[0] - mat.at<cv::Vec3b>(479 - mat_y, mat_x)[1];
  146. }
  147. // WriteTxtCloud(ETC_PATH"Cloud2Mat/data/c.txt", c);
  148. // 右后
  149. for (auto &point: d->points) {
  150. int mat_x = (point.z + 0.1) * 161 + 480;
  151. int mat_y = (point.y) * 161;
  152. int mat_depth = fabs(car_body_rect.center.x - point.x) * 100 + 0;
  153. mat_x = MAX(mat_x, 480);
  154. mat_x = MIN(mat_x, 639);
  155. mat_y = MAX(mat_y, 0);
  156. mat_y = MIN(mat_y, 479);
  157. mat_depth = MAX(mat_depth, 0);
  158. mat_depth = MIN(mat_depth, 255);
  159. mat.at<cv::Vec3b>(479 - mat_y, mat_x)[0] = MAX(mat.at<cv::Vec3b>(479 - mat_y, mat_x)[0], mat_depth);
  160. mat.at<cv::Vec3b>(479 - mat_y, mat_x)[1] = MIN(mat.at<cv::Vec3b>(479 - mat_y, mat_x)[1], mat_depth);
  161. mat.at<cv::Vec3b>(479 - mat_y, mat_x)[2] = mat.at<cv::Vec3b>(479 - mat_y, mat_x)[0] - mat.at<cv::Vec3b>(479 - mat_y, mat_x)[1];
  162. }
  163. // WriteTxtCloud(ETC_PATH"Cloud2Mat/data/d.txt", d);
  164. //
  165. // cv::imwrite(ETC_PATH"Cloud2Mat/data/mat.jpg", mat);
  166. resetColor(mat);
  167. return EXIT_SUCCESS;
  168. }
  169. int Points2Mat(pcl::PointCloud<pcl::PointXYZ>::Ptr &clouds, cv::Mat &mat) {
  170. //离群点过滤
  171. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered(new pcl::PointCloud<pcl::PointXYZ>);
  172. pcl::StatisticalOutlierRemoval<pcl::PointXYZ> sor;
  173. sor.setInputCloud(clouds);
  174. sor.setMeanK(50); //K近邻搜索点个数
  175. sor.setStddevMulThresh(0.7); //标准差倍数
  176. sor.setNegative(false); //保留未滤波点(内点)
  177. sor.filter(*cloud_filtered); //保存滤波结果到cloud_filter
  178. // 获取点云外接矩形
  179. cv::RotatedRect car_body_rect;
  180. Point3D_tool::getMinRect(car_body_rect, cloud_filtered);
  181. // 初始化裁剪参数
  182. pcl::PointCloud<pcl::PointXYZ>::Ptr left_clouds(new pcl::PointCloud<pcl::PointXYZ>);
  183. pcl::PointCloud<pcl::PointXYZ>::Ptr right_clouds(new pcl::PointCloud<pcl::PointXYZ>);
  184. cv::Point2f car_body_rect_vertex[4];
  185. car_body_rect.points(car_body_rect_vertex);
  186. float minx = 1e8, maxx = -1e8;
  187. for (auto &point:car_body_rect_vertex) {
  188. minx = MIN(minx, point.x);
  189. maxx = MAX(maxx, point.x);
  190. }
  191. // 裁出左侧点云
  192. pcl::PassThrough<pcl::PointXYZ> cut_pass;
  193. cut_pass.setFilterFieldName("x");
  194. cut_pass.setFilterLimits(minx, car_body_rect.center.x);
  195. cut_pass.setFilterLimitsNegative(false);
  196. cut_pass.setInputCloud(clouds);
  197. cut_pass.filter(*left_clouds);
  198. for (auto &point: left_clouds->points) {
  199. int mat_x = -(point.y - 3.2) * 100;
  200. int mat_y = (point.z + 2.4) * 100;
  201. int mat_depth = (car_body_rect.center.x - point.x) * 100 + 0;
  202. mat_x = MAX(mat_x, 0);
  203. mat_x = MIN(mat_x, 639);
  204. mat_y = MAX(mat_y, 240);
  205. mat_y = MIN(mat_y, 479);
  206. mat_depth = MAX(mat_depth, 0);
  207. mat_depth = MIN(mat_depth, 255);
  208. mat.at<cv::Vec3b>(479 - mat_y, mat_x)[0] = MAX(mat.at<cv::Vec3b>(479 - mat_y, mat_x)[0], mat_depth);
  209. mat.at<cv::Vec3b>(479 - mat_y, mat_x)[1] = MIN(mat.at<cv::Vec3b>(479 - mat_y, mat_x)[1], mat_depth);
  210. mat.at<cv::Vec3b>(479 - mat_y, mat_x)[2] = mat.at<cv::Vec3b>(479 - mat_y, mat_x)[0] - mat.at<cv::Vec3b>(479 - mat_y, mat_x)[1];
  211. }
  212. // 裁出右侧点云
  213. cut_pass.setFilterFieldName("x");
  214. cut_pass.setFilterLimits(car_body_rect.center.x, maxx);
  215. cut_pass.setFilterLimitsNegative(false);
  216. cut_pass.setInputCloud(clouds);
  217. cut_pass.filter(*right_clouds);
  218. for (auto &point: right_clouds->points) {
  219. int mat_x = -(point.y - 3.2) * 100;
  220. int mat_y = point.z * 100;
  221. int mat_depth = (point.x - car_body_rect.center.x) * 100 + 0;
  222. mat_x = MAX(mat_x, 0);
  223. mat_x = MIN(mat_x, 639);
  224. mat_y = MAX(mat_y, 0);
  225. mat_y = MIN(mat_y, 239);
  226. mat_depth = MAX(mat_depth, 0);
  227. mat_depth = MIN(mat_depth, 255);
  228. mat.at<cv::Vec3b>(479 - mat_y, mat_x)[0] = MAX(mat.at<cv::Vec3b>(479 - mat_y, mat_x)[0], mat_depth);
  229. mat.at<cv::Vec3b>(479 - mat_y, mat_x)[1] = MIN(mat.at<cv::Vec3b>(479 - mat_y, mat_x)[1], mat_depth);
  230. mat.at<cv::Vec3b>(479 - mat_y, mat_x)[2] = mat.at<cv::Vec3b>(479 - mat_y, mat_x)[0] - mat.at<cv::Vec3b>(479 - mat_y, mat_x)[1];
  231. }
  232. resetColor(mat);
  233. return EXIT_SUCCESS;
  234. }
  235. int main(int argc, char* argv[]) {
  236. ZX::InitGlog("Cloud2Mat", ETC_PATH"Cloud2Mat/Cloud2MatLog/");
  237. std::string prototxt_path = ETC_PATH"Cloud2Mat/velodyne_manager.prototxt";
  238. velodyne::velodyneManagerParams t_velo_params;
  239. if (!proto_tool::read_proto_param(prototxt_path, t_velo_params)) {
  240. LOG(ERROR) << "read_proto_param failed.";
  241. return EXIT_FAILURE;
  242. }
  243. for (auto &m_region: t_velo_params.region()) {
  244. // 裁剪块
  245. pcl::PointCloud<pcl::PointXYZ>::Ptr boundingbox_ptr(new pcl::PointCloud<pcl::PointXYZ>);
  246. //创建多边形区域指针
  247. for (auto &point: m_region.cut_box().cut_points()) {
  248. LOG(INFO) << point.DebugString();
  249. boundingbox_ptr->push_back(pcl::PointXYZ(point.x(), point.y(), point.z()));
  250. }
  251. std::vector<pcl::Vertices> m_polygons; //设置动态数组用于保存凸包顶点
  252. pcl::PointCloud<pcl::PointXYZ>::Ptr m_surface_hull = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);
  253. pcl::ConvexHull<pcl::PointXYZ> hull; //建立一个凸包对象
  254. hull.setInputCloud(boundingbox_ptr); //设置凸包的维度
  255. hull.reconstruct(*m_surface_hull, m_polygons); //计算凸包结果
  256. // 找到该区域目录下所有点云文件
  257. auto file_list = GetFiles(ETC_PATH"Cloud2Mat/data/" + std::to_string(m_region.region_id()));
  258. for (auto &file: file_list) {
  259. LOG(INFO) << file;
  260. // file = "/home/zx/Measure/Modules/Cloud2Mat/etc/data/3219/20230918-103314/failture.txt";
  261. if (file.substr(file.find_last_of('.') + 1) != "txt") {
  262. // remove(file.c_str());
  263. continue;
  264. }
  265. auto mat_file = file.substr(0, file.length() - 4) + ".jpg";
  266. auto four_mat_file = file.substr(0, file.length() - 4) + "_four.jpg";
  267. cv::Mat mat = cv::Mat(MatSmallSize, CV_8UC3, cv::Scalar(0, 255, 0));// 创建双通道黑色图像。
  268. cv::Mat four_mat = cv::Mat(MatSmallSize, CV_8UC3, cv::Scalar(0, 255, 0));// 创建双通道黑色图像。
  269. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);
  270. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_cut = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);
  271. pcl::PointCloud<pcl::PointXYZ>::Ptr four_cloud_cut = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);
  272. ReadTxtCloud(file, cloud);
  273. pcl::CropHull<pcl::PointXYZ> bb_filter; //创建CropHull对象
  274. bb_filter.setInputCloud(cloud); //设置输入点云
  275. bb_filter.setDim(2); //设置维度
  276. bb_filter.setHullIndices(m_polygons); //输入封闭多边形的顶点
  277. bb_filter.setHullCloud(m_surface_hull); //输入封闭多边形的形状
  278. bb_filter.filter(*cloud_cut);
  279. pcl::PassThrough<pcl::PointXYZ> cut_pass;
  280. cut_pass.setInputCloud(cloud_cut);
  281. cut_pass.setFilterFieldName("z");
  282. cut_pass.setFilterLimits(m_region.minz(), m_region.minz() + 2);
  283. cut_pass.setFilterLimitsNegative(false);
  284. cut_pass.filter(*cloud_cut);
  285. pcl::copyPointCloud(*cloud_cut, *four_cloud_cut);
  286. // WriteTxtCloud(lll, cloud_cut);
  287. Points2Mat(cloud_cut, mat);
  288. cv::imwrite(mat_file, mat);
  289. usleep(1000 * 10);
  290. Points2MatSegmentWheels(four_cloud_cut, four_mat);
  291. cv::imwrite(four_mat_file, four_mat);
  292. usleep(1000 * 10);
  293. // exit(EXIT_FAILURE);
  294. }
  295. }
  296. return EXIT_SUCCESS;
  297. }