From f049aa7674f3f3ef985f14e65de3debfeb163b3a Mon Sep 17 00:00:00 2001 From: yao Date: Tue, 4 Jun 2013 15:59:21 +0800 Subject: [PATCH] use GoodFeaturesToTrackDetector_OCL --- samples/ocl/pyrlk_optical_flow.cpp | 54 ++++++++++++++---------------- 1 file changed, 25 insertions(+), 29 deletions(-) diff --git a/samples/ocl/pyrlk_optical_flow.cpp b/samples/ocl/pyrlk_optical_flow.cpp index 1b2b1d3798..cc8d886f79 100644 --- a/samples/ocl/pyrlk_optical_flow.cpp +++ b/samples/ocl/pyrlk_optical_flow.cpp @@ -29,6 +29,7 @@ static double getTime(){ static void download(const oclMat& d_mat, vector& vec) { + vec.clear(); vec.resize(d_mat.cols); Mat mat(1, d_mat.cols, CV_32FC2, (void*)&vec[0]); d_mat.download(mat); @@ -36,6 +37,7 @@ static void download(const oclMat& d_mat, vector& vec) static void download(const oclMat& d_mat, vector& vec) { + vec.clear(); vec.resize(d_mat.cols); Mat mat(1, d_mat.cols, CV_8UC1, (void*)&vec[0]); d_mat.download(mat); @@ -119,14 +121,15 @@ int main(int argc, const char* argv[]) bool useCPU = cmd.get("s"); bool useCamera = cmd.get("c"); int inputName = cmd.get("c"); - oclMat d_nextPts, d_status; + oclMat d_nextPts, d_status; + GoodFeaturesToTrackDetector_OCL d_features(points); Mat frame0 = imread(fname0, cv::IMREAD_GRAYSCALE); Mat frame1 = imread(fname1, cv::IMREAD_GRAYSCALE); PyrLKOpticalFlow d_pyrLK; - vector pts; - vector nextPts; - vector status; + vector pts(points); + vector nextPts(points); + vector status(points); vector err; if (frame0.empty() || frame1.empty()) @@ -199,29 +202,24 @@ int main(int argc, const char* argv[]) ptr1 = frame0Gray; } - pts.clear(); - - cv::goodFeaturesToTrack(ptr0, pts, points, 0.01, 0.0); - - if (pts.size() == 0) - { - continue; - } - if (useCPU) { - cv::calcOpticalFlowPyrLK(ptr0, ptr1, pts, nextPts, status, err); + pts.clear(); + goodFeaturesToTrack(ptr0, pts, points, 0.01, 0.0); + if(pts.size() == 0) + continue; + calcOpticalFlowPyrLK(ptr0, ptr1, pts, nextPts, status, err); } else { - oclMat d_prevPts(1, points, CV_32FC2, (void*)&pts[0]); - - d_pyrLK.sparse(oclMat(ptr0), oclMat(ptr1), d_prevPts, d_nextPts, d_status); - - download(d_prevPts, pts); + oclMat d_img(ptr0), d_prevPts; + d_features(d_img, d_prevPts); + if(!d_prevPts.rows || !d_prevPts.cols) + continue; + d_pyrLK.sparse(d_img, oclMat(ptr1), d_prevPts, d_nextPts, d_status); + d_features.downloadPoints(d_prevPts,pts); download(d_nextPts, nextPts); download(d_status, status); - } if (i%2 == 1) frame1.copyTo(frameCopy); @@ -246,21 +244,19 @@ nocamera: for(int i = 0; i <= LOOP_NUM;i ++) { cout << "loop" << i << endl; - if (i > 0) workBegin(); - - cv::goodFeaturesToTrack(frame0, pts, points, 0.01, minDist); + if (i > 0) workBegin(); if (useCPU) { - cv::calcOpticalFlowPyrLK(frame0, frame1, pts, nextPts, status, err); + goodFeaturesToTrack(frame0, pts, points, 0.01, minDist); + calcOpticalFlowPyrLK(frame0, frame1, pts, nextPts, status, err); } else { - oclMat d_prevPts(1, points, CV_32FC2, (void*)&pts[0]); - - d_pyrLK.sparse(oclMat(frame0), oclMat(frame1), d_prevPts, d_nextPts, d_status); - - download(d_prevPts, pts); + oclMat d_img(frame0), d_prevPts; + d_features(d_img, d_prevPts); + d_pyrLK.sparse(d_img, oclMat(frame1), d_prevPts, d_nextPts, d_status); + d_features.downloadPoints(d_prevPts, pts); download(d_nextPts, nextPts); download(d_status, status); }