/
arena.cpp
233 lines (184 loc) · 3.9 KB
/
arena.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
#include "arena.h"
Mat imageR;
Mat imageC;
int flag = 0,flagc=0;
int cnt = 0;
bool drawing_box = false;
bool sel = false;
Rect box;
Point2f point;
void draw_box( Mat img, Rect rect ){
rectangle( img,rect,cvScalar(0xff,0x00,0x00) );
}
void draw_circle( Mat img, Point2f center, int radius ){
circle( img,center,radius,cvScalar(0x00,0xff,0x00),CV_FILLED );
}
static void onMouseRect( int event, int x, int y, int, void* )
{
switch( event ){
case CV_EVENT_MOUSEMOVE:
if( drawing_box ){
box.width = x-box.x;
box.height = y-box.y;
}
break;
case CV_EVENT_LBUTTONDOWN:
drawing_box = true;
box = cvRect( x, y, 0, 0 );
break;
case CV_EVENT_LBUTTONUP:
drawing_box = false;
if( box.width < 0 ){
box.x += box.width;
box.width *= -1;
}
if( box.height < 0 ){
box.y += box.height;
box.height *= -1;
}
box.x+=1;
box.y+=1;
box.width-=1;
box.height-=1;
draw_box( imageR, box );
flag=1;
break;
}
}
Rect selectRect( Mat argm )
{
Mat image0 , ROI;
Rect b;
char c;
argm.copyTo(image0);
if( image0.empty() )
{
printf("Image empty\n");
return Rect();
}
image0.copyTo(imageR);
namedWindow( "image", 1 );
setMouseCallback( "image", onMouseRect, 0 );
while(1)
{
if(drawing_box)
{
draw_box(imageR,box);
}
if(flag==1)
{
ROI = imageR(box);
imshow("ROI",ROI);
c = waitKey(0);
if(c=='c')
{
break;
}
else
{
ROI.release();
destroyWindow("ROI");
image0.copyTo(imageR);
flag = 0;
}
}
imshow("image",imageR);
if(waitKey(15)==27)
return Rect();
image0.copyTo(imageR);
}
b = box;
return b;
}
static void onMouseSelect( int event, int x, int y, int, void* )
{
switch( event ){
case CV_EVENT_MOUSEMOVE:
{
if(sel)
point = Point2f((float)x,(float)y);
break;
}
case CV_EVENT_LBUTTONDOWN:
{
point = Point2f((float)x,(float)y);
sel = true;
break;
}
case CV_EVENT_LBUTTONUP:
{
sel = false;
point = Point2f((float)x,(float)y);
draw_circle(imageC,point,10);
cnt++;
flagc=1;
break;
}
}
}
Point2f* selectPoint(Mat src)
{
Point2f* points;
points = (Point2f*)malloc(sizeof(Point2f)*4);
char c;
if( src.empty() )
{
printf("Image empty\n");
return NULL;
}
src.copyTo(imageC);
namedWindow( "SelectPoints", 1 );
cnt = 0;
setMouseCallback( "SelectPoints", onMouseSelect, 0 );
while(cnt<5)
{
if(flagc==1)
{
imshow("SelectPoints",imageC);
c = waitKey(0);
if(c=='c')
{
points[cnt-1] = point;
printf("\ncnt:%d\n",cnt);
if(cnt == 4)
break;
}
else
{
cnt--;
printf("\ncnt:%d",cnt);
src.copyTo(imageC);
}
flagc = 0;
cout<<"point selection done"<<endl;
}
imshow("SelectPoints",imageC);
if(waitKey(15)==27)
return NULL;
//src.copyTo(imageC);
}
printf("\nReturning\n");
return points;
}
Mat getTransformMat(Point2f* srcq )
{
Point2f srcQuad[4],dstQuad[4];
Mat warp_perspective;
srcQuad[0] = srcq[0];
srcQuad[1] = srcq[1];
srcQuad[2] = srcq[2];
srcQuad[3] = srcq[3];
dstQuad[0] = Point2f(192.0/5.0,0.0);
dstQuad[1] = Point2f(3008.0/5.0,0.0);
dstQuad[2] = Point2f(3008.0/5.0,float(image.rows));
dstQuad[3] = Point2f(192.0/5.0,float(image.rows));
warp_perspective= getPerspectiveTransform(srcQuad,dstQuad);
//warpPerspective(src, warped, warp_perspective, Size(640,480));
return warp_perspective;
}
Mat perspectiveArena(Mat warp_Mat, Mat src )
{
Mat warped;
warpPerspective(src, warped, warp_Mat, Size(image.cols, image.rows));
return warped;
}