@@ -207,15 +207,16 @@ template <typename T> inline T mapValue(T x, T a, T b, T c, T d)
207
207
int main (int argc, const char * argv[])
208
208
{
209
209
const char * keys =
210
- " { h help | | print help message }"
210
+ " { h help | | print help message }"
211
211
" { l left | ../data/pic1.png | specify left image }"
212
212
" { r right | ../data/pic2.png | specify right image }"
213
- " { gray | | use grayscale sources [PyrLK Sparse] }"
214
- " { win_size | 21 | specify windows size [PyrLK] }"
215
- " { max_level | 3 | specify max level [PyrLK] }"
216
- " { iters | 30 | specify iterations count [PyrLK] }"
217
- " { points | 4000 | specify points count [GoodFeatureToTrack] }"
218
- " { min_dist | 0 | specify minimal distance between points [GoodFeatureToTrack] }" ;
213
+ " { flow | sparse | specify flow type [PyrLK] }"
214
+ " { gray | | use grayscale sources [PyrLK Sparse] }"
215
+ " { win_size | 21 | specify windows size [PyrLK] }"
216
+ " { max_level | 3 | specify max level [PyrLK] }"
217
+ " { iters | 30 | specify iterations count [PyrLK] }"
218
+ " { points | 4000 | specify points count [GoodFeatureToTrack] }"
219
+ " { min_dist | 0 | specify minimal distance between points [GoodFeatureToTrack] }" ;
219
220
220
221
CommandLineParser cmd (argc, argv, keys);
221
222
@@ -235,6 +236,22 @@ int main(int argc, const char* argv[])
235
236
return -1 ;
236
237
}
237
238
239
+ string flow_type = cmd.get <string>(" flow" );
240
+ bool is_sparse = true ;
241
+ if (flow_type == " sparse" )
242
+ {
243
+ is_sparse = true ;
244
+ }
245
+ else if (flow_type == " dense" )
246
+ {
247
+ is_sparse = false ;
248
+ }
249
+ else
250
+ {
251
+ cerr << " please specify 'sparse' or 'dense' as flow type" << endl;
252
+ return -1 ;
253
+ }
254
+
238
255
bool useGray = cmd.has (" gray" );
239
256
int winSize = cmd.get <int >(" win_size" );
240
257
int maxLevel = cmd.get <int >(" max_level" );
@@ -251,8 +268,14 @@ int main(int argc, const char* argv[])
251
268
return -1 ;
252
269
}
253
270
254
- namedWindow (" PyrLK [Sparse]" , WINDOW_NORMAL);
255
- namedWindow (" PyrLK [Dense] Flow Field" , WINDOW_NORMAL);
271
+ if (is_sparse)
272
+ {
273
+ namedWindow (" PyrLK [Sparse]" , WINDOW_NORMAL);
274
+ }
275
+ else
276
+ {
277
+ namedWindow (" PyrLK [Dense] Flow Field" , WINDOW_NORMAL);
278
+ }
256
279
257
280
cout << " Image size : " << frame0.cols << " x " << frame0.rows << endl;
258
281
cout << " Points count : " << points << endl;
@@ -265,55 +288,50 @@ int main(int argc, const char* argv[])
265
288
cv::cvtColor (frame1, frame1Gray, COLOR_BGR2GRAY);
266
289
267
290
// goodFeaturesToTrack
268
-
269
291
GpuMat d_frame0Gray (frame0Gray);
270
292
GpuMat d_prevPts;
271
293
272
294
Ptr<cuda::CornersDetector> detector = cuda::createGoodFeaturesToTrackDetector (d_frame0Gray.type (), points, 0.01 , minDist);
273
-
274
295
detector->detect (d_frame0Gray, d_prevPts);
275
296
276
- // Sparse
277
-
278
- Ptr<cuda::SparsePyrLKOpticalFlow> d_pyrLK_sparse = cuda::SparsePyrLKOpticalFlow::create (
279
- Size (winSize, winSize), maxLevel, iters);
280
-
281
297
GpuMat d_frame0 (frame0);
282
298
GpuMat d_frame1 (frame1);
283
299
GpuMat d_frame1Gray (frame1Gray);
284
300
GpuMat d_nextPts;
285
301
GpuMat d_status;
286
-
287
- d_pyrLK_sparse->calc (useGray ? d_frame0Gray : d_frame0, useGray ? d_frame1Gray : d_frame1, d_prevPts, d_nextPts, d_status);
288
-
289
- // Dense
290
-
291
- Ptr<cuda::DensePyrLKOpticalFlow> d_pyrLK_dense = cuda::DensePyrLKOpticalFlow::create (
292
- Size (winSize, winSize), maxLevel, iters);
293
-
294
302
GpuMat d_flow (frame0.size (), CV_32FC2);
295
303
296
- d_pyrLK_dense->calc (d_frame0Gray, d_frame1Gray, d_flow);
297
-
298
- // Draw arrows
299
-
300
- vector<Point2f> prevPts (d_prevPts.cols );
301
- download (d_prevPts, prevPts);
302
-
303
- vector<Point2f> nextPts (d_nextPts.cols );
304
- download (d_nextPts, nextPts);
304
+ if (is_sparse)
305
+ {
306
+ // Sparse
307
+ Ptr<cuda::SparsePyrLKOpticalFlow> d_pyrLK_sparse = cuda::SparsePyrLKOpticalFlow::create (
308
+ Size (winSize, winSize), maxLevel, iters);
309
+ d_pyrLK_sparse->calc (useGray ? d_frame0Gray : d_frame0, useGray ? d_frame1Gray : d_frame1, d_prevPts, d_nextPts, d_status);
305
310
306
- vector<uchar> status (d_status.cols );
307
- download (d_status, status);
311
+ // Draw arrows
312
+ vector<Point2f> prevPts (d_prevPts.cols );
313
+ download (d_prevPts, prevPts);
308
314
309
- drawArrows (frame0, prevPts, nextPts, status, Scalar ( 255 , 0 , 0 ) );
310
- imshow ( " PyrLK [Sparse] " , frame0 );
315
+ vector<Point2f> nextPts (d_nextPts. cols );
316
+ download (d_nextPts, nextPts );
311
317
312
- // Draw flows
318
+ vector<uchar> status (d_status.cols );
319
+ download (d_status, status);
313
320
314
- showFlow (" PyrLK [Dense] Flow Field" , d_flow);
321
+ drawArrows (frame0, prevPts, nextPts, status, Scalar (255 , 0 , 0 ));
322
+ imshow (" PyrLK [Sparse]" , frame0);
323
+ }
324
+ else {
325
+ // Dense
326
+ Ptr<cuda::DensePyrLKOpticalFlow> d_pyrLK_dense = cuda::DensePyrLKOpticalFlow::create (
327
+ Size (winSize, winSize), maxLevel, iters);
328
+ d_pyrLK_dense->calc (d_frame0Gray, d_frame1Gray, d_flow);
329
+
330
+ // Draw flows
331
+ showFlow (" PyrLK [Dense] Flow Field" , d_flow);
332
+ }
315
333
316
- waitKey ();
334
+ waitKey (0 );
317
335
318
336
return 0 ;
319
- }
337
+ }
0 commit comments