trajectory.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #pragma once
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5. #include <map>
  6. #include <list>
  7. #include "opencv2/highgui/highgui.hpp"
  8. #include "opencv2/imgproc/imgproc.hpp"
  9. #include "opencv2/dnn.hpp"
  10. #include "opencv2/opencv.hpp"
  11. #include "opencv2/highgui.hpp"
  12. #if (CV_MAJOR_VERSION >= 3)
  13. #include "opencv2/imgcodecs/imgcodecs.hpp"
  14. #endif
  15. using namespace std;
  16. using namespace cv;
  17. class Trajectory
  18. {
  19. private:
  20. std::map<int, std::vector<cv::Point>> m_trajectoryData_map;
  21. Point pointAdd(Point p, Point q){
  22. p.x += q.x;
  23. p.y += q.y;
  24. return p;
  25. }
  26. Point pointTimes(float c, Point p){
  27. p.x *= c;
  28. p.y *= c;
  29. return p;
  30. }
  31. Point Bernstein(float u, cv::Point qi,cv::Point mid,cv::Point mo){
  32. Point a, b, c, r;
  33. a = pointTimes(pow(u, 2), mo);
  34. b = pointTimes(pow((1 - u), 2), qi);
  35. c = pointTimes(2 * u*(1 - u), mid);
  36. r = pointAdd(pointAdd(a, b), c);
  37. return r;
  38. }
  39. public:
  40. Trajectory(){};
  41. ~Trajectory(){};
  42. void addTraData(int track_id, cv::Point center){
  43. if(find(track_id)){
  44. if(m_trajectoryData_map[track_id].size() >= 30){
  45. m_trajectoryData_map[track_id].erase(m_trajectoryData_map[track_id].begin());
  46. }
  47. }
  48. m_trajectoryData_map[track_id].push_back(center);
  49. }
  50. bool find(int track_id){
  51. std::map<int, std::vector<cv::Point>>::iterator iter = m_trajectoryData_map.find(track_id);
  52. if(iter != m_trajectoryData_map.end()){
  53. return true;
  54. }
  55. return false;
  56. }
  57. void delTraData(int track_id){
  58. if(find(track_id)){
  59. m_trajectoryData_map.erase(track_id);
  60. }
  61. }
  62. void updataTraData(std::list<int> track_id){
  63. std::map<int, std::vector<cv::Point>> trajectoryData_map;
  64. std::map<int, std::vector<cv::Point>>::iterator map_iter;
  65. for (map_iter = m_trajectoryData_map.begin(); map_iter != m_trajectoryData_map.end(); map_iter++)
  66. {
  67. for (std::list<int>::iterator iter = track_id.begin(); iter != track_id.end(); iter++)
  68. {
  69. if(map_iter->first == *iter){
  70. trajectoryData_map[map_iter->first] = map_iter->second;
  71. }
  72. }
  73. }
  74. m_trajectoryData_map = trajectoryData_map;
  75. }
  76. void dramTrajectory(cv::Mat img){
  77. std::map<int, std::vector<cv::Point>>::iterator iter;
  78. for (iter = m_trajectoryData_map.begin(); iter != m_trajectoryData_map.end(); iter++)
  79. {
  80. std::vector<cv::Point> Points = iter->second;
  81. for (size_t i = 2; i < Points.size(); i += 2)
  82. {
  83. cv::Point pre, last, mid;
  84. pre = Points[i - 2];
  85. mid = Points[i - 1];
  86. last = Points[i];
  87. cv::Point pt_pre = Points[i - 2];
  88. cv::Point pt_now;
  89. for (size_t k = 0; k <= 10; k++)
  90. {
  91. float u = (float)k / 10;
  92. cv::Point new_point = Bernstein(u, pre, mid, last);
  93. pt_now.x = (int)new_point.x;
  94. pt_now.y = (int)new_point.y;
  95. cv::line(img, pt_pre, pt_now, Scalar(0, 255, 0), 2, 16, 0);//绘制直线
  96. pt_pre = pt_now;
  97. }
  98. }
  99. }
  100. }
  101. };