/
demoMain.cpp
384 lines (349 loc) · 9.21 KB
/
demoMain.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include <opencv2/imgcodecs/imgcodecs.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include "floatMul.h"
#include "featureSupport.h"
#include <string>
#include <iostream>
#include <iomanip>
#ifdef HAVE_CUDA
// CUDA includes
#include <cuda_runtime.h>
#endif // HAVE_CUDA
typedef unsigned char uchar;
cv::VideoCapture capture;
const char dumpFilename[] = "dump.png";
const char windowName[] = "demo";
const char header[] = "precision: ";
const char* imagePath[][3] = {
{ "defaultMaskHalf.png", "defaultMaskFloat.png", "defaultMaskGray.png"},
{ "lenaMaskHalf.png", "lenaMaskFloat.png", "lenaMaskGray.png"},
};
short *gainCuda = NULL;
float *gainFloatCuda = NULL;
uchar *gainByteCuda = NULL;
uchar *imageCuda = NULL;
uchar *imageResult = NULL;
const cv::Size vgaSize = cv::Size(640,480);
enum precision
{
precisionHalf,
precisionFloat,
precisionByte,
};
enum device
{
useCpuSimd,
useGpu,
};
void computeStatistics(double time, char key)
{
const int cHistoryMax = 16;
static double history[cHistoryMax];
static double square [cHistoryMax];
static int iHistory = 0;
switch (key)
{
case 'h':
case 'H':
case 'f':
case 'F':
case 'b':
case 'B':
case 'c':
case 'C':
case 'g':
case 'G':
for (int i = 0; i < cHistoryMax; i++)
{
history[i] = 0.0f;
square[i] = 0.0f;
}
iHistory = 0;
break;
default:
break;
}
history[iHistory] = time;
square[iHistory] = time * time;
double sum = 0.0f;
double squareSum = 0.0f;
for (int i = 0; i < cHistoryMax; i++)
{
sum += history[i];
squareSum += square[i];
}
double average = sum / (double)cHistoryMax;
squareSum /= cHistoryMax;
double variance = squareSum - (average * average);
std::cout << "average: " << std::fixed << std::setprecision(3) << average * 1000.0f << "[ms] ";
std::cout << "stddev: " << std::fixed << std::setprecision(3) << sqrt(variance) * 1000.0f << "[ms] \r";
std::cout << std::flush;
iHistory++;
iHistory = iHistory & (cHistoryMax-1);
}
#ifdef HAVE_CUDA
extern "C" void
launchCudaProcessHalf(dim3 grid, dim3 block, int sbytes,
short *gain,
uchar *imageInput,
uchar *imageOutput,
int imgw);
extern "C" void
launchCudaProcessFloat(dim3 grid, dim3 block, int sbytes,
float *gain,
uchar *imageInput,
uchar *imageOutput,
int imgw);
extern "C" void
launchCudaProcessByte(dim3 grid, dim3 block, int sbytes,
uchar *gain,
uchar *imageInput,
uchar *imageOutput,
int imgw);
double multiplyImageCuda(cv::Mat &image, cv::Mat gain)
{
unsigned int image_width = image.cols;
unsigned int image_height = image.rows;
unsigned int imageSizeGray = image_width * image_height;
unsigned int imageSizeColor = imageSizeGray * 3;
cudaMemcpy(imageCuda, image.data, imageSizeColor*sizeof(char), cudaMemcpyHostToDevice);
// calculate grid size
dim3 block(16, 16, 1);
dim3 grid(image_width / block.x, image_height / block.y, 1);
int64 begin, end;
switch (gain.elemSize())
{
case 1:
begin = cv::getTickCount();
cudaMemcpy(gainByteCuda, gain.data, imageSizeGray*sizeof(char), cudaMemcpyHostToDevice);
end = cv::getTickCount();
launchCudaProcessByte(grid, block, 0, gainByteCuda, imageCuda, imageResult, image_width);
break;
case 2:
begin = cv::getTickCount();
cudaMemcpy(gainCuda, gain.data, imageSizeGray*sizeof(short), cudaMemcpyHostToDevice);
end = cv::getTickCount();
launchCudaProcessHalf(grid, block, 0, gainCuda, imageCuda, imageResult, image_width);
break;
case 4:
begin = cv::getTickCount();
cudaMemcpy(gainFloatCuda, gain.data, imageSizeGray*sizeof(float), cudaMemcpyHostToDevice);
end = cv::getTickCount();
launchCudaProcessFloat(grid, block, 0, gainFloatCuda, imageCuda, imageResult, image_width);
break;
}
cudaMemcpy(image.data, imageResult, imageSizeColor*sizeof(char), cudaMemcpyDeviceToHost);
double tickCountElapsed = double(end - begin);
return tickCountElapsed/(double)cv::getTickFrequency();
}
#endif // HAVE_CUDA
double multiplyImage(cv::Mat &image, cv::Mat gain)
{
cv::Mat stub, b, g, r, stubGain;
std::vector<cv::Mat> arrayColor;
arrayColor.push_back(b);
arrayColor.push_back(g);
arrayColor.push_back(r);
cv::split(image, arrayColor);
int64 begin, end;
begin = cv::getTickCount();
switch (gain.elemSize())
{
case 1:
gain.convertTo(stubGain, CV_32FC1, 1/255.0f);
for (unsigned int i = 0; i < arrayColor.size(); i++)
{
arrayColor[i].convertTo(stub, CV_32FC1);
multiply(stub, stubGain, arrayColor[i], 1.0, CV_8UC1);
}
break;
case 2:
for (unsigned int i = 0; i < arrayColor.size(); i++)
{
multiply(arrayColor[i].data, (short*)gain.data, arrayColor[i].data, arrayColor[i].rows*arrayColor[i].cols);
}
break;
case 4:
default:
for (unsigned int i = 0; i < arrayColor.size(); i++)
{
arrayColor[i].convertTo(stub, CV_32FC1);
multiply(stub, gain, arrayColor[i], 1.0, CV_8UC1);
}
break;
}
end = cv::getTickCount();
cv::merge(arrayColor, image);
double tickCountElapsed = double(end - begin);
return tickCountElapsed/(double)cv::getTickFrequency();
}
bool isFinish(char key)
{
bool finishKey = false;
switch (key)
{
case 'q':
case 'Q':
case 27: // ESC key
finishKey = true;
default:
break;
}
return finishKey;
}
#ifdef HAVE_CUDA
void initArray(cv::Mat &image)
{
unsigned int w = image.cols;
unsigned int h = image.rows;
unsigned int s = w * h;
unsigned int c = s * 3;
cudaMalloc((short**)&gainCuda, (s*sizeof(short)));
cudaMalloc((float**)&gainFloatCuda, (s*sizeof(float)));
cudaMalloc((uchar**)&gainByteCuda, (s*sizeof(uchar)));
cudaMalloc((uchar**)&imageCuda, (c*sizeof(uchar)));
cudaMalloc((uchar**)&imageResult, (c*sizeof(uchar)));
}
void releaseArray()
{
cudaFree((void*)gainCuda);
cudaFree((void*)gainFloatCuda);
cudaFree((void*)gainByteCuda);
cudaFree((void*)imageCuda);
cudaFree((void*)imageResult);
}
bool initCuda()
{
int devID = 0;
int device_count= 0;
cudaGetDeviceCount(&device_count);
if (device_count < 1)
{
return false;
}
cudaSetDevice(devID);
return true;
}
#else
void initArray(cv::Mat &image){}
void releaseArray(){}
bool initCuda(){ return false; }
#endif // HAVE_CUDA
int main(int argc, char**argv)
{
capture.open(0);
if (capture.isOpened() == false)
{
std::cerr << "no capture device found" << std::endl;
return 1;
}
capture.set(CV_CAP_PROP_FRAME_WIDTH, vgaSize.width);
capture.set(CV_CAP_PROP_FRAME_HEIGHT, vgaSize.height);
if (capture.get(cv::CAP_PROP_FRAME_WIDTH) != (double)vgaSize.width || capture.get(cv::CAP_PROP_FRAME_HEIGHT) != (double)vgaSize.height)
{
std::cerr << "current device doesn't support " << vgaSize.width << "x" << vgaSize.height << " size" << std::endl;
return 2;
}
cv::Mat image;
capture >> image;
cv::namedWindow(windowName);
cv::imshow(windowName, image);
initCuda();
initArray(image);
char key = -1;
enum device statusDevice = useCpuSimd;
enum precision statusPrecision = precisionFloat;
int index = 1;
cv::Mat stub = cv::imread(imagePath[index][0], cv::IMREAD_UNCHANGED);
cv::Mat gain = cv::Mat(stub.rows, stub.cols/2, CV_16SC1, stub.data);
double elapsedTime;
while (isFinish(key) == false)
{
capture >> image;
switch (key)
{
case 'h':
case 'H':
// switch to half precision
statusPrecision = precisionHalf;
std::cout << std::endl << header << "half " << std::endl;
stub = cv::imread(imagePath[index][0], cv::IMREAD_UNCHANGED);
gain = cv::Mat(stub.rows, stub.cols/2, CV_16SC1, stub.data);
break;
case 'f':
case 'F':
// switch to single precision
statusPrecision = precisionFloat;
std::cout << std::endl << header << "single" << std::endl;
stub = cv::imread(imagePath[index][1], cv::IMREAD_UNCHANGED);
gain = cv::Mat(stub.rows, stub.cols, CV_32FC1, stub.data);
break;
case 'b':
case 'B':
// switch to gray gain
statusPrecision = precisionByte;
std::cout << std::endl << header << "char" << std::endl;
gain = cv::imread(imagePath[index][2], cv::IMREAD_GRAYSCALE);
break;
case '0':
case '1':
index = key - '0';
switch (statusPrecision)
{
case precisionHalf:
// precision half
stub = cv::imread(imagePath[index][0], cv::IMREAD_UNCHANGED);
gain = cv::Mat(stub.rows, stub.cols/2, CV_16SC1, stub.data);
break;
case precisionFloat:
// precision single
stub = cv::imread(imagePath[index][1], cv::IMREAD_UNCHANGED);
gain = cv::Mat(stub.rows, stub.cols, CV_32FC1, stub.data);
break;
case precisionByte:
// precision single
gain = cv::imread(imagePath[index][2], cv::IMREAD_GRAYSCALE);
break;
default:
break;
}
break;
case 'c':
case 'C':
std::cout << std::endl << "Using CPU SIMD " << std::endl;
statusDevice = useCpuSimd;
break;
case 'g':
case 'G':
std::cout << std::endl << "Using GPU " << std::endl;
statusDevice = useGpu;
break;
default:
break;
}
if (statusDevice == useCpuSimd)
{
elapsedTime = multiplyImage(image, gain);
}
else
{
#ifdef HAVE_CUDA
// CUDA
elapsedTime = multiplyImageCuda(image, gain);
#endif // HAVE_CUDA
}
computeStatistics(elapsedTime, key);
if (key == 's' || key == 'S')
{
cv::imwrite(dumpFilename, image);
}
cv::imshow(windowName, image);
key = cv::waitKey(1);
}
std::cout << std::endl;
cv::destroyAllWindows();
releaseArray();
return 0;
}