BYTETrackerUtils.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. #include "BYTETracker.h"
  2. #include "Lapjv.h"
  3. #include "STrack.h"
  4. vector<STrack *> BYTETracker::joint_stracks(vector<STrack *> &tlista, vector <STrack> &tlistb) {
  5. map<int, int> exists;
  6. vector < STrack * > res;
  7. for (int i = 0; i < tlista.size(); i++) {
  8. exists.insert(pair<int, int>(tlista[i]->track_id, 1));
  9. res.push_back(tlista[i]);
  10. }
  11. for (int i = 0; i < tlistb.size(); i++) {
  12. int tid = tlistb[i].track_id;
  13. if (!exists[tid] || exists.count(tid) == 0) {
  14. exists[tid] = 1;
  15. res.push_back(&tlistb[i]);
  16. }
  17. }
  18. return res;
  19. }
  20. vector <STrack> BYTETracker::joint_stracks(vector <STrack> &tlista, vector <STrack> &tlistb) {
  21. std::map<int, int> exists;
  22. vector <STrack> res;
  23. for (int i = 0; i < tlista.size(); i++) {
  24. exists.insert(pair<int, int>(tlista[i].track_id, 1));
  25. res.push_back(tlista[i]);
  26. }
  27. for (int i = 0; i < tlistb.size(); i++) {
  28. int tid = tlistb[i].track_id;
  29. if (!exists[tid] || exists.count(tid) == 0) {
  30. exists[tid] = 1;
  31. res.push_back(tlistb[i]);
  32. }
  33. }
  34. return res;
  35. }
  36. vector <STrack> BYTETracker::sub_stracks(vector <STrack> &tlista, vector <STrack> &tlistb) {
  37. map<int, STrack> stracks;
  38. for (int i = 0; i < tlista.size(); i++) {
  39. stracks.insert(pair<int, STrack>(tlista[i].track_id, tlista[i]));
  40. }
  41. for (int i = 0; i < tlistb.size(); i++) {
  42. int tid = tlistb[i].track_id;
  43. if (stracks.count(tid) != 0) {
  44. stracks.erase(tid);
  45. }
  46. }
  47. vector <STrack> res;
  48. std::map<int, STrack>::iterator it;
  49. for (it = stracks.begin(); it != stracks.end(); ++it) {
  50. res.push_back(it->second);
  51. }
  52. return res;
  53. }
  54. void BYTETracker::remove_duplicate_stracks(vector <STrack> &resa, vector <STrack> &resb, vector <STrack> &stracksa,
  55. vector <STrack> &stracksb) {
  56. vector <vector<float>> pdist = iou_distance(stracksa, stracksb);
  57. vector <pair<int, int>> pairs;
  58. for (int i = 0; i < pdist.size(); i++) {
  59. for (int j = 0; j < pdist[i].size(); j++) {
  60. if (pdist[i][j] < 0.15) {
  61. pairs.push_back(pair<int, int>(i, j));
  62. }
  63. }
  64. }
  65. vector<int> dupa, dupb;
  66. for (int i = 0; i < pairs.size(); i++) {
  67. int timep = stracksa[pairs[i].first].frame_id - stracksa[pairs[i].first].start_frame;
  68. int timeq = stracksb[pairs[i].second].frame_id - stracksb[pairs[i].second].start_frame;
  69. if (timep > timeq)
  70. dupb.push_back(pairs[i].second);
  71. else
  72. dupa.push_back(pairs[i].first);
  73. }
  74. for (int i = 0; i < stracksa.size(); i++) {
  75. vector<int>::iterator iter = find(dupa.begin(), dupa.end(), i);
  76. if (iter == dupa.end()) {
  77. resa.push_back(stracksa[i]);
  78. }
  79. }
  80. for (int i = 0; i < stracksb.size(); i++) {
  81. vector<int>::iterator iter = find(dupb.begin(), dupb.end(), i);
  82. if (iter == dupb.end()) {
  83. resb.push_back(stracksb[i]);
  84. }
  85. }
  86. }
  87. void
  88. BYTETracker::linear_assignment(vector <vector<float>> &cost_matrix, int cost_matrix_size, int cost_matrix_size_size,
  89. float thresh,
  90. vector <vector<int>> &matches, vector<int> &unmatched_a, vector<int> &unmatched_b) {
  91. if (cost_matrix.size() == 0) {
  92. for (int i = 0; i < cost_matrix_size; i++) {
  93. unmatched_a.push_back(i);
  94. }
  95. for (int i = 0; i < cost_matrix_size_size; i++) {
  96. unmatched_b.push_back(i);
  97. }
  98. return;
  99. }
  100. vector<int> rowsol;
  101. vector<int> colsol;
  102. float c = lapjv(cost_matrix, rowsol, colsol, true, thresh);
  103. for (int i = 0; i < rowsol.size(); i++) {
  104. if (rowsol[i] >= 0) {
  105. vector<int> match;
  106. match.push_back(i);
  107. match.push_back(rowsol[i]);
  108. matches.push_back(match);
  109. } else {
  110. unmatched_a.push_back(i);
  111. }
  112. }
  113. for (int i = 0; i < colsol.size(); i++) {
  114. if (colsol[i] < 0) {
  115. unmatched_b.push_back(i);
  116. }
  117. }
  118. }
  119. vector <vector<float>> BYTETracker::ious(vector <vector<float>> &atlbrs, vector <vector<float>> &btlbrs) {
  120. vector <vector<float>> ious;
  121. if (atlbrs.size() * btlbrs.size() == 0)
  122. return ious;
  123. ious.resize(atlbrs.size());
  124. for (int i = 0; i < ious.size(); i++) {
  125. ious[i].resize(btlbrs.size());
  126. }
  127. //bbox_ious
  128. for (int k = 0; k < btlbrs.size(); k++) {
  129. vector<float> ious_tmp;
  130. float box_area = (btlbrs[k][2] - btlbrs[k][0] + 1) * (btlbrs[k][3] - btlbrs[k][1] + 1);
  131. for (int n = 0; n < atlbrs.size(); n++) {
  132. float iw = min(atlbrs[n][2], btlbrs[k][2]) - max(atlbrs[n][0], btlbrs[k][0]) + 1;
  133. if (iw > 0) {
  134. float ih = min(atlbrs[n][3], btlbrs[k][3]) - max(atlbrs[n][1], btlbrs[k][1]) + 1;
  135. if (ih > 0) {
  136. float ua = (atlbrs[n][2] - atlbrs[n][0] + 1) * (atlbrs[n][3] - atlbrs[n][1] + 1) + box_area -
  137. iw * ih;
  138. ious[n][k] = iw * ih / ua;
  139. } else {
  140. ious[n][k] = 0.0;
  141. }
  142. } else {
  143. ious[n][k] = 0.0;
  144. }
  145. }
  146. }
  147. return ious;
  148. }
  149. vector <vector<float>>
  150. BYTETracker::iou_distance(vector<STrack *> &atracks, vector <STrack> &btracks, int &dist_size, int &dist_size_size) {
  151. vector <vector<float>> cost_matrix;
  152. if (atracks.size() * btracks.size() == 0) {
  153. dist_size = atracks.size();
  154. dist_size_size = btracks.size();
  155. return cost_matrix;
  156. }
  157. vector <vector<float>> atlbrs, btlbrs;
  158. for (int i = 0; i < atracks.size(); i++) {
  159. atlbrs.push_back(atracks[i]->tlbr);
  160. }
  161. for (int i = 0; i < btracks.size(); i++) {
  162. btlbrs.push_back(btracks[i].tlbr);
  163. }
  164. dist_size = atracks.size();
  165. dist_size_size = btracks.size();
  166. vector <vector<float>> _ious = ious(atlbrs, btlbrs);
  167. for (int i = 0; i < _ious.size(); i++) {
  168. vector<float> _iou;
  169. for (int j = 0; j < _ious[i].size(); j++) {
  170. _iou.push_back(1 - _ious[i][j]);
  171. }
  172. cost_matrix.push_back(_iou);
  173. }
  174. return cost_matrix;
  175. }
  176. vector <vector<float>> BYTETracker::iou_distance(vector <STrack> &atracks, vector <STrack> &btracks) {
  177. vector <vector<float>> atlbrs, btlbrs;
  178. for (int i = 0; i < atracks.size(); i++) {
  179. atlbrs.push_back(atracks[i].tlbr);
  180. }
  181. for (int i = 0; i < btracks.size(); i++) {
  182. btlbrs.push_back(btracks[i].tlbr);
  183. }
  184. vector <vector<float>> _ious = ious(atlbrs, btlbrs);
  185. vector <vector<float>> cost_matrix;
  186. for (int i = 0; i < _ious.size(); i++) {
  187. vector<float> _iou;
  188. for (int j = 0; j < _ious[i].size(); j++) {
  189. _iou.push_back(1 - _ious[i][j]);
  190. }
  191. cost_matrix.push_back(_iou);
  192. }
  193. return cost_matrix;
  194. }
  195. double BYTETracker::lapjv(const vector <vector<float>> &cost, vector<int> &rowsol, vector<int> &colsol,
  196. bool extend_cost, float cost_limit, bool return_cost) {
  197. vector <vector<float>> cost_c;
  198. cost_c.assign(cost.begin(), cost.end());
  199. vector <vector<float>> cost_c_extended;
  200. int n_rows = cost.size();
  201. int n_cols = cost[0].size();
  202. rowsol.resize(n_rows);
  203. colsol.resize(n_cols);
  204. int n = 0;
  205. if (n_rows == n_cols) {
  206. n = n_rows;
  207. } else {
  208. if (!extend_cost) {
  209. cout << "set extend_cost=True" << endl;
  210. system("pause");
  211. exit(0);
  212. }
  213. }
  214. if (extend_cost || cost_limit < LONG_MAX) {
  215. n = n_rows + n_cols;
  216. cost_c_extended.resize(n);
  217. for (int i = 0; i < cost_c_extended.size(); i++)
  218. cost_c_extended[i].resize(n);
  219. if (cost_limit < LONG_MAX) {
  220. for (int i = 0; i < cost_c_extended.size(); i++) {
  221. for (int j = 0; j < cost_c_extended[i].size(); j++) {
  222. cost_c_extended[i][j] = cost_limit / 2.0;
  223. }
  224. }
  225. } else {
  226. float cost_max = -1;
  227. for (int i = 0; i < cost_c.size(); i++) {
  228. for (int j = 0; j < cost_c[i].size(); j++) {
  229. if (cost_c[i][j] > cost_max)
  230. cost_max = cost_c[i][j];
  231. }
  232. }
  233. for (int i = 0; i < cost_c_extended.size(); i++) {
  234. for (int j = 0; j < cost_c_extended[i].size(); j++) {
  235. cost_c_extended[i][j] = cost_max + 1;
  236. }
  237. }
  238. }
  239. for (int i = n_rows; i < cost_c_extended.size(); i++) {
  240. for (int j = n_cols; j < cost_c_extended[i].size(); j++) {
  241. cost_c_extended[i][j] = 0;
  242. }
  243. }
  244. for (int i = 0; i < n_rows; i++) {
  245. for (int j = 0; j < n_cols; j++) {
  246. cost_c_extended[i][j] = cost_c[i][j];
  247. }
  248. }
  249. cost_c.clear();
  250. cost_c.assign(cost_c_extended.begin(), cost_c_extended.end());
  251. }
  252. double **cost_ptr;
  253. cost_ptr = new double *[sizeof(double *) * n];
  254. for (int i = 0; i < n; i++)
  255. cost_ptr[i] = new double[sizeof(double) * n];
  256. for (int i = 0; i < n; i++) {
  257. for (int j = 0; j < n; j++) {
  258. cost_ptr[i][j] = cost_c[i][j];
  259. }
  260. }
  261. int *x_c = new int[sizeof(int) * n];
  262. int *y_c = new int[sizeof(int) * n];
  263. int ret = lapjv_internal(n, cost_ptr, x_c, y_c);
  264. if (ret != 0) {
  265. std::cout << "Calculate Wrong!" << endl;
  266. system("pause");
  267. exit(0);
  268. }
  269. double opt = 0.0;
  270. if (n != n_rows) {
  271. for (int i = 0; i < n; i++) {
  272. if (x_c[i] >= n_cols)
  273. x_c[i] = -1;
  274. if (y_c[i] >= n_rows)
  275. y_c[i] = -1;
  276. }
  277. for (int i = 0; i < n_rows; i++) {
  278. rowsol[i] = x_c[i];
  279. }
  280. for (int i = 0; i < n_cols; i++) {
  281. colsol[i] = y_c[i];
  282. }
  283. if (return_cost) {
  284. for (int i = 0; i < rowsol.size(); i++) {
  285. if (rowsol[i] != -1) {
  286. //cout << i << "\t" << rowsol[i] << "\t" << cost_ptr[i][rowsol[i]] << endl;
  287. opt += cost_ptr[i][rowsol[i]];
  288. }
  289. }
  290. }
  291. } else if (return_cost) {
  292. for (int i = 0; i < rowsol.size(); i++) {
  293. opt += cost_ptr[i][rowsol[i]];
  294. }
  295. }
  296. for (int i = 0; i < n; i++) {
  297. delete[]cost_ptr[i];
  298. }
  299. delete[]cost_ptr;
  300. delete[]x_c;
  301. delete[]y_c;
  302. return opt;
  303. }