-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.cpp
255 lines (199 loc) · 6.65 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
#include "opencv2/opencv.hpp"
#include "opencv2/gpu/gpu.hpp"
#include <sys/time.h>
#include <cstdio>
#include <cmath>
#include <cstdarg>
using namespace cv;
#define FEED_SIZE 4
#define PER_FRAME_TIME_LOGGING 0
#define SHOW_FEED_WINDOW 1
#define SHOW_OTHER_WINDOWS 1
#define SHOW_OUTPUT_WINDOW 1
#define DRAW_DEBUG_DATA 1
#if (FEED_SIZE == 1)
const int FEED_WIDTH = 320;
const int FEED_HEIGHT = 240;
#endif
#if (FEED_SIZE == 2)
const int FEED_WIDTH = 640;
const int FEED_HEIGHT = 480;
#endif
#if (FEED_SIZE == 3)
const int FEED_WIDTH = 1280;
const int FEED_HEIGHT = 960;
#endif
#if (FEED_SIZE == 4)
const int FEED_WIDTH = 1920;
const int FEED_HEIGHT = 1080;
#endif
double avgCaptureTime = 0,
avgConversionTime = 0,
avgSplitTime = 0,
avgProcessingTime = 0,
avgDisplayTime = 0;
long captureTime = 0,
conversionTime = 0,
splitTime = 0,
processingTime = 0,
displayTime = 0;
int nFrames = 0;
const double areaRatio = 0.65;
void initGUI() {
#if (SHOW_FEED_WINDOW == 1)
namedWindow("feed");
#endif
#if (SHOW_OTHER_WINDOWS == 1)
namedWindow("hue");
namedWindow("sat");
namedWindow("val");
namedWindow("balloonyness");
#endif
#if (SHOW_OUTPUT_WINDOW == 1)
namedWindow("debugOverlay");
#endif
}
void recordTime(long delta, double *avgTime) {
*avgTime = (*avgTime * nFrames + delta) / (nFrames + 1);
}
long getTimeDelta(struct timeval timea, struct timeval timeb) {
return 1000000 * (timeb.tv_sec - timea.tv_sec) +
(int(timeb.tv_usec) - int(timea.tv_usec));
}
void log(const char* msg, ...) {
#if (PER_FRAME_TIME_LOGGING == 1)
va_list args;
va_start(args, msg);
printf(msg, args);
#endif
}
void captureFrame(VideoCapture &camera, Mat &frame_host, gpu::GpuMat &frame, Mat &debugOverlay) {
struct timeval timea, timeb;
gettimeofday(&timea, NULL);
camera >> frame_host;
debugOverlay = frame_host.clone();
frame.upload(frame_host);
gettimeofday(&timeb, NULL);
captureTime = getTimeDelta(timea, timeb);
log("capture frame time used:\t%ld\n", captureTime);
}
void convertToHSV(gpu::GpuMat &frame, gpu::GpuMat &hue, gpu::GpuMat &sat, gpu::GpuMat &val) {
struct timeval timea, timeb;
gpu::GpuMat hsv;
vector<gpu::GpuMat> hsvplanes(3);
hsvplanes[0] = hue;
hsvplanes[1] = sat;
hsvplanes[2] = val;
gettimeofday(&timea, NULL);
gpu::cvtColor(frame, hsv, CV_BGR2HSV);
gettimeofday(&timeb, NULL);
conversionTime = getTimeDelta(timea, timeb);
log("color conversion time used:\t%ld\n", conversionTime);
gettimeofday(&timea, NULL);
gpu::split(hsv, hsvplanes);
hue = hsvplanes[0];
sat = hsvplanes[1];
val = hsvplanes[2];
gettimeofday(&timeb, NULL);
splitTime = getTimeDelta(timea, timeb);
log("split planes time used: \t%ld\n", splitTime);
}
void processFrame(gpu::GpuMat &hue, gpu::GpuMat &sat, gpu::GpuMat &balloonyness, Mat &debugOverlay) {
struct timeval timea, timeb;
gpu::GpuMat huered, scalehuered, scalesat, thresh;
Mat thresh_host;
vector< vector< Point > > contours;
gettimeofday(&timea, NULL);
gpu::absdiff(hue, Scalar(90), huered);
gpu::divide(huered, Scalar(4), scalehuered);
gpu::divide(sat, Scalar(16), scalesat);
gpu::multiply(scalehuered, scalesat, balloonyness);
gpu::threshold(balloonyness, thresh, 200, 255, THRESH_BINARY);
thresh.download(thresh_host);
findContours(thresh_host, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
#if (DRAW_DEBUG_DATA == 1)
drawContours(debugOverlay, contours, -1, Scalar(255, 0, 0));
#endif
vector< Point2f > circleCenters(contours.size());
vector< float > circleRadii(contours.size());
Point2f center;
float radius;
for (int n = 0; n < contours.size(); ++n) {
minEnclosingCircle(contours[n], center, radius);
#if (DRAW_DEBUG_DATA == 1)
circle(debugOverlay, center, radius, Scalar(0, 255, 255));
#endif
if (contourArea(contours[n]) >= areaRatio * radius*radius*3.1415926) {
circle(debugOverlay, center, radius, Scalar(0, 255, 0), 2);
}
}
gettimeofday(&timeb, NULL);
processingTime = getTimeDelta(timea, timeb);
log("frame processing time used:\t%ld\n", processingTime);
}
void displayOutput(Mat frame, gpu::GpuMat hue, gpu::GpuMat sat, gpu::GpuMat val, gpu::GpuMat balloonyness, Mat debugOverlay) {
struct timeval timea, timeb;
gettimeofday(&timea, NULL);
#if (SHOW_FEED_WINDOW == 1)
imshow("feed", frame);
#endif
#if (SHOW_OTHER_WINDOWS ==1)
Mat hue_host, sat_host, val_host, balloonyness_host;
hue.download(hue_host);
sat.download(sat_host);
val.download(val_host);
balloonyness.download(balloonyness_host);
imshow("hue", hue_host);
imshow("sat", sat_host);
imshow("val", val_host);
imshow("balloonyness", balloonyness_host);
#endif
#if (SHOW_OUTPUT_WINDOW == 1)
imshow("debugOverlay", debugOverlay);
#endif
gettimeofday(&timeb, NULL);
displayTime = getTimeDelta(timea, timeb);
log("display frame time used:\t%ld\n", displayTime);
}
int main() {
struct timeval timea, timeb, startTime, endTime;
gettimeofday(&startTime, NULL);
Mat frame_host, thresh_host, debugOverlay;
gpu::GpuMat frame, hsv, hue, sat, val, huered, scalehuered, scalesat, balloonyness, thresh;
VideoCapture camera(0);
camera.set(CV_CAP_PROP_FRAME_WIDTH, FEED_WIDTH);
camera.set(CV_CAP_PROP_FRAME_HEIGHT, FEED_HEIGHT);
log("optimized code: %d\n", useOptimized());
log("cuda devices: %d\n", gpu::getCudaEnabledDeviceCount());
log("current device: %d\n", gpu::getDevice());
initGUI();
log("starting balloon recognition\n");
while(true) {
captureFrame(camera, frame_host, frame, debugOverlay);
convertToHSV(frame, hue, sat, val);
processFrame(hue, sat, balloonyness, debugOverlay);
displayOutput(frame_host, hue, sat, val, balloonyness, debugOverlay);
recordTime(captureTime, &avgCaptureTime);
recordTime(conversionTime, &avgConversionTime);
recordTime(splitTime, &avgSplitTime);
recordTime(processingTime, &avgProcessingTime);
recordTime(displayTime, &avgDisplayTime);
++nFrames;
if (waitKey(30) >= 0) {
break;
}
}
gettimeofday(&endTime, NULL);
long totalTimeUsec = getTimeDelta(startTime, endTime);
double totalTimeSec = double(totalTimeUsec)/1000000.0;
printf("key press detected. printing statistics.\n");
printf("%d frames captured over %ld microseconds (%lf seconds)\n", nFrames,
totalTimeUsec, totalTimeSec);
printf("ran at %lf Frames per Second\n", nFrames/totalTimeSec);
printf("average capture frame time used:\t%lf\n", avgCaptureTime);
printf("average color conversion time used:\t%lf\n", avgConversionTime);
printf("average split planes time used: \t%lf\n", avgSplitTime);
printf("average frame processing time used:\t%lf\n", avgProcessingTime);
printf("average display frame time used:\t%lf\n", avgDisplayTime);
printf("terminating...\n");
}