-
Notifications
You must be signed in to change notification settings - Fork 4
/
ssl_feature_gen.cpp
257 lines (215 loc) · 6.75 KB
/
ssl_feature_gen.cpp
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
#include<bits/stdc++.h>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
#include <ctime>
#include <fstream>
#include <sstream>
using namespace cv;
using namespace std;
#define SSL_FIELD_WIDTH 6000
#define SSL_FIELD_HEIGHT 4500
#define ROWS 200
#define COLS 300
int DBOX_RADIUS = (int)(1000.0*ROWS/SSL_FIELD_HEIGHT); //66
int DBOX_SIDE = (int)(500.0*ROWS/SSL_FIELD_HEIGHT); //33
#define BOT_RADIUS 15
#define DATASET 100000
#define DBOX_BOTS 3
#define HALFLINE_BOTS 2
float PI = 180.0/3.14159265;
#define ERROR 2.2
int left_dbox_x(int i, int print)
{
if(i <= ROWS/2 - DBOX_SIDE/2) //i < 200 - 16 = 184
{
int y = ROWS/2 - i - DBOX_SIDE/2; //y = 184 - i
if(y<2) return DBOX_RADIUS; //boundary case
if(y-1 >= DBOX_RADIUS) return BOT_RADIUS;
if(print)
{
printf("%d %d\n",DBOX_RADIUS,DBOX_SIDE);
printf("i = %d\ny = %d\nacos = %lf\ntan = %lf\nx = %d\n", i, y, acos((float)(y-1)/DBOX_RADIUS)*PI, tan(acos((float)(y-1)/DBOX_RADIUS)), (int)(tan(acos((float)(y-1)/DBOX_RADIUS))*y));
}
int x = (int)(y*tan(acos((float)(y-1)/DBOX_RADIUS)));
if(x >= DBOX_RADIUS) return DBOX_RADIUS;
return x;
}
else if(i < ROWS/2 + DBOX_SIDE/2) //i < 200 + 16 = 216
{
if(print) printf("inmid\n");
return DBOX_RADIUS;
}
else
{
int y = i - ROWS/2 - DBOX_SIDE/2; //i - 200 - 16 = i - 216
if(y<=2) return DBOX_RADIUS;
if(y-1 >= DBOX_RADIUS) return BOT_RADIUS;
if(print) printf("i = %d\ny = %d\nacos = %lf\ntan = %lf\nx = %d\n", i, y, acos((float)(y-1)/DBOX_RADIUS)*PI, tan(acos((float)(y-1)/DBOX_RADIUS)), (int)(tan(acos((float)(y-1)/DBOX_RADIUS))*y));
int x = (int)(y*tan(acos((float)(y-1)/DBOX_RADIUS)));
if(x >= DBOX_RADIUS) return DBOX_RADIUS;
return x;
}
}
int right_dbox_x(int i)
{
if(i <= ROWS/2 - DBOX_SIDE/2)
{
int y = ROWS/2 - i - DBOX_SIDE/2;
if(y<2) return DBOX_RADIUS;
int x = COLS - (int)(y*tan(acos((float)(y-1)/DBOX_RADIUS)));
if(x <= COLS - DBOX_RADIUS) return COLS - DBOX_RADIUS;
return x;
}
else if(i < ROWS/2 + DBOX_SIDE/2)
{
return COLS - DBOX_RADIUS;
}
else
{
int y = i - ROWS/2 - DBOX_SIDE/2;
if(y<2) return DBOX_RADIUS;
int x = COLS - (int)(y*tan(acos((float)(y-1)/DBOX_RADIUS)));
if(x <= COLS - DBOX_RADIUS) return COLS - DBOX_RADIUS;
return x;
}
}
int isValid(int i , int j , Mat img, int r)
{
if ( i < img.rows && j < img.cols && i >= 0 && j >= 0)
return 1;
return 0;
}
Mat circle(Mat a, int i, int j, int r)
{
int p,q;
//i = i-r;
//j = j-r;
for(p=i-r;p<i+r;p++)
for(q=j-sqrt(pow(r,2)-pow(p-i,2));q<j+sqrt(pow(r,2)-pow(p-i,2));q++)
{
if(isValid(q,p,a,r))
a.at<uchar>(p,q)=255;
}
return a;
}
Mat draw_field_lines(Mat img)
{
for(int i=0; i<ROWS; i++)
{
if(i > ROWS/2 - DBOX_SIDE/2 - DBOX_RADIUS && i < ROWS/2 + DBOX_SIDE/2 + DBOX_RADIUS)
{
int j1;
//int j2;
j1 = left_dbox_x(i,0) - 1;
//j2 = right_dbox_x(i) - 1;
//printf("(%d,%d)---------------\n",j1,j2);
//if(j1<0 || j2<0) {printf("(%d,%d)\n%d\n",j1,j2,(int)(acos(23.0/27)*PI)); print=1;}
//else print=0;
img.at<uchar>(i,j1) = 150;
//img.at<uchar>(i,j2) = 150;
}
img.at<uchar>(i,COLS/2) = 150;
}
return img;
}
Mat random_dbox_bots(Mat img, ofstream *myfile)
{
int dbox_botPos_i[DBOX_BOTS];
dbox_botPos_i[0] = ROWS/2 - (int)((float)DBOX_SIDE*ERROR/2) - rand()%DBOX_RADIUS + BOT_RADIUS;
dbox_botPos_i[1] = ROWS/2 - (int)(DBOX_SIDE*ERROR/2) + rand()%((int)(DBOX_SIDE*ERROR - 2*BOT_RADIUS)) + BOT_RADIUS;
dbox_botPos_i[2] = ROWS/2 + (int)(DBOX_SIDE*ERROR/2) + rand()%DBOX_RADIUS - BOT_RADIUS;
for(int i=0; i<DBOX_BOTS; i++)
{
int y = dbox_botPos_i[i];
int x = left_dbox_x(y,0) - 1;
img = circle(img,y,x,BOT_RADIUS+1);
*myfile << x << " " << y << " " ;
}
return img;
}
/*Mat defensive_dbox_bots(Mat img, ofstream *myfile)
{
int i = ROWS/2 - (int)(DBOX_SIDE*ERROR/2) + rand()%((int)(DBOX_SIDE*ERROR - 2*BOT_RADIUS)) + BOT_RADIUS;
int i1 = i - BOT_RADIUS;
int i2 = i + BOT_RADIUS;
img = circle(img,i1,DBOX_RADIUS,BOT_RADIUS+1);
img = circle(img,i2,DBOX_RADIUS,BOT_RADIUS+1);
*myfile << DBOX_RADIUS << " " << i1 << " " << DBOX_RADIUS << " " << i2 << " ";
i = rand()%(DBOX_RADIUS*2) - DBOX_RADIUS;
if(i < 0) {i = ROWS/2 - (int)(DBOX_SIDE*ERROR/2) + i + BOT_RADIUS; printf("i<0\n");}
else {i = ROWS/2 + (int)(DBOX_SIDE*ERROR/2) + i - BOT_RADIUS; printf("i>0\n");}
img = circle(img,i,left_dbox_x(i)-1,BOT_RADIUS+1);
*myfile << left_dbox_x(i)-1 << " " << i << " ";
return img;
}*/
Mat defensive_dbox_bots(Mat img, ofstream *myfile)
{
int i = rand()%(DBOX_SIDE + 2*DBOX_RADIUS - 2*BOT_RADIUS);
//printf("i=%d\n",i);
int i1 = ROWS/2 - DBOX_SIDE/2 - DBOX_RADIUS + i; //118 + i
int i2 = ROWS/2 - DBOX_SIDE/2 - DBOX_RADIUS + i + 2*BOT_RADIUS;
int j1 = left_dbox_x(i1,0)-1;
int j2 = left_dbox_x(i2,0)-1;
img = circle(img,i1,j1,BOT_RADIUS+1);
img = circle(img,i2,j2,BOT_RADIUS+1);
*myfile << j1 << " " << i1 << " " << j2 << " " << i2 << " ";
int i3;
if(i <= BOT_RADIUS)
i3 = ROWS/2 - DBOX_SIDE/2 - DBOX_RADIUS + BOT_RADIUS + i + rand()%(DBOX_SIDE + 2*DBOX_RADIUS - 2*BOT_RADIUS - i);
else if(i <= DBOX_SIDE + 2*DBOX_RADIUS - 3*BOT_RADIUS) //i <= 33 + 2*66 - 3*15 = 33 + 132 - 45 = 120
{
i3 = rand()%(DBOX_SIDE + 2*DBOX_RADIUS - 8*BOT_RADIUS) - (DBOX_SIDE + 2*DBOX_RADIUS - 8*BOT_RADIUS)/2;
if(i3<0)
i3 = ROWS/2 - DBOX_SIDE/2 - DBOX_RADIUS - 2*BOT_RADIUS + i + i3;
else
i3 = ROWS/2 - DBOX_SIDE/2 - DBOX_RADIUS + 4*BOT_RADIUS + i + i3;
}
else
i3 = ROWS/2 - DBOX_SIDE/2 - DBOX_RADIUS + BOT_RADIUS + rand()%(i - 3*BOT_RADIUS);
//printf("i3=%d\n",i3);
int j3 = left_dbox_x(i3,0) - 1;
img = circle(img,i3,j3,BOT_RADIUS+1);
*myfile << j3 << " " << i3 << " ";
return img;
}
Mat random_halfline_bots(Mat img, ofstream *myfile)
{
int halfline_botPos_i[HALFLINE_BOTS];
for(int i=0; i<HALFLINE_BOTS; i++)
{
int flag;
do
{
flag = 1;
halfline_botPos_i[i] = BOT_RADIUS + rand()%(ROWS - 2*BOT_RADIUS);
for(int j=0; j<i; j++)
if(abs(halfline_botPos_i[j] - halfline_botPos_i[i]) < 2*BOT_RADIUS) flag=0;
}while(flag == 0);
img = circle(img,halfline_botPos_i[i],COLS/2,BOT_RADIUS+1);
*myfile << halfline_botPos_i[i] << " ";
}
*myfile << endl;
return img;
}
int main()
{
ofstream myfile;
myfile.open ("data/ssl_field_features.txt");
for(int count=0; count<DATASET; count++)
{
stringstream ss;
ss<<(count);
string img_name = "defensive_dataset/img" + ss.str() + ".jpg";
Mat img(ROWS,COLS,CV_8UC1,Scalar(0));
//img = draw_field_lines(img);
//img = random_dbox_bots(img,myfile);
img = defensive_dbox_bots(img,&myfile);
img = random_halfline_bots(img,&myfile);
/*namedWindow("SHOW",WINDOW_NORMAL);
imshow("SHOW",img);
while(waitKey(10)!=27){}*/
imwrite(img_name,img);
}
return 0;
}