Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/samples/gpu/opticalflow_nvidia_api.cpp
16337 views
1
#if defined _MSC_VER && _MSC_VER >= 1400
2
#pragma warning( disable : 4201 4408 4127 4100)
3
#endif
4
5
#include <iostream>
6
#include <iomanip>
7
#include <memory>
8
#include <exception>
9
#include <ctime>
10
#include <ctype.h>
11
12
#include <iostream>
13
#include <iomanip>
14
#include "opencv2/core/cuda.hpp"
15
#include "opencv2/cudalegacy.hpp"
16
#include "opencv2/highgui.hpp"
17
18
#include "opencv2/core/core_c.h" // FIXIT legacy API
19
#include "opencv2/highgui/highgui_c.h" // FIXIT legacy API
20
21
#if !defined(HAVE_CUDA)
22
int main( int, const char** )
23
{
24
std::cout << "Please compile the library with CUDA support" << std::endl;
25
return -1;
26
}
27
#else
28
29
//using std::shared_ptr;
30
using cv::Ptr;
31
32
#define PARAM_LEFT "--left"
33
#define PARAM_RIGHT "--right"
34
#define PARAM_SCALE "--scale"
35
#define PARAM_ALPHA "--alpha"
36
#define PARAM_GAMMA "--gamma"
37
#define PARAM_INNER "--inner"
38
#define PARAM_OUTER "--outer"
39
#define PARAM_SOLVER "--solver"
40
#define PARAM_TIME_STEP "--time-step"
41
#define PARAM_HELP "--help"
42
43
Ptr<INCVMemAllocator> g_pGPUMemAllocator;
44
Ptr<INCVMemAllocator> g_pHostMemAllocator;
45
46
class RgbToMonochrome
47
{
48
public:
49
float operator ()(unsigned char b, unsigned char g, unsigned char r)
50
{
51
float _r = static_cast<float>(r)/255.0f;
52
float _g = static_cast<float>(g)/255.0f;
53
float _b = static_cast<float>(b)/255.0f;
54
return (_r + _g + _b)/3.0f;
55
}
56
};
57
58
class RgbToR
59
{
60
public:
61
float operator ()(unsigned char /*b*/, unsigned char /*g*/, unsigned char r)
62
{
63
return static_cast<float>(r)/255.0f;
64
}
65
};
66
67
68
class RgbToG
69
{
70
public:
71
float operator ()(unsigned char /*b*/, unsigned char g, unsigned char /*r*/)
72
{
73
return static_cast<float>(g)/255.0f;
74
}
75
};
76
77
class RgbToB
78
{
79
public:
80
float operator ()(unsigned char b, unsigned char /*g*/, unsigned char /*r*/)
81
{
82
return static_cast<float>(b)/255.0f;
83
}
84
};
85
86
template<class T>
87
NCVStatus CopyData(IplImage *image, Ptr<NCVMatrixAlloc<Ncv32f> >& dst)
88
{
89
dst = Ptr<NCVMatrixAlloc<Ncv32f> > (new NCVMatrixAlloc<Ncv32f> (*g_pHostMemAllocator, image->width, image->height));
90
ncvAssertReturn (dst->isMemAllocated (), NCV_ALLOCATOR_BAD_ALLOC);
91
92
unsigned char *row = reinterpret_cast<unsigned char*> (image->imageData);
93
T convert;
94
for (int i = 0; i < image->height; ++i)
95
{
96
for (int j = 0; j < image->width; ++j)
97
{
98
if (image->nChannels < 3)
99
{
100
dst->ptr ()[j + i*dst->stride ()] = static_cast<float> (*(row + j*image->nChannels))/255.0f;
101
}
102
else
103
{
104
unsigned char *color = row + j * image->nChannels;
105
dst->ptr ()[j +i*dst->stride ()] = convert (color[0], color[1], color[2]);
106
}
107
}
108
row += image->widthStep;
109
}
110
return NCV_SUCCESS;
111
}
112
113
template<class T>
114
NCVStatus CopyData(const IplImage *image, const NCVMatrixAlloc<Ncv32f> &dst)
115
{
116
unsigned char *row = reinterpret_cast<unsigned char*> (image->imageData);
117
T convert;
118
for (int i = 0; i < image->height; ++i)
119
{
120
for (int j = 0; j < image->width; ++j)
121
{
122
if (image->nChannels < 3)
123
{
124
dst.ptr ()[j + i*dst.stride ()] = static_cast<float>(*(row + j*image->nChannels))/255.0f;
125
}
126
else
127
{
128
unsigned char *color = row + j * image->nChannels;
129
dst.ptr ()[j +i*dst.stride()] = convert (color[0], color[1], color[2]);
130
}
131
}
132
row += image->widthStep;
133
}
134
return NCV_SUCCESS;
135
}
136
137
static NCVStatus LoadImages (const char *frame0Name,
138
const char *frame1Name,
139
int &width,
140
int &height,
141
Ptr<NCVMatrixAlloc<Ncv32f> > &src,
142
Ptr<NCVMatrixAlloc<Ncv32f> > &dst,
143
IplImage *&firstFrame,
144
IplImage *&lastFrame)
145
{
146
IplImage *image;
147
image = cvLoadImage (frame0Name);
148
if (image == 0)
149
{
150
std::cout << "Could not open '" << frame0Name << "'\n";
151
return NCV_FILE_ERROR;
152
}
153
154
firstFrame = image;
155
// copy data to src
156
ncvAssertReturnNcvStat (CopyData<RgbToMonochrome> (image, src));
157
158
IplImage *image2;
159
image2 = cvLoadImage (frame1Name);
160
if (image2 == 0)
161
{
162
std::cout << "Could not open '" << frame1Name << "'\n";
163
return NCV_FILE_ERROR;
164
}
165
lastFrame = image2;
166
167
ncvAssertReturnNcvStat (CopyData<RgbToMonochrome> (image2, dst));
168
169
width = image->width;
170
height = image->height;
171
172
return NCV_SUCCESS;
173
}
174
175
template<typename T>
176
inline T Clamp (T x, T a, T b)
177
{
178
return ((x) > (a) ? ((x) < (b) ? (x) : (b)) : (a));
179
}
180
181
template<typename T>
182
inline T MapValue (T x, T a, T b, T c, T d)
183
{
184
x = Clamp (x, a, b);
185
return c + (d - c) * (x - a) / (b - a);
186
}
187
188
static NCVStatus ShowFlow (NCVMatrixAlloc<Ncv32f> &u, NCVMatrixAlloc<Ncv32f> &v, const char *name)
189
{
190
IplImage *flowField;
191
192
NCVMatrixAlloc<Ncv32f> host_u(*g_pHostMemAllocator, u.width(), u.height());
193
ncvAssertReturn(host_u.isMemAllocated(), NCV_ALLOCATOR_BAD_ALLOC);
194
195
NCVMatrixAlloc<Ncv32f> host_v (*g_pHostMemAllocator, u.width (), u.height ());
196
ncvAssertReturn (host_v.isMemAllocated (), NCV_ALLOCATOR_BAD_ALLOC);
197
198
ncvAssertReturnNcvStat (u.copySolid (host_u, 0));
199
ncvAssertReturnNcvStat (v.copySolid (host_v, 0));
200
201
float *ptr_u = host_u.ptr ();
202
float *ptr_v = host_v.ptr ();
203
204
float maxDisplacement = 1.0f;
205
206
for (Ncv32u i = 0; i < u.height (); ++i)
207
{
208
for (Ncv32u j = 0; j < u.width (); ++j)
209
{
210
float d = std::max ( fabsf(*ptr_u), fabsf(*ptr_v) );
211
if (d > maxDisplacement) maxDisplacement = d;
212
++ptr_u;
213
++ptr_v;
214
}
215
ptr_u += u.stride () - u.width ();
216
ptr_v += v.stride () - v.width ();
217
}
218
219
CvSize image_size = cvSize (u.width (), u.height ());
220
flowField = cvCreateImage (image_size, IPL_DEPTH_8U, 4);
221
if (flowField == 0) return NCV_NULL_PTR;
222
223
unsigned char *row = reinterpret_cast<unsigned char *> (flowField->imageData);
224
225
ptr_u = host_u.ptr();
226
ptr_v = host_v.ptr();
227
for (int i = 0; i < flowField->height; ++i)
228
{
229
for (int j = 0; j < flowField->width; ++j)
230
{
231
(row + j * flowField->nChannels)[0] = 0;
232
(row + j * flowField->nChannels)[1] = static_cast<unsigned char> (MapValue (-(*ptr_v), -maxDisplacement, maxDisplacement, 0.0f, 255.0f));
233
(row + j * flowField->nChannels)[2] = static_cast<unsigned char> (MapValue (*ptr_u , -maxDisplacement, maxDisplacement, 0.0f, 255.0f));
234
(row + j * flowField->nChannels)[3] = 255;
235
++ptr_u;
236
++ptr_v;
237
}
238
row += flowField->widthStep;
239
ptr_u += u.stride () - u.width ();
240
ptr_v += v.stride () - v.width ();
241
}
242
243
cvShowImage (name, flowField);
244
245
return NCV_SUCCESS;
246
}
247
248
static IplImage *CreateImage (NCVMatrixAlloc<Ncv32f> &h_r, NCVMatrixAlloc<Ncv32f> &h_g, NCVMatrixAlloc<Ncv32f> &h_b)
249
{
250
CvSize imageSize = cvSize (h_r.width (), h_r.height ());
251
IplImage *image = cvCreateImage (imageSize, IPL_DEPTH_8U, 4);
252
if (image == 0) return 0;
253
254
unsigned char *row = reinterpret_cast<unsigned char*> (image->imageData);
255
256
for (int i = 0; i < image->height; ++i)
257
{
258
for (int j = 0; j < image->width; ++j)
259
{
260
int offset = j * image->nChannels;
261
int pos = i * h_r.stride () + j;
262
row[offset + 0] = static_cast<unsigned char> (h_b.ptr ()[pos] * 255.0f);
263
row[offset + 1] = static_cast<unsigned char> (h_g.ptr ()[pos] * 255.0f);
264
row[offset + 2] = static_cast<unsigned char> (h_r.ptr ()[pos] * 255.0f);
265
row[offset + 3] = 255;
266
}
267
row += image->widthStep;
268
}
269
return image;
270
}
271
272
static void PrintHelp ()
273
{
274
std::cout << "Usage help:\n";
275
std::cout << std::setiosflags(std::ios::left);
276
std::cout << "\t" << std::setw(15) << PARAM_ALPHA << " - set alpha\n";
277
std::cout << "\t" << std::setw(15) << PARAM_GAMMA << " - set gamma\n";
278
std::cout << "\t" << std::setw(15) << PARAM_INNER << " - set number of inner iterations\n";
279
std::cout << "\t" << std::setw(15) << PARAM_LEFT << " - specify left image\n";
280
std::cout << "\t" << std::setw(15) << PARAM_RIGHT << " - specify right image\n";
281
std::cout << "\t" << std::setw(15) << PARAM_OUTER << " - set number of outer iterations\n";
282
std::cout << "\t" << std::setw(15) << PARAM_SCALE << " - set pyramid scale factor\n";
283
std::cout << "\t" << std::setw(15) << PARAM_SOLVER << " - set number of basic solver iterations\n";
284
std::cout << "\t" << std::setw(15) << PARAM_TIME_STEP << " - set frame interpolation time step\n";
285
std::cout << "\t" << std::setw(15) << PARAM_HELP << " - display this help message\n";
286
}
287
288
static int ProcessCommandLine(int argc, char **argv,
289
Ncv32f &timeStep,
290
char *&frame0Name,
291
char *&frame1Name,
292
NCVBroxOpticalFlowDescriptor &desc)
293
{
294
timeStep = 0.25f;
295
for (int iarg = 1; iarg < argc; ++iarg)
296
{
297
if (strcmp(argv[iarg], PARAM_LEFT) == 0)
298
{
299
if (iarg + 1 < argc)
300
{
301
frame0Name = argv[++iarg];
302
}
303
else
304
return -1;
305
}
306
if (strcmp(argv[iarg], PARAM_RIGHT) == 0)
307
{
308
if (iarg + 1 < argc)
309
{
310
frame1Name = argv[++iarg];
311
}
312
else
313
return -1;
314
}
315
else if(strcmp(argv[iarg], PARAM_SCALE) == 0)
316
{
317
if (iarg + 1 < argc)
318
desc.scale_factor = static_cast<Ncv32f>(atof(argv[++iarg]));
319
else
320
return -1;
321
}
322
else if(strcmp(argv[iarg], PARAM_ALPHA) == 0)
323
{
324
if (iarg + 1 < argc)
325
desc.alpha = static_cast<Ncv32f>(atof(argv[++iarg]));
326
else
327
return -1;
328
}
329
else if(strcmp(argv[iarg], PARAM_GAMMA) == 0)
330
{
331
if (iarg + 1 < argc)
332
desc.gamma = static_cast<Ncv32f>(atof(argv[++iarg]));
333
else
334
return -1;
335
}
336
else if(strcmp(argv[iarg], PARAM_INNER) == 0)
337
{
338
if (iarg + 1 < argc)
339
desc.number_of_inner_iterations = static_cast<Ncv32u>(atoi(argv[++iarg]));
340
else
341
return -1;
342
}
343
else if(strcmp(argv[iarg], PARAM_OUTER) == 0)
344
{
345
if (iarg + 1 < argc)
346
desc.number_of_outer_iterations = static_cast<Ncv32u>(atoi(argv[++iarg]));
347
else
348
return -1;
349
}
350
else if(strcmp(argv[iarg], PARAM_SOLVER) == 0)
351
{
352
if (iarg + 1 < argc)
353
desc.number_of_solver_iterations = static_cast<Ncv32u>(atoi(argv[++iarg]));
354
else
355
return -1;
356
}
357
else if(strcmp(argv[iarg], PARAM_TIME_STEP) == 0)
358
{
359
if (iarg + 1 < argc)
360
timeStep = static_cast<Ncv32f>(atof(argv[++iarg]));
361
else
362
return -1;
363
}
364
else if(strcmp(argv[iarg], PARAM_HELP) == 0)
365
{
366
PrintHelp ();
367
return 0;
368
}
369
}
370
return 0;
371
}
372
373
374
int main(int argc, char **argv)
375
{
376
char *frame0Name = 0, *frame1Name = 0;
377
Ncv32f timeStep = 0.01f;
378
379
NCVBroxOpticalFlowDescriptor desc;
380
381
desc.alpha = 0.197f;
382
desc.gamma = 50.0f;
383
desc.number_of_inner_iterations = 10;
384
desc.number_of_outer_iterations = 77;
385
desc.number_of_solver_iterations = 10;
386
desc.scale_factor = 0.8f;
387
388
int result = ProcessCommandLine (argc, argv, timeStep, frame0Name, frame1Name, desc);
389
if (argc == 1 || result)
390
{
391
PrintHelp();
392
return result;
393
}
394
395
cv::cuda::printShortCudaDeviceInfo(cv::cuda::getDevice());
396
397
std::cout << "OpenCV / NVIDIA Computer Vision\n";
398
std::cout << "Optical Flow Demo: Frame Interpolation\n";
399
std::cout << "=========================================\n";
400
std::cout << "Press:\n ESC to quit\n 'a' to move to the previous frame\n 's' to move to the next frame\n";
401
402
int devId;
403
ncvAssertCUDAReturn(cudaGetDevice(&devId), -1);
404
cudaDeviceProp devProp;
405
ncvAssertCUDAReturn(cudaGetDeviceProperties(&devProp, devId), -1);
406
std::cout << "Using GPU: " << devId << "(" << devProp.name <<
407
"), arch=" << devProp.major << "." << devProp.minor << std::endl;
408
409
g_pGPUMemAllocator = Ptr<INCVMemAllocator> (new NCVMemNativeAllocator (NCVMemoryTypeDevice, static_cast<Ncv32u>(devProp.textureAlignment)));
410
ncvAssertPrintReturn (g_pGPUMemAllocator->isInitialized (), "Device memory allocator isn't initialized", -1);
411
412
g_pHostMemAllocator = Ptr<INCVMemAllocator> (new NCVMemNativeAllocator (NCVMemoryTypeHostPageable, static_cast<Ncv32u>(devProp.textureAlignment)));
413
ncvAssertPrintReturn (g_pHostMemAllocator->isInitialized (), "Host memory allocator isn't initialized", -1);
414
415
int width, height;
416
417
Ptr<NCVMatrixAlloc<Ncv32f> > src_host;
418
Ptr<NCVMatrixAlloc<Ncv32f> > dst_host;
419
420
IplImage *firstFrame, *lastFrame;
421
if (frame0Name != 0 && frame1Name != 0)
422
{
423
ncvAssertReturnNcvStat (LoadImages (frame0Name, frame1Name, width, height, src_host, dst_host, firstFrame, lastFrame));
424
}
425
else
426
{
427
ncvAssertReturnNcvStat (LoadImages ("frame10.bmp", "frame11.bmp", width, height, src_host, dst_host, firstFrame, lastFrame));
428
}
429
430
Ptr<NCVMatrixAlloc<Ncv32f> > src (new NCVMatrixAlloc<Ncv32f> (*g_pGPUMemAllocator, src_host->width (), src_host->height ()));
431
ncvAssertReturn(src->isMemAllocated(), -1);
432
433
Ptr<NCVMatrixAlloc<Ncv32f> > dst (new NCVMatrixAlloc<Ncv32f> (*g_pGPUMemAllocator, src_host->width (), src_host->height ()));
434
ncvAssertReturn (dst->isMemAllocated (), -1);
435
436
ncvAssertReturnNcvStat (src_host->copySolid ( *src, 0 ));
437
ncvAssertReturnNcvStat (dst_host->copySolid ( *dst, 0 ));
438
439
#if defined SAFE_MAT_DECL
440
#undef SAFE_MAT_DECL
441
#endif
442
#define SAFE_MAT_DECL(name, allocator, sx, sy) \
443
NCVMatrixAlloc<Ncv32f> name(*allocator, sx, sy);\
444
ncvAssertReturn(name.isMemAllocated(), -1);
445
446
SAFE_MAT_DECL (u, g_pGPUMemAllocator, width, height);
447
SAFE_MAT_DECL (v, g_pGPUMemAllocator, width, height);
448
449
SAFE_MAT_DECL (uBck, g_pGPUMemAllocator, width, height);
450
SAFE_MAT_DECL (vBck, g_pGPUMemAllocator, width, height);
451
452
SAFE_MAT_DECL (h_r, g_pHostMemAllocator, width, height);
453
SAFE_MAT_DECL (h_g, g_pHostMemAllocator, width, height);
454
SAFE_MAT_DECL (h_b, g_pHostMemAllocator, width, height);
455
456
std::cout << "Estimating optical flow\nForward...\n";
457
458
if (NCV_SUCCESS != NCVBroxOpticalFlow (desc, *g_pGPUMemAllocator, *src, *dst, u, v, 0))
459
{
460
std::cout << "Failed\n";
461
return -1;
462
}
463
464
std::cout << "Backward...\n";
465
if (NCV_SUCCESS != NCVBroxOpticalFlow (desc, *g_pGPUMemAllocator, *dst, *src, uBck, vBck, 0))
466
{
467
std::cout << "Failed\n";
468
return -1;
469
}
470
471
// matrix for temporary data
472
SAFE_MAT_DECL (d_temp, g_pGPUMemAllocator, width, height);
473
474
// first frame color components (GPU memory)
475
SAFE_MAT_DECL (d_r, g_pGPUMemAllocator, width, height);
476
SAFE_MAT_DECL (d_g, g_pGPUMemAllocator, width, height);
477
SAFE_MAT_DECL (d_b, g_pGPUMemAllocator, width, height);
478
479
// second frame color components (GPU memory)
480
SAFE_MAT_DECL (d_rt, g_pGPUMemAllocator, width, height);
481
SAFE_MAT_DECL (d_gt, g_pGPUMemAllocator, width, height);
482
SAFE_MAT_DECL (d_bt, g_pGPUMemAllocator, width, height);
483
484
// intermediate frame color components (GPU memory)
485
SAFE_MAT_DECL (d_rNew, g_pGPUMemAllocator, width, height);
486
SAFE_MAT_DECL (d_gNew, g_pGPUMemAllocator, width, height);
487
SAFE_MAT_DECL (d_bNew, g_pGPUMemAllocator, width, height);
488
489
// interpolated forward flow
490
SAFE_MAT_DECL (ui, g_pGPUMemAllocator, width, height);
491
SAFE_MAT_DECL (vi, g_pGPUMemAllocator, width, height);
492
493
// interpolated backward flow
494
SAFE_MAT_DECL (ubi, g_pGPUMemAllocator, width, height);
495
SAFE_MAT_DECL (vbi, g_pGPUMemAllocator, width, height);
496
497
// occlusion masks
498
SAFE_MAT_DECL (occ0, g_pGPUMemAllocator, width, height);
499
SAFE_MAT_DECL (occ1, g_pGPUMemAllocator, width, height);
500
501
// prepare color components on host and copy them to device memory
502
ncvAssertReturnNcvStat (CopyData<RgbToR> (firstFrame, h_r));
503
ncvAssertReturnNcvStat (CopyData<RgbToG> (firstFrame, h_g));
504
ncvAssertReturnNcvStat (CopyData<RgbToB> (firstFrame, h_b));
505
506
ncvAssertReturnNcvStat (h_r.copySolid ( d_r, 0 ));
507
ncvAssertReturnNcvStat (h_g.copySolid ( d_g, 0 ));
508
ncvAssertReturnNcvStat (h_b.copySolid ( d_b, 0 ));
509
510
ncvAssertReturnNcvStat (CopyData<RgbToR> (lastFrame, h_r));
511
ncvAssertReturnNcvStat (CopyData<RgbToG> (lastFrame, h_g));
512
ncvAssertReturnNcvStat (CopyData<RgbToB> (lastFrame, h_b));
513
514
ncvAssertReturnNcvStat (h_r.copySolid ( d_rt, 0 ));
515
ncvAssertReturnNcvStat (h_g.copySolid ( d_gt, 0 ));
516
ncvAssertReturnNcvStat (h_b.copySolid ( d_bt, 0 ));
517
518
std::cout << "Interpolating...\n";
519
std::cout.precision (4);
520
521
std::vector<IplImage*> frames;
522
frames.push_back (firstFrame);
523
524
// compute interpolated frames
525
for (Ncv32f timePos = timeStep; timePos < 1.0f; timePos += timeStep)
526
{
527
ncvAssertCUDAReturn (cudaMemset (ui.ptr (), 0, ui.pitch () * ui.height ()), NCV_CUDA_ERROR);
528
ncvAssertCUDAReturn (cudaMemset (vi.ptr (), 0, vi.pitch () * vi.height ()), NCV_CUDA_ERROR);
529
530
ncvAssertCUDAReturn (cudaMemset (ubi.ptr (), 0, ubi.pitch () * ubi.height ()), NCV_CUDA_ERROR);
531
ncvAssertCUDAReturn (cudaMemset (vbi.ptr (), 0, vbi.pitch () * vbi.height ()), NCV_CUDA_ERROR);
532
533
ncvAssertCUDAReturn (cudaMemset (occ0.ptr (), 0, occ0.pitch () * occ0.height ()), NCV_CUDA_ERROR);
534
ncvAssertCUDAReturn (cudaMemset (occ1.ptr (), 0, occ1.pitch () * occ1.height ()), NCV_CUDA_ERROR);
535
536
NppStInterpolationState state;
537
// interpolation state should be filled once except pSrcFrame0, pSrcFrame1, and pNewFrame
538
// we will only need to reset buffers content to 0 since interpolator doesn't do this itself
539
state.size = NcvSize32u (width, height);
540
state.nStep = d_r.pitch ();
541
state.pSrcFrame0 = d_r.ptr ();
542
state.pSrcFrame1 = d_rt.ptr ();
543
state.pFU = u.ptr ();
544
state.pFV = v.ptr ();
545
state.pBU = uBck.ptr ();
546
state.pBV = vBck.ptr ();
547
state.pos = timePos;
548
state.pNewFrame = d_rNew.ptr ();
549
state.ppBuffers[0] = occ0.ptr ();
550
state.ppBuffers[1] = occ1.ptr ();
551
state.ppBuffers[2] = ui.ptr ();
552
state.ppBuffers[3] = vi.ptr ();
553
state.ppBuffers[4] = ubi.ptr ();
554
state.ppBuffers[5] = vbi.ptr ();
555
556
// interpolate red channel
557
nppiStInterpolateFrames (&state);
558
559
// reset buffers
560
ncvAssertCUDAReturn (cudaMemset (ui.ptr (), 0, ui.pitch () * ui.height ()), NCV_CUDA_ERROR);
561
ncvAssertCUDAReturn (cudaMemset (vi.ptr (), 0, vi.pitch () * vi.height ()), NCV_CUDA_ERROR);
562
563
ncvAssertCUDAReturn (cudaMemset (ubi.ptr (), 0, ubi.pitch () * ubi.height ()), NCV_CUDA_ERROR);
564
ncvAssertCUDAReturn (cudaMemset (vbi.ptr (), 0, vbi.pitch () * vbi.height ()), NCV_CUDA_ERROR);
565
566
ncvAssertCUDAReturn (cudaMemset (occ0.ptr (), 0, occ0.pitch () * occ0.height ()), NCV_CUDA_ERROR);
567
ncvAssertCUDAReturn (cudaMemset (occ1.ptr (), 0, occ1.pitch () * occ1.height ()), NCV_CUDA_ERROR);
568
569
// interpolate green channel
570
state.pSrcFrame0 = d_g.ptr ();
571
state.pSrcFrame1 = d_gt.ptr ();
572
state.pNewFrame = d_gNew.ptr ();
573
574
nppiStInterpolateFrames (&state);
575
576
// reset buffers
577
ncvAssertCUDAReturn (cudaMemset (ui.ptr (), 0, ui.pitch () * ui.height ()), NCV_CUDA_ERROR);
578
ncvAssertCUDAReturn (cudaMemset (vi.ptr (), 0, vi.pitch () * vi.height ()), NCV_CUDA_ERROR);
579
580
ncvAssertCUDAReturn (cudaMemset (ubi.ptr (), 0, ubi.pitch () * ubi.height ()), NCV_CUDA_ERROR);
581
ncvAssertCUDAReturn (cudaMemset (vbi.ptr (), 0, vbi.pitch () * vbi.height ()), NCV_CUDA_ERROR);
582
583
ncvAssertCUDAReturn (cudaMemset (occ0.ptr (), 0, occ0.pitch () * occ0.height ()), NCV_CUDA_ERROR);
584
ncvAssertCUDAReturn (cudaMemset (occ1.ptr (), 0, occ1.pitch () * occ1.height ()), NCV_CUDA_ERROR);
585
586
// interpolate blue channel
587
state.pSrcFrame0 = d_b.ptr ();
588
state.pSrcFrame1 = d_bt.ptr ();
589
state.pNewFrame = d_bNew.ptr ();
590
591
nppiStInterpolateFrames (&state);
592
593
// copy to host memory
594
ncvAssertReturnNcvStat (d_rNew.copySolid (h_r, 0));
595
ncvAssertReturnNcvStat (d_gNew.copySolid (h_g, 0));
596
ncvAssertReturnNcvStat (d_bNew.copySolid (h_b, 0));
597
598
// convert to IplImage
599
IplImage *newFrame = CreateImage (h_r, h_g, h_b);
600
if (newFrame == 0)
601
{
602
std::cout << "Could not create new frame in host memory\n";
603
break;
604
}
605
frames.push_back (newFrame);
606
std::cout << timePos * 100.0f << "%\r";
607
}
608
std::cout << std::setw (5) << "100%\n";
609
610
frames.push_back (lastFrame);
611
612
Ncv32u currentFrame;
613
currentFrame = 0;
614
615
ShowFlow (u, v, "Forward flow");
616
ShowFlow (uBck, vBck, "Backward flow");
617
618
cvShowImage ("Interpolated frame", frames[currentFrame]);
619
620
bool qPressed = false;
621
while ( !qPressed )
622
{
623
int key = toupper (cvWaitKey (10));
624
switch (key)
625
{
626
case 27:
627
qPressed = true;
628
break;
629
case 'A':
630
if (currentFrame > 0) --currentFrame;
631
cvShowImage ("Interpolated frame", frames[currentFrame]);
632
break;
633
case 'S':
634
if (currentFrame < frames.size()-1) ++currentFrame;
635
cvShowImage ("Interpolated frame", frames[currentFrame]);
636
break;
637
}
638
}
639
640
cvDestroyAllWindows ();
641
642
std::vector<IplImage*>::iterator iter;
643
for (iter = frames.begin (); iter != frames.end (); ++iter)
644
{
645
cvReleaseImage (&(*iter));
646
}
647
648
return 0;
649
}
650
651
#endif
652
653