grab_occlusion.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. #include <sstream>
  2. #include <opencv2\highgui\highgui.hpp>
  3. #include "grab_occlusion.h"
  4. #include "utils.h"
  5. namespace graft_cv {
  6. CStemResultInfos::CStemResultInfos(double seedling_dist,
  7. int holes_number,
  8. double x_min,
  9. double x_max,
  10. double z_min,
  11. double z_max,
  12. std::string pcd_id,
  13. CGcvLogger*pLog)
  14. : m_pLogger(pLog)
  15. , m_seedling_dist(seedling_dist)
  16. , m_holes_number(holes_number)
  17. , m_xmin(x_min)
  18. , m_xmax(x_max)
  19. , m_zmin(z_min)
  20. , m_zmax(z_max)
  21. //, m_append_counter(0)
  22. , m_pcdId(pcd_id)
  23. , m_max_size(50)
  24. {
  25. gen_root_centers();
  26. }
  27. CStemResultInfos::~CStemResultInfos()
  28. {}
  29. void CStemResultInfos::append(
  30. CStemResult& sr
  31. )
  32. {
  33. m_infos.insert(m_infos.begin(), sr);
  34. if (m_infos.size() > m_max_size) {
  35. m_infos.pop_back();
  36. }
  37. //m_append_counter += 1;
  38. //每次都更新
  39. _update_root_centers(sr);
  40. //if (m_append_counter % 10 == 0) {
  41. // //定期写入数据
  42. // _update_root_centers();
  43. // write_root_centers(m_json_filename);
  44. //}
  45. }
  46. void CStemResultInfos::clear()
  47. {
  48. m_infos.clear();
  49. }
  50. void CStemResultInfos::get_root_centers(
  51. std::vector<CStemResult>&rst
  52. )
  53. {
  54. rst.clear();
  55. for (auto& sr : m_root_centers) {
  56. rst.push_back(sr);
  57. }
  58. }
  59. void CStemResultInfos::_update_root_centers(CStemResult& sr) {
  60. //直接在m_root_centers中找到最接近的中心,如果小于指定距离,更新m_root_centers
  61. double d1 = m_seedling_dist / 4.0;
  62. double d_min = 1.0e6;
  63. int min_root_idx = -1;
  64. for (int i = 0; i < m_root_centers.size(); ++i) {
  65. double dist = m_root_centers.at(i).calcluate_distance(sr);
  66. if (dist < d_min) {
  67. d_min = dist;
  68. min_root_idx = i;
  69. }
  70. }
  71. if (d_min < d1 && min_root_idx >= 0) {
  72. //更新指定中心
  73. double mu_x, mu_y, mu_z;
  74. CStemResult& min_root = m_root_centers.at(min_root_idx);
  75. mu_x = min_root.root_x;
  76. if (min_root.root_count == 0) {
  77. mu_y = sr.root_y;
  78. mu_z = sr.root_z;
  79. }
  80. else {
  81. mu_y = (min_root.root_y * min_root.root_count +
  82. sr.root_y * sr.root_count) / (double)(min_root.root_count + sr.root_count);
  83. mu_z = (min_root.root_z * min_root.root_count +
  84. sr.root_z * sr.root_count) / (double)(min_root.root_count + sr.root_count);
  85. }
  86. min_root.root_x = mu_x;
  87. min_root.root_y = mu_y;
  88. min_root.root_z = mu_z;
  89. min_root.root_count += sr.root_count;
  90. }
  91. }
  92. void CStemResultInfos::gen_root_centers()
  93. {
  94. //根据 m_seedling_dist, m_holes_number, m_xmin, m_xmax生成初始的穴位中心
  95. //以m_xmin, m_xmax的中间点为中心,分别找到间隔m_seedling_dist的m_holes_number个穴位中心
  96. //初始的z设成-1,等待更新赋值
  97. double x_mid = 0.5 * (m_xmin + m_xmax);
  98. double holes_mid = 0.5 * (m_holes_number - 1) * m_seedling_dist;
  99. double x0 = x_mid - holes_mid;
  100. double z_mid = 0.5 * (m_zmin + m_zmax);
  101. m_root_centers.clear();
  102. for (int i=0; i<m_holes_number; ++i) {
  103. double x = x0 + i * m_seedling_dist;
  104. double y = 0;
  105. double z = z_mid;
  106. int size = 0;
  107. int count = 0;
  108. CStemResult sr = CStemResult(x, y, z, size, std::string(""), count);
  109. m_root_centers.push_back(sr);
  110. }
  111. }
  112. CSeedlingStatus::CSeedlingStatus(
  113. int dtype,
  114. double step,
  115. double x_min,
  116. double x_max,
  117. double pc_mean_dist,
  118. CGcvLogger*pLog)
  119. : m_pLogger(pLog)
  120. , m_dtype(dtype)
  121. , m_bin_step(step)
  122. , m_xmin(x_min)
  123. , m_xmax(x_max)
  124. , m_record_cursor(-1)
  125. , m_global_cursor(-1)
  126. , m_max_size(50)
  127. , m_pc_mean_dist(pc_mean_dist)
  128. {
  129. int x0 = int(x_min);
  130. int x1 = int(x_max);
  131. m_hist_length = int((x1 - x0) / step);
  132. m_history_point_size = cv::Mat::zeros(m_max_size, 1, CV_32F);
  133. }
  134. CSeedlingStatus::~CSeedlingStatus()
  135. {}
  136. void CSeedlingStatus::set_x_centers(std::vector<double>&cx)
  137. {
  138. m_center_x.clear();
  139. for (auto&x : cx) {
  140. m_center_x.push_back(x);
  141. }
  142. std::sort(m_center_x.begin(), m_center_x.end());
  143. double seedling_distance = m_center_x.at(1) - m_center_x.at(0);
  144. m_idx_low.clear();
  145. m_idx_up.clear();
  146. for (int i = 0; i < m_center_x.size(); ++i) {
  147. int idx_low = int((m_center_x.at(i) - 0.5 * seedling_distance - m_xmin) / m_bin_step);
  148. int idx_up = int((m_center_x.at(i) + 0.5 * seedling_distance - m_xmin) / m_bin_step);
  149. m_idx_low.push_back(idx_low);
  150. m_idx_up.push_back(idx_up);
  151. }
  152. m_history_status = cv::Mat::zeros(m_max_size, m_center_x.size(), CV_8U);
  153. m_history_histogram = cv::Mat::zeros(m_max_size, m_center_x.size(), CV_32F);
  154. }
  155. void CSeedlingStatus::append_hist(
  156. std::vector<int>&xhist //input
  157. //std::vector<bool>&xstatus //output
  158. )
  159. {
  160. assert(xhist.size() == m_hist_length);
  161. std::vector<float>center_count;
  162. calculate_center_size(xhist, center_count);
  163. assert(center_count.size() == m_center_x.size());
  164. m_global_cursor++;
  165. if (m_record_cursor < m_max_size-1) {
  166. m_record_cursor++;
  167. float pc_size = 0.0;
  168. for (int i = 0; i < center_count.size(); ++i) {
  169. m_history_histogram.at<float>(m_record_cursor,i) = center_count.at(i);
  170. pc_size += center_count.at(i);
  171. }
  172. m_history_point_size.at<float>(m_record_cursor, 0) = pc_size;
  173. //get_status(xstatus);
  174. }
  175. else {
  176. //数据上移一行,数据放在最后一行
  177. for (int row = 0; row < m_history_histogram.rows - 1; ++row) {
  178. for (int col = 0; col < m_history_histogram.cols; ++col) {
  179. m_history_histogram.at<float>(row, col) = m_history_histogram.at<float>(row + 1, col);
  180. }
  181. }
  182. for (int row = 0; row < m_history_point_size.rows - 1; ++row) {
  183. m_history_point_size.at<float>(row, 0) = m_history_point_size.at<float>(row+1, 0);
  184. }
  185. float pc_size = 0.0;
  186. for (int i = 0; i < center_count.size(); ++i) {
  187. m_history_histogram.at<float>(m_history_histogram.rows - 1, i) = center_count.at(i);
  188. pc_size += center_count.at(i);
  189. }
  190. m_history_point_size.at<float>(m_history_point_size.rows - 1, 0) = pc_size;
  191. //get_status(xstatus);
  192. }
  193. }
  194. void CSeedlingStatus::calculate_center_size(std::vector<int>&xhist, std::vector<float>&x_count)
  195. {
  196. x_count.clear();
  197. for (int i = 0; i < m_center_x.size(); ++i) {
  198. int idx_low = m_idx_low.at(i);
  199. int idx_up = m_idx_up.at(i);
  200. float valid_pc_cnt = 0;
  201. for (int k = idx_low; k <= idx_up; ++k) {
  202. if (k < 0 || k >= xhist.size()) { continue; }
  203. valid_pc_cnt += xhist.at(k);
  204. }
  205. x_count.push_back(valid_pc_cnt);
  206. }
  207. }
  208. //void CSeedlingStatus::calculate_center_size(int cursor, std::vector<float>&x_count)
  209. //{
  210. // x_count.clear();
  211. // for (int i = 0; i < m_center_x.size(); ++i) {
  212. // int idx_low = m_idx_low.at(i);
  213. // int idx_up = m_idx_up.at(i);
  214. // float valid_pc_cnt = 0;
  215. // for (int k = idx_low; k <= idx_up; ++k) {
  216. // valid_pc_cnt += m_history_histogram.at<float>(cursor, i);
  217. // }
  218. // x_count.push_back(valid_pc_cnt);
  219. // }
  220. //}
  221. //void CSeedlingStatus::get_status(std::vector<bool>&xstatus)
  222. //{
  223. // //根据历史信息评估:
  224. // // 1)位置上是否有苗
  225. // // 2)历史上是否已经取走苗
  226. // // 3)是否有新进的苗
  227. //
  228. // xstatus.clear();
  229. // xstatus.assign(m_center_x.size(), false);
  230. //
  231. // //1 如果没有记录输入,返回没有苗
  232. // if (m_record_cursor < 0) { return; }
  233. //
  234. // //2 如果是第一次,没有参考,用自身的分布阈值判断是否有苗(可能不准确,但没有其他办法)
  235. // //点云分布情况分析
  236. // // 每个bin的数量阈值设定m_bin_step宽度,至少有10毫米的点云,才认为有苗
  237. // float th_hist = m_bin_step * 10 / m_pc_mean_dist / m_pc_mean_dist;
  238. // if (m_record_cursor == 0) {
  239. // //std::vector<bool> hist_status;
  240. // //hist_status.assign(m_hist_length, false);
  241. // //for (int i = 0; i < m_hist_length; ++i) {
  242. // // if (m_history_histogram.at<float>(m_record_cursor, i) > th_hist) {
  243. // // hist_status.at(i) = true;
  244. // // }
  245. // //}
  246. // //for (int i = 0; i < m_center_x.size(); ++i) {
  247. // // int idx_low = m_idx_low.at(i);
  248. // // int idx_up = m_idx_up.at(i);
  249. // // int valid_bin_cnt = 0;
  250. // // for (int k = idx_low; k < idx_up; ++k) {
  251. // // if (hist_status.at(k)) { valid_bin_cnt++; }
  252. // // }
  253. // // double valid_ratio = (double)valid_bin_cnt / (double)(idx_up - idx_low);
  254. // // xstatus.at(i) = valid_ratio > 0.5;
  255. // //}
  256. // ////update m_history_status
  257. // //for (int i = 0; i < m_history_status.cols; ++i) {
  258. // // m_history_status.at<unsigned char>(m_record_cursor, i) = xstatus.at(i);
  259. // //}
  260. // //return;
  261. //
  262. // std::vector<float> x_count;
  263. // calculate_center_size(m_record_cursor, x_count);
  264. // for (int i = 0; i < x_count.size(); ++i) {
  265. // int idx_low = m_idx_low.at(i);
  266. // int idx_up = m_idx_up.at(i);
  267. // xstatus.at(i) = x_count.at(i) > (idx_up - idx_low + 1) * th_hist;
  268. // }
  269. // //update m_history_status
  270. // for (int i = 0; i < m_history_status.cols; ++i) {
  271. // m_history_status.at<unsigned char>(m_record_cursor, i) = xstatus.at(i);
  272. // }
  273. // return;
  274. // }
  275. //
  276. // //3 2次或更多,通过前后2次差分析苗取走的情况
  277. // //3.1 计算被取走的点云位置分布
  278. // std::vector<float>hist_diff;
  279. // hist_diff.assign(m_hist_length, 0.0);
  280. // float sum_dn = 0.0;
  281. // float sum_n = 0.0;
  282. // float diff_cnt = 0.0;
  283. // for (int i = 0; i < m_hist_length; ++i) {
  284. // diff_cnt = m_history_histogram.at<float>(m_record_cursor - 1, i) -
  285. // m_history_histogram.at<float>(m_record_cursor, i);
  286. // hist_diff.at(i) = diff_cnt;
  287. // sum_n += diff_cnt;
  288. // sum_dn += diff_cnt * i;
  289. // }
  290. // /*if (m_record_cursor < m_max_size) {
  291. // for (int i = 0; i < m_hist_length; ++i) {
  292. // diff_cnt = m_history_histogram.at<float>(m_record_cursor - 1, i) -
  293. // m_history_histogram.at<float>(m_record_cursor, i);
  294. // hist_diff.at(i) = diff_cnt;
  295. // sum_n += diff_cnt;
  296. // sum_dn += diff_cnt * i;
  297. // }
  298. // }
  299. // else {
  300. // for (int i = 0; i < m_hist_length; ++i) {
  301. // diff_cnt = m_history_histogram.at<float>(m_max_size - 2, i) -
  302. // m_history_histogram.at<float>(m_max_size - 1, i);
  303. // hist_diff.at(i) = diff_cnt;
  304. // sum_n += diff_cnt;
  305. // sum_dn += diff_cnt * i;
  306. // }
  307. //
  308. // }*/
  309. //
  310. // //3.2 统计增减点云的状态,区分点云增加,点云减小,点云没变化的部分
  311. // std::vector<int> hist_status_2d; //3种状态记录: -1取走,0没变化,1上苗
  312. // hist_status_2d.assign(m_hist_length, 0);
  313. // int add_cnt = 0;
  314. // int sub_cnt = 0;
  315. // for (int i = 0; i < m_hist_length; ++i) {
  316. // if (hist_diff.at(i) > th_hist) {
  317. // hist_status_2d.at(i) = -1;
  318. // sub_cnt += 1;
  319. // }
  320. // if (hist_diff.at(i) < -th_hist) {
  321. // hist_status_2d.at(i) = 1;
  322. // add_cnt += 1;
  323. // }
  324. // }
  325. //
  326. // //3.3 判断苗的整体情况
  327. // double seedling_distance = m_center_x.at(1) - m_center_x.at(0); //株间距离
  328. // double grid_one_seedling = seedling_distance / m_bin_step; //每穴位占histogram的桶数
  329. // //3.3.1进一排苗
  330. // if (add_cnt > grid_one_seedling*3.0) {
  331. // xstatus.assign(m_center_x.size(), true);
  332. // //update m_history_status
  333. // if (m_record_cursor == m_global_cursor) {
  334. // if (m_record_cursor < m_max_size) {
  335. // for (int i = 0; i < m_history_status.cols; ++i) {
  336. // m_history_status.at<unsigned char>(m_record_cursor, i) = xstatus.at(i);
  337. // }
  338. // }
  339. // else {
  340. //
  341. // }
  342. // }
  343. // else {
  344. // //数据上移一行,数据放在最后一行
  345. // for (int row = 0; row < m_history_status.rows - 1; ++row) {
  346. // for (int col = 0; col < m_history_status.cols; ++col) {
  347. // m_history_status.at<float>(row, col) = m_history_status.at<float>(row + 1, col);
  348. // }
  349. // }
  350. // /*memcpy_s(m_history_status.data,
  351. // m_history_status.step[0] * (m_max_size - 1),
  352. // m_history_status.data + m_history_status.step[0],
  353. // m_history_status.step[0] * (m_max_size - 1));
  354. // */
  355. // for (int i = 0; i < m_history_status.cols; ++i) {
  356. // m_history_status.at<unsigned char>(m_max_size-1, i) = xstatus.at(i);
  357. // }
  358. // }
  359. // return;
  360. // }
  361. //
  362. // std::vector<size_t>sorted_idx;
  363. // std::vector<float>sub_seedling_score; //移出植株得分,记录每个穴位上点云变化得分
  364. // //3.3.2 变化很小,说明没有改变(没能成功抓走)
  365. // if (add_cnt + sub_cnt < 0.5 * grid_one_seedling) {
  366. // goto no_change;
  367. // }
  368. // //3.3.3 否则的话,就是抓走过一个苗
  369. // //找到被取走的苗的中心,然后根据dtype确定有苗的位置
  370. // //找覆盖范围最大的区域
  371. // double sub_cent_indx = sum_dn / sum_n; //计算改变范围的中心,目前没用到
  372. // sub_seedling_score.assign(m_center_x.size(), 0.0);
  373. // for (int idx = 0; idx < hist_status_2d.size(); ++idx) {
  374. // if (hist_status_2d.at(idx) >= 0) {
  375. // //这个histogram上没有移出,不统计, hist_status_2d的值域:-1取走,0没变化,1上苗
  376. // continue;
  377. // }
  378. // for (int i = 0; i < m_center_x.size(); ++i) {
  379. // int idx_low = m_idx_low.at(i);
  380. // int idx_up = m_idx_up.at(i);
  381. // if (idx >= idx_low && idx < idx_up) {
  382. // sub_seedling_score.at(i) += 1.0;
  383. // }
  384. // }
  385. // }
  386. // int sub_pos = -1;
  387. // sorted_idx = sort_indexes_e(sub_seedling_score, false);
  388. // for (auto& idx : sorted_idx) {
  389. // if (sub_seedling_score.at(idx) < 0.25 * grid_one_seedling) {
  390. // //如果改变量,不到穴位范围的一半,不认为是移走的
  391. // continue;
  392. // }
  393. // if (m_history_status.at<unsigned char>(m_record_cursor - 1, idx) == 0) {
  394. // //如果这个位置上一帧就没有苗,那判别也是错误的
  395. // continue;
  396. // }
  397. // sub_pos = idx;
  398. // break;//找到得分最高,并且满足条件的位置,就是被抓走的位置,跳出
  399. // }
  400. // if (sub_pos >= 0) {
  401. // xstatus.assign(m_center_x.size(), false);
  402. // int cursor = m_record_cursor-1;
  403. // for (int i = 0; i < m_history_status.cols; ++i) {
  404. // if (m_history_status.at<unsigned char>(cursor, i) == 1) {
  405. // xstatus.at(i) = true;
  406. // }
  407. // }
  408. // xstatus.at(sub_pos) = false;
  409. //
  410. // //update m_history_status
  411. // if (m_record_cursor == m_global_cursor) {
  412. // for (int i = 0; i < m_history_status.cols; ++i) {
  413. // m_history_status.at<unsigned char>(m_record_cursor, i) = xstatus.at(i);
  414. // }
  415. // }
  416. // else{
  417. // //数据上移一行,数据放在最后一行
  418. // for (int row = 0; row < m_history_status.rows - 1; ++row) {
  419. // for (int col = 0; col < m_history_status.cols; ++col) {
  420. // m_history_status.at<float>(row, col) = m_history_status.at<float>(row + 1, col);
  421. // }
  422. // }
  423. // /*memcpy_s(m_history_status.data,
  424. // m_history_status.step[0] * (m_max_size - 1),
  425. // m_history_status.data + m_history_status.step[0],
  426. // m_history_status.step[0] * (m_max_size - 1));*/
  427. // for (int i = 0; i < m_history_status.cols; ++i) {
  428. // m_history_status.at<unsigned char>(m_max_size - 1, i) = xstatus.at(i);
  429. // }
  430. // }
  431. // return;
  432. // }
  433. // else {
  434. // //如果没有找到有效位置,按没有变化处理
  435. // goto no_change;
  436. // }
  437. //
  438. //no_change:
  439. // //没有改变,用上一次的结果
  440. // xstatus.assign(m_center_x.size(), true);
  441. // //update m_history_status
  442. // if (m_record_cursor == m_global_cursor) {
  443. // for (int i = 0; i < m_history_status.cols; ++i) {
  444. // m_history_status.at<unsigned char>(m_record_cursor, i) =
  445. // m_history_status.at<unsigned char>(m_record_cursor - 1, i);
  446. // if (m_history_status.at<unsigned char>(m_record_cursor - 1, i) == 0) {
  447. // xstatus.at(i) = false;
  448. // }
  449. // }
  450. // }
  451. // else{
  452. // //数据上移一行,数据放在最后一行
  453. // for (int row = 0; row < m_history_status.rows - 1; ++row) {
  454. // for (int col = 0; col < m_history_status.cols; ++col) {
  455. // m_history_status.at<float>(row, col) = m_history_status.at<float>(row + 1, col);
  456. // }
  457. // }
  458. // /*memcpy_s(m_history_status.data,
  459. // m_history_status.step[0] * (m_max_size - 1),
  460. // m_history_status.data + m_history_status.step[0],
  461. // m_history_status.step[0] * (m_max_size - 1));*/
  462. // for (int i = 0; i < m_history_status.cols; ++i) {
  463. // m_history_status.at<unsigned char>(m_max_size - 1, i) =
  464. // m_history_status.at<unsigned char>(m_max_size - 2, i);
  465. // if (m_history_status.at<unsigned char>(m_max_size - 2, i) == 0) {
  466. // xstatus.at(i) = false;
  467. // }
  468. // }
  469. // }
  470. //}
  471. int CSeedlingStatus::get_status(int& max_idx, float&max_change)
  472. {
  473. //根据历史信息评估:
  474. // 1)评估是否新上苗
  475. // 2)判断被取走的那个苗
  476. //状态 1 ---此次为新上苗, 0 无显著变化, -1 上一次取苗成功
  477. int status = 1;
  478. //1 如果没有记录输入,返回没有苗
  479. if (m_record_cursor <= 0) {
  480. return status;
  481. }
  482. float change_threshold = 500.0;//点云变化,是否有苗新加或取走的阈值
  483. float change_count = m_history_point_size.at<float>(m_record_cursor, 0) - m_history_point_size.at<float>(m_record_cursor - 1, 0);
  484. if (change_count > 2.0 * change_threshold) {
  485. status = 1;
  486. }
  487. else {
  488. if (change_count < -change_threshold) {
  489. status = -1;
  490. }
  491. else {
  492. status = 0;
  493. }
  494. }
  495. max_change = 0;
  496. max_idx = 0;
  497. for (int i = 0; i < m_history_histogram.cols; ++i) {
  498. float d = m_history_histogram.at<float>(m_record_cursor, i) - m_history_histogram.at<float>(m_record_cursor - 1, i);
  499. if (fabs(d) > fabs(max_change)) {
  500. max_change = d;
  501. max_idx = i;
  502. }
  503. }
  504. if (max_change < -change_threshold) {
  505. status = -1;
  506. }
  507. return status;
  508. }
  509. void CSeedlingStatus::real_result_update(
  510. std::vector<pcl::PointXYZ>& root
  511. )
  512. {
  513. m_stem_status.clear();
  514. m_stem_status.assign(m_center_x.size(), 0);
  515. for (auto& p : root) {
  516. int mini = -1;
  517. float mind = 1000.0;
  518. for (int i = 0; i < m_center_x.size();++i) {
  519. float d = fabs(m_center_x.at(i) - p.x);
  520. if (d < mind) {
  521. mind = d;
  522. mini = i;
  523. }
  524. }
  525. if (mini >= 0) {
  526. m_stem_status.at(mini) = 1;
  527. }
  528. }
  529. }
  530. void CSeedlingStatus::occlusion_result_update(
  531. std::vector<int>& leaf_occlusion
  532. )
  533. {
  534. assert(m_center_x.size() == leaf_occlusion.size());
  535. assert(m_center_x.size() == m_stem_status.size());
  536. for (int i = 0; i < m_center_x.size(); ++i) {
  537. if (m_stem_status.at(i)==0 && leaf_occlusion.at(i)>0) {
  538. m_stem_status.at(i) = 2;
  539. }
  540. }
  541. }
  542. //获取植株的状态,是否有苗
  543. // 在real_result_update()和occlusion_result_update()调用后
  544. // 调用此函数,获取茎的状态
  545. void CSeedlingStatus::get_stem_status(
  546. std::vector<int>&stem_status
  547. )
  548. {
  549. //状态 1 ---此次为新上苗, 0 无显著变化, -1 上一次取苗成功
  550. float max_change_points_count;
  551. int max_change_center_idx;
  552. int status = get_status(max_change_center_idx, max_change_points_count);
  553. if (status > 0) {
  554. m_center_grabed_record.clear();
  555. m_center_grabed_record.assign(m_center_x.size(), -1);
  556. }
  557. stem_status.clear();
  558. if (status >= 0) {
  559. //用当前识别的结果
  560. }
  561. else {
  562. m_center_grabed_record.at(max_change_center_idx) = 0;
  563. //如果max_change_center_idx位置的苗上一次有苗,本次为2,那么久将2改成1
  564. if (max_change_points_count < 0) {
  565. if (m_history_status.at<unsigned char>(m_record_cursor - 1, max_change_center_idx) != 0) {
  566. if (m_stem_status.at(max_change_center_idx) == 2) {
  567. m_stem_status.at(max_change_center_idx) = 0;
  568. }
  569. }
  570. }
  571. }
  572. //更新根据: m_center_grabed_record更新m_stem_status中的叶子遮挡
  573. for (int i = 0; i < m_stem_status.size(); ++i) {
  574. if (m_center_grabed_record.at(i) == 0 && m_stem_status.at(i) == 2) {
  575. m_stem_status.at(i) = 0;
  576. }
  577. }
  578. for (int i = 0; i < m_stem_status.size(); ++i) {
  579. stem_status.push_back(m_stem_status.at(i));
  580. }
  581. //更新m_history_status
  582. if (m_record_cursor == m_global_cursor) {
  583. for (int i = 0; i < m_history_status.cols; ++i) {
  584. m_history_status.at<unsigned char>(m_record_cursor, i) = m_stem_status.at(i);
  585. }
  586. }
  587. else{
  588. //数据上移一行,数据放在最后一行
  589. for (int row = 0; row < m_history_status.rows - 1; ++row) {
  590. for (int col = 0; col < m_history_status.cols; ++col) {
  591. m_history_status.at<float>(row, col) = m_history_status.at<float>(row + 1, col);
  592. }
  593. }
  594. for (int i = 0; i < m_history_status.cols; ++i) {
  595. m_history_status.at<unsigned char>(m_max_size - 1, i) = m_stem_status.at(i);
  596. }
  597. }
  598. }
  599. }