fusion.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. #ifndef FUSION_HPP
  2. #define FUSION_HPP
  3. #include "fusion_probabilities.h"
  4. #include "fusionobjectarray.pb.h"
  5. namespace iv {
  6. namespace fusion {
  7. // 先将毫米波和图像进行融合,将融合后的数据放入fusion中
  8. //void RadarCameraFusion(iv::vision::cameraobjectarray& camera_object_array, iv::radar::radarobjectarray& radar_object_array, iv::fusion::fusionobjectarray& Ra_Ca_fusion_object)
  9. //{
  10. // for(int i = 0; i<camera_object_array.obj_size(); i++)
  11. // {
  12. // std::vector<int> obj_index;
  13. // for(int j = 0; j<radar_object_array.obj_size(); j++)
  14. // {
  15. // if(radar_object_array.obj(j).bvalid() == false) continue;
  16. // if(!(FusionProbabilities::ComputeRadarCameraFusionProb(radar_object_array.obj(j),camera_object_array.obj(i)))) continue;
  17. //// else if () {
  18. //// }
  19. // else {
  20. // obj_index.push_back(j);
  21. // }
  22. // }
  23. // if(obj_index.size()==0)
  24. // {
  25. // iv::fusion::fusionobject fusion_object;
  26. // fusion_object.set_id(camera_object_array.obj(i).id());
  27. // fusion_object.set_type(camera_object_array.obj(i).type());
  28. // fusion_object.set_prob(camera_object_array.obj(i).con());
  29. // iv::fusion::Dimension dimension;
  30. // iv::fusion::Dimension *obj_dimension;
  31. // dimension.set_x(camera_object_array.obj(i).w());
  32. // dimension.set_z(camera_object_array.obj(i).h());
  33. // obj_dimension = fusion_object.mutable_dimensions();
  34. // obj_dimension->CopyFrom(dimension);
  35. // iv::fusion::fusionobject *po = Ra_Ca_fusion_object.add_obj();
  36. // po->CopyFrom(fusion_object);
  37. // }else if (obj_index.size()==1)
  38. // {
  39. // iv::fusion::fusionobject fusion_object;
  40. // fusion_object.set_id(camera_object_array.obj(i).id());
  41. // fusion_object.set_type(camera_object_array.obj(i).type());
  42. // fusion_object.set_prob(camera_object_array.obj(i).con());
  43. // iv::fusion::PointXYZ centroid;
  44. // iv::fusion::PointXYZ *centerpoint;
  45. // centroid.set_x(radar_object_array.obj(obj_index.at(0)).x());
  46. // centroid.set_y(radar_object_array.obj(obj_index.at(0)).y());
  47. // centerpoint=fusion_object.mutable_centroid();
  48. // centerpoint->CopyFrom(centroid);
  49. // iv::fusion::Dimension dimension;
  50. // iv::fusion::Dimension *obj_dimension;
  51. // dimension.set_x(camera_object_array.obj(i).w());
  52. // dimension.set_z(camera_object_array.obj(i).h());
  53. // obj_dimension = fusion_object.mutable_dimensions();
  54. // obj_dimension->CopyFrom(dimension);
  55. // iv::fusion::VelXY vel_relative;
  56. // iv::fusion::VelXY *velrelative;
  57. // vel_relative.set_x(radar_object_array.obj(obj_index.at(0)).vx());
  58. // vel_relative.set_y(radar_object_array.obj(obj_index.at(0)).vy());
  59. // velrelative = fusion_object.mutable_vel_relative();
  60. // velrelative->CopyFrom(vel_relative);
  61. // iv::fusion::fusionobject *po = Ra_Ca_fusion_object.add_obj();
  62. // po->CopyFrom(fusion_object);
  63. // }else
  64. // {
  65. // }
  66. // obj_index.clear();
  67. // }
  68. //}
  69. float ComputeDis(cv::Point2f po1, cv::Point2f po2)
  70. {
  71. return (sqrt(pow((po1.x-po2.x),2) + pow((po1.y-po2.y),2)));
  72. }
  73. void RLfusion( iv::lidar::objectarray& lidar_object_array, iv::radar::radarobjectarray& radar_object_array, iv::fusion::fusionobjectarray& lidar_radar_fusion_object_array)
  74. {
  75. lidar_radar_fusion_object_array.Clear();
  76. std::cout<<"~~~~~~~~~~~~"<<lidar_object_array.obj_size()<<std::endl;
  77. for(int i = 0; i<lidar_object_array.obj_size(); i++)
  78. {
  79. std::vector<int> fuindex;
  80. for(int j =0; j<radar_object_array.obj_size(); j++)
  81. {
  82. if(radar_object_array.obj(j).bvalid() == false) continue;
  83. if(!(iv::fusion::FusionProbabilities::ComputRadarLidarmatch(radar_object_array.obj(j),lidar_object_array.obj(i))))
  84. {
  85. fuindex.push_back(j);
  86. }
  87. }
  88. if(fuindex.size() == 0)
  89. {
  90. iv::fusion::fusionobject fusion_object;
  91. fusion_object.set_id(lidar_object_array.obj(i).id());
  92. fusion_object.set_type_name(lidar_object_array.obj(i).type_name());
  93. // fusion_object.set_prob(lidar_object_array.obj(i).type_probs());
  94. fusion_object.set_yaw(lidar_object_array.obj(i).tyaw());
  95. Vel_Struct vels;
  96. iv::fusion::VelXY vel;
  97. iv::fusion::VelXY *vel_;
  98. vels = iv::fusion::Transformation::lidarVelTra(lidar_object_array.obj(i).tyaw(),lidar_object_array.obj(i).velocity_linear_x());
  99. vel.set_x(vels.vx);
  100. vel.set_y(vels.vy);
  101. vel_ = fusion_object.mutable_vel_relative();
  102. vel_->CopyFrom(vel);
  103. iv::fusion::PointXYZ centroid;
  104. iv::fusion::PointXYZ *centroid_;
  105. centroid.set_x(lidar_object_array.obj(i).centroid().x());
  106. centroid.set_y(lidar_object_array.obj(i).centroid().y());
  107. centroid.set_z(lidar_object_array.obj(i).centroid().z());
  108. centroid_ = fusion_object.mutable_centroid();
  109. centroid_->CopyFrom(centroid);
  110. iv::fusion::Dimension dimension;
  111. iv::fusion::Dimension *dimension_;
  112. dimension.set_x(lidar_object_array.obj(i).dimensions().x());
  113. dimension.set_y(lidar_object_array.obj(i).dimensions().y());
  114. dimension.set_z(lidar_object_array.obj(i).dimensions().z());
  115. dimension_ = fusion_object.mutable_dimensions();
  116. dimension_->CopyFrom(dimension);
  117. for(int k =0 ;k<lidar_object_array.obj(i).cloud().size(); k++)
  118. {
  119. iv::fusion::PointXYZI *pointcloud_ = fusion_object.add_cloud();
  120. pointcloud_->set_x(lidar_object_array.obj(i).cloud(k).x());
  121. pointcloud_->set_y(lidar_object_array.obj(i).cloud(k).y());
  122. pointcloud_->set_z(lidar_object_array.obj(i).cloud(k).z());
  123. pointcloud_->set_i(lidar_object_array.obj(i).cloud(k).i());
  124. }
  125. iv::fusion::fusionobject *pobj = lidar_radar_fusion_object_array.add_obj();
  126. pobj->CopyFrom(fusion_object);
  127. }else
  128. {
  129. std::vector<float> dis;
  130. cv::Point2f po1;
  131. po1.x = lidar_object_array.obj(i).centroid().x();
  132. po1.y = lidar_object_array.obj(i).centroid().y();
  133. for(std::vector<int>::iterator d = fuindex.begin(); d !=fuindex.end(); d++)
  134. {
  135. cv::Point2f po2;
  136. po2.x = radar_object_array.obj(*d).x();
  137. po2.y = radar_object_array.obj(*d).y();
  138. dis.push_back(ComputeDis(po1,po2));
  139. }
  140. auto smallest = std::min_element(std::begin(dis), std::end(dis));
  141. int index = std::distance(std::begin(dis), smallest);
  142. dis.clear();
  143. iv::fusion::fusionobject fusion_object;
  144. fusion_object.set_id(lidar_object_array.obj(i).id());
  145. fusion_object.set_type_name(lidar_object_array.obj(i).type_name());
  146. // fusion_object.set_prob(lidar_object_array.obj(i).type_probs());
  147. fusion_object.set_yaw(lidar_object_array.obj(i).tyaw());
  148. iv::fusion::VelXY vel;
  149. iv::fusion::VelXY *vel_;
  150. vel.set_x(radar_object_array.obj(fuindex[index]).vx());
  151. vel.set_y(radar_object_array.obj(fuindex[index]).vy());
  152. vel_ = fusion_object.mutable_vel_relative();
  153. vel_->CopyFrom(vel);
  154. iv::fusion::PointXYZ centroid;
  155. iv::fusion::PointXYZ *centroid_;
  156. centroid.set_x(radar_object_array.obj(fuindex[index]).x());
  157. centroid.set_y(radar_object_array.obj(fuindex[index]).y());
  158. centroid.set_z(lidar_object_array.obj(i).centroid().z());
  159. centroid_ = fusion_object.mutable_centroid();
  160. centroid_->CopyFrom(centroid);
  161. iv::fusion::Dimension dimension;
  162. iv::fusion::Dimension *dimension_;
  163. dimension.set_x(lidar_object_array.obj(i).dimensions().x());
  164. dimension.set_y(lidar_object_array.obj(i).dimensions().y());
  165. dimension.set_z(lidar_object_array.obj(i).dimensions().z());
  166. dimension_ = fusion_object.mutable_dimensions();
  167. dimension_->CopyFrom(dimension);
  168. for(int k =0 ;k<lidar_object_array.obj(i).cloud().size(); k++)
  169. {
  170. iv::fusion::PointXYZI *pointcloud_ = fusion_object.add_cloud();
  171. pointcloud_->set_x(lidar_object_array.obj(i).cloud(k).x());
  172. pointcloud_->set_y(lidar_object_array.obj(i).cloud(k).y());
  173. pointcloud_->set_z(lidar_object_array.obj(i).cloud(k).z());
  174. pointcloud_->set_i(lidar_object_array.obj(i).cloud(k).i());
  175. }
  176. iv::fusion::fusionobject *pobj = lidar_radar_fusion_object_array.add_obj();
  177. pobj->CopyFrom(fusion_object);
  178. }
  179. fuindex.clear();
  180. }
  181. }
  182. void RLVfusion( iv::fusion::fusionobjectarray& li_ra_fusion_array, iv::vision::ObstacleInfo& vision_object_array, iv::fusion::fusionobjectarray& li_ra_ca_fusion_array)
  183. {
  184. li_ra_ca_fusion_array.Clear();
  185. for(int i = 0; i<li_ra_fusion_array.obj_size();i++)
  186. {
  187. std::vector<int> findexs;
  188. std::vector<int> flags;
  189. for(int j = 0; j<vision_object_array.bbox_3d_size(); j++)
  190. {
  191. int result = iv::fusion::FusionProbabilities::ComputeLidarCameraFusionProb(li_ra_fusion_array.obj(i), vision_object_array.bbox_3d(j));
  192. if(result <20) continue;
  193. else {
  194. findexs.push_back(j);
  195. flags.push_back(result);
  196. }
  197. }
  198. if(findexs.size() == 0)
  199. {
  200. iv::fusion::fusionobject fusionobject;
  201. fusionobject.set_id(li_ra_fusion_array.obj(i).id());
  202. fusionobject.set_yaw(li_ra_fusion_array.obj(i).yaw());
  203. fusionobject.set_type_name(li_ra_fusion_array.obj(i).type_name());
  204. fusionobject.set_prob(li_ra_fusion_array.obj(i).prob());
  205. iv::fusion::VelXY vel;
  206. iv::fusion::VelXY *vel_;
  207. vel.set_x(li_ra_fusion_array.obj(i).vel_relative().x());
  208. vel.set_y(li_ra_fusion_array.obj(i).vel_relative().y());
  209. vel_ = fusionobject.mutable_vel_relative();
  210. vel_->CopyFrom(vel);
  211. iv::fusion::PointXYZ centroid;
  212. iv::fusion::PointXYZ *centroid_;
  213. centroid.set_x(li_ra_fusion_array.obj(i).centroid().x());
  214. centroid.set_y(li_ra_fusion_array.obj(i).centroid().y());
  215. centroid.set_z(li_ra_fusion_array.obj(i).centroid().z());
  216. centroid_ = fusionobject.mutable_centroid();
  217. centroid_->CopyFrom(centroid);
  218. iv::fusion::Dimension dimension;
  219. iv::fusion::Dimension *dimension_;
  220. dimension.set_x(li_ra_fusion_array.obj(i).dimensions().x());
  221. dimension.set_y(li_ra_fusion_array.obj(i).dimensions().y());
  222. dimension.set_z(li_ra_fusion_array.obj(i).dimensions().z());
  223. dimension_ = fusionobject.mutable_dimensions();
  224. dimension_->CopyFrom(dimension);
  225. for(int m =0 ;m<li_ra_fusion_array.obj(i).cloud().size(); m++)
  226. {
  227. iv::fusion::PointXYZI pointcloud;
  228. iv::fusion::PointXYZI *pointcloud_;
  229. pointcloud.set_x(li_ra_fusion_array.obj(i).cloud(m).x());
  230. pointcloud.set_y(li_ra_fusion_array.obj(i).cloud(m).y());
  231. pointcloud.set_z(li_ra_fusion_array.obj(i).cloud(m).z());
  232. pointcloud.set_i(li_ra_fusion_array.obj(i).cloud(m).i());
  233. pointcloud_= fusionobject.add_cloud();
  234. pointcloud_->CopyFrom(pointcloud);
  235. }
  236. iv::fusion::fusionobject *pbj = li_ra_ca_fusion_array.add_obj();
  237. pbj->CopyFrom(fusionobject);
  238. }else
  239. {
  240. auto bigest = std::min_element(std::begin(flags), std::end(flags));
  241. int index = std::distance(std::begin(flags), bigest);
  242. iv::fusion::fusionobject fusionobject;
  243. fusionobject.set_id(li_ra_fusion_array.obj(i).id());
  244. fusionobject.set_yaw(li_ra_fusion_array.obj(i).yaw());
  245. // fusionobject.set_prob(vision_object_array.bbox_3d(findexs[index]).);
  246. fusionobject.set_type_name(vision_object_array.bbox_3d(findexs[index]).category());
  247. iv::fusion::VelXY vel;
  248. iv::fusion::VelXY *vel_;
  249. vel.set_x(li_ra_fusion_array.obj(i).vel_relative().x());
  250. vel.set_y(li_ra_fusion_array.obj(i).vel_relative().y());
  251. vel_ = fusionobject.mutable_vel_relative();
  252. vel_->CopyFrom(vel);
  253. iv::fusion::PointXYZ centroid;
  254. iv::fusion::PointXYZ *centroid_;
  255. centroid.set_x(li_ra_fusion_array.obj(i).centroid().x());
  256. centroid.set_y(li_ra_fusion_array.obj(i).centroid().y());
  257. centroid.set_z(li_ra_fusion_array.obj(i).centroid().z());
  258. centroid_ = fusionobject.mutable_centroid();
  259. centroid_->CopyFrom(centroid);
  260. iv::fusion::Dimension dimension;
  261. iv::fusion::Dimension *dimension_;
  262. dimension.set_x(li_ra_fusion_array.obj(i).dimensions().x());
  263. dimension.set_y(li_ra_fusion_array.obj(i).dimensions().y());
  264. dimension.set_z(li_ra_fusion_array.obj(i).dimensions().z());
  265. dimension_ = fusionobject.mutable_dimensions();
  266. dimension_->CopyFrom(dimension);
  267. for(int n =0 ;n<li_ra_ca_fusion_array.obj(i).cloud().size(); n++)
  268. {
  269. iv::fusion::PointXYZI pointcloud;
  270. iv::fusion::PointXYZI *pointcloud_;
  271. pointcloud.set_x(li_ra_fusion_array.obj(i).cloud(n).x());
  272. pointcloud.set_y(li_ra_fusion_array.obj(i).cloud(n).y());
  273. pointcloud.set_z(li_ra_fusion_array.obj(i).cloud(n).z());
  274. pointcloud.set_i(li_ra_fusion_array.obj(i).cloud(n).i());
  275. pointcloud_= fusionobject.add_cloud();
  276. pointcloud_->CopyFrom(pointcloud);
  277. }
  278. iv::fusion::fusionobject *pobj = li_ra_ca_fusion_array.add_obj();
  279. pobj->CopyFrom(fusionobject);
  280. }
  281. findexs.clear();
  282. flags.clear();
  283. }
  284. }
  285. }
  286. }
  287. #endif // FUSION_HPP