-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
executable file
·311 lines (256 loc) · 7.11 KB
/
main.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
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
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <time.h>
#include "camera.h"
#include "graphics.h"
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#define DEFAULT_HUE_MIN 0
#define DEFAULT_HUE_MAX 60
#define DEFAULT_SAT_MIN 100
#define DEFAULT_SAT_MAX 255
#define DEFAULT_VAL_MIN 100
#define DEFAULT_VAL_MAX 255
#define DEFAULT_WIDTH 320
#define DEFAULT_HEIGHT 320
float minTargetRadiusFactor = 0.05;
bool do_thresholding = true;
int flipv = 0;
int fliph = 0;
int awbmode = 1;
int hue_min = DEFAULT_HUE_MIN;
int hue_max = DEFAULT_HUE_MAX;
int sat_min = DEFAULT_SAT_MIN;
int sat_max = DEFAULT_SAT_MAX;
int val_min = DEFAULT_VAL_MIN;
int val_max = DEFAULT_VAL_MAX;
int cap_width = DEFAULT_WIDTH;
int cap_height= DEFAULT_HEIGHT;
static bool parse_min_max( int argc, const char **argv, const char *arg, int *i, int *min_p, int *max_p, int max_val );
static bool parse_int( int argc, const char **argv, const char *arg, int *i, int *val_p );
static bool parse_args( int argc, const char **argv );
static void usage( const char *name );
static int64_t gettime( void );
using namespace std;
int main(int argc, const char **argv)
{
if (!parse_args( argc, argv))
{
usage( argv[0]);
return -1;
}
//init graphics and the camera
InitGraphics();
CCamera* cam = StartCamera(cap_width, cap_height, 30, 1, true, awbmode, flipv, fliph );
GfxTexture texture;
texture.Create(cap_width, cap_height);
cv::Mat frame;
printf("Starting capture: %dx%d\n", cap_width, cap_height );
if ( do_thresholding )
{
printf("Applying color threshold: hue %d..%d, sat %d..%d, val %d..%d\n", hue_min, hue_max, sat_min, sat_max, val_min, val_max);
}
if ( fliph )
{
printf("image flipped horizontally\n");
}
if ( flipv )
{
printf("image flipped vertically\n");
}
int64_t start = gettime();
int64_t update_interval = 5*1000;
int frames = 0;
while (true)
{
const void* frame_data; int frame_sz;
if(cam->BeginReadFrame(0,frame_data,frame_sz))
{
frame = cv::Mat(cap_height, cap_width, CV_8UC4, (void*)frame_data);
if (do_thresholding)
{
cv::cvtColor(frame, frame, CV_RGB2HSV);
}
cam->EndReadFrame(0);
if (do_thresholding)
{
cv::inRange(frame, cv::Scalar(hue_min / 2, sat_min, val_min), cv::Scalar(hue_max / 2, sat_max , val_max), frame);
cv::Mat str_el = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3, 3));
morphologyEx(frame, frame, cv::MORPH_OPEN, str_el);
morphologyEx(frame, frame, cv::MORPH_CLOSE, str_el);
std::vector<cv::Point2i> center;
std::vector<int> radius;
{
std::vector<std::vector<cv::Point> > contours;
std::vector<cv::Vec4i> heirarchy;
cv::findContours( frame.clone(), contours, heirarchy, CV_RETR_TREE, CV_CHAIN_APPROX_NONE);
size_t count = contours.size();
float minR = ((float)cap_width)*minTargetRadiusFactor;
for( int i=0; i<count; i++)
{
cv::Point2f c;
float r;
cv::minEnclosingCircle( contours[i], c, r);
if ( r >= minR)
{
center.push_back(c);
radius.push_back(r);
}
}
}
cvtColor( frame, frame, CV_GRAY2RGBA);
// draw all the bounding circles
size_t count = center.size();
for( int i = 0; i < count; i++)
{
cv::circle(frame, center[i], radius[i], cv::Scalar(255,0,0, 255), 3);
}
}
}
//begin frame, draw the texture then end frame (the bit of maths just fits the image to the screen while maintaining aspect ratio)
BeginFrame();
texture.SetPixels(frame.data);
float aspect_ratio = float(cap_width)/float(cap_height);
float screen_aspect_ratio = 1280.f/720.f;
DrawTextureRect(&texture,-aspect_ratio/screen_aspect_ratio,-1.f,aspect_ratio/screen_aspect_ratio,1.f);
EndFrame();
// update frame rate results periodically
frames++;
int64_t now = gettime();
int64_t interval = now - start;
if ( interval > update_interval )
{
int hfps = (frames*100000)/interval;
printf("Frame rate: %d.%02d fps\n", hfps/100, hfps%100);
// reset
start = now;
frames = 0;
}
}
StopCamera();
}
bool parse_args( int argc, const char **argv)
{
for (int i = 1; i<argc; i++)
{
const char *arg = argv[i];
if ( *arg++ != '-')
{
return false;
}
char sw = *arg++;
switch ( sw )
{
case 'f' :
{
switch (*arg++)
{
case 'v': flipv = 1; break;
case 'h': fliph = 1; break;
default: return false;
}
break;
}
case 'r':
{
do_thresholding = false;
break;
}
case 'H':
{
if ( !parse_min_max( argc, argv, arg, &i, &hue_min, &hue_max, 360))
{
return false;
}
break;
}
case 'S':
{
if ( !parse_min_max( argc, argv, arg, &i, &sat_min, &sat_max, 255))
{
return false;
}
break;
}
case 'V':
{
if ( !parse_min_max( argc, argv, arg, &i, &val_min, &val_max, 255))
{
return false;
}
break;
}
case 'w':
{
if ( !parse_int( argc, argv, arg, &i, &cap_width))
{
return false;
}
break;
}
case 'h':
{
if ( !parse_int( argc, argv, arg, &i, &cap_height))
{
return false;
}
break;
}
default: return false;
}
}
return true;
}
bool parse_min_max( int argc, const char **argv, const char *arg, int *i, int *min_p, int *max_p, int max_val)
{
if (*arg == 0)
{
if (++(*i) >= argc )
{
return false;
}
arg = argv[*i];
}
int min, max;
if ( sscanf(arg, "%u..%u", &min, &max) != 2)
{
return false;
}
*min_p = min >= 0 ?( min < max_val ? (min <= max ? min : max) : max_val) : 0;
*max_p = max >= 0 ?( max < max_val ? (max >= min ? max : min) : max_val) : 0;
return true;
}
bool parse_int( int argc, const char **argv, const char *arg, int *i, int *val_p )
{
if (*arg == 0)
{
if (++(*i) >= argc )
{
return false;
}
arg = argv[*i];
}
*val_p = atoi(arg);
return true;
}
void usage( const char *name )
{
printf("Usage: %s [options]\n", name );
printf(" Where options are:\n");
printf(" -fh : flip image horizontally\n");
printf(" -fv : flip image vertically\n");
printf(" -w <pixels> : capture image width - defailt %d\n", DEFAULT_WIDTH);
printf(" -h <pixels> : capture image height - defailt %d\n", DEFAULT_HEIGHT);
printf(" -r : don't process input\n");
printf(" -H <min>-<max> : hue range (0..360) - defailt %d..%d\n", DEFAULT_HUE_MIN, DEFAULT_HUE_MAX);
printf(" -S <min>-<max> : saturation range (0..255) - defailt %d..%d\n", DEFAULT_SAT_MIN, DEFAULT_SAT_MAX);
printf(" -V <min>-<max> : value range (0..255) - defailt %d..%d\n", DEFAULT_VAL_MIN, DEFAULT_VAL_MAX);
printf("\n example: %s -h 10..50 -s 50..255 -v 100..200\n", name);
}
int64_t gettime( void )
{
struct timespec now;
clock_gettime(CLOCK_REALTIME, &now);
return (int64_t)now.tv_sec*1000 + (int64_t)now.tv_nsec/1000000;
}