forked from opencv/opencv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstereo_multi.cpp
500 lines (401 loc) · 14.3 KB
/
stereo_multi.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
// This sample demonstrates working on one piece of data using two GPUs.
// It splits input into two parts and processes them separately on different GPUs.
#ifdef WIN32
#define NOMINMAX
#include <windows.h>
#else
#include <pthread.h>
#include <unistd.h>
#endif
#include <iostream>
#include <iomanip>
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/cudastereo.hpp"
#include "tick_meter.hpp"
using namespace std;
using namespace cv;
using namespace cv::cuda;
///////////////////////////////////////////////////////////
// Thread
// OS-specific wrappers for multi-threading
#ifdef WIN32
class Thread
{
struct UserData
{
void (*func)(void* userData);
void* param;
};
static DWORD WINAPI WinThreadFunction(LPVOID lpParam)
{
UserData* userData = static_cast<UserData*>(lpParam);
userData->func(userData->param);
return 0;
}
UserData userData_;
HANDLE thread_;
DWORD threadId_;
public:
Thread(void (*func)(void* userData), void* userData)
{
userData_.func = func;
userData_.param = userData;
thread_ = CreateThread(
NULL, // default security attributes
0, // use default stack size
WinThreadFunction, // thread function name
&userData_, // argument to thread function
0, // use default creation flags
&threadId_); // returns the thread identifier
}
~Thread()
{
CloseHandle(thread_);
}
void wait()
{
WaitForSingleObject(thread_, INFINITE);
}
};
#else
class Thread
{
struct UserData
{
void (*func)(void* userData);
void* param;
};
static void* PThreadFunction(void* lpParam)
{
UserData* userData = static_cast<UserData*>(lpParam);
userData->func(userData->param);
return 0;
}
pthread_t thread_;
UserData userData_;
public:
Thread(void (*func)(void* userData), void* userData)
{
userData_.func = func;
userData_.param = userData;
pthread_create(&thread_, NULL, PThreadFunction, &userData_);
}
~Thread()
{
pthread_detach(thread_);
}
void wait()
{
pthread_join(thread_, NULL);
}
};
#endif
///////////////////////////////////////////////////////////
// StereoSingleGpu
// Run Stereo algorithm on single GPU
class StereoSingleGpu
{
public:
explicit StereoSingleGpu(int deviceId = 0);
~StereoSingleGpu();
void compute(const Mat& leftFrame, const Mat& rightFrame, Mat& disparity);
private:
int deviceId_;
GpuMat d_leftFrame;
GpuMat d_rightFrame;
GpuMat d_disparity;
Ptr<cuda::StereoBM> d_alg;
};
StereoSingleGpu::StereoSingleGpu(int deviceId) : deviceId_(deviceId)
{
cuda::setDevice(deviceId_);
d_alg = cuda::createStereoBM(256);
}
StereoSingleGpu::~StereoSingleGpu()
{
cuda::setDevice(deviceId_);
d_leftFrame.release();
d_rightFrame.release();
d_disparity.release();
d_alg.release();
}
void StereoSingleGpu::compute(const Mat& leftFrame, const Mat& rightFrame, Mat& disparity)
{
cuda::setDevice(deviceId_);
d_leftFrame.upload(leftFrame);
d_rightFrame.upload(rightFrame);
d_alg->compute(d_leftFrame, d_rightFrame, d_disparity);
d_disparity.download(disparity);
}
///////////////////////////////////////////////////////////
// StereoMultiGpuThread
// Run Stereo algorithm on two GPUs using different host threads
class StereoMultiGpuThread
{
public:
StereoMultiGpuThread();
~StereoMultiGpuThread();
void compute(const Mat& leftFrame, const Mat& rightFrame, Mat& disparity);
private:
GpuMat d_leftFrames[2];
GpuMat d_rightFrames[2];
GpuMat d_disparities[2];
Ptr<cuda::StereoBM> d_algs[2];
struct StereoLaunchData
{
int deviceId;
Mat leftFrame;
Mat rightFrame;
Mat disparity;
GpuMat* d_leftFrame;
GpuMat* d_rightFrame;
GpuMat* d_disparity;
Ptr<cuda::StereoBM> d_alg;
};
static void launchGpuStereoAlg(void* userData);
};
StereoMultiGpuThread::StereoMultiGpuThread()
{
cuda::setDevice(0);
d_algs[0] = cuda::createStereoBM(256);
cuda::setDevice(1);
d_algs[1] = cuda::createStereoBM(256);
}
StereoMultiGpuThread::~StereoMultiGpuThread()
{
cuda::setDevice(0);
d_leftFrames[0].release();
d_rightFrames[0].release();
d_disparities[0].release();
d_algs[0].release();
cuda::setDevice(1);
d_leftFrames[1].release();
d_rightFrames[1].release();
d_disparities[1].release();
d_algs[1].release();
}
void StereoMultiGpuThread::compute(const Mat& leftFrame, const Mat& rightFrame, Mat& disparity)
{
disparity.create(leftFrame.size(), CV_8UC1);
// Split input data onto two parts for each GPUs.
// We add small border for each part,
// because original algorithm doesn't calculate disparity on image borders.
// With such padding we will get output in the middle of final result.
StereoLaunchData launchDatas[2];
launchDatas[0].deviceId = 0;
launchDatas[0].leftFrame = leftFrame.rowRange(0, leftFrame.rows / 2 + 32);
launchDatas[0].rightFrame = rightFrame.rowRange(0, rightFrame.rows / 2 + 32);
launchDatas[0].disparity = disparity.rowRange(0, leftFrame.rows / 2);
launchDatas[0].d_leftFrame = &d_leftFrames[0];
launchDatas[0].d_rightFrame = &d_rightFrames[0];
launchDatas[0].d_disparity = &d_disparities[0];
launchDatas[0].d_alg = d_algs[0];
launchDatas[1].deviceId = 1;
launchDatas[1].leftFrame = leftFrame.rowRange(leftFrame.rows / 2 - 32, leftFrame.rows);
launchDatas[1].rightFrame = rightFrame.rowRange(leftFrame.rows / 2 - 32, leftFrame.rows);
launchDatas[1].disparity = disparity.rowRange(leftFrame.rows / 2, leftFrame.rows);
launchDatas[1].d_leftFrame = &d_leftFrames[1];
launchDatas[1].d_rightFrame = &d_rightFrames[1];
launchDatas[1].d_disparity = &d_disparities[1];
launchDatas[1].d_alg = d_algs[1];
Thread thread0(launchGpuStereoAlg, &launchDatas[0]);
Thread thread1(launchGpuStereoAlg, &launchDatas[1]);
thread0.wait();
thread1.wait();
}
void StereoMultiGpuThread::launchGpuStereoAlg(void* userData)
{
StereoLaunchData* data = static_cast<StereoLaunchData*>(userData);
cuda::setDevice(data->deviceId);
data->d_leftFrame->upload(data->leftFrame);
data->d_rightFrame->upload(data->rightFrame);
data->d_alg->compute(*data->d_leftFrame, *data->d_rightFrame, *data->d_disparity);
if (data->deviceId == 0)
data->d_disparity->rowRange(0, data->d_disparity->rows - 32).download(data->disparity);
else
data->d_disparity->rowRange(32, data->d_disparity->rows).download(data->disparity);
}
///////////////////////////////////////////////////////////
// StereoMultiGpuStream
// Run Stereo algorithm on two GPUs from single host thread using async API
class StereoMultiGpuStream
{
public:
StereoMultiGpuStream();
~StereoMultiGpuStream();
void compute(const HostMem& leftFrame, const HostMem& rightFrame, HostMem& disparity);
private:
GpuMat d_leftFrames[2];
GpuMat d_rightFrames[2];
GpuMat d_disparities[2];
Ptr<cuda::StereoBM> d_algs[2];
Ptr<Stream> streams[2];
};
StereoMultiGpuStream::StereoMultiGpuStream()
{
cuda::setDevice(0);
d_algs[0] = cuda::createStereoBM(256);
streams[0] = makePtr<Stream>();
cuda::setDevice(1);
d_algs[1] = cuda::createStereoBM(256);
streams[1] = makePtr<Stream>();
}
StereoMultiGpuStream::~StereoMultiGpuStream()
{
cuda::setDevice(0);
d_leftFrames[0].release();
d_rightFrames[0].release();
d_disparities[0].release();
d_algs[0].release();
streams[0].release();
cuda::setDevice(1);
d_leftFrames[1].release();
d_rightFrames[1].release();
d_disparities[1].release();
d_algs[1].release();
streams[1].release();
}
void StereoMultiGpuStream::compute(const HostMem& leftFrame, const HostMem& rightFrame, HostMem& disparity)
{
disparity.create(leftFrame.size(), CV_8UC1);
// Split input data onto two parts for each GPUs.
// We add small border for each part,
// because original algorithm doesn't calculate disparity on image borders.
// With such padding we will get output in the middle of final result.
Mat leftFrameHdr = leftFrame.createMatHeader();
Mat rightFrameHdr = rightFrame.createMatHeader();
Mat disparityHdr = disparity.createMatHeader();
Mat disparityPart0 = disparityHdr.rowRange(0, leftFrame.rows / 2);
Mat disparityPart1 = disparityHdr.rowRange(leftFrame.rows / 2, leftFrame.rows);
cuda::setDevice(0);
d_leftFrames[0].upload(leftFrameHdr.rowRange(0, leftFrame.rows / 2 + 32), *streams[0]);
d_rightFrames[0].upload(rightFrameHdr.rowRange(0, leftFrame.rows / 2 + 32), *streams[0]);
d_algs[0]->compute(d_leftFrames[0], d_rightFrames[0], d_disparities[0], *streams[0]);
d_disparities[0].rowRange(0, leftFrame.rows / 2).download(disparityPart0, *streams[0]);
cuda::setDevice(1);
d_leftFrames[1].upload(leftFrameHdr.rowRange(leftFrame.rows / 2 - 32, leftFrame.rows), *streams[1]);
d_rightFrames[1].upload(rightFrameHdr.rowRange(leftFrame.rows / 2 - 32, leftFrame.rows), *streams[1]);
d_algs[1]->compute(d_leftFrames[1], d_rightFrames[1], d_disparities[1], *streams[1]);
d_disparities[1].rowRange(32, d_disparities[1].rows).download(disparityPart1, *streams[1]);
cuda::setDevice(0);
streams[0]->waitForCompletion();
cuda::setDevice(1);
streams[1]->waitForCompletion();
}
///////////////////////////////////////////////////////////
// main
int main(int argc, char** argv)
{
if (argc != 3)
{
cerr << "Usage: stereo_multi_gpu <left_video> <right_video>" << endl;
return -1;
}
const int numDevices = getCudaEnabledDeviceCount();
if (numDevices != 2)
{
cerr << "Two GPUs are required" << endl;
return -1;
}
for (int i = 0; i < numDevices; ++i)
{
DeviceInfo devInfo(i);
if (!devInfo.isCompatible())
{
cerr << "CUDA module was't built for GPU #" << i << " ("
<< devInfo.name() << ", CC " << devInfo.majorVersion()
<< devInfo.minorVersion() << endl;
return -1;
}
printShortCudaDeviceInfo(i);
}
VideoCapture leftVideo(argv[1]);
VideoCapture rightVideo(argv[2]);
if (!leftVideo.isOpened())
{
cerr << "Can't open " << argv[1] << " video file" << endl;
return -1;
}
if (!rightVideo.isOpened())
{
cerr << "Can't open " << argv[2] << " video file" << endl;
return -1;
}
cout << endl;
cout << "This sample demonstrates working on one piece of data using two GPUs." << endl;
cout << "It splits input into two parts and processes them separately on different GPUs." << endl;
cout << endl;
Mat leftFrame, rightFrame;
HostMem leftGrayFrame, rightGrayFrame;
StereoSingleGpu gpu0Alg(0);
StereoSingleGpu gpu1Alg(1);
StereoMultiGpuThread multiThreadAlg;
StereoMultiGpuStream multiStreamAlg;
Mat disparityGpu0;
Mat disparityGpu1;
Mat disparityMultiThread;
HostMem disparityMultiStream;
Mat disparityGpu0Show;
Mat disparityGpu1Show;
Mat disparityMultiThreadShow;
Mat disparityMultiStreamShow;
TickMeter tm;
cout << "-------------------------------------------------------------------" << endl;
cout << "| Frame | GPU 0 ms | GPU 1 ms | Multi Thread ms | Multi Stream ms |" << endl;
cout << "-------------------------------------------------------------------" << endl;
for (int i = 0;; ++i)
{
leftVideo >> leftFrame;
rightVideo >> rightFrame;
if (leftFrame.empty() || rightFrame.empty())
break;
if (leftFrame.size() != rightFrame.size())
{
cerr << "Frames have different sizes" << endl;
return -1;
}
leftGrayFrame.create(leftFrame.size(), CV_8UC1);
rightGrayFrame.create(leftFrame.size(), CV_8UC1);
cvtColor(leftFrame, leftGrayFrame.createMatHeader(), COLOR_BGR2GRAY);
cvtColor(rightFrame, rightGrayFrame.createMatHeader(), COLOR_BGR2GRAY);
tm.reset(); tm.start();
gpu0Alg.compute(leftGrayFrame.createMatHeader(), rightGrayFrame.createMatHeader(),
disparityGpu0);
tm.stop();
const double gpu0Time = tm.getTimeMilli();
tm.reset(); tm.start();
gpu1Alg.compute(leftGrayFrame.createMatHeader(), rightGrayFrame.createMatHeader(),
disparityGpu1);
tm.stop();
const double gpu1Time = tm.getTimeMilli();
tm.reset(); tm.start();
multiThreadAlg.compute(leftGrayFrame.createMatHeader(), rightGrayFrame.createMatHeader(),
disparityMultiThread);
tm.stop();
const double multiThreadTime = tm.getTimeMilli();
tm.reset(); tm.start();
multiStreamAlg.compute(leftGrayFrame, rightGrayFrame, disparityMultiStream);
tm.stop();
const double multiStreamTime = tm.getTimeMilli();
cout << "| " << setw(5) << i << " | "
<< setw(8) << setprecision(1) << fixed << gpu0Time << " | "
<< setw(8) << setprecision(1) << fixed << gpu1Time << " | "
<< setw(15) << setprecision(1) << fixed << multiThreadTime << " | "
<< setw(15) << setprecision(1) << fixed << multiStreamTime << " |" << endl;
resize(disparityGpu0, disparityGpu0Show, Size(1024, 768), 0, 0, INTER_AREA);
resize(disparityGpu1, disparityGpu1Show, Size(1024, 768), 0, 0, INTER_AREA);
resize(disparityMultiThread, disparityMultiThreadShow, Size(1024, 768), 0, 0, INTER_AREA);
resize(disparityMultiStream.createMatHeader(), disparityMultiStreamShow, Size(1024, 768), 0, 0, INTER_AREA);
imshow("disparityGpu0", disparityGpu0Show);
imshow("disparityGpu1", disparityGpu1Show);
imshow("disparityMultiThread", disparityMultiThreadShow);
imshow("disparityMultiStream", disparityMultiStreamShow);
const int key = waitKey(30) & 0xff;
if (key == 27)
break;
}
cout << "-------------------------------------------------------------------" << endl;
return 0;
}