Commit 4cf7a963 authored by Vadim Pisarevsky's avatar Vadim Pisarevsky Committed by OpenCV Buildbot

Merge pull request #946 from bitwangyaoyao:2.4_samples2

parents 51f81eec f049aa76
...@@ -29,6 +29,7 @@ static double getTime(){ ...@@ -29,6 +29,7 @@ static double getTime(){
static void download(const oclMat& d_mat, vector<Point2f>& vec) static void download(const oclMat& d_mat, vector<Point2f>& vec)
{ {
vec.clear();
vec.resize(d_mat.cols); vec.resize(d_mat.cols);
Mat mat(1, d_mat.cols, CV_32FC2, (void*)&vec[0]); Mat mat(1, d_mat.cols, CV_32FC2, (void*)&vec[0]);
d_mat.download(mat); d_mat.download(mat);
...@@ -36,6 +37,7 @@ static void download(const oclMat& d_mat, vector<Point2f>& vec) ...@@ -36,6 +37,7 @@ static void download(const oclMat& d_mat, vector<Point2f>& vec)
static void download(const oclMat& d_mat, vector<uchar>& vec) static void download(const oclMat& d_mat, vector<uchar>& vec)
{ {
vec.clear();
vec.resize(d_mat.cols); vec.resize(d_mat.cols);
Mat mat(1, d_mat.cols, CV_8UC1, (void*)&vec[0]); Mat mat(1, d_mat.cols, CV_8UC1, (void*)&vec[0]);
d_mat.download(mat); d_mat.download(mat);
...@@ -119,14 +121,15 @@ int main(int argc, const char* argv[]) ...@@ -119,14 +121,15 @@ int main(int argc, const char* argv[])
bool useCPU = cmd.get<bool>("s"); bool useCPU = cmd.get<bool>("s");
bool useCamera = cmd.get<bool>("c"); bool useCamera = cmd.get<bool>("c");
int inputName = cmd.get<int>("c"); int inputName = cmd.get<int>("c");
oclMat d_nextPts, d_status;
oclMat d_nextPts, d_status;
GoodFeaturesToTrackDetector_OCL d_features(points);
Mat frame0 = imread(fname0, cv::IMREAD_GRAYSCALE); Mat frame0 = imread(fname0, cv::IMREAD_GRAYSCALE);
Mat frame1 = imread(fname1, cv::IMREAD_GRAYSCALE); Mat frame1 = imread(fname1, cv::IMREAD_GRAYSCALE);
PyrLKOpticalFlow d_pyrLK; PyrLKOpticalFlow d_pyrLK;
vector<cv::Point2f> pts; vector<cv::Point2f> pts(points);
vector<cv::Point2f> nextPts; vector<cv::Point2f> nextPts(points);
vector<unsigned char> status; vector<unsigned char> status(points);
vector<float> err; vector<float> err;
if (frame0.empty() || frame1.empty()) if (frame0.empty() || frame1.empty())
...@@ -199,29 +202,24 @@ int main(int argc, const char* argv[]) ...@@ -199,29 +202,24 @@ int main(int argc, const char* argv[])
ptr1 = frame0Gray; ptr1 = frame0Gray;
} }
pts.clear();
cv::goodFeaturesToTrack(ptr0, pts, points, 0.01, 0.0);
if (pts.size() == 0)
{
continue;
}
if (useCPU) 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 else
{ {
oclMat d_prevPts(1, points, CV_32FC2, (void*)&pts[0]); oclMat d_img(ptr0), d_prevPts;
d_features(d_img, d_prevPts);
d_pyrLK.sparse(oclMat(ptr0), oclMat(ptr1), d_prevPts, d_nextPts, d_status); if(!d_prevPts.rows || !d_prevPts.cols)
continue;
download(d_prevPts, pts); 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_nextPts, nextPts);
download(d_status, status); download(d_status, status);
} }
if (i%2 == 1) if (i%2 == 1)
frame1.copyTo(frameCopy); frame1.copyTo(frameCopy);
...@@ -246,21 +244,19 @@ nocamera: ...@@ -246,21 +244,19 @@ nocamera:
for(int i = 0; i <= LOOP_NUM;i ++) for(int i = 0; i <= LOOP_NUM;i ++)
{ {
cout << "loop" << i << endl; cout << "loop" << i << endl;
if (i > 0) workBegin(); if (i > 0) workBegin();
cv::goodFeaturesToTrack(frame0, pts, points, 0.01, minDist);
if (useCPU) 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 else
{ {
oclMat d_prevPts(1, points, CV_32FC2, (void*)&pts[0]); oclMat d_img(frame0), d_prevPts;
d_features(d_img, d_prevPts);
d_pyrLK.sparse(oclMat(frame0), oclMat(frame1), d_prevPts, d_nextPts, d_status); d_pyrLK.sparse(d_img, oclMat(frame1), d_prevPts, d_nextPts, d_status);
d_features.downloadPoints(d_prevPts, pts);
download(d_prevPts, pts);
download(d_nextPts, nextPts); download(d_nextPts, nextPts);
download(d_status, status); download(d_status, status);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment