grab_occlusion.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #pragma once
  2. #include <vector>
  3. #include <string>
  4. #include <math.h>
  5. #include <pcl\point_types.h>
  6. #include <pcl\point_cloud.h>
  7. #include "logger.h"
  8. namespace graft_cv {
  9. class CStemResult {
  10. public:
  11. CStemResult() :
  12. root_x(0.0),
  13. root_z(0.0),
  14. stem_size(0),
  15. batch_id(std::string("")),
  16. root_count(0)
  17. {};
  18. CStemResult(double x,double y, double z, int size, std::string& bid, int rcount)
  19. {
  20. root_x = x;
  21. root_y = y;
  22. root_z = z;
  23. stem_size = size;
  24. batch_id = std::string(bid);
  25. root_count = rcount;
  26. };
  27. double calcluate_distance(const CStemResult& sr) {
  28. double dist = 0.0;
  29. dist = sqrt((this->root_x - sr.root_x) *(this->root_x - sr.root_x) +
  30. //(this->root_y - sr.root_y) *(this->root_y - sr.root_y) +
  31. (this->root_z - sr.root_z) *(this->root_z - sr.root_z));
  32. return dist;
  33. };
  34. double root_x; //识别到的茎根x坐标
  35. double root_y;
  36. double root_z; //识别到的茎根z坐标
  37. int stem_size; //识别到的茎根矩形邻域点云数量
  38. std::string batch_id; //识别到的茎批次(每一次处理,批次一致)
  39. int root_count; //统计root的累计个数
  40. private:
  41. };
  42. class CStemResultInfos {
  43. public:
  44. CStemResultInfos(
  45. double seedling_dist,
  46. int holes_number,
  47. double x_min,
  48. double x_max,
  49. double z_min,
  50. double z_max,
  51. double z_cent,
  52. std::string pcd_id,
  53. CGcvLogger*pLog=0);
  54. ~CStemResultInfos();
  55. //void set_json_filename(std::string& filename);
  56. //void read_root_centers(std::string& filename);//读取历史数据,每次启动时,去取
  57. //void write_root_centers(std::string& filename);//保存当前数据,间隔一定时间保存一次
  58. void append(CStemResult& sr);
  59. void clear();
  60. void get_root_centers(std::vector<CStemResult>&rst);
  61. protected:
  62. CGcvLogger * m_pLogger;
  63. int m_max_size; //m_infos最大长度
  64. std::vector<CStemResult> m_infos;
  65. double m_seedling_dist; //植株间距,毫米
  66. std::vector<CStemResult> m_root_centers; //茎根位置历史平均值
  67. //int m_append_counter;
  68. //std::string m_json_filename;
  69. std::string m_pcdId;
  70. int m_holes_number;
  71. double m_xmin;
  72. double m_xmax;
  73. double m_zmin;
  74. double m_zmax;
  75. double m_zcent;
  76. void _update_root_centers(CStemResult& sr); //通过当前的数据生成茎中心位置,聚类
  77. //void _filter_root_centers(double d1, double d2); //对生成的根中心m_root_centers进行过滤,剔除异常
  78. //void euclidean_clustering_ttsas(
  79. // pcl::PointCloud<pcl::PointXYZ>::Ptr in_cloud,
  80. // double d1, double d2,
  81. // std::vector<pcl::PointXYZ>&cluster_center,
  82. // std::vector<std::vector<int>> &clustr_member
  83. //);
  84. void gen_root_centers();
  85. };
  86. //植株状态,记录植株点云分布情况,用于确定植株是否被取走,穴位是否有苗
  87. class CSeedlingStatus {
  88. public:
  89. CSeedlingStatus(
  90. int dtype, //1-砧木, 0-穗苗
  91. double step, //x方向直方图点云分布,每个bin的宽度(默认5毫米)
  92. double x_min, //x的下限
  93. double x_max, //x的上限
  94. double pc_mean_dist, //点云,两点间平均距离
  95. CGcvLogger*pLog = 0);
  96. ~CSeedlingStatus();
  97. void set_x_centers(std::vector<double>&cx);
  98. void append_hist(std::vector<int>&xhist //input
  99. //std::vector<bool>&xstatus //output
  100. );
  101. //实际茎检测的结果,更新到历史记录中
  102. void real_result_update(
  103. std::vector<pcl::PointXYZ>& root, //识别到茎根坐标
  104. std::vector<int>& target_filtered_stem_status //识别到茎的状态,1--有茎, 2--叶子遮挡
  105. );
  106. //实际叶遮挡检测的结果,更新到历史记录中
  107. void occlusion_result_update(
  108. std::vector<int>& leaf_occlusion
  109. );
  110. //获取植株的状态,是否有苗
  111. void get_stem_status(
  112. std::vector<int>&xstatus
  113. );
  114. protected:
  115. CGcvLogger * m_pLogger;
  116. int m_max_size; //最大记录长度
  117. int m_record_cursor;
  118. int m_global_cursor;
  119. cv::Mat m_history_histogram; //历史点云每个穴位的点云数量
  120. cv::Mat m_history_point_size; //历史点云框内数量
  121. cv::Mat m_history_status; //历史穴位是有苗状态
  122. std::vector<int> m_idx_low; //穴位对应的序号低位
  123. std::vector<int> m_idx_up; //穴位对应的序号低位
  124. int m_dtype; //1-砧木, 0-穗苗
  125. double m_xmin; //x的下限
  126. double m_xmax; //x的上限
  127. double m_bin_step; //x方向直方图点云分布,每个bin的宽度(默认5毫米)
  128. double m_pc_mean_dist; //点云,两点间平均距离
  129. std::vector<double>m_center_x;
  130. int m_hist_length;
  131. std::vector<int>m_center_grabed_record;//植株被取走的记录, -1--不确定, 0--已抓取
  132. int get_status(int& max_idx, float&max_change);
  133. void calculate_center_size(
  134. std::vector<int>&xhist, //input
  135. std::vector<float>&x_count//output
  136. );
  137. std::vector<int> m_stem_status;//0--没有,1--茎检测有结果, 2---遮挡检测有结果
  138. };
  139. }