added option to specify flow type

pull/8131/head
atinfinity 8 years ago
parent 18dbed0e0d
commit 9552206a4c
  1. 100
      samples/gpu/pyrlk_optical_flow.cpp

@ -207,15 +207,16 @@ template <typename T> inline T mapValue(T x, T a, T b, T c, T d)
int main(int argc, const char* argv[]) int main(int argc, const char* argv[])
{ {
const char* keys = const char* keys =
"{ h help | | print help message }" "{ h help | | print help message }"
"{ l left | ../data/pic1.png | specify left image }" "{ l left | ../data/pic1.png | specify left image }"
"{ r right | ../data/pic2.png | specify right image }" "{ r right | ../data/pic2.png | specify right image }"
"{ gray | | use grayscale sources [PyrLK Sparse] }" "{ flow | sparse | specify flow type [PyrLK] }"
"{ win_size | 21 | specify windows size [PyrLK] }" "{ gray | | use grayscale sources [PyrLK Sparse] }"
"{ max_level | 3 | specify max level [PyrLK] }" "{ win_size | 21 | specify windows size [PyrLK] }"
"{ iters | 30 | specify iterations count [PyrLK] }" "{ max_level | 3 | specify max level [PyrLK] }"
"{ points | 4000 | specify points count [GoodFeatureToTrack] }" "{ iters | 30 | specify iterations count [PyrLK] }"
"{ min_dist | 0 | specify minimal distance between points [GoodFeatureToTrack] }"; "{ points | 4000 | specify points count [GoodFeatureToTrack] }"
"{ min_dist | 0 | specify minimal distance between points [GoodFeatureToTrack] }";
CommandLineParser cmd(argc, argv, keys); CommandLineParser cmd(argc, argv, keys);
@ -235,6 +236,22 @@ int main(int argc, const char* argv[])
return -1; return -1;
} }
string flow_type = cmd.get<string>("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"); bool useGray = cmd.has("gray");
int winSize = cmd.get<int>("win_size"); int winSize = cmd.get<int>("win_size");
int maxLevel = cmd.get<int>("max_level"); int maxLevel = cmd.get<int>("max_level");
@ -251,8 +268,14 @@ int main(int argc, const char* argv[])
return -1; return -1;
} }
namedWindow("PyrLK [Sparse]", WINDOW_NORMAL); if(is_sparse)
namedWindow("PyrLK [Dense] Flow Field", WINDOW_NORMAL); {
namedWindow("PyrLK [Sparse]", WINDOW_NORMAL);
}
else
{
namedWindow("PyrLK [Dense] Flow Field", WINDOW_NORMAL);
}
cout << "Image size : " << frame0.cols << " x " << frame0.rows << endl; cout << "Image size : " << frame0.cols << " x " << frame0.rows << endl;
cout << "Points count : " << points << endl; cout << "Points count : " << points << endl;
@ -265,55 +288,50 @@ int main(int argc, const char* argv[])
cv::cvtColor(frame1, frame1Gray, COLOR_BGR2GRAY); cv::cvtColor(frame1, frame1Gray, COLOR_BGR2GRAY);
// goodFeaturesToTrack // goodFeaturesToTrack
GpuMat d_frame0Gray(frame0Gray); GpuMat d_frame0Gray(frame0Gray);
GpuMat d_prevPts; GpuMat d_prevPts;
Ptr<cuda::CornersDetector> detector = cuda::createGoodFeaturesToTrackDetector(d_frame0Gray.type(), points, 0.01, minDist); Ptr<cuda::CornersDetector> detector = cuda::createGoodFeaturesToTrackDetector(d_frame0Gray.type(), points, 0.01, minDist);
detector->detect(d_frame0Gray, d_prevPts); detector->detect(d_frame0Gray, d_prevPts);
// Sparse
Ptr<cuda::SparsePyrLKOpticalFlow> d_pyrLK_sparse = cuda::SparsePyrLKOpticalFlow::create(
Size(winSize, winSize), maxLevel, iters);
GpuMat d_frame0(frame0); GpuMat d_frame0(frame0);
GpuMat d_frame1(frame1); GpuMat d_frame1(frame1);
GpuMat d_frame1Gray(frame1Gray); GpuMat d_frame1Gray(frame1Gray);
GpuMat d_nextPts; GpuMat d_nextPts;
GpuMat d_status; 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<cuda::DensePyrLKOpticalFlow> d_pyrLK_dense = cuda::DensePyrLKOpticalFlow::create(
Size(winSize, winSize), maxLevel, iters);
GpuMat d_flow(frame0.size(), CV_32FC2); GpuMat d_flow(frame0.size(), CV_32FC2);
d_pyrLK_dense->calc(d_frame0Gray, d_frame1Gray, d_flow); if (is_sparse)
{
// Draw arrows // Sparse
Ptr<cuda::SparsePyrLKOpticalFlow> d_pyrLK_sparse = cuda::SparsePyrLKOpticalFlow::create(
vector<Point2f> prevPts(d_prevPts.cols); Size(winSize, winSize), maxLevel, iters);
download(d_prevPts, prevPts); d_pyrLK_sparse->calc(useGray ? d_frame0Gray : d_frame0, useGray ? d_frame1Gray : d_frame1, d_prevPts, d_nextPts, d_status);
vector<Point2f> nextPts(d_nextPts.cols);
download(d_nextPts, nextPts);
vector<uchar> status(d_status.cols); // Draw arrows
download(d_status, status); vector<Point2f> prevPts(d_prevPts.cols);
download(d_prevPts, prevPts);
drawArrows(frame0, prevPts, nextPts, status, Scalar(255, 0, 0)); vector<Point2f> nextPts(d_nextPts.cols);
imshow("PyrLK [Sparse]", frame0); download(d_nextPts, nextPts);
// Draw flows vector<uchar> 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<cuda::DensePyrLKOpticalFlow> 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; return 0;
} }
Loading…
Cancel
Save