-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswitch_detect.cpp
More file actions
312 lines (289 loc) · 8.06 KB
/
switch_detect.cpp
File metadata and controls
312 lines (289 loc) · 8.06 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*图像检测部分*/
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/utility.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "switch_detect.h"
using namespace cv;
using namespace std;
vector<Vec4f> lines_adv;
vector<Lines> candidate;
vector<vector<Lines>> groups(21);
vector<Lines> tmp;
vector<Lines> edge_fin;
vector<int> direction(181);
int haveballas;
void LSD(string pic_path)
//对图像进行LSD线检测
{
//freopen("./length_lsd6.txt", "w", stdout);
Mat image=imread(pic_path,IMREAD_GRAYSCALE);
Ptr<LineSegmentDetector> ls=createLineSegmentDetector(LSD_REFINE_STD);
ls->detect(image, lines_adv);
Mat drawnlines(image);
ls->drawSegments(drawnlines, lines_adv);
//imshow("LSD", drawnlines);
//waitKey();
}
//对直线长度排序
void InsertSortByLength(vector<Lines> &l) {
for(size_t i = 1; i < l.size(); ++i){
for(int j = i;j > 0;--j){
if(l[j].length > l[j - 1].length){
Lines temp = l[j];
l[j] = l[j-1];
l[j-1] = temp;
}
}
}
}
//对原点到直线的距离进行排序
void InsertSortByRho(vector<Lines> &l) {
for(size_t i = 1;i < l.size();++i){
for(int j = i;j > 0;--j){
if(l[j].rho < l[j - 1].rho){
Lines temp = l[j];
l[j] = l[j-1];
l[j-1] = temp;
}
}
}
}
/*对有砟和无砟进行检测*/
int have_ballas() {
int count = 0;
int count1 = 0;
for(size_t i = 0; i<lines_adv.size(); i++)
{
Vec4f l =lines_adv[i];
if(gettheta(l) < 0)
direction[gettheta(l)+180]++;
else
direction[gettheta(l)]++;
}
printf("角度\t数量\t\n");
for(size_t j = 0; j<direction.size(); j++)
{
printf("%zu\t%d\t\n",j,direction[j]);
if (direction[j] > thresh_num)
count++;
}
printf("count = %d\n", count);
for(size_t j=0; j<direction.size(); j++)
{
count1 = count1 + direction[j];
}
printf("count = %d\n", count1);
//if(count > thresh_sum)
if(count1 > pan)
{
haveballas = 1;
printf("该场景为有砟场景\n");
//return true;
}
else
{
haveballas = 0;
printf("该场景为无砟场景\n");
//return false;
}
return haveballas;
}
/*对直线进行筛选*/
int select() {
int isswitch = 0;
for(size_t i=0;i<lines_adv.size();i++)
{
Vec4f la=lines_adv[i];
//初步筛选符合轨道边缘条件的直线
if ((getlength(la) > a1))
{
if(gettheta(la) < 0 && gettheta(la)+180 >= 80 && gettheta(la)+180 <= 100)
candidate.push_back(vec_init(la, static_cast<int>(i)));
if(gettheta(la) >=0 && gettheta(la) >= 80 && gettheta(la) <=100)
candidate.push_back(vec_init(la, static_cast<int>(i)));
}
}
printf("candidate序号\t 长度\t theta\t rho\t\n");
for (size_t c = 0; c< candidate.size();c++)
{
if(candidate[c].theta < 0)
printf("%d\t%lf\t%d\t%lf\t\n",candidate[c].seq,candidate[c].length,candidate[c].theta + 180,candidate[c].rho);
else
printf("%d\t%lf\t%d\t%lf\t\n",candidate[c].seq,candidate[c].length,candidate[c].theta,candidate[c].rho);
}
//分组
for(size_t j = 0; j<candidate.size(); j++)
{
Lines l = candidate[j];
if (l.theta == 80)
groups[0].push_back(l);
else if (l.theta == 81)
groups[1].push_back(l);
else if (l.theta == 82)
groups[2].push_back(l);
else if (l.theta == 83)
groups[3].push_back(l);
else if (l.theta == 84)
groups[4].push_back(l);
else if (l.theta == 85)
groups[5].push_back(l);
else if (l.theta ==86)
groups[6].push_back(l);
else if (l.theta == 87)
groups[7].push_back(l);
else if (l.theta == 88)
groups[8].push_back(l);
else if (l.theta == 89)
groups[9].push_back(l);
else if (l.theta == 90)
groups[10].push_back(l);
else if (l.theta == 91)
groups[11].push_back(l);
else if (l.theta == 92)
groups[12].push_back(l);
else if (l.theta == 93)
groups[13].push_back(l);
else if (l.theta == 94)
groups[14].push_back(l);
else if (l.theta == 95)
groups[15].push_back(l);
else if (l.theta == 96)
groups[16].push_back(l);
else if (l.theta == 97)
groups[17].push_back(l);
else if (l.theta == 98)
groups[18].push_back(l);
else if (l.theta == 99)
groups[19].push_back(l);
else
groups[20].push_back(l);
}
//对每个分组进行筛选
for(size_t k = 0; k< groups.size();k++ )
{
if (groups[k].size() == 0)
continue;
InsertSortByLength(groups[k]);
if(groups[k][0].length > a2)
tmp.push_back(groups[k][0]);
for(size_t p = 1;p<groups[k].size();p++)
if(groups[k][p].length > a2 && abs(groups[k][p].rho - groups[k][p-1].rho) > r1)
tmp.push_back(groups[k][p]);
}
//对rho增序排序
InsertSortByRho(tmp);
//最终筛选
int z = 0;
if(haveballas == 0)
{
size_t n;
for( n = 0;n<tmp.size();)
{
if(tmp[n].rho<f1)
{
n++;
}
else if(tmp[n].rho>=f1)
{
edge_fin.push_back(tmp[n]);
break;
}
}
for(size_t m = n+1; m<tmp.size(); m++)
{
if(abs(tmp[m].rho - edge_fin[z].rho) > r2 && tmp[m].rho> f1 && tmp[m].rho < f2)
{
edge_fin.push_back(tmp[m]);
z++;
}
}
}
else
{
edge_fin.push_back(tmp[0]);
for(size_t m = 1; m<tmp.size(); m++)
{
if(abs(tmp[m].rho - edge_fin[z].rho) > r2)
{
edge_fin.push_back(tmp[m]);
z++;
}
}
}
printf("\n");
printf("edge序号\t长度\ttheta\trho\t\n");
for (size_t c = 0; c< edge_fin.size();c++)
{
if (edge_fin[c].theta < 0)
printf("%d\t%lf\t%d\t%lf\t\n",edge_fin[c].seq,edge_fin[c].length,edge_fin[c].theta + 180,edge_fin[c].rho);
else
printf("%d\t%lf\t%d\t%lf\t\n",edge_fin[c].seq,edge_fin[c].length,edge_fin[c].theta,edge_fin[c].rho);
}
//给出判断
if(edge_fin.size() <= 2)
{
printf("图像属于单轨场景\n");
isswitch = 0;
}
else
{
printf("图像属于道岔场景\n");
isswitch = 1;
}
return isswitch;
}
//计算直线长度
double getlength(Vec4f l){
return sqrt(pow((l[0]-l[2]), 2) + pow((l[1]-l[3]),2));
}
//计算直线与x轴的角度
int gettheta(Vec4f l) {
double arc;
if (l[0] == l[2])
return 90;
else {
arc = atan((l[1] - l[3]) / (l[0] - l[2]));
return int(arc * 180 / pi);
}
}
//计算原点到直线的距离
double getrho(Vec4f l) {
double k;
if(l[0] == l[2])
return l[0];
else
{
k = (l[1] - l[3]) / (l[0] - l[2]);
return abs(k * l[0] - l[1])/sqrt(1 + pow(k,2));
}
}
int getseq(int i) {
return i+1;
}
//结构体数组初始化
Lines vec_init(Vec4f l, int i) {
Lines line;
line.length = getlength(l);
line.seq = getseq(i);
line.rho = getrho(l);
line.theta = gettheta(l);
return line;
}
void clear_arrays()
{
lines_adv.clear();
lines_adv.shrink_to_fit();
candidate.clear();
candidate.shrink_to_fit();
for (size_t i = 0; i<groups.size();i++)
groups[i].clear();
tmp.clear();
tmp.shrink_to_fit();
edge_fin.clear();
edge_fin.shrink_to_fit();
for (size_t j = 0; j<direction.size();j++)
direction[j] = 0;
}