From 9552206a4c49e6e6cd88b9ab2382b920759dcfe2 Mon Sep 17 00:00:00 2001 From: atinfinity Date: Sun, 12 Feb 2017 23:33:57 +0900 Subject: [PATCH] added option to specify flow type --- samples/gpu/pyrlk_optical_flow.cpp | 100 +++++++++++++++++------------ 1 file changed, 59 insertions(+), 41 deletions(-) diff --git a/samples/gpu/pyrlk_optical_flow.cpp b/samples/gpu/pyrlk_optical_flow.cpp index 315301b6e3..11c8b731a0 100644 --- a/samples/gpu/pyrlk_optical_flow.cpp +++ b/samples/gpu/pyrlk_optical_flow.cpp @@ -207,15 +207,16 @@ template inline T mapValue(T x, T a, T b, T c, T d) int main(int argc, const char* argv[]) { const char* keys = - "{ h help | | print help message }" + "{ h help | | print help message }" "{ l left | ../data/pic1.png | specify left image }" "{ r right | ../data/pic2.png | specify right image }" - "{ gray | | use grayscale sources [PyrLK Sparse] }" - "{ win_size | 21 | specify windows size [PyrLK] }" - "{ max_level | 3 | specify max level [PyrLK] }" - "{ iters | 30 | specify iterations count [PyrLK] }" - "{ points | 4000 | specify points count [GoodFeatureToTrack] }" - "{ min_dist | 0 | specify minimal distance between points [GoodFeatureToTrack] }"; + "{ flow | sparse | specify flow type [PyrLK] }" + "{ gray | | use grayscale sources [PyrLK Sparse] }" + "{ win_size | 21 | specify windows size [PyrLK] }" + "{ max_level | 3 | specify max level [PyrLK] }" + "{ iters | 30 | specify iterations count [PyrLK] }" + "{ points | 4000 | specify points count [GoodFeatureToTrack] }" + "{ min_dist | 0 | specify minimal distance between points [GoodFeatureToTrack] }"; CommandLineParser cmd(argc, argv, keys); @@ -235,6 +236,22 @@ int main(int argc, const char* argv[]) return -1; } + string flow_type = cmd.get("flow"); + bool is_sparse = true; + if (flow_type == "sparse") + { + is_sparse = true; + } + else if (flow_type == "dense") + { + is_sparse = false; + } + else + { + cerr << "please specify 'sparse' or 'dense' as flow type" << endl; + return -1; + } + bool useGray = cmd.has("gray"); int winSize = cmd.get("win_size"); int maxLevel = cmd.get("max_level"); @@ -251,8 +268,14 @@ int main(int argc, const char* argv[]) return -1; } - namedWindow("PyrLK [Sparse]", WINDOW_NORMAL); - namedWindow("PyrLK [Dense] Flow Field", WINDOW_NORMAL); + if(is_sparse) + { + namedWindow("PyrLK [Sparse]", WINDOW_NORMAL); + } + else + { + namedWindow("PyrLK [Dense] Flow Field", WINDOW_NORMAL); + } cout << "Image size : " << frame0.cols << " x " << frame0.rows << endl; cout << "Points count : " << points << endl; @@ -265,55 +288,50 @@ int main(int argc, const char* argv[]) cv::cvtColor(frame1, frame1Gray, COLOR_BGR2GRAY); // goodFeaturesToTrack - GpuMat d_frame0Gray(frame0Gray); GpuMat d_prevPts; Ptr detector = cuda::createGoodFeaturesToTrackDetector(d_frame0Gray.type(), points, 0.01, minDist); - detector->detect(d_frame0Gray, d_prevPts); - // Sparse - - Ptr d_pyrLK_sparse = cuda::SparsePyrLKOpticalFlow::create( - Size(winSize, winSize), maxLevel, iters); - GpuMat d_frame0(frame0); GpuMat d_frame1(frame1); GpuMat d_frame1Gray(frame1Gray); GpuMat d_nextPts; GpuMat d_status; - - d_pyrLK_sparse->calc(useGray ? d_frame0Gray : d_frame0, useGray ? d_frame1Gray : d_frame1, d_prevPts, d_nextPts, d_status); - - // Dense - - Ptr d_pyrLK_dense = cuda::DensePyrLKOpticalFlow::create( - Size(winSize, winSize), maxLevel, iters); - GpuMat d_flow(frame0.size(), CV_32FC2); - d_pyrLK_dense->calc(d_frame0Gray, d_frame1Gray, d_flow); - - // Draw arrows - - vector prevPts(d_prevPts.cols); - download(d_prevPts, prevPts); - - vector nextPts(d_nextPts.cols); - download(d_nextPts, nextPts); + if (is_sparse) + { + // Sparse + Ptr d_pyrLK_sparse = cuda::SparsePyrLKOpticalFlow::create( + Size(winSize, winSize), maxLevel, iters); + d_pyrLK_sparse->calc(useGray ? d_frame0Gray : d_frame0, useGray ? d_frame1Gray : d_frame1, d_prevPts, d_nextPts, d_status); - vector status(d_status.cols); - download(d_status, status); + // Draw arrows + vector prevPts(d_prevPts.cols); + download(d_prevPts, prevPts); - drawArrows(frame0, prevPts, nextPts, status, Scalar(255, 0, 0)); - imshow("PyrLK [Sparse]", frame0); + vector nextPts(d_nextPts.cols); + download(d_nextPts, nextPts); - // Draw flows + vector status(d_status.cols); + download(d_status, status); - showFlow("PyrLK [Dense] Flow Field", d_flow); + drawArrows(frame0, prevPts, nextPts, status, Scalar(255, 0, 0)); + imshow("PyrLK [Sparse]", frame0); + } + else { + // Dense + Ptr d_pyrLK_dense = cuda::DensePyrLKOpticalFlow::create( + Size(winSize, winSize), maxLevel, iters); + d_pyrLK_dense->calc(d_frame0Gray, d_frame1Gray, d_flow); + + // Draw flows + showFlow("PyrLK [Dense] Flow Field", d_flow); + } - waitKey(); + waitKey(0); return 0; -} +} \ No newline at end of file