Blame view

vpt/src/VPT/sort/TrafficStatistics.cpp 4.75 KB
144cd8c4   Hu Chunming   v6.0.0 替换模型
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
  #include "TrafficStatistics.h"

  

  

  TrafficStatistics::TrafficStatistics(StatisticsMethod method)

  {

  	statisticsMethod = method;

  	mLineCounter = 0;

  }

  

  

  TrafficStatistics::~TrafficStatistics()

  {

  }

  

  void TrafficStatistics::SetLineArray(VPT_Line *line, int linecount)

  {

  	mLineCounter = linecount;

  	for (int i = 0; i < mLineCounter; i++)

  	{

  		lineArray.push_back(line[i]);

  	}

  }

  

  void TrafficStatistics::GetTrafficArray(VPT_ObjInfo *result, int resSize, vector<KalmanBoxTracker> &trackers, vector <VPT_TrafficResult> &trafficArray)

  {

  	if (trafficArray.size() != mLineCounter)

  	{

  		cout << "ERROR: The number of lines doesn't match the size of trafficArray! " << endl;

  		return;

  	}

  

  	switch (statisticsMethod)

  	{

  	case LINE_RECT_INTERSECTION:

  		break;

  	case LINE_LINE_INTERSECTION:

  		LineLineMethod(result, trackers, resSize, trafficArray);

  		break;

  	case LINE_AND_RECT_INTERSECTION:

  		break;

  	default:

  		break;

  	}

  	

  }

  

  

  void TrafficStatistics::LineRectMethod(VPT_ObjInfo *result, vector<KalmanBoxTracker> &trackers, int resSize, vector <VPT_TrafficResult> &trafficArray)

  {

  	/*int i = 0, j = 0;

  	for (i = 0; i < mLineCounter; i++)

  	{

  		for (j = 0; j < resSize; j++)

  		{

  			if ()

  		}

  	}*/

  }

  

  void TrafficStatistics::LineLineMethod(VPT_ObjInfo *result, vector<KalmanBoxTracker> &trackers, int resSize, vector <VPT_TrafficResult> &trafficArray)

  {

  	int line_i = 0, i = 0, j = 0;

  	for (line_i = 0; line_i < mLineCounter; line_i++)

  	{

  		POINT line_begin(lineArray[line_i].begin_x, lineArray[line_i].begin_y);

  		POINT line_end(lineArray[line_i].end_x, lineArray[line_i].begin_y);

  

  		for (i = 0; i < resSize; i++)

  		{

  			for (j = 0; j < trackers.size(); j++)

  			{

6ca63d90   Hu Chunming   提交v4.0.0
72
  				if (trackers[j].istraffic == false && result[i].id == trackers[j].id)		//该条轨迹在该帧有运动

144cd8c4   Hu Chunming   v6.0.0 替换模型
73
74
75
76
77
  				{

  					int size = trackers[j].trackLine.size();

  					if (size < 2 ) continue;

  

  					if (line_line_intersection(line_begin, line_end, POINT(result[i].center_x, result[i].center_y), \

6ca63d90   Hu Chunming   提交v4.0.0
78
  						POINT(trackers[j].trackLine[size - 2].x, trackers[j].trackLine[size - 2].y) )== true)	//若有交点 返回true

144cd8c4   Hu Chunming   v6.0.0 替换模型
79
80
81
82
83
84
85
86
87
88
89
  					{

  						trackers[j].istraffic = true;

  						trafficArray[line_i].traffic[result[i].index]++;

  					}

  				}

  			}

  		}

  	}

  

  	

  

6ca63d90   Hu Chunming   提交v4.0.0
90
91
  	////方法1:计算矩形框是否相交

  	//if (line_rect_intersection(line_begin, line_end, tracker[j].listinfo[listmax].left, tracker[j].listinfo[listmax].top, tracker[j].listinfo[listmax].right, tracker[j].listinfo[listmax].bottom))	//若有交点

144cd8c4   Hu Chunming   v6.0.0 替换模型
92
93
94
95
  	//{

  	//	tracker[j].rectIntersected = true;

  	//}

  

6ca63d90   Hu Chunming   提交v4.0.0
96
  	//方法3:算前后帧中心点连线和拌线的是否相交

144cd8c4   Hu Chunming   v6.0.0 替换模型
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
  	

  

  }

  

  void TrafficStatistics::LineAndRectMethod(VPT_ObjInfo *result, vector<KalmanBoxTracker> &trackers, int resSize, vector <VPT_TrafficResult> &trafficArray)

  {

  	/*int i = 0, j = 0;

  	for (i = 0; i < mLineCounter; i++)

  	{

  		for (j = 0; j < resSize; j++)

  		{

  			if ()

  		}

  	}*/

  }

  

  //---------------------------by zl ---------------------------------------------/

  bool TrafficStatistics::line_rect_intersection(POINT start_p, POINT end_p, int left, int top, int right, int bottom)

  {

  	int a = start_p.y - end_p.y;

  	int b = end_p.x - start_p.x;

  	int c = start_p.x* end_p.y - end_p.x* start_p.y;

  

6ca63d90   Hu Chunming   提交v4.0.0
120
121
122
  	////思路:先看线段所在直线是否与矩形相交,如果不相交则必为 “F”,

  	////如果相交,则看线段的两个点是否在矩形的同一边(即两点的 xy)  坐标都比矩形的小 x(y) 坐标小,或者大),

  	////若在同一边则为“F”,否则就是相交的情况。

144cd8c4   Hu Chunming   v6.0.0 替换模型
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
  	if ((a* left + b*top + c >= 0 && a* right + b* bottom + c <= 0) ||

  		(a* left + b*top + c <= 0 && a* right + b* bottom + c >= 0) ||

  		(a* left + b*bottom + c >= 0 && a* right + b* top + c <= 0) ||

  		(a* left + b*bottom + c >= 0 && a* right + b* top + c <= 0))

  	{

  		if (left > right)

  		{

  			swap(left, right);

  		}

  		if (top < bottom)

  		{

  			swap(top, bottom);

  		}

  		if ((start_p.x < left && end_p.x < left) ||

  			(start_p.x > right && end_p.x < left) ||

  			(start_p.y > top && end_p.y > top) ||

6ca63d90   Hu Chunming   提交v4.0.0
139
  			(start_p.y < bottom && end_p.y < bottom))  ///判断线段是否在矩形一侧

144cd8c4   Hu Chunming   v6.0.0 替换模型
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
  		{

  			return false;

  		}

  		else

  		{

  			return true;

  		}

  	}

  	else

  	{

  		return false;

  	}

  }

  

  ///------------alg 2------------  

6ca63d90   Hu Chunming   提交v4.0.0
155
  //叉积  

144cd8c4   Hu Chunming   v6.0.0 替换模型
156
157
158
159
160
  double mult(POINT a, POINT b, POINT c)

  {

  	return (a.x - c.x)*(b.y - c.y) - (b.x - c.x)*(a.y - c.y);

  }

  

6ca63d90   Hu Chunming   提交v4.0.0
161
  //aa, bb为一条线段两端点 cc, dd为另一条线段的两端点 相交返回true, 不相交返回false  

144cd8c4   Hu Chunming   v6.0.0 替换模型
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
  bool TrafficStatistics::line_line_intersection(POINT aa, POINT bb, POINT cc, POINT dd)

  {

  	if (max(aa.x, bb.x)<min(cc.x, dd.x))

  	{

  		return false;

  	}

  	if (max(aa.y, bb.y)<min(cc.y, dd.y))

  	{

  		return false;

  	}

  	if (max(cc.x, dd.x)<min(aa.x, bb.x))

  	{

  		return false;

  	}

  	if (max(cc.y, dd.y)<min(aa.y, bb.y))

  	{

  		return false;

  	}

  	if (mult(cc, bb, aa)*mult(bb, dd, aa)<0)

  	{

  		return false;

  	}

  	if (mult(aa, dd, cc)*mult(dd, bb, cc)<0)

  	{

  		return false;

  	}

  	return true;

  }