grab_point_rs.cpp 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990
  1. /*
  2. 通过点云数据识别抓取位置信息
  3. 1)获取点云
  4. 2)剔除离群点
  5. 3)降采样
  6. 4)植株检测
  7. 5)选出最前,最右侧植株
  8. 6)植株抓取位置检测
  9. */
  10. #include <pcl\io\ply_io.h>
  11. #include <pcl\visualization\cloud_viewer.h>
  12. #include <pcl\filters\crop_box.h>
  13. #include <pcl\filters\radius_outlier_removal.h>
  14. #include <pcl\filters\statistical_outlier_removal.h>
  15. #include <pcl\filters\voxel_grid.h>
  16. #include <pcl\common\common.h>
  17. #include <pcl\features\moment_of_inertia_estimation.h>
  18. #include <pcl\segmentation\sac_segmentation.h>
  19. #include <math.h>
  20. #include "grab_point_rs.h"
  21. #include "utils.h"
  22. #define PI std::acos(-1)
  23. namespace graft_cv {
  24. CRootStockGrabPoint::CRootStockGrabPoint(ConfigParam&cp, CGcvLogger*pLog/*=0*/)
  25. :m_cparam(cp),
  26. m_pLogger(pLog),
  27. m_dtype(0),
  28. m_pcdId(""),
  29. m_ppImgSaver(0),
  30. m_1st_row_zmean_rs(-1.0),
  31. m_1st_row_zmean_sc(-1.0)
  32. {
  33. }
  34. CRootStockGrabPoint::~CRootStockGrabPoint() {}
  35. float* CRootStockGrabPoint::get_raw_point_cloud(int &data_size)
  36. {
  37. data_size = m_raw_cloud->width * m_raw_cloud->height;
  38. if (data_size == 0) {
  39. return 0;
  40. }
  41. else {
  42. pcl::PointXYZ* pp = m_raw_cloud->points.data();
  43. return (float*)pp->data;
  44. }
  45. }
  46. int CRootStockGrabPoint::load_data(
  47. float*pPoint,
  48. int pixel_size,
  49. int pt_size,
  50. int dtype,
  51. const char* fn/* = 0*/)
  52. {
  53. int rst = 0;
  54. m_dtype = dtype;
  55. //generate image id
  56. if (m_dtype == 0) {
  57. m_pcdId = getImgId(img_type::sola_sc_pcd);
  58. }
  59. else {
  60. m_pcdId = getImgId(img_type::sola_rs_pcd);
  61. }
  62. //1 get point cloud data
  63. if (pPoint != 0 && pt_size>0) {
  64. //read point
  65. m_raw_cloud.reset(new pcl::PointCloud<pcl::PointXYZ>);
  66. int step = pixel_size;
  67. for (int i = 0; i < pt_size; ++i) {
  68. pcl::PointXYZ pt = { pPoint[i*step], pPoint[i*step + 1] , pPoint[i*step + 2] };
  69. m_raw_cloud->push_back(pt);
  70. }
  71. rst = m_raw_cloud->width * m_raw_cloud->height;
  72. if (m_pLogger) {
  73. stringstream buff;
  74. buff << m_pcdId <<": load raw point cloud " << rst << " data points";
  75. m_pLogger->INFO(buff.str());
  76. }
  77. }
  78. else if (fn != 0) {
  79. //read file
  80. rst = this->read_ply_file(fn);
  81. }
  82. else {//error
  83. if (m_pLogger) {
  84. m_pLogger->ERRORINFO(m_pcdId + ": no valid input");
  85. return (-1);
  86. }
  87. }
  88. if (m_ppImgSaver && *m_ppImgSaver) {
  89. (*m_ppImgSaver)->saveBinPly(m_raw_cloud, m_pcdId);
  90. }
  91. if (m_cparam.image_show) {
  92. viewer_cloud(m_raw_cloud, m_pcdId + std::string(": raw point cloud"));
  93. }
  94. return rst;
  95. }
  96. int CRootStockGrabPoint::grab_point_detect(
  97. PositionInfo& posinfo
  98. )
  99. {
  100. // dtype == 0, scion
  101. // dtype != 0, rootstock
  102. // 输入,穗苗--0, 砧木--1
  103. if (m_raw_cloud->width * m_raw_cloud->height < 1) {
  104. if (m_pLogger) {
  105. stringstream buff;
  106. buff << m_pcdId << ": m_raw_cloud point cloud " << m_raw_cloud->width * m_raw_cloud->height << " data points";
  107. m_pLogger->ERRORINFO(buff.str());
  108. }
  109. return 1;
  110. }
  111. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  112. //1 crop filter
  113. if (m_pLogger) {
  114. m_pLogger->INFO(m_pcdId + ": begin crop");
  115. }
  116. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_inbox(new pcl::PointCloud<pcl::PointXYZ>);
  117. pcl::CropBox<pcl::PointXYZ> box_filter;
  118. if (m_dtype != 0) {//rootstock
  119. box_filter.setMin(Eigen::Vector4f(m_cparam.rs_grab_xmin, m_cparam.rs_grab_ymin, m_cparam.rs_grab_zmin, 1));
  120. box_filter.setMax(Eigen::Vector4f(m_cparam.rs_grab_xmax, m_cparam.rs_grab_ymax, m_cparam.rs_grab_zmax, 1));
  121. }
  122. else {//scion
  123. box_filter.setMin(Eigen::Vector4f(m_cparam.sc_grab_xmin, m_cparam.sc_grab_ymin, m_cparam.sc_grab_zmin, 1));
  124. box_filter.setMax(Eigen::Vector4f(m_cparam.sc_grab_xmax, m_cparam.sc_grab_ymax, m_cparam.sc_grab_zmax, 1));
  125. }
  126. box_filter.setNegative(false);
  127. box_filter.setInputCloud(m_raw_cloud);
  128. box_filter.filter(*cloud_inbox);
  129. if (m_pLogger) {
  130. stringstream buff;
  131. buff << m_pcdId << ": CropBox croped point cloud " << cloud_inbox->width * cloud_inbox->height << " data points";
  132. m_pLogger->INFO(buff.str());
  133. }
  134. if (cloud_inbox->width * cloud_inbox->height < 1) {
  135. return 1;
  136. }
  137. if (m_cparam.image_show) {
  138. viewer_cloud(cloud_inbox, std::string("cloud_inbox"));
  139. }
  140. if (m_pLogger) {
  141. m_pLogger->INFO(m_pcdId + ": end crop");
  142. }
  143. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  144. //2 filtler with radius remove
  145. if (m_pLogger) {
  146. m_pLogger->INFO(m_pcdId + ": begin ror");
  147. }
  148. int nb_points = 20;
  149. double stem_radius = m_cparam.rs_grab_stem_diameter / 2.0;
  150. if(m_dtype == 0){
  151. stem_radius = m_cparam.sc_grab_stem_diameter / 2.0;
  152. }
  153. double remove_radius = stem_radius * 2.0;
  154. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_ror(new pcl::PointCloud<pcl::PointXYZ>);
  155. pcl::RadiusOutlierRemoval<pcl::PointXYZ> ror(false);
  156. ror.setInputCloud(cloud_inbox);
  157. ror.setRadiusSearch(remove_radius);
  158. ror.setMinNeighborsInRadius(nb_points);
  159. ror.filter(*cloud_ror);
  160. if (m_pLogger) {
  161. stringstream buff;
  162. buff << m_pcdId <<": RadiusOutlierRemoval filtered point cloud " << cloud_ror->width * cloud_ror->height << " data points. param stem_radius="<<
  163. stem_radius<<", nb_points="<< nb_points;
  164. m_pLogger->INFO(buff.str());
  165. }
  166. if (cloud_ror->width * cloud_ror->height < 1) {
  167. return 1;
  168. }
  169. /*if (m_cparam.image_show) {
  170. viewer_cloud(cloud_ror, std::string("cloud_ror"));
  171. }*/
  172. if (m_pLogger) {
  173. m_pLogger->INFO(m_pcdId + ": end ror");
  174. }
  175. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  176. //3 voxel grid down sampling
  177. if (m_pLogger) {
  178. m_pLogger->INFO(m_pcdId + ": begin down");
  179. }
  180. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_dowm_sampled(new pcl::PointCloud<pcl::PointXYZ>);
  181. pcl::VoxelGrid<pcl::PointXYZ> outrem;
  182. outrem.setInputCloud(cloud_ror);
  183. //outrem.setLeafSize(stem_radius, stem_radius, stem_radius);
  184. double voxel_size = m_cparam.rs_grab_voxel_size;
  185. if (m_dtype == 0) { voxel_size = m_cparam.sc_grab_voxel_size; }
  186. outrem.setLeafSize(voxel_size, voxel_size, voxel_size);
  187. outrem.filter(*cloud_dowm_sampled);
  188. if (m_pLogger) {
  189. stringstream buff;
  190. buff << m_pcdId <<": VoxelGrid dowm_sampled point cloud " << cloud_dowm_sampled->width * cloud_dowm_sampled->height << " data points (if < 50, return)";
  191. m_pLogger->INFO(buff.str());
  192. }
  193. if (cloud_dowm_sampled->width * cloud_dowm_sampled->height < 50) {
  194. return 1;
  195. }
  196. /*if (m_cparam.image_show) {
  197. viewer_cloud(cloud_dowm_sampled, std::string("cloud_dowm_sampled"));
  198. }*/
  199. if (m_pLogger) {
  200. m_pLogger->INFO(m_pcdId + ": end down");
  201. }
  202. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  203. //4 seedling position,找到第一个位置上的植株
  204. std::vector<int> first_seedling_cloud_idx; //存储找到的第一个位置上植株茎上直线点的index
  205. pcl::PointXYZ xz_center; //存储找到的第一个位置上植株根部坐标
  206. pcl::ModelCoefficients::Ptr line_model; //存储找到的第一个位置上植株茎直线模型
  207. if (m_pLogger) {
  208. m_pLogger->INFO(m_pcdId + ": begin find seedling position");
  209. }
  210. find_seedling_position_key(cloud_dowm_sampled, first_seedling_cloud_idx, xz_center, line_model);
  211. if (m_pLogger) {
  212. stringstream buff;
  213. buff << m_pcdId <<": after find_seedling_position(), foud first seedling seeds points size " << first_seedling_cloud_idx.size();
  214. m_pLogger->INFO(buff.str());
  215. }
  216. if (m_pLogger) {
  217. m_pLogger->INFO(m_pcdId + ": end find seedling position");
  218. }
  219. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  220. //5 nearest seedling grab point selection,找到最接近指定高度的最优抓取点坐标
  221. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_seedling_seed(new pcl::PointCloud<pcl::PointXYZ>);
  222. pcl::copyPointCloud(*cloud_dowm_sampled, first_seedling_cloud_idx, *cloud_seedling_seed);
  223. //改进思路:将茎直线上cloud_seedling_seed的点,提取周围邻域xz的宽度,在相同邻域cloud_dowm_sampled点云内提取xz的宽度
  224. //对比两个宽度,变化很大的,说明周边有异物(叶柄或叶子),不适合作为抓取位置;否则就是抓取位置
  225. //在众多抓取位置上,选择离指定高度最近的点作为抓取位置
  226. //
  227. pcl::PointXYZ selected_pt;
  228. int selected_pt_idx;
  229. std::vector<int> optimal_seeds_idx;
  230. get_optimal_seed_compare(
  231. cloud_dowm_sampled, //input
  232. cloud_seedling_seed, //input
  233. line_model, //input
  234. selected_pt, //output
  235. selected_pt_idx, //output
  236. optimal_seeds_idx); //output
  237. if (selected_pt_idx < 0) {
  238. if (m_pLogger) {
  239. m_pLogger->ERRORINFO(m_pcdId + ": Not found valid fork point");
  240. }
  241. return 1;
  242. }
  243. if (m_pLogger) {
  244. m_pLogger->INFO(m_pcdId + ": end get_optimal_seed");
  245. }
  246. if (m_dtype == 0) {
  247. posinfo.sc_grab_x = selected_pt.x;
  248. posinfo.sc_grab_y = selected_pt.y;
  249. posinfo.sc_grab_z = selected_pt.z;
  250. }
  251. else {
  252. posinfo.rs_grab_x = selected_pt.x;
  253. posinfo.rs_grab_y = selected_pt.y;
  254. posinfo.rs_grab_z = selected_pt.z;
  255. }
  256. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  257. //6 保存处理结果到图片
  258. cv::Mat rst_img = cv::Mat::zeros(cv::Size(1280,640),CV_8UC1);
  259. gen_result_img(cloud_dowm_sampled, selected_pt, rst_img);
  260. if (m_ppImgSaver && *m_ppImgSaver) {
  261. (*m_ppImgSaver)->saveImage(rst_img, m_pcdId + "_rst_0");
  262. }
  263. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  264. //7 debug 显示结果
  265. if (m_cparam.image_show) {
  266. pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_cand_demo(new pcl::PointCloud<pcl::PointXYZRGB>);
  267. pcl::copyPointCloud(*cloud_dowm_sampled, *cloud_cand_demo);
  268. for (auto& pt : *cloud_cand_demo) {
  269. pt.r = 255;
  270. pt.g = 255;
  271. pt.b = 255;
  272. }
  273. int cnt = 0;
  274. for (auto& idx : optimal_seeds_idx) {
  275. int p_idx = first_seedling_cloud_idx.at(idx);
  276. /*if (p_idx == optimal_seeds_idx[selected_pt_idx]) {
  277. cloud_cand_demo->points[p_idx].r = 0;
  278. cloud_cand_demo->points[p_idx].g = 255;
  279. cloud_cand_demo->points[p_idx].b = 0;
  280. }
  281. else {*/
  282. cloud_cand_demo->points.at(p_idx).r = 255;
  283. cloud_cand_demo->points.at(p_idx).g = 0;
  284. cloud_cand_demo->points.at(p_idx).b = 0;
  285. /*} */
  286. if (cnt > optimal_seeds_idx.size()) { break; }
  287. cnt++;
  288. }
  289. pcl::PointXYZRGB pt_grab = {0,255,0};
  290. pt_grab.x = selected_pt.x;
  291. pt_grab.y = selected_pt.y;
  292. pt_grab.z = selected_pt.z;
  293. pcl::PointXYZRGB pt_grab_ = { 0,255,120 };
  294. pt_grab_.x = selected_pt.x;
  295. pt_grab_.y = selected_pt.y+0.2;
  296. pt_grab_.z = selected_pt.z;
  297. cloud_cand_demo->push_back(pt_grab);
  298. //viewer_cloud(cloud_cand_demo, std::string("cloud_cand_demo"));
  299. viewer_cloud_debug(cloud_cand_demo, selected_pt, xz_center, std::string("cloud_cand_demo"));
  300. imshow_wait("rst_img", rst_img);
  301. }
  302. return 0;
  303. }
  304. //生成结果图片
  305. void CRootStockGrabPoint::gen_result_img(
  306. pcl::PointCloud<pcl::PointXYZ>::Ptr in_cloud, //输入,整体点云cloud_dowm_sampled,
  307. pcl::PointXYZ& selected_pt, //输入,selected_pt,
  308. cv::Mat& rst_img //输出,图片, 640*1280
  309. )
  310. {
  311. if (rst_img.empty()) { return; }
  312. if (rst_img.rows != 640 && rst_img.cols != 1280) { return; }
  313. int cx = 640; //图像中心点0
  314. int cy = 320; //图像中心点0
  315. float res = 0.33333; //分辨率,1个像素0.333mm
  316. //绘制坐标轴
  317. int arrow_len = 20;
  318. cv::line(rst_img, cv::Point(0, cy), cv::Point(cx, cy), cv::Scalar(128));
  319. cv::line(rst_img, cv::Point(0, cy), cv::Point(arrow_len, cy - int(arrow_len/2)), cv::Scalar(128));
  320. cv::line(rst_img, cv::Point(0, cy), cv::Point(arrow_len, cy + int(arrow_len/2)), cv::Scalar(128));
  321. cv::line(rst_img, cv::Point(cx, 0), cv::Point(cx, cy), cv::Scalar(128));
  322. cv::line(rst_img, cv::Point(cx, 0), cv::Point(cx - int(arrow_len/2), arrow_len), cv::Scalar(128));
  323. cv::line(rst_img, cv::Point(cx, 0), cv::Point(cx + int(arrow_len/2), arrow_len), cv::Scalar(128));
  324. cv::putText(rst_img, std::string("x"), cv::Point(20, cy-10), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(128));
  325. cv::putText(rst_img, std::string("y"), cv::Point(cx+10, 20), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(128));
  326. //绘制所有点
  327. int x, y;
  328. for (auto&pt : in_cloud->points) {
  329. x = cx - int(pt.x / res + 0.5);
  330. y = cy - int(pt.y / res + 0.5);
  331. if (x < 0 || x >= rst_img.cols) { continue; }
  332. if (y < 0 || y >= rst_img.rows) { continue; }
  333. rst_img.at<unsigned char>(y, x) = 255;
  334. }
  335. //绘制抓取点坐标
  336. int grab_x = cx - int(selected_pt.x / res + 0.5);
  337. int grab_y = cy - int(selected_pt.y / res + 0.5);
  338. int radius = 30;
  339. cv::line(rst_img, cv::Point(grab_x - radius, grab_y - radius), cv::Point(grab_x + radius, grab_y + radius), cv::Scalar(128));
  340. cv::line(rst_img, cv::Point(grab_x - radius, grab_y + radius), cv::Point(grab_x + radius, grab_y - radius), cv::Scalar(128));
  341. }
  342. int CRootStockGrabPoint::read_ply_file(const char* fn)
  343. {
  344. m_raw_cloud.reset( new pcl::PointCloud<pcl::PointXYZ>);
  345. if (pcl::io::loadPLYFile<pcl::PointXYZ>(fn, *m_raw_cloud) == -1) {
  346. if (m_pLogger) {
  347. m_pLogger->ERRORINFO(m_pcdId + ": could not load file: "+std::string(fn));
  348. }
  349. return (-1);
  350. }
  351. if (m_pLogger) {
  352. stringstream buff;
  353. buff << m_pcdId <<": load raw point cloud " << m_raw_cloud->width * m_raw_cloud->height << " data points";
  354. m_pLogger->INFO(buff.str());
  355. }
  356. return m_raw_cloud->width * m_raw_cloud->height;
  357. }
  358. double CRootStockGrabPoint::compute_nearest_neighbor_distance(pcl::PointCloud<pcl::PointXYZ>::Ptr pcd)
  359. {
  360. pcl::KdTreeFLANN<pcl::PointXYZ> tree;
  361. tree.setInputCloud(pcd);
  362. int k = 2;
  363. double res = 0.0;
  364. int n_points = 0;
  365. for (size_t i = 0; i < pcd->size(); ++i) {
  366. std::vector<int> idx(k);
  367. std::vector<float> sqr_distances(k);
  368. if (tree.nearestKSearch(i, k, idx, sqr_distances) == k) {
  369. for (int id = 1; id < k; ++id) {
  370. res += sqrt(sqr_distances.at(id));
  371. ++n_points;
  372. }
  373. }
  374. }
  375. if (n_points > 0) {
  376. res /= (double)n_points;
  377. }
  378. return res;
  379. }
  380. //void CRootStockGrabPoint::find_tray_top_edge(
  381. //pcl::PointCloud<pcl::PointXYZ>::Ptr in_cloud,
  382. //float & tray_top_edge_y
  383. //)
  384. //{
  385. // tray_top_edge_y = -1000.0;
  386. // //以毫米为单位构建vector
  387. // pcl::PointXYZ min_point_aabb;
  388. // pcl::PointXYZ max_point_aabb;
  389. // pcl::MomentOfInertiaEstimation<pcl::PointXYZ> feature_extractor;
  390. // feature_extractor.setInputCloud(in_cloud);
  391. // feature_extractor.compute();
  392. // feature_extractor.getAABB(min_point_aabb, max_point_aabb);
  393. // float miny = min_point_aabb.y;
  394. // float maxy = max_point_aabb.y;
  395. // if(maxy - miny <5.0){
  396. // tray_top_edge_y = maxy;
  397. // return;
  398. // }
  399. // std::vector<int> y_cnt_hist(int(maxy - miny), 0);
  400. // for(auto& pt : in_cloud->points){
  401. // int idx = (int)(pt.y - miny);
  402. // if(idx<0){continue;}
  403. // if (idx >= y_cnt_hist.size()) { continue; }
  404. // y_cnt_hist.at(idx) += 1;
  405. // }
  406. // //从上半部分找到最大值,作为平面上沿
  407. // int idx_part = (int)(y_cnt_hist.size() / 2);
  408. // int idx_edge = std::max_element(y_cnt_hist.begin(), y_cnt_hist.begin() + idx_part) - y_cnt_hist.begin();
  409. // tray_top_edge_y = miny + (float)(idx_edge + 2.0);
  410. //}
  411. //void CRootStockGrabPoint::find_seedling_position_line_based(
  412. // pcl::PointCloud<pcl::PointXYZ>::Ptr in_cloud,
  413. // std::vector<int> &first_seedling_cloud_idx,
  414. // pcl::PointXYZ&xz_center
  415. //)
  416. //{
  417. // //找到穴盘上沿z,最优抓取的z,再在最优抓取的z基础上加上3作为有效范围
  418. // float tray_y = -1000.0;
  419. // float top_y = 0.0;
  420. // find_tray_top_edge(in_cloud, tray_y);
  421. // if (tray_y < -500.0) {
  422. // if (m_dtype == 0) {
  423. // //scion
  424. // tray_y = m_cparam.sc_grab_y_opt-2.0;
  425. // }
  426. // else {
  427. // tray_y = m_cparam.rs_grab_y_opt-2.0;
  428. // }
  429. // }
  430. // //up limit
  431. // if (m_dtype == 0) {
  432. // top_y = m_cparam.sc_grab_zmax;
  433. // if (top_y > m_cparam.sc_grab_y_opt + 3.0) {
  434. // top_y = m_cparam.sc_grab_y_opt + 3.0;
  435. // }
  436. // }
  437. // else {
  438. // top_y = m_cparam.rs_grab_zmax;
  439. // if (top_y > m_cparam.rs_grab_y_opt + 3.0) {
  440. // top_y = m_cparam.rs_grab_y_opt + 3.0;
  441. // }
  442. // }
  443. // //sub cloud
  444. // pcl::PointCloud<pcl::PointXYZ>::Ptr sub_cloud(new pcl::PointCloud<pcl::PointXYZ>);
  445. // pcl::CropBox<pcl::PointXYZ> box_filter;
  446. // if (m_dtype != 0) {//rootstock
  447. // box_filter.setMin(Eigen::Vector4f(m_cparam.rs_grab_xmin, tray_y, m_cparam.rs_grab_zmin, 1));
  448. // box_filter.setMax(Eigen::Vector4f(m_cparam.rs_grab_xmax, top_y, m_cparam.rs_grab_zmax, 1));
  449. // }
  450. // else {//scion
  451. // box_filter.setMin(Eigen::Vector4f(m_cparam.sc_grab_xmin, tray_y, m_cparam.sc_grab_zmin, 1));
  452. // box_filter.setMax(Eigen::Vector4f(m_cparam.sc_grab_xmax, top_y, m_cparam.sc_grab_zmax, 1));
  453. // }
  454. // box_filter.setNegative(false);
  455. // box_filter.setInputCloud(in_cloud);
  456. // box_filter.filter(*sub_cloud);
  457. // if (m_cparam.image_show) {
  458. // viewer_cloud(sub_cloud, std::string("sub inbox"));
  459. // }
  460. // //在sub_cloud中进行直线检测
  461. // segement_line(sub_cloud);
  462. //}
  463. //void CRootStockGrabPoint::segement_line(
  464. // pcl::PointCloud<pcl::PointXYZ>::Ptr in_cloud
  465. //)
  466. //{
  467. // pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients);
  468. // pcl::PointIndices::Ptr inliers(new pcl::PointIndices);
  469. // pcl::SACSegmentation<pcl::PointXYZ> seg;
  470. // seg.setOptimizeCoefficients(true);
  471. // seg.setModelType(pcl::SACMODEL_LINE);
  472. // seg.setMethodType(pcl::SAC_RANSAC);
  473. // seg.setDistanceThreshold(0.5);
  474. // seg.setInputCloud(in_cloud);
  475. // seg.segment(*inliers, *coefficients);
  476. // if (m_cparam.image_show) {
  477. // pcl::PointCloud<pcl::PointXYZ>::Ptr line_cloud(new pcl::PointCloud<pcl::PointXYZ>);
  478. // pcl::copyPointCloud(*in_cloud, *inliers, *line_cloud);
  479. // viewer_cloud(line_cloud, std::string("cloud_line"));
  480. // }
  481. //}
  482. ////////////////////////////////////////////////////////////
  483. void CRootStockGrabPoint::gen_all_seedling_positions(
  484. pcl::PointXYZ&key_center, //输入,已知的苗的坐标
  485. std::vector<pcl::PointXYZ>&candidate_center //输出,有倾斜苗的坐标
  486. )
  487. {
  488. //生成所有的植株位置,并排序
  489. candidate_center.clear();
  490. //找到z最小的那一株,并找出和它一排的那些
  491. float step_harf = m_cparam.rs_grab_seedling_dist / 2.0;
  492. float xmin = m_cparam.rs_grab_xmin;
  493. float xmax = m_cparam.rs_grab_xmax;
  494. float zmin = m_cparam.rs_grab_zmin;
  495. float zmax = m_cparam.rs_grab_zmax;
  496. int col_from = -8;
  497. int col_to = 8;
  498. int col_step = 1;
  499. if (m_dtype == 0) {
  500. step_harf = m_cparam.sc_grab_seedling_dist / 2.0;
  501. xmin = m_cparam.sc_grab_xmin;
  502. xmax = m_cparam.sc_grab_xmax;
  503. zmin = m_cparam.sc_grab_zmin;
  504. zmax = m_cparam.sc_grab_zmax;
  505. }
  506. // 生成所有可能的植株位置中心点
  507. for (int row = -8; row <= 4; row += 1) {
  508. float cz = key_center.z + row * step_harf/2.0;
  509. if (cz < zmin || cz > zmax) { continue; }
  510. for (int col = 4*col_from; col <= 4*col_to; col += col_step) {
  511. float cx = key_center.x + col * step_harf/2;
  512. if (cx < xmin || cx > xmax) { continue; }
  513. //保存
  514. pcl::PointXYZ pc(cx, 0.0, cz);
  515. candidate_center.push_back(pc);
  516. }
  517. }
  518. }
  519. void CRootStockGrabPoint::nms_box(
  520. std::vector<pcl::PointXYZ>&clt_root_v,
  521. std::vector<float>&valid_box_cc_dist,
  522. float hole_step_radius,
  523. std::vector<pcl::PointXYZ>& target_toot)
  524. {
  525. target_toot.clear();
  526. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
  527. for (auto&p : clt_root_v) { cloud->points.push_back(p); }
  528. pcl::KdTreeFLANN<pcl::PointXYZ> kdtree;
  529. kdtree.setInputCloud(cloud);
  530. std::vector<int>idx;
  531. std::vector<float>dist_sqr;
  532. std::vector<int> tar_idx;
  533. for (size_t i = 0; i < cloud->points.size(); ++i) {
  534. int k = kdtree.radiusSearch(cloud->points.at(i), hole_step_radius, idx, dist_sqr);
  535. std::vector<float> cc_tmp;
  536. for (auto& j : idx) { cc_tmp.push_back(valid_box_cc_dist.at(j)); }
  537. int min_id = std::min_element(cc_tmp.begin(), cc_tmp.end()) - cc_tmp.begin();
  538. if (min_id == 0) {
  539. tar_idx.push_back(i);
  540. }
  541. }
  542. for (auto& i : tar_idx) {
  543. target_toot.push_back(clt_root_v.at(i));
  544. }
  545. }
  546. //通过指定位置内,取部分点云分析是否存在真正的茎,真茎位置保存到target_filtered
  547. void CRootStockGrabPoint::line_filter(
  548. pcl::PointCloud<pcl::PointXYZ>::Ptr in_cloud, //输入点云
  549. float radius, //输入,取点半径
  550. float ymin,
  551. float ymax,
  552. std::vector<pcl::PointXYZ>&target_root, //输入,位置
  553. std::vector<pcl::PointXYZ>&target_filtered, //输出,位置
  554. std::vector<pcl::PointXYZ>&target_filtered_root, //输出,茎上根部点坐标
  555. std::vector<std::vector<int>>&target_filtered_element, //输出,茎上点index
  556. std::vector<pcl::ModelCoefficients::Ptr>& target_filtered_models//输出,茎直线模型
  557. )
  558. {
  559. target_filtered.clear();
  560. target_filtered_element.clear();
  561. target_filtered_root.clear();
  562. if (target_root.size() == 0) { return; }
  563. for (auto&p : target_root) {
  564. // 构建box,获取植株点云
  565. pcl::PointCloud<pcl::PointXYZ>::Ptr seedling_inbox(new pcl::PointCloud<pcl::PointXYZ>);
  566. pcl::CropBox<pcl::PointXYZ> box_filter;
  567. std::vector<int>inbox_idx;
  568. pcl::PointXYZ min_point_aabb_ex;
  569. pcl::PointXYZ max_point_aabb_ex;
  570. min_point_aabb_ex.x = p.x - radius;
  571. min_point_aabb_ex.y = ymin;
  572. min_point_aabb_ex.z = p.z - radius;
  573. max_point_aabb_ex.x = p.x + radius;
  574. max_point_aabb_ex.y = ymax;
  575. max_point_aabb_ex.z = p.z + radius;
  576. box_filter.setMin(Eigen::Vector4f(min_point_aabb_ex.x, min_point_aabb_ex.y, min_point_aabb_ex.z, 1));
  577. box_filter.setMax(Eigen::Vector4f(max_point_aabb_ex.x, max_point_aabb_ex.y, max_point_aabb_ex.z, 1));
  578. box_filter.setNegative(false);
  579. box_filter.setInputCloud(in_cloud);
  580. box_filter.filter(inbox_idx);
  581. pcl::copyPointCloud(*in_cloud, inbox_idx, *seedling_inbox);
  582. //植株点云直线查找
  583. //找到inbox点云中的直线
  584. float stem_radius = m_cparam.rs_grab_stem_diameter / 2.0;
  585. if (m_dtype == 0) {
  586. stem_radius = m_cparam.sc_grab_stem_diameter / 2.0;
  587. }
  588. pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients);
  589. pcl::PointIndices::Ptr inliers(new pcl::PointIndices);
  590. pcl::SACSegmentation<pcl::PointXYZ> seg;
  591. seg.setOptimizeCoefficients(true);
  592. seg.setModelType(pcl::SACMODEL_LINE);
  593. seg.setDistanceThreshold(stem_radius);
  594. seg.setInputCloud(seedling_inbox);
  595. seg.segment(*inliers, *coefficients);
  596. pcl::PointCloud<pcl::PointXYZ>::Ptr stem_cloud(new pcl::PointCloud<pcl::PointXYZ>);
  597. pcl::copyPointCloud(*seedling_inbox, *inliers, *stem_cloud);
  598. //点数过滤
  599. int min_stem_pts = m_cparam.rs_grab_stem_min_pts;
  600. if (m_dtype == 0) { min_stem_pts = m_cparam.sc_grab_stem_min_pts; }
  601. if (inliers->indices.size() < int(min_stem_pts / 2)) { continue; }
  602. //y方向分布范围滤波
  603. pcl::PointXYZ min_v;
  604. pcl::PointXYZ max_v;
  605. pcl::getMinMax3D(*stem_cloud, min_v, max_v);
  606. float dy = max_v.y - min_v.y;
  607. if (dy / (ymax - ymin) < 0.5) { continue; }
  608. //y方向分布中心滤波
  609. float dy_c = 0.5*(max_v.y + min_v.y);
  610. if ((dy_c - ymin) / (ymax - ymin) > 0.75) { continue; }
  611. //倾斜角度过滤
  612. float xz = std::sqrt(coefficients->values.at(3) * coefficients->values.at(3) +
  613. coefficients->values.at(5) * coefficients->values.at(5));
  614. float a = std::atan2f(coefficients->values.at(4), xz);
  615. a = std::fabs(a) * 180.0 / 3.14159;
  616. if (a < 45.0) { continue; }
  617. target_filtered.push_back(p);
  618. float min_y = 10000.0;
  619. int min_y_idx = -1;
  620. for (size_t j = 0; j < stem_cloud->points.size();++j) {
  621. pcl::PointXYZ &spt = stem_cloud->at(j);
  622. if (spt.y < min_y) {
  623. min_y = spt.y;
  624. min_y_idx = j;
  625. }
  626. }
  627. pcl::PointXYZ tr_pt(stem_cloud->points.at(min_y_idx).x, stem_cloud->points.at(min_y_idx).y, stem_cloud->points.at(min_y_idx).z);
  628. target_filtered_root.push_back(tr_pt);
  629. int idx_raw = 0;
  630. std::vector<int> out_idx;
  631. for (auto& idx : inliers->indices) {
  632. idx_raw = inbox_idx.at(idx);
  633. out_idx.push_back(idx_raw);
  634. }
  635. target_filtered_element.push_back(out_idx);
  636. target_filtered_models.push_back(coefficients);
  637. // debug show
  638. /*if (m_cparam.image_show) {
  639. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_line(new pcl::PointCloud<pcl::PointXYZ>);
  640. pcl::copyPointCloud(*seedling_inbox, *inliers, *cloud_line);
  641. pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("line in cluster"));
  642. viewer->addPointCloud<pcl::PointXYZ>(seedling_inbox, "cluster");
  643. viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 1, 1, 1, "cluster");
  644. viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "cluster");
  645. viewer->addPointCloud(cloud_line, "fit_line");
  646. viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 0, 1, 0, "fit_line");
  647. viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "fit_line");
  648. while (!viewer->wasStopped()) {
  649. viewer->spinOnce(100);
  650. }
  651. }*/
  652. }
  653. }
  654. void CRootStockGrabPoint::find_seedling_position_key(
  655. pcl::PointCloud<pcl::PointXYZ>::Ptr in_cloud,
  656. std::vector<int> &first_seedling_cloud_idx,
  657. pcl::PointXYZ&xz_center,
  658. pcl::ModelCoefficients::Ptr& line_model
  659. )
  660. {
  661. // 确定植株inbox范围
  662. float hole_step = m_cparam.rs_grab_seedling_dist - 5.0; //穴盘中穴间距
  663. if (m_dtype == 0) {
  664. hole_step = m_cparam.sc_grab_seedling_dist - 5.0;
  665. }
  666. float hole_step_radius = hole_step / 2.0;
  667. // 点云降维到xz平面,y=0
  668. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud2d(new pcl::PointCloud < pcl::PointXYZ>);
  669. pcl::copyPointCloud(*in_cloud, *cloud2d);
  670. for (auto&pt : *cloud2d) {
  671. pt.y = 0.0;
  672. }
  673. // 在xz平面内统计点的密度
  674. double radius = m_cparam.rs_grab_stem_diameter;
  675. if (m_dtype == 0) {
  676. radius = m_cparam.sc_grab_stem_diameter;
  677. }
  678. std::vector<int> counter;
  679. pcl::KdTreeFLANN<pcl::PointXYZ> kdtree;
  680. kdtree.setInputCloud(cloud2d);
  681. std::vector<int>idx;
  682. std::vector<float>dist_sqr;
  683. for (size_t i = 0; i < cloud2d->points.size(); ++i) {
  684. int k = kdtree.radiusSearch(cloud2d->points.at(i), radius, idx, dist_sqr);
  685. counter.push_back(k);
  686. }
  687. int max_density_idx = std::max_element(counter.begin(), counter.end()) - counter.begin();
  688. int max_density = counter.at(max_density_idx);
  689. if (m_cparam.image_show) {
  690. //显示密度最大的点的位置
  691. pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_tmp(new pcl::PointCloud<pcl::PointXYZRGB>);
  692. pcl::copyPointCloud(*in_cloud, *cloud_tmp);
  693. for (auto& pt : *cloud_tmp) {
  694. pt.r = 255;
  695. pt.g = 255;
  696. pt.b = 255;
  697. }
  698. std::vector<pcl::PointXYZ>cc;
  699. cc.push_back(in_cloud->points.at(max_density_idx));
  700. viewer_cloud_cluster(cloud_tmp, cc, string("key"));
  701. }
  702. // 生成植株的中心及box
  703. std::vector<pcl::PointXYZ>clt_root;
  704. std::vector<pcl::PointXYZ> clt_box;
  705. float ymin = m_cparam.rs_grab_ymin;
  706. float ymax = m_cparam.rs_grab_ymax;
  707. if (m_dtype == 0) {
  708. ymin = m_cparam.sc_grab_ymin;
  709. ymax = m_cparam.sc_grab_ymax;
  710. }
  711. gen_all_seedling_positions(in_cloud->points.at(max_density_idx), clt_root);
  712. // 每个位置点云情况,过滤
  713. std::vector<int> valid_index; //初步有效的矩形index
  714. std::vector<int> valid_box_pts; //立方体内点云数量
  715. std::vector<float>valid_box_cc_dist;//重心和矩形中心距离
  716. for (size_t i = 0; i < clt_root.size();++i) {
  717. pcl::PointXYZ &p = clt_root.at(i);
  718. pcl::PointCloud<pcl::PointXYZ>::Ptr seedling_inbox(new pcl::PointCloud<pcl::PointXYZ>);
  719. pcl::CropBox<pcl::PointXYZ> box_filter;
  720. std::vector<int>inbox_idx;
  721. pcl::PointXYZ min_point_aabb_ex;
  722. pcl::PointXYZ max_point_aabb_ex;
  723. min_point_aabb_ex.x = p.x - hole_step_radius;
  724. min_point_aabb_ex.y = ymin;
  725. min_point_aabb_ex.z = p.z - hole_step_radius;
  726. max_point_aabb_ex.x = p.x + hole_step_radius;
  727. max_point_aabb_ex.y = ymax;
  728. max_point_aabb_ex.z = p.z + hole_step_radius;
  729. box_filter.setMin(Eigen::Vector4f(min_point_aabb_ex.x, min_point_aabb_ex.y, min_point_aabb_ex.z, 1));
  730. box_filter.setMax(Eigen::Vector4f(max_point_aabb_ex.x, max_point_aabb_ex.y, max_point_aabb_ex.z, 1));
  731. box_filter.setNegative(false);
  732. box_filter.setInputCloud(in_cloud);
  733. box_filter.filter(inbox_idx);
  734. pcl::copyPointCloud(*in_cloud, inbox_idx, *seedling_inbox);
  735. //点数过滤
  736. if (inbox_idx.size() < int(max_density/3)) { continue; }
  737. //y方向分布范围滤波
  738. pcl::PointXYZ min_v;
  739. pcl::PointXYZ max_v;
  740. pcl::getMinMax3D(*seedling_inbox, min_v, max_v);
  741. float dy = max_v.y - min_v.y;
  742. if (dy / (ymax - ymin) < 0.35) { continue; }
  743. //y方向分布中心滤波
  744. float dy_c = 0.5*(max_v.y + min_v.y);
  745. if ((dy_c-ymin) / (ymax - ymin) > 0.75) { continue; }
  746. //计算中心
  747. Eigen::Vector4f mean;
  748. pcl::compute3DCentroid(*seedling_inbox, mean);
  749. double cc_dist = std::sqrt((mean[0] - p.x)*(mean[0] - p.x) + (mean[2] - p.z)*(mean[2] - p.z));
  750. valid_index.push_back(i);
  751. valid_box_pts.push_back(inbox_idx.size());
  752. valid_box_cc_dist.push_back((float)cc_dist);
  753. }
  754. // 找到局部最大值,找出真正的位置
  755. std::vector<pcl::PointXYZ> clt_root_v;
  756. std::vector<pcl::PointXYZ> clt_box_v;
  757. for (auto&i : valid_index) {
  758. pcl::PointXYZ&p = clt_root.at(i);
  759. pcl::PointXYZ p_show(p.x, ymin, p.z);
  760. clt_root_v.push_back(p_show);
  761. pcl::PointXYZ min_point_aabb_ex;
  762. pcl::PointXYZ max_point_aabb_ex;
  763. min_point_aabb_ex.x = p.x - hole_step_radius;
  764. min_point_aabb_ex.y = ymin;
  765. min_point_aabb_ex.z = p.z - hole_step_radius;
  766. max_point_aabb_ex.x = p.x + hole_step_radius;
  767. max_point_aabb_ex.y = ymax;
  768. max_point_aabb_ex.z = p.z + hole_step_radius;
  769. clt_box_v.push_back(min_point_aabb_ex);
  770. clt_box_v.push_back(max_point_aabb_ex);
  771. }
  772. // 显示
  773. if (m_cparam.image_show) {
  774. std::vector<std::vector<int> > clt_line_idx;
  775. viewer_cloud_cluster_box(in_cloud, clt_root_v, clt_box_v, clt_line_idx, std::string("valid_box"));
  776. }
  777. std::vector<pcl::PointXYZ> target_root;
  778. nms_box(clt_root_v, valid_box_cc_dist, hole_step_radius, target_root);
  779. // 显示
  780. if (m_cparam.image_show) {
  781. std::vector<std::vector<int> > clt_line_idx;
  782. std::vector<pcl::PointXYZ>clt_box_tmp;
  783. for (auto&p : target_root) {
  784. pcl::PointXYZ p_show(p.x, ymin, p.z);
  785. clt_root_v.push_back(p_show);
  786. pcl::PointXYZ min_point_aabb_ex;
  787. pcl::PointXYZ max_point_aabb_ex;
  788. min_point_aabb_ex.x = p.x - hole_step_radius;
  789. min_point_aabb_ex.y = ymin;
  790. min_point_aabb_ex.z = p.z - hole_step_radius;
  791. max_point_aabb_ex.x = p.x + hole_step_radius;
  792. max_point_aabb_ex.y = ymax;
  793. max_point_aabb_ex.z = p.z + hole_step_radius;
  794. clt_box_tmp.push_back(min_point_aabb_ex);
  795. clt_box_tmp.push_back(max_point_aabb_ex);
  796. }
  797. viewer_cloud_cluster_box(in_cloud, target_root, clt_box_tmp, clt_line_idx, std::string("nms_box"));
  798. }
  799. // 根据得到的位置,直线检测,并过滤
  800. std::vector<pcl::PointXYZ>target_filtered;
  801. std::vector<std::vector<int>> target_member;//输出,茎上点index
  802. std::vector<pcl::PointXYZ>target_filtered_root;
  803. std::vector<pcl::ModelCoefficients::Ptr> target_filtered_models; //茎直线模型
  804. line_filter(in_cloud, hole_step_radius,ymin,ymax, target_root, target_filtered,
  805. target_filtered_root,
  806. target_member,
  807. target_filtered_models);
  808. if (m_cparam.image_show) {
  809. std::vector<pcl::PointXYZ> final_box;
  810. for (auto&pt : target_filtered_root) {
  811. //扩展矩形范围
  812. pcl::PointXYZ min_point_aabb_ex;
  813. pcl::PointXYZ max_point_aabb_ex;
  814. min_point_aabb_ex.x = pt.x - hole_step_radius;
  815. min_point_aabb_ex.y = ymin;
  816. min_point_aabb_ex.z = pt.z - hole_step_radius;
  817. max_point_aabb_ex.x = pt.x + hole_step_radius;
  818. max_point_aabb_ex.y = ymax;
  819. max_point_aabb_ex.z = pt.z + hole_step_radius;
  820. final_box.push_back(min_point_aabb_ex);
  821. final_box.push_back(max_point_aabb_ex);
  822. }
  823. viewer_cloud_cluster_box(in_cloud, target_filtered_root, final_box, target_member, std::string("line_filter"));
  824. }
  825. //sort cluster center, get the frontest leftest one
  826. //找最小z,然后计算平均z
  827. float min_root_z = 10000.0;
  828. for(auto&pt : target_filtered_root) {
  829. if (pt.z < min_root_z) { min_root_z = pt.z; }
  830. }
  831. //通过最小z,保守的找到和他一排的植株
  832. std::vector<int> first_row_index;
  833. float mean_z = 0.0;
  834. for (int idx_r = 0; idx_r < target_filtered_root.size();++idx_r) {
  835. pcl::PointXYZ&pt = target_filtered_root.at(idx_r);
  836. if (std::fabs(pt.z - min_root_z) < hole_step) {
  837. first_row_index.push_back(idx_r);
  838. mean_z += pt.z;
  839. }
  840. }
  841. //如果第一排的植株大于3,计算平均值
  842. float first_row_num = (float)first_row_index.size();
  843. mean_z /= first_row_num;
  844. if (first_row_num >= 4) {
  845. if (m_dtype == 0) {
  846. m_1st_row_zmean_sc = mean_z;
  847. }
  848. else{ m_1st_row_zmean_rs = mean_z; }
  849. }
  850. else {
  851. if( m_dtype == 0) {
  852. if (m_1st_row_zmean_sc > 0) { mean_z = m_1st_row_zmean_sc; }
  853. }
  854. else {
  855. if (m_1st_row_zmean_rs > 0) { mean_z = m_1st_row_zmean_rs; }
  856. }
  857. }
  858. //通过mean_z再次寻找第一排的植株
  859. first_row_index.clear();
  860. for (int idx_r = 0; idx_r < target_filtered_root.size(); ++idx_r) {
  861. pcl::PointXYZ&pt = target_filtered_root.at(idx_r);
  862. if (std::fabs(pt.z - mean_z) < hole_step) {
  863. first_row_index.push_back(idx_r);
  864. }
  865. }
  866. std::vector<float> cluster_index;
  867. for (int i=0; i<first_row_index.size();++i){
  868. int raw_idx = first_row_index.at(i);
  869. pcl::PointXYZ&pt = target_filtered_root.at(raw_idx);
  870. cluster_index.push_back(pt.x);
  871. }
  872. int first_idx = std::min_element(cluster_index.begin(), cluster_index.end()) - cluster_index.begin();
  873. if (m_dtype == 0) {
  874. first_idx = std::max_element(cluster_index.begin(), cluster_index.end()) - cluster_index.begin();
  875. }
  876. first_idx = first_row_index.at(first_idx);
  877. first_seedling_cloud_idx.clear();
  878. for (auto&v : target_member.at(first_idx)) {
  879. first_seedling_cloud_idx.push_back(v);
  880. }
  881. xz_center.x = target_filtered_root.at(first_idx).x;
  882. xz_center.y = target_filtered_root.at(first_idx).y;
  883. xz_center.z = target_filtered_root.at(first_idx).z;
  884. line_model = target_filtered_models.at(first_idx);
  885. if (m_pLogger) {
  886. stringstream buff;
  887. buff << m_pcdId << ": euclidean_clustering_ttsas, find cluster center(" << xz_center.x
  888. <<", "<< xz_center.y<<", "<< xz_center.z<<")";
  889. m_pLogger->INFO(buff.str());
  890. }
  891. }
  892. void CRootStockGrabPoint::tilted_seedling_discover(
  893. std::vector<pcl::PointXYZ>&cluster_center, //输入,已知的苗的坐标
  894. std::vector<pcl::PointXYZ>&tilted_center //输出,有倾斜苗的坐标
  895. )
  896. {
  897. tilted_center.clear();
  898. if (cluster_center.size() == 0) { return; }
  899. //找到z最小的那一株,并找出和它一排的那些
  900. float step_harf = m_cparam.rs_grab_seedling_dist / 2.0;
  901. float xmin = m_cparam.rs_grab_xmin;
  902. float xmax = m_cparam.rs_grab_xmax;
  903. float zmin = m_cparam.rs_grab_zmin;
  904. float zmax = m_cparam.rs_grab_zmax;
  905. if (m_dtype == 0) {
  906. step_harf = m_cparam.sc_grab_seedling_dist / 2.0;
  907. xmin = m_cparam.sc_grab_xmin;
  908. xmax = m_cparam.sc_grab_xmax;
  909. zmin = m_cparam.sc_grab_zmin;
  910. zmax = m_cparam.sc_grab_zmax;
  911. }
  912. auto minz_it = std::min_element(cluster_center.begin(), cluster_center.end(),
  913. [](const pcl::PointXYZ& p1, const pcl::PointXYZ& p2)-> bool {return p1.z < p2.z; });
  914. float minz = minz_it->z;
  915. float meanz = 0.0;
  916. float cnt = 0.0;
  917. for (auto&p : cluster_center) {
  918. if (fabs(p.z - minz) < step_harf) { meanz += p.z; cnt += 1.0; }
  919. }
  920. if(cnt>0.0){meanz /= cnt;}
  921. else { meanz = minz; }
  922. // 生成所有可能的植株位置中心点
  923. std::vector<pcl::PointXYZ>candidate_centers;
  924. for (int row = -1; row <= 1; row += 1) {
  925. for (int col = -6; col <= 6; col += 1) {
  926. float cx = minz_it->x + col * 2.0 * step_harf;
  927. float cz = meanz + row * 2.0 * step_harf;
  928. if (cx < xmin || cx > xmax) { continue; }
  929. if (cz < zmin || cz > zmax) { continue; }
  930. // 邻域内已经存在植株的跳过
  931. bool with_seedling = false;
  932. for (auto&p : cluster_center) {
  933. float d = std::sqrt((p.x - cx) * (p.x - cx) + (p.z - cz) * (p.z - cz));
  934. if (d < 2.0*step_harf) {
  935. with_seedling=true;
  936. break;
  937. }
  938. }
  939. if (!with_seedling) {//保存
  940. pcl::PointXYZ pc(cx,0.0,cz);
  941. candidate_centers.push_back(pc);
  942. }
  943. }
  944. }
  945. //针对每一个candidat_centers的邻域进行检查,检测是否有茎目标
  946. for (auto&pc : candidate_centers) {
  947. }
  948. }
  949. void CRootStockGrabPoint::find_seedling_position(
  950. pcl::PointCloud<pcl::PointXYZ>::Ptr in_cloud,
  951. std::vector<int> &first_seedling_cloud_idx,
  952. pcl::PointXYZ&xz_center
  953. )
  954. {
  955. // 确定植株inbox范围
  956. float hole_step = m_cparam.rs_grab_seedling_dist - 10.0; //穴盘中穴间距
  957. if (m_dtype == 0) {
  958. hole_step = m_cparam.sc_grab_seedling_dist - 10.0;
  959. }
  960. float hole_step_radius = hole_step / 2.0;
  961. // 点云降维到xz平面,y=0
  962. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud2d(new pcl::PointCloud < pcl::PointXYZ>);
  963. pcl::copyPointCloud(*in_cloud, *cloud2d);
  964. for (auto&pt : *cloud2d) {
  965. pt.y = 0.0;
  966. }
  967. /*if(m_cparam.image_show){
  968. viewer_cloud(cloud2d, std::string("cloud2d"));
  969. } */
  970. // 在xz平面内统计点的密度
  971. double radius = m_cparam.rs_grab_stem_diameter;
  972. if (m_dtype == 0) {
  973. radius = m_cparam.sc_grab_stem_diameter;
  974. }
  975. std::vector<int> counter;
  976. pcl::KdTreeFLANN<pcl::PointXYZ> kdtree;
  977. kdtree.setInputCloud(cloud2d);
  978. std::vector<int>idx;
  979. std::vector<float>dist_sqr;
  980. for (size_t i = 0; i < cloud2d->points.size(); ++i) {
  981. int k = kdtree.radiusSearch(cloud2d->points.at(i), radius, idx, dist_sqr);
  982. counter.push_back(k);
  983. }
  984. // 计算根据密度分割的自动阈值
  985. int th = (int)(otsu(counter));
  986. // 得到茎目标的点云序号,和对应的点云cloud2d_pos
  987. std::vector<int> index;
  988. for (size_t i = 0; i < counter.size(); ++i) {
  989. if (counter.at(i) >= th) {
  990. index.push_back(i);
  991. }
  992. }
  993. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud2d_pos(new pcl::PointCloud < pcl::PointXYZ>);
  994. pcl::copyPointCloud(*cloud2d, index, *cloud2d_pos);
  995. if (m_pLogger) {
  996. stringstream buff;
  997. buff << m_pcdId <<": get 2d seedling seed point cloud " << index.size() << " data points with thrreshold " << th;
  998. m_pLogger->INFO(buff.str());
  999. }
  1000. if (m_cparam.image_show) {
  1001. viewer_cloud(cloud2d_pos, std::string("cloud2d_pos"));
  1002. }
  1003. //对茎的点云进行clustering,得到不同的茎的分组
  1004. double d1 = m_cparam.rs_grab_stem_diameter;
  1005. double d2 = m_cparam.rs_grab_stem_diameter * 3.0;
  1006. if (m_dtype == 0) {
  1007. d1 = m_cparam.sc_grab_stem_diameter;
  1008. d2 = m_cparam.sc_grab_stem_diameter * 3.0;
  1009. }
  1010. std::vector<pcl::PointXYZ>cluster_center;
  1011. std::vector<std::vector<int>> cluster_member;
  1012. euclidean_clustering_ttsas(cloud2d_pos, d1, d2, cluster_center, cluster_member);
  1013. if (m_pLogger) {
  1014. stringstream buff;
  1015. buff << m_pcdId <<": euclidean_clustering_ttsas: " << cluster_center.size() << " t1=" << d1
  1016. << " t2=" << d2;
  1017. m_pLogger->INFO(buff.str());
  1018. }
  1019. if (m_cparam.image_show) {
  1020. pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_cluster(new pcl::PointCloud<pcl::PointXYZRGB>);
  1021. pcl::copyPointCloud(*cloud2d_pos, *cloud_cluster);
  1022. for (auto& pt : *cloud_cluster) {
  1023. pt.r = 255;
  1024. pt.g = 255;
  1025. pt.b = 255;
  1026. }
  1027. viewer_cloud_cluster(cloud_cluster, cluster_center, std::string("cloud2d_cluster_pos"));
  1028. }
  1029. // 扩展检测第一排的可能位置,找到倾斜的苗
  1030. std::vector<pcl::PointXYZ> tilted_center;
  1031. tilted_seedling_discover(cluster_center, tilted_center);
  1032. //对每个类别进行检测,剔除不是茎的类别
  1033. std::vector<pcl::PointXYZ> clt_root;
  1034. std::vector<pcl::PointXYZ> clt_box;
  1035. std::vector<std::vector<int> > clt_line_idx;
  1036. for (size_t i = 0; i < cluster_center.size(); ++i) {
  1037. pcl::PointIndices cluster_idxs;
  1038. for (auto idx_clt : cluster_member.at(i)) {
  1039. int idx_raw = index.at(idx_clt);
  1040. cluster_idxs.indices.push_back(idx_raw);
  1041. }
  1042. pcl::PointCloud<pcl::PointXYZ>::Ptr clt_cloud(new pcl::PointCloud < pcl::PointXYZ>);
  1043. pcl::copyPointCloud(*in_cloud, cluster_idxs, *clt_cloud);
  1044. //计算每一个茎的外接矩形
  1045. pcl::PointXYZ min_point_aabb;
  1046. pcl::PointXYZ max_point_aabb;
  1047. pcl::MomentOfInertiaEstimation<pcl::PointXYZ> feature_extractor;
  1048. feature_extractor.setInputCloud(clt_cloud);
  1049. feature_extractor.compute();
  1050. feature_extractor.getAABB(min_point_aabb, max_point_aabb);
  1051. //扩展矩形范围
  1052. pcl::PointXYZ min_point_aabb_ex;
  1053. pcl::PointXYZ max_point_aabb_ex;
  1054. min_point_aabb_ex.x = 0.5*(min_point_aabb.x + max_point_aabb.x) - hole_step_radius;
  1055. min_point_aabb_ex.y = m_cparam.sc_grab_ymin;
  1056. if (m_dtype != 0) { min_point_aabb_ex.y = m_cparam.rs_grab_ymin; }
  1057. min_point_aabb_ex.z = 0.5*(min_point_aabb.z + max_point_aabb.z) - hole_step_radius;
  1058. max_point_aabb_ex.x = 0.5*(min_point_aabb.x + max_point_aabb.x) + hole_step_radius;
  1059. max_point_aabb_ex.y = m_cparam.sc_grab_ymax;
  1060. if (m_dtype != 0) { max_point_aabb_ex.y = m_cparam.rs_grab_ymax; }
  1061. max_point_aabb_ex.z = 0.5*(min_point_aabb.z + max_point_aabb.z) + hole_step_radius;
  1062. /////////////////////////////////
  1063. //扩展矩形内直线检测
  1064. std::vector<int>line_idx;
  1065. find_line_in_cube(in_cloud, min_point_aabb_ex, max_point_aabb_ex, line_idx);
  1066. clt_line_idx.push_back(line_idx);
  1067. //找到line_idx中y最小的那个点的idx
  1068. int line_root_idx = -1;
  1069. float line_root_y_min = 10000.0;
  1070. for (size_t lidx = 0; lidx < line_idx.size(); ++lidx) {
  1071. int raw_idx = line_idx.at(lidx);
  1072. if (in_cloud->at(raw_idx).y < line_root_y_min) {
  1073. line_root_y_min = in_cloud->at(raw_idx).y;
  1074. line_root_idx = raw_idx;
  1075. }
  1076. }
  1077. //找到底部中心点,然后找到根部坐标
  1078. pcl::PointXYZ pt_root;
  1079. pt_root = in_cloud->at(line_root_idx);
  1080. clt_root.push_back(pt_root);
  1081. clt_box.push_back(min_point_aabb_ex);
  1082. clt_box.push_back(max_point_aabb_ex);
  1083. //viewer_cloud(clt_cloud, std::string("cluster"));
  1084. }
  1085. if (m_cparam.image_show) {
  1086. viewer_cloud_cluster_box(in_cloud, clt_root, clt_box, clt_line_idx, std::string("cloud2d_cluster_box"));
  1087. }
  1088. //sort cluster center, get the frontest leftest one
  1089. std::vector<float> cluster_index;
  1090. for (auto&pt : clt_root) {
  1091. float idx = pt.x - pt.z;
  1092. cluster_index.push_back(idx);
  1093. }
  1094. int first_idx = std::min_element(cluster_index.begin(), cluster_index.end()) - cluster_index.begin();
  1095. if (m_dtype == 0) {
  1096. first_idx = std::max_element(cluster_index.begin(), cluster_index.end()) - cluster_index.begin();
  1097. }
  1098. first_seedling_cloud_idx.clear();
  1099. for (auto&v : clt_line_idx.at(first_idx)) {
  1100. first_seedling_cloud_idx.push_back(v);
  1101. }
  1102. xz_center.x = clt_root.at(first_idx).x;
  1103. xz_center.y = clt_root.at(first_idx).y;
  1104. xz_center.z = clt_root.at(first_idx).z;
  1105. if (m_pLogger) {
  1106. stringstream buff;
  1107. buff << m_pcdId <<": euclidean_clustering_ttsas, find cluster center(" << xz_center.x
  1108. << ", " << xz_center.y << ", " << xz_center.z << ")";
  1109. m_pLogger->INFO(buff.str());
  1110. }
  1111. }
  1112. void CRootStockGrabPoint::find_line_in_cube(
  1113. pcl::PointCloud<pcl::PointXYZ>::Ptr in_cloud, //输入,整体点云
  1114. pcl::PointXYZ& min_pt_ex, //输入,
  1115. pcl::PointXYZ& max_pt_ex, //输入,
  1116. std::vector<int>& out_idx //输出,直线上点的index, 基于输入整体点云
  1117. )
  1118. {
  1119. out_idx.clear();
  1120. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_inbox(new pcl::PointCloud<pcl::PointXYZ>);
  1121. pcl::CropBox<pcl::PointXYZ> box_filter;
  1122. std::vector<int>inbox_idx;
  1123. box_filter.setMin(Eigen::Vector4f(min_pt_ex.x, min_pt_ex.y, min_pt_ex.z, 1));
  1124. box_filter.setMax(Eigen::Vector4f(max_pt_ex.x, max_pt_ex.y, max_pt_ex.z, 1));
  1125. box_filter.setNegative(false);
  1126. box_filter.setInputCloud(in_cloud);
  1127. box_filter.filter(inbox_idx);
  1128. pcl::copyPointCloud(*in_cloud, inbox_idx, *cloud_inbox);
  1129. //找到inbox点云中的直线
  1130. float stem_radius = m_cparam.rs_grab_stem_diameter / 2.0;
  1131. if (m_dtype == 0) {
  1132. stem_radius = m_cparam.sc_grab_stem_diameter / 2.0;
  1133. }
  1134. pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients);
  1135. pcl::PointIndices::Ptr inliers(new pcl::PointIndices);
  1136. pcl::SACSegmentation<pcl::PointXYZ> seg;
  1137. seg.setOptimizeCoefficients(true);
  1138. seg.setModelType(pcl::SACMODEL_LINE);
  1139. seg.setDistanceThreshold(stem_radius);
  1140. seg.setInputCloud(cloud_inbox);
  1141. seg.segment(*inliers, *coefficients);
  1142. int idx_raw = 0;
  1143. for (auto& idx : inliers->indices) {
  1144. idx_raw = inbox_idx.at(idx);
  1145. out_idx.push_back(idx_raw);
  1146. }
  1147. if (m_cparam.image_show) {
  1148. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_line(new pcl::PointCloud<pcl::PointXYZ>);
  1149. pcl::copyPointCloud(*cloud_inbox, *inliers, *cloud_line);
  1150. pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("line in cluster"));
  1151. viewer->addPointCloud<pcl::PointXYZ>(cloud_inbox, "cluster");
  1152. viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 1, 1, 1, "cluster");
  1153. viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "cluster");
  1154. viewer->addPointCloud(cloud_line, "fit_line");
  1155. viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 0, 1, 0, "fit_line");
  1156. viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "fit_line");
  1157. while (!viewer->wasStopped()) {
  1158. viewer->spinOnce(100);
  1159. }
  1160. }
  1161. }
  1162. void CRootStockGrabPoint::crop_nn_analysis(
  1163. pcl::PointCloud<pcl::PointXYZ>::Ptr in_cloud,
  1164. pcl::PointCloud<pcl::PointXYZ>::Ptr seed_cloud,
  1165. double dist_mean,
  1166. std::vector<double>&mass_indices,
  1167. std::vector<int>& candidate_idx
  1168. )
  1169. {
  1170. candidate_idx.clear();
  1171. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_inbox(new pcl::PointCloud<pcl::PointXYZ>);
  1172. pcl::CropBox<pcl::PointXYZ> box_filter;
  1173. box_filter.setNegative(false);
  1174. box_filter.setInputCloud(in_cloud);
  1175. double radius = 5;
  1176. double rx = 0.8;
  1177. double ry = 1.0;
  1178. double rz = 0.8;
  1179. double cx, cy, cz;
  1180. double dz;
  1181. for (size_t i = 0; i < seed_cloud->points.size(); ++i) {
  1182. cx = seed_cloud->points.at(i).x;
  1183. cy = seed_cloud->points.at(i).y;
  1184. cz = seed_cloud->points.at(i).z;
  1185. box_filter.setMin(Eigen::Vector4f(cx - rx*radius, cy - ry*radius, cz - rz*radius, 1));
  1186. box_filter.setMax(Eigen::Vector4f(cx + rx*radius, cy + ry*radius, cz + rz*radius, 1));
  1187. box_filter.filter(*cloud_inbox);
  1188. //dz
  1189. Eigen::Vector4f min_point;
  1190. Eigen::Vector4f max_point;
  1191. pcl::getMinMax3D(*cloud_inbox, min_point, max_point);
  1192. dz = max_point(2) - min_point(2);
  1193. //project to xy-plane
  1194. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_inbox_xy(new pcl::PointCloud<pcl::PointXYZ>);
  1195. pcl::copyPointCloud(*cloud_inbox, *cloud_inbox_xy);
  1196. for (auto&pt : *cloud_inbox_xy) { pt.z = 0.0; }
  1197. //pca
  1198. double dx_obb;
  1199. double dy_obb;
  1200. double angle_obb;
  1201. cal_obb_2d(cloud_inbox_xy, 0, dx_obb, dy_obb, angle_obb);
  1202. try {
  1203. double dx_grid = dx_obb / dist_mean;
  1204. double dy_grid = dy_obb / dist_mean;
  1205. double dz_grid = dz / dist_mean;
  1206. double fill_ratio = cloud_inbox->points.size() / dx_grid / dy_grid / dz_grid;
  1207. double y_ratio = dy_obb / dx_obb/2;
  1208. y_ratio = pow(y_ratio, 2);
  1209. double a_ratio = cos((angle_obb - 90)*PI / 180.0);
  1210. a_ratio = pow(a_ratio, 3);
  1211. double mass_index = fill_ratio*y_ratio*a_ratio;
  1212. mass_indices.push_back(mass_index);
  1213. if (m_pLogger) {
  1214. stringstream buff;
  1215. buff << i << "/" << seed_cloud->points.size() << " dx=" << dx_obb << ", dy=" << dy_obb << ", fill_ratio=" << fill_ratio
  1216. << ", y_ratio=" << y_ratio << ", a_ratio=" << a_ratio << ", mass_index=" << mass_index;
  1217. m_pLogger->INFO(buff.str());
  1218. }
  1219. }
  1220. catch (...) {
  1221. mass_indices.push_back(0);
  1222. }
  1223. }
  1224. // sort by mass_indices
  1225. std::vector<size_t> sort_idx = sort_indexes_e(mass_indices, false);
  1226. for (auto&v : sort_idx) { candidate_idx.push_back((int)v); }
  1227. }
  1228. void CRootStockGrabPoint::euclidean_clustering_ttsas(
  1229. pcl::PointCloud<pcl::PointXYZ>::Ptr in_cloud,
  1230. double d1, double d2,
  1231. std::vector<pcl::PointXYZ>&cluster_center,
  1232. std::vector<std::vector<int>> &clustr_member
  1233. )
  1234. {
  1235. if (m_pLogger) {
  1236. stringstream buff;
  1237. buff << m_pcdId <<": euclidean_clustering_ttsas() begin...";
  1238. m_pLogger->INFO(buff.str());
  1239. }
  1240. std::vector<int> cluster_weight;
  1241. std::vector<int> data_stat;
  1242. std::vector<pcl::PointXYZ>cluster_center_raw;
  1243. std::vector<std::vector<int>> clustr_member_raw;
  1244. for (size_t i = 0; i < in_cloud->points.size(); ++i) { data_stat.push_back(0); }
  1245. size_t data_len = in_cloud->points.size();
  1246. int exists_change = 0;
  1247. int prev_change = 0;
  1248. int cur_change = 0;
  1249. int m = 0;
  1250. while (std::find(data_stat.begin(), data_stat.end(), 0) != data_stat.end()) {
  1251. bool new_while_first = true;
  1252. for (size_t i = 0; i < data_len; ++i) {
  1253. if (data_stat.at(i) == 0 && new_while_first && exists_change == 0) {
  1254. new_while_first = false;
  1255. std::vector<int> idx;
  1256. idx.push_back(i);
  1257. clustr_member_raw.push_back(idx);
  1258. pcl::PointXYZ center;
  1259. center.x = in_cloud->points.at(i).x;
  1260. center.y = in_cloud->points.at(i).y;
  1261. center.z = in_cloud->points.at(i).z;
  1262. cluster_center_raw.push_back(center);
  1263. data_stat.at(i) = 1;
  1264. cur_change += 1;
  1265. cluster_weight.push_back(1);
  1266. m += 1;
  1267. }
  1268. else if (data_stat[i] == 0) {
  1269. std::vector<float> distances;
  1270. for (size_t j = 0; j < clustr_member_raw.size(); ++j) {
  1271. std::vector<float> distances_sub;
  1272. for (size_t jj = 0; jj < clustr_member_raw.at(j).size(); ++jj) {
  1273. size_t ele_idx = clustr_member_raw.at(j).at(jj);
  1274. double d = sqrt(
  1275. (in_cloud->points.at(i).x - in_cloud->points.at(ele_idx).x) * (in_cloud->points.at(i).x - in_cloud->points.at(ele_idx).x) +
  1276. (in_cloud->points.at(i).y - in_cloud->points.at(ele_idx).y) * (in_cloud->points.at(i).y - in_cloud->points.at(ele_idx).y) +
  1277. (in_cloud->points.at(i).z - in_cloud->points.at(ele_idx).z) * (in_cloud->points.at(i).z - in_cloud->points.at(ele_idx).z));
  1278. distances_sub.push_back(d);
  1279. }
  1280. double min_dist = *std::min_element(distances_sub.begin(), distances_sub.end());
  1281. distances.push_back(min_dist);
  1282. }
  1283. int min_idx = std::min_element(distances.begin(), distances.end()) - distances.begin();
  1284. if (distances.at(min_idx) < d1) {
  1285. data_stat.at(i) = 1;
  1286. double w = cluster_weight.at(min_idx);
  1287. cluster_weight.at(min_idx) += 1;
  1288. clustr_member_raw.at(min_idx).push_back(i);
  1289. cluster_center_raw.at(min_idx).x = (cluster_center_raw.at(min_idx).x * w + in_cloud->points.at(i).x) / (w + 1);
  1290. cluster_center_raw.at(min_idx).y = (cluster_center_raw.at(min_idx).y * w + in_cloud->points.at(i).y) / (w + 1);
  1291. cluster_center_raw.at(min_idx).z = (cluster_center_raw.at(min_idx).z * w + in_cloud->points.at(i).z) / (w + 1);
  1292. cur_change += 1;
  1293. }
  1294. else if (distances.at(min_idx) > d2) {
  1295. std::vector<int> idx;
  1296. idx.push_back(i);
  1297. clustr_member_raw.push_back(idx);
  1298. pcl::PointXYZ center;
  1299. center.x = in_cloud->points.at(i).x;
  1300. center.y = in_cloud->points.at(i).y;
  1301. center.z = in_cloud->points.at(i).z;
  1302. cluster_center_raw.push_back(center);
  1303. data_stat.at(i) = 1;
  1304. cur_change += 1;
  1305. cluster_weight.push_back(1);
  1306. m += 1;
  1307. }
  1308. }
  1309. else if (data_stat.at(i)== 1) {
  1310. cur_change += 1;
  1311. }
  1312. }
  1313. exists_change = fabs(cur_change - prev_change);
  1314. prev_change = cur_change;
  1315. cur_change = 0;
  1316. }
  1317. // copy result
  1318. for (size_t i = 0; i < clustr_member_raw.size(); ++i) {
  1319. if (clustr_member_raw.at(i).size() < 20) { continue; }
  1320. cluster_center.push_back(cluster_center_raw.at(i));
  1321. clustr_member.push_back(clustr_member_raw.at(i));
  1322. }
  1323. if (m_pLogger) {
  1324. stringstream buff;
  1325. buff << m_pcdId <<": euclidean_clustering_ttsas() end";
  1326. m_pLogger->INFO(buff.str());
  1327. }
  1328. }
  1329. void CRootStockGrabPoint::cal_obb_2d(
  1330. pcl::PointCloud<pcl::PointXYZ>::Ptr in_cloud,
  1331. int axis, //0--xy, 1--zy
  1332. double &dx_obb,
  1333. double &dy_obb,
  1334. double &angle_obb
  1335. )
  1336. {
  1337. // asign value
  1338. Eigen::MatrixXd pts(in_cloud->points.size(), 2);
  1339. for (size_t i = 0; i < in_cloud->points.size(); ++i) {
  1340. if (axis == 0) {
  1341. pts(i, 0) = in_cloud->points.at(i).x;
  1342. }
  1343. else {
  1344. pts(i, 0) = in_cloud->points.at(i).z;
  1345. }
  1346. pts(i, 1) = in_cloud->points.at(i).y;
  1347. }
  1348. // centerlize
  1349. Eigen::MatrixXd mu = pts.colwise().mean();
  1350. Eigen::RowVectorXd mu_row = mu;
  1351. pts.rowwise() -= mu_row;
  1352. //calculate covariance
  1353. Eigen::MatrixXd C = pts.adjoint()*pts;
  1354. C = C.array() / (pts.cols() - 1);
  1355. //compute eig
  1356. Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> eig(C);
  1357. Eigen::MatrixXd d = eig.eigenvalues();
  1358. Eigen::MatrixXd v = eig.eigenvectors();
  1359. //projection
  1360. Eigen::MatrixXd p = pts * v;
  1361. Eigen::VectorXd minv = p.colwise().minCoeff();
  1362. Eigen::VectorXd maxv = p.colwise().maxCoeff();
  1363. Eigen::VectorXd range = maxv - minv;
  1364. dy_obb = range(1);
  1365. dx_obb = range(0);
  1366. angle_obb = std::atan2(v(1, 1), v(0, 1));
  1367. if (angle_obb < 0) { angle_obb = PI + angle_obb; }
  1368. angle_obb = angle_obb * 180 / PI;
  1369. }
  1370. //void CRootStockGrabPoint::get_optimal_seed(
  1371. // pcl::PointCloud<pcl::PointXYZ>::Ptr in_cloud,
  1372. // pcl::PointXYZ&pt,
  1373. // int &pt_idx)
  1374. //{
  1375. // double d1 = m_cparam.rs_grab_stem_diameter;
  1376. // double d2 = m_cparam.rs_grab_stem_diameter * 4.0;
  1377. // if (m_dtype != 0) {
  1378. // d1 = m_cparam.sc_grab_stem_diameter;
  1379. // d2 = m_cparam.sc_grab_stem_diameter * 4.0;
  1380. // }
  1381. // std::vector<pcl::PointXYZ>cluster_center;
  1382. // std::vector<std::vector<int>> cluster_member;
  1383. // euclidean_clustering_ttsas(in_cloud, d1, d2, cluster_center, cluster_member);
  1384. // float min_y_dist = 1.0e6;
  1385. // int opt_idx = -1;
  1386. // int member_size = 5;
  1387. // float y_opt = m_cparam.rs_grab_y_opt;
  1388. // if (m_dtype != 0) {
  1389. // y_opt = m_cparam.sc_grab_y_opt;
  1390. // }
  1391. // for (int i = 0; i < cluster_member.size(); ++i) {
  1392. // if (cluster_member.at(i).size() < member_size) {
  1393. // continue;
  1394. // }
  1395. // if (fabs(cluster_center.at(i).y -y_opt) < min_y_dist) {
  1396. // min_y_dist = fabs(cluster_center.at(i).y - y_opt);
  1397. // opt_idx = i;
  1398. // }
  1399. // }
  1400. // if (opt_idx < 0) {
  1401. // if (m_pLogger) {
  1402. // stringstream buff;
  1403. // buff << "get_optimal_seed failed, get invalid optimal cluster id";
  1404. // m_pLogger->ERRORINFO(buff.str());
  1405. // }
  1406. // return;
  1407. // }
  1408. // //find nearest pt
  1409. // float nearest_dist = 1.0e6;
  1410. // int nearest_idx = -1;
  1411. // for (int i = 0; i < cluster_member.at(opt_idx).size(); ++i) {
  1412. // int idx = cluster_member.at(opt_idx).at(i);
  1413. // float dist = fabs(in_cloud->points.at(idx).x - cluster_center.at(opt_idx).x) +
  1414. // fabs(in_cloud->points.at(idx).y - cluster_center.at(opt_idx).y) +
  1415. // fabs(in_cloud->points.at(idx).z - cluster_center.at(opt_idx).z);
  1416. // if (dist < nearest_dist) {
  1417. // nearest_dist = dist;
  1418. // nearest_idx = idx;
  1419. // }
  1420. // }
  1421. // pt.x = in_cloud->points.at(nearest_idx).x;
  1422. // pt.y = in_cloud->points.at(nearest_idx).y;
  1423. // pt.z = in_cloud->points.at(nearest_idx).z;
  1424. // pt_idx = nearest_idx;
  1425. //}
  1426. void CRootStockGrabPoint::get_optimal_seed(
  1427. pcl::PointCloud<pcl::PointXYZ>::Ptr in_cloud,
  1428. pcl::PointXYZ&pt,
  1429. int &pt_idx)
  1430. {
  1431. /*double d1 = m_cparam.rs_grab_stem_diameter;
  1432. double d2 = m_cparam.rs_grab_stem_diameter * 4.0;
  1433. if (m_dtype != 0) {
  1434. d1 = m_cparam.sc_grab_stem_diameter;
  1435. d2 = m_cparam.sc_grab_stem_diameter * 4.0;
  1436. }
  1437. std::vector<pcl::PointXYZ>cluster_center;
  1438. std::vector<std::vector<int>> cluster_member;
  1439. euclidean_clustering_ttsas(in_cloud, d1, d2, cluster_center, cluster_member);*/
  1440. float min_y_dist = 1.0e6;
  1441. int opt_idx = -1;
  1442. int member_size = 5;
  1443. float y_opt = m_cparam.rs_grab_y_opt;
  1444. if (m_dtype == 0) {
  1445. y_opt = m_cparam.sc_grab_y_opt;
  1446. }
  1447. for (int i = 0; i < in_cloud->points.size(); ++i) {
  1448. /*if (cluster_member.at(i).size() < member_size) {
  1449. continue;
  1450. }*/
  1451. if (fabs(in_cloud->points.at(i).y - y_opt) < min_y_dist) {
  1452. min_y_dist = fabs(in_cloud->points.at(i).y - y_opt);
  1453. opt_idx = i;
  1454. }
  1455. }
  1456. if (opt_idx < 0) {
  1457. if (m_pLogger) {
  1458. stringstream buff;
  1459. buff << m_pcdId <<": get_optimal_seed failed, get invalid optimal cluster id";
  1460. m_pLogger->ERRORINFO(buff.str());
  1461. }
  1462. return;
  1463. }
  1464. //find nearest pt
  1465. /*float nearest_dist = 1.0e6;
  1466. int nearest_idx = -1;
  1467. for (int i = 0; i < cluster_member.at(opt_idx).size(); ++i) {
  1468. int idx = cluster_member.at(opt_idx).at(i);
  1469. float dist = fabs(in_cloud->points.at(idx).x - cluster_center.at(opt_idx).x) +
  1470. fabs(in_cloud->points.at(idx).y - cluster_center.at(opt_idx).y) +
  1471. fabs(in_cloud->points.at(idx).z - cluster_center.at(opt_idx).z);
  1472. if (dist < nearest_dist) {
  1473. nearest_dist = dist;
  1474. nearest_idx = idx;
  1475. }
  1476. }*/
  1477. pt.x = in_cloud->points.at(opt_idx).x;
  1478. pt.y = in_cloud->points.at(opt_idx).y;
  1479. pt.z = in_cloud->points.at(opt_idx).z;
  1480. pt_idx = opt_idx;
  1481. }
  1482. //通过比较直线点云和原始点云相同位置邻域内xz的范围,确定此点是否是无干扰点的茎
  1483. //void CRootStockGrabPoint::get_optimal_seed_compare(
  1484. // pcl::PointCloud<pcl::PointXYZ>::Ptr in_cloud, // 全部有效点云
  1485. // pcl::PointCloud<pcl::PointXYZ>::Ptr in_line_cloud, // 茎上直线点云
  1486. // pcl::PointXYZ&pt, // 返回抓取的点坐标
  1487. // int &pt_idx, // 返回点index
  1488. // std::vector<int>& valid_line_index // 返回有效直线点index
  1489. //)
  1490. //{
  1491. // valid_line_index.clear();
  1492. // float th = 0.75; //原始点云和直线点云邻域尺寸增加原来的20%,不能当做抓取点
  1493. // pt_idx = -1;
  1494. // pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_inbox(new pcl::PointCloud<pcl::PointXYZ>);
  1495. // pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_inbox_line(new pcl::PointCloud<pcl::PointXYZ>);
  1496. // pcl::CropBox<pcl::PointXYZ> box_filter;
  1497. // box_filter.setNegative(false);
  1498. // box_filter.setInputCloud(in_cloud);
  1499. // pcl::CropBox<pcl::PointXYZ> box_filter_line;
  1500. // box_filter_line.setNegative(false);
  1501. // box_filter_line.setInputCloud(in_line_cloud);
  1502. // float radius = m_cparam.rs_grab_stem_diameter;
  1503. // float opt_y = m_cparam.rs_grab_y_opt;
  1504. // if (m_dtype == 0) {
  1505. // radius = m_cparam.sc_grab_stem_diameter;
  1506. // opt_y = m_cparam.sc_grab_y_opt;
  1507. // }
  1508. // float rx = 1.5;
  1509. // float ry = 1.5;
  1510. // float rz = 1.5;
  1511. // float cx, cy, cz;
  1512. // float dz,dx, dz_line, dx_line;
  1513. // float dist_min = 10000.0;
  1514. // for (size_t i = 0; i < in_line_cloud->points.size(); ++i) {
  1515. // cx = in_line_cloud->points.at(i).x;
  1516. // cy = in_line_cloud->points.at(i).y;
  1517. // cz = in_line_cloud->points.at(i).z;
  1518. // //////////////////////////////////////////////////////////////////
  1519. // //原始点云,获取指定区域的dx,dz
  1520. // box_filter.setMin(Eigen::Vector4f(cx - rx*radius, cy - ry*radius, cz - rz*radius, 1));
  1521. // box_filter.setMax(Eigen::Vector4f(cx + rx*radius, cy + ry*radius, cz + rz*radius, 1));
  1522. // box_filter.filter(*cloud_inbox);
  1523. // Eigen::Vector4f min_point;
  1524. // Eigen::Vector4f max_point;
  1525. // pcl::getMinMax3D(*cloud_inbox, min_point, max_point);
  1526. // dz = max_point(2) - min_point(2);
  1527. // dx = max_point(0) - min_point(0);
  1528. // /////////////////////////////////////////////////////////////////////
  1529. // //直线点云,获取指定区域的dx_line,dz_line
  1530. // box_filter_line.setMin(Eigen::Vector4f(cx - rx*radius, cy - ry*radius, cz - rz*radius, 1));
  1531. // box_filter_line.setMax(Eigen::Vector4f(cx + rx*radius, cy + ry*radius, cz + rz*radius, 1));
  1532. // box_filter_line.filter(*cloud_inbox_line);
  1533. // pcl::getMinMax3D(*cloud_inbox_line, min_point, max_point);
  1534. // dz_line = max_point(2) - min_point(2);
  1535. // dx_line = max_point(0) - min_point(0);
  1536. // float ratio_ex = (dx + dz - dz_line - dx_line) / (dz_line + dx_line);
  1537. // if (ratio_ex > th) {
  1538. // valid_line_index.push_back(i);
  1539. // if (fabs(cy - opt_y) < dist_min) {
  1540. // dist_min = fabs(cy - opt_y);
  1541. // pt_idx = i;
  1542. // }
  1543. // }
  1544. // }
  1545. // if (pt_idx >= 0) {
  1546. // pt = in_line_cloud->points.at(pt_idx);
  1547. // }
  1548. //
  1549. //}
  1550. void CRootStockGrabPoint::get_optimal_seed_compare(
  1551. pcl::PointCloud<pcl::PointXYZ>::Ptr in_cloud, //input 全部有效点云
  1552. pcl::PointCloud<pcl::PointXYZ>::Ptr in_line_cloud, //input 茎上直线点云
  1553. pcl::ModelCoefficients::Ptr line_model, //input
  1554. pcl::PointXYZ&pt, // 返回抓取的点坐标
  1555. int &pt_idx, // 返回点index
  1556. std::vector<int>& valid_line_index // 返回有效直线点index
  1557. )
  1558. {
  1559. valid_line_index.clear();
  1560. float th_ratio = 1.0; //原始点云和直线点云邻域尺寸增加原来的20%,不能当做抓取点
  1561. pt_idx = -1;
  1562. float ymin, ymax;
  1563. ymin = m_cparam.rs_grab_ymin;
  1564. ymax = m_cparam.rs_grab_ymax;
  1565. if (m_dtype == 0) {
  1566. ymin = m_cparam.sc_grab_ymin;
  1567. ymax = m_cparam.sc_grab_ymax;
  1568. }
  1569. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_inbox(new pcl::PointCloud<pcl::PointXYZ>);
  1570. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_inbox_line(new pcl::PointCloud<pcl::PointXYZ>);
  1571. std::vector<float> stem_width;
  1572. std::vector<pcl::PointXYZ>online_points;
  1573. pcl::CropBox<pcl::PointXYZ> box_filter;
  1574. box_filter.setNegative(false);
  1575. box_filter.setInputCloud(in_cloud);
  1576. pcl::CropBox<pcl::PointXYZ> box_filter_line;
  1577. box_filter_line.setNegative(false);
  1578. box_filter_line.setInputCloud(in_line_cloud);
  1579. float radius = m_cparam.rs_grab_stem_diameter;
  1580. float opt_y = m_cparam.rs_grab_y_opt;
  1581. if (m_dtype == 0) {
  1582. radius = m_cparam.sc_grab_stem_diameter;
  1583. opt_y = m_cparam.sc_grab_y_opt;
  1584. }
  1585. float rx = 1.5;
  1586. float ry = 1.5;
  1587. float rz = 1.5;
  1588. float cx, cy, cz;
  1589. float dz, dx;
  1590. //float dz,dx, dz_line, dx_line;
  1591. //float dist_min = 10000.0;
  1592. //for (size_t i = 0; i < in_line_cloud->points.size(); ++i) {
  1593. cy = ymin;
  1594. float t = (cy - line_model->values.at(1)) / line_model->values.at(4);
  1595. while(cy<ymax){
  1596. cx = line_model->values.at(3) * t + line_model->values.at(0);
  1597. cz = line_model->values.at(5) * t + line_model->values.at(2);
  1598. //////////////////////////////////////////////////////////////////
  1599. //原始点云,获取指定区域的dx,dz
  1600. box_filter.setMin(Eigen::Vector4f(cx - rx*radius, cy - 2, cz - rz*radius, 1));
  1601. box_filter.setMax(Eigen::Vector4f(cx + rx*radius, cy + 2, cz + rz*radius, 1));
  1602. box_filter.filter(*cloud_inbox);
  1603. if (cloud_inbox->points.size() > 10) {
  1604. Eigen::Vector4f min_point;
  1605. Eigen::Vector4f max_point;
  1606. pcl::getMinMax3D(*cloud_inbox, min_point, max_point);
  1607. dz = max_point(2) - min_point(2);
  1608. dx = max_point(0) - min_point(0);
  1609. if (dx > dz) {
  1610. stem_width.push_back(dx);
  1611. }
  1612. else {
  1613. stem_width.push_back(dz);
  1614. }
  1615. }
  1616. else {
  1617. stem_width.push_back(0);
  1618. }
  1619. pcl::PointXYZ tmp_pt(cx,cy,cz);
  1620. online_points.push_back(tmp_pt);
  1621. cy += 1.0;
  1622. }
  1623. std::vector<float>valid_stem_width;
  1624. for (auto&w : stem_width) { valid_stem_width.push_back(w); }
  1625. float mu = get_hist_mean(valid_stem_width);
  1626. float stdv = get_hist_std(valid_stem_width, mu);
  1627. int max_pos = std::max_element(stem_width.begin(), stem_width.end()) - stem_width.begin();
  1628. float max_val = stem_width.at(max_pos);
  1629. float th = mu + th_ratio * stdv;
  1630. if (th < max_val) {
  1631. int i = 0;
  1632. for (; i < stem_width.size(); ++i) {
  1633. if (stem_width.at(i) >= th) {
  1634. break;
  1635. }
  1636. }
  1637. max_pos = i;
  1638. }
  1639. /////////////////////////////////////////////////////////////////////
  1640. //直线点云,获取指定区域的dx_line,dz_line
  1641. Eigen::Vector4f min_point;
  1642. Eigen::Vector4f max_point;
  1643. cx = online_points.at(max_pos).x;
  1644. cy = online_points.at(max_pos).y;
  1645. cz = online_points.at(max_pos).z;
  1646. box_filter_line.setMin(Eigen::Vector4f(cx - rx*radius, cy - 2, cz - rz*radius, 1));
  1647. box_filter_line.setMax(Eigen::Vector4f(cx + rx*radius, cy + 2, cz + rz*radius, 1));
  1648. box_filter_line.filter(*cloud_inbox_line);
  1649. pcl::getMinMax3D(*cloud_inbox_line, min_point, max_point);
  1650. float z_mu = 0.5 * (max_point(2) + min_point(2));
  1651. float x_mu = 0.5 * (max_point(0) + min_point(0));
  1652. pt.x = x_mu;
  1653. pt.y = cy;
  1654. pt.z = z_mu;
  1655. pt_idx = max_pos;
  1656. }
  1657. void CRootStockGrabPoint::viewer_cloud(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud, std::string&winname)
  1658. {
  1659. pcl::visualization::CloudViewer viewer(winname);
  1660. //viewer.runOnVisualizationThreadOnce(viewerOneOff);
  1661. viewer.showCloud(cloud);
  1662. while (!viewer.wasStopped()) {
  1663. boost::this_thread::sleep(boost::posix_time::microseconds(100000));
  1664. }
  1665. }
  1666. void CRootStockGrabPoint::viewer_cloud(pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud, std::string&winname)
  1667. {
  1668. pcl::visualization::CloudViewer viewer(winname);
  1669. //viewer.runOnVisualizationThreadOnce(viewerOneOff);
  1670. viewer.showCloud(cloud);
  1671. while (!viewer.wasStopped()) {
  1672. boost::this_thread::sleep(boost::posix_time::microseconds(100000));
  1673. }
  1674. }
  1675. void CRootStockGrabPoint::viewer_cloud_debug(
  1676. pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud,
  1677. pcl::PointXYZ &p,
  1678. pcl::PointXYZ &root_pt,
  1679. std::string&winname)
  1680. {
  1681. pcl::visualization::PCLVisualizer viewer(winname);
  1682. //viewer.runOnVisualizationThreadOnce(viewerOneOff);
  1683. viewer.addPointCloud(cloud);
  1684. viewer.addCoordinateSystem();
  1685. pcl::PointXYZ p0, x1, y1,p00,x0,y0, root_px0, root_px1, root_py0,root_py1;
  1686. p0.x = p.x;
  1687. p0.y = p.y;
  1688. p0.z = p.z;
  1689. x1.x = p.x + 100.0;
  1690. x1.y = p.y;
  1691. x1.z = p.z;
  1692. y1.x = p.x;
  1693. y1.y = p.y + 20.0;
  1694. y1.z = p.z;
  1695. p00.x = 0.0;
  1696. p00.y = 0.0;
  1697. p00.z = p.z;
  1698. x0.x = 600.0;
  1699. x0.y = 0;
  1700. x0.z = p.z;
  1701. y0.x = 0.0;
  1702. y0.y = 300.0;
  1703. y0.z = p.z;
  1704. root_px0.x = root_pt.x - 5.0;
  1705. root_px0.y = root_pt.y;
  1706. root_px0.z = root_pt.z;
  1707. root_px1.x = root_pt.x + 5.0;
  1708. root_px1.y = root_pt.y;
  1709. root_px1.z = root_pt.z;
  1710. root_py0.x = root_pt.x;
  1711. root_py0.y = root_pt.y - 5.0;
  1712. root_py0.z = root_pt.z;
  1713. root_py1.x = root_pt.x;
  1714. root_py1.y = root_pt.y + 5.0;
  1715. root_py1.z = root_pt.z;
  1716. viewer.addLine(p0, x1, 255, 0, 0, "x");
  1717. viewer.addLine(p0, y1, 0, 255, 0, "y");
  1718. viewer.addLine(p00, x0, 255, 0, 0, "x0");
  1719. viewer.addLine(p00, y0, 0, 255, 0, "y0");
  1720. viewer.addLine(root_px0, root_px1, 255, 0, 0, "rootx");
  1721. viewer.addLine(root_py0, root_py1, 0, 255, 0, "rooty");
  1722. while (!viewer.wasStopped()) {
  1723. viewer.spinOnce(100);
  1724. boost::this_thread::sleep(boost::posix_time::microseconds(100000));
  1725. }
  1726. }
  1727. void CRootStockGrabPoint::viewer_cloud_cluster(
  1728. pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud,
  1729. std::vector<pcl::PointXYZ>cluster_center,
  1730. std::string&winname)
  1731. {
  1732. pcl::visualization::PCLVisualizer viewer(winname);
  1733. viewer.addPointCloud(cloud);
  1734. viewer.addCoordinateSystem();
  1735. int cnt = 0;
  1736. char buf[8];
  1737. for (auto& p : cluster_center) {
  1738. pcl::PointXYZ px0, px1, py1, py0;
  1739. px0.x = p.x-5.0;
  1740. px0.y = p.y;
  1741. px0.z = p.z;
  1742. px1.x = p.x + 5.0;
  1743. px1.y = p.y;
  1744. px1.z = p.z;
  1745. py0.x = p.x;
  1746. py0.y = p.y - 5.0;
  1747. py0.z = p.z;
  1748. py1.x = p.x;
  1749. py1.y = p.y + 5.0;
  1750. py1.z = p.z;
  1751. viewer.addLine(px0, px1, 255, 0, 0, "x" + string(_itoa(cnt, buf,10)));
  1752. viewer.addLine(py0, py1, 0, 255, 0, "y" + string(_itoa(cnt, buf, 10)));
  1753. cnt += 1;
  1754. }
  1755. while (!viewer.wasStopped()) {
  1756. viewer.spinOnce(100);
  1757. boost::this_thread::sleep(boost::posix_time::microseconds(100000));
  1758. }
  1759. }
  1760. void CRootStockGrabPoint::viewer_cloud_cluster_box(
  1761. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud,
  1762. std::vector<pcl::PointXYZ>&cluster_center,
  1763. std::vector<pcl::PointXYZ>&cluster_box,
  1764. std::vector<std::vector<int> >& clt_line_idx,
  1765. std::string&winname)
  1766. {
  1767. pcl::visualization::PCLVisualizer viewer(winname);
  1768. viewer.addCoordinateSystem();
  1769. viewer.addPointCloud<pcl::PointXYZ>(cloud, "all_cloud");
  1770. viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 1, 1, 1, "all_cloud");
  1771. viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "all_cloud");
  1772. int cnt = 0;
  1773. char buf[8];
  1774. for (size_t i = 0; i < cluster_center.size();++i) {
  1775. pcl::PointXYZ &p = cluster_center.at(i);
  1776. pcl::PointXYZ px0, px1, py1, py0;
  1777. px0.x = p.x - 5.0;
  1778. px0.y = p.y;
  1779. px0.z = p.z;
  1780. px1.x = p.x + 5.0;
  1781. px1.y = p.y;
  1782. px1.z = p.z;
  1783. py0.x = p.x;
  1784. py0.y = p.y - 5.0;
  1785. py0.z = p.z;
  1786. py1.x = p.x;
  1787. py1.y = p.y + 5.0;
  1788. py1.z = p.z;
  1789. viewer.addLine(px0, px1, 255, 0, 0, "x" + string(_itoa(cnt, buf, 10)));
  1790. viewer.addLine(py0, py1, 0, 255, 0, "y" + string(_itoa(cnt, buf, 10)));
  1791. //box
  1792. pcl::PointXYZ & min_pt = cluster_box.at(2 * i);
  1793. pcl::PointXYZ & max_pt = cluster_box.at(2 * i + 1);
  1794. viewer.addCube(min_pt.x, max_pt.x, min_pt.y, max_pt.y, min_pt.z, max_pt.z, 0.5,0.5,0.0,"AABB_"+string(_itoa(cnt, buf, 10)));
  1795. viewer.setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_REPRESENTATION,
  1796. pcl::visualization::PCL_VISUALIZER_REPRESENTATION_WIREFRAME, "AABB_" + string(_itoa(cnt, buf, 10)));
  1797. if (clt_line_idx.size()>i && clt_line_idx.at(i).size() > 0) {
  1798. pcl::PointCloud<pcl::PointXYZ>::Ptr line_cloud(new pcl::PointCloud<pcl::PointXYZ>);
  1799. pcl::copyPointCloud(*cloud, clt_line_idx.at(i), *line_cloud);
  1800. viewer.addPointCloud(line_cloud, "fit_line" + string(_itoa(cnt, buf, 10)));
  1801. viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 0, 1, 0, "fit_line" + string(_itoa(cnt, buf, 10)));
  1802. viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "fit_line" + string(_itoa(cnt, buf, 10)));
  1803. }
  1804. cnt += 1;
  1805. }
  1806. while (!viewer.wasStopped()) {
  1807. viewer.spinOnce(100);
  1808. boost::this_thread::sleep(boost::posix_time::microseconds(100000));
  1809. }
  1810. }
  1811. };