detect_wheel_ceres3d.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // Created by zx on 2020/7/1.
  3. //
  4. #ifndef DETECT_WHEEL_CERES_3D_H
  5. #define DETECT_WHEEL_CERES_3D_H
  6. #include <ceres/ceres.h>
  7. #include <glog/logging.h>
  8. #include <pcl/point_types.h>
  9. #include <pcl/point_cloud.h>
  10. #include <pcl/common/centroid.h>
  11. #include <opencv2/opencv.hpp>
  12. #include <iostream>
  13. #include <string>
  14. #include <chrono>
  15. #include "boost/format.hpp"
  16. #include "hybrid_grid.hpp"
  17. #include "wheel_model_3d.h"
  18. #include "tool/point3D_tool.h"
  19. class detect_wheel_ceres3d {
  20. public:
  21. struct Loss_result
  22. {
  23. public:
  24. double lf_loss;
  25. double rf_loss;
  26. double lb_loss;
  27. double rb_loss;
  28. double total_avg_loss;
  29. Loss_result() {
  30. lf_loss = 0;
  31. rf_loss = 0;
  32. lb_loss = 0;
  33. rb_loss = 0;
  34. total_avg_loss = 0;
  35. }
  36. Loss_result& operator=(const Loss_result& loss_result)
  37. {
  38. this->lf_loss = loss_result.lf_loss;
  39. this->rf_loss = loss_result.rf_loss;
  40. this->lb_loss = loss_result.lb_loss;
  41. this->rb_loss = loss_result.rb_loss;
  42. this->total_avg_loss = loss_result.total_avg_loss;
  43. return *this;
  44. }
  45. };
  46. struct Detect_result
  47. {
  48. public:
  49. double cx;
  50. double cy;
  51. double theta;
  52. double wheel_base;
  53. double wheel_width;
  54. double width;
  55. double length;
  56. double body_width;
  57. double front_theta;
  58. // 220110 added by yct
  59. double single_wheel_width;
  60. double single_wheel_length;
  61. bool success;
  62. Loss_result loss;
  63. Detect_result() {
  64. cx = 0;
  65. cy = 0;
  66. theta = 0;
  67. wheel_base = 0;
  68. wheel_width = 0;
  69. width = 0;
  70. length = 0;
  71. body_width = 0;
  72. front_theta = 0;
  73. single_wheel_width = 0;
  74. single_wheel_length = 0;
  75. success = false;
  76. loss = Loss_result();
  77. }
  78. Detect_result& operator=(const Detect_result& detect_result)
  79. {
  80. this->cx = detect_result.cx;
  81. this->cy = detect_result.cy;
  82. this->theta = detect_result.theta;
  83. this->wheel_base = detect_result.wheel_base;
  84. this->wheel_width = detect_result.wheel_width;
  85. this->width = detect_result.width;
  86. this->length = detect_result.length;
  87. this->front_theta = detect_result.front_theta;
  88. this->single_wheel_width = detect_result.single_wheel_width;
  89. this->single_wheel_length = detect_result.single_wheel_length;
  90. this->success = detect_result.success;
  91. this->body_width=detect_result.body_width;
  92. this->loss=detect_result.loss;
  93. return *this;
  94. }
  95. std::string to_string()
  96. {
  97. char buf[512];
  98. sprintf(buf, "cx:%.3f cy:%.3f th:%.3f wb:%.3f wwd:%.3f lg:%.3f fth:%.3f suc:%s sww:%.3f swl:%.3f ", cx, cy, theta, wheel_base, wheel_width, length, front_theta, success ? "true" : "false", single_wheel_width, single_wheel_length);
  99. return std::string(buf);
  100. }
  101. };
  102. // 传入参数为左右两个车轮的插值模板的点云数据
  103. detect_wheel_ceres3d(pcl::PointCloud<pcl::PointXYZ>::Ptr left_grid_cloud,
  104. pcl::PointCloud<pcl::PointXYZ>::Ptr right_grid_cloud);
  105. ~detect_wheel_ceres3d();
  106. bool detect(std::vector<pcl::PointCloud<pcl::PointXYZ>::Ptr> cloud_vec, Detect_result &detect_result, std::string &error_info);
  107. void save_debug_data(std::string file );
  108. protected:
  109. void save_model(std::string file);
  110. // 投影车轮点云,获取外接矩形,以此矩形中心估算轴距
  111. bool wheebase_estimate(double &wheelbase, double &wheel_width, double &wheel_length);
  112. // 外接矩形估算车轮宽度,以此调整模型比例,此比例变换点云贴合模型
  113. bool wheel_size_estimate(double &wheel_width, double &wheel_length, double car_angle, float &ratio);
  114. // 根据点云计算外接矩形
  115. /**
  116. * @brief 输入点云,通过cv库计算点云的最小外接矩形,并判断矩形是否异常
  117. * @param cloud 输入的点云数据;
  118. * @param rect 返回最小外接矩形;
  119. * @param theta 最小外接矩形的旋转角度,与cv库求最小外接矩形的angle不一样;
  120. * @param length 外接矩形的长边;
  121. * @param width 外接矩形的短边;
  122. * @return 如果输入点云异常或最小外接矩形的长不属于(0.3, 0.9),宽不属于(0.1, 0.3)都会返回false
  123. */
  124. bool get_wheel_rect(pcl::PointCloud<pcl::PointXYZ> cloud, cv::RotatedRect &rect, double &theta, double &length, double &width);
  125. // 根据点云与模型比例参数更新模型
  126. // 模型更新很慢,通常仅初始化时调用该函数
  127. bool update_model(float ratio = 1.0f);
  128. bool Solve(Detect_result &detect_result, Loss_result &loss_result, std::string &error_info);
  129. void get_costs(double* variable,double &lf, double &rf, double &lr, double &rr);
  130. public:
  131. pcl::PointCloud<pcl::PointXYZ> m_left_front_transformed_cloud; //左前点云
  132. pcl::PointCloud<pcl::PointXYZ> m_right_front_transformed_cloud; //右前点云
  133. pcl::PointCloud<pcl::PointXYZ> m_left_rear_transformed_cloud; //左后点云
  134. pcl::PointCloud<pcl::PointXYZ> m_right_rear_transformed_cloud; //右后点云
  135. pcl::PointCloud<pcl::PointXYZ> m_left_front_cloud; //左前点云
  136. pcl::PointCloud<pcl::PointXYZ> m_right_front_cloud; //右前点云
  137. pcl::PointCloud<pcl::PointXYZ> m_left_rear_cloud; //左后点云
  138. pcl::PointCloud<pcl::PointXYZ> m_right_rear_cloud; //右后点云
  139. private:
  140. HybridGrid* mp_left_grid;
  141. HybridGrid* mp_right_grid;
  142. // 左右模型原始点云, 用于动态调整模型大小
  143. pcl::PointCloud<pcl::PointXYZ> m_left_model_cloud;
  144. pcl::PointCloud<pcl::PointXYZ> m_right_model_cloud;
  145. };
  146. #endif //DETECT_WHEEL_CERES_3D_H