From 11eacb9d143ecf6f4b0b56d2fe0f634e0f3ca428 Mon Sep 17 00:00:00 2001 From: Vladislav Vinogradov Date: Wed, 25 Apr 2012 07:03:32 +0000 Subject: [PATCH] Bug #1823 : fixed patch size calculation in PyrLKOpticalFlow --- modules/gpu/src/pyrlk.cpp | 78 +++++++++++++++++------------- samples/gpu/pyrlk_optical_flow.cpp | 36 ++++++++++---- 2 files changed, 71 insertions(+), 43 deletions(-) diff --git a/modules/gpu/src/pyrlk.cpp b/modules/gpu/src/pyrlk.cpp index 83fc1f7856..27222829c1 100644 --- a/modules/gpu/src/pyrlk.cpp +++ b/modules/gpu/src/pyrlk.cpp @@ -53,20 +53,20 @@ void cv::gpu::PyrLKOpticalFlow::dense(const GpuMat&, const GpuMat&, GpuMat&, Gpu #else /* !defined (HAVE_CUDA) */ -namespace cv { namespace gpu { namespace device +namespace cv { namespace gpu { namespace device { - namespace pyrlk + namespace pyrlk { void loadConstants(int cn, float minEigThreshold, int2 winSize, int iters); - void calcSharrDeriv_gpu(DevMem2Db src, DevMem2D_ dx_buf, DevMem2D_ dy_buf, DevMem2D_ dIdx, DevMem2D_ dIdy, int cn, + void calcSharrDeriv_gpu(DevMem2Db src, DevMem2D_ dx_buf, DevMem2D_ dy_buf, DevMem2D_ dIdx, DevMem2D_ dIdy, int cn, cudaStream_t stream = 0); void lkSparse_gpu(DevMem2Db I, DevMem2Db J, DevMem2D_ dIdx, DevMem2D_ dIdy, - const float2* prevPts, float2* nextPts, uchar* status, float* err, bool GET_MIN_EIGENVALS, int ptcount, + const float2* prevPts, float2* nextPts, uchar* status, float* err, bool GET_MIN_EIGENVALS, int ptcount, int level, dim3 block, dim3 patch, cudaStream_t stream = 0); - void lkDense_gpu(DevMem2Db I, DevMem2Db J, DevMem2D_ dIdx, DevMem2D_ dIdy, + void lkDense_gpu(DevMem2Db I, DevMem2Db J, DevMem2D_ dIdx, DevMem2D_ dIdy, DevMem2Df u, DevMem2Df v, DevMem2Df* err, bool GET_MIN_EIGENVALS, cudaStream_t stream = 0); } }}} @@ -91,7 +91,7 @@ void cv::gpu::PyrLKOpticalFlow::buildImagePyramid(const GpuMat& img0, vector 32 && winSize.width > 2 * winSize.height) + { + block.x = 32; + block.y = 8; + } + else + { + block.x = block.y = 16; + } + + patch.x = (winSize.width + block.x - 1) / block.x; + patch.y = (winSize.height + block.y - 1) / block.y; + + block.z = patch.z = 1; + } +} + void cv::gpu::PyrLKOpticalFlow::sparse(const GpuMat& prevImg, const GpuMat& nextImg, const GpuMat& prevPts, GpuMat& nextPts, GpuMat& status, GpuMat* err) { using namespace cv::gpu::device::pyrlk; - + if (prevPts.empty()) { nextPts.release(); @@ -136,32 +159,21 @@ void cv::gpu::PyrLKOpticalFlow::sparse(const GpuMat& prevImg, const GpuMat& next return; } - derivLambda = std::min(std::max(derivLambda, 0.0), 1.0); + derivLambda = std::min(std::max(derivLambda, 0.0), 1.0); iters = std::min(std::max(iters, 0), 100); const int cn = prevImg.channels(); - dim3 block; - - if (winSize.width * cn > 32) - { - block.x = 32; - block.y = 8; - } - else - { - block.x = block.y = 16; - } - - dim3 patch((winSize.width * cn + block.x - 1) / block.x, (winSize.height + block.y - 1) / block.y); + dim3 block, patch; + calcPatchSize(winSize, cn, block, patch); CV_Assert(derivLambda >= 0); CV_Assert(maxLevel >= 0 && winSize.width > 2 && winSize.height > 2); CV_Assert(prevImg.size() == nextImg.size() && prevImg.type() == nextImg.type()); CV_Assert(patch.x > 0 && patch.x < 6 && patch.y > 0 && patch.y < 6); CV_Assert(prevPts.rows == 1 && prevPts.type() == CV_32FC2); - + if (useInitialFlow) CV_Assert(nextPts.size() == prevPts.size() && nextPts.type() == CV_32FC2); else @@ -176,19 +188,19 @@ void cv::gpu::PyrLKOpticalFlow::sparse(const GpuMat& prevImg, const GpuMat& next if (err) ensureSizeIsEnough(1, prevPts.cols, CV_32FC1, *err); - + // build the image pyramids. // we pad each level with +/-winSize.{width|height} // pixels to simplify the further patch extraction. buildImagePyramid(prevImg, prevPyr_, true); buildImagePyramid(nextImg, nextPyr_, true); - + // dI/dx ~ Ix, dI/dy ~ Iy ensureSizeIsEnough(prevImg.rows + winSize.height * 2, prevImg.cols + winSize.width * 2, CV_MAKETYPE(CV_16S, cn), dx_buf_); ensureSizeIsEnough(prevImg.rows + winSize.height * 2, prevImg.cols + winSize.width * 2, CV_MAKETYPE(CV_16S, cn), dy_buf_); - + loadConstants(cn, minEigThreshold, make_int2(winSize.width, winSize.height), iters); for (int level = maxLevel; level >= 0; level--) @@ -204,8 +216,8 @@ void cv::gpu::PyrLKOpticalFlow::sparse(const GpuMat& prevImg, const GpuMat& next calcSharrDeriv(prevPyr_[level], dIdx, dIdy); - lkSparse_gpu(prevPyr_[level], nextPyr_[level], dIdx, dIdy, - prevPts.ptr(), nextPts.ptr(), status.ptr(), level == 0 && err ? err->ptr() : 0, getMinEigenVals, prevPts.cols, + lkSparse_gpu(prevPyr_[level], nextPyr_[level], dIdx, dIdy, + prevPts.ptr(), nextPts.ptr(), status.ptr(), level == 0 && err ? err->ptr() : 0, getMinEigenVals, prevPts.cols, level, block, patch); } } @@ -214,7 +226,7 @@ void cv::gpu::PyrLKOpticalFlow::dense(const GpuMat& prevImg, const GpuMat& nextI { using namespace cv::gpu::device::pyrlk; - derivLambda = std::min(std::max(derivLambda, 0.0), 1.0); + derivLambda = std::min(std::max(derivLambda, 0.0), 1.0); iters = std::min(std::max(iters, 0), 100); @@ -222,7 +234,7 @@ void cv::gpu::PyrLKOpticalFlow::dense(const GpuMat& prevImg, const GpuMat& nextI CV_Assert(prevImg.size() == nextImg.size() && prevImg.type() == nextImg.type()); CV_Assert(derivLambda >= 0); CV_Assert(maxLevel >= 0 && winSize.width > 2 && winSize.height > 2); - + if (useInitialFlow) { CV_Assert(u.size() == prevImg.size() && u.type() == CV_32FC1); @@ -239,7 +251,7 @@ void cv::gpu::PyrLKOpticalFlow::dense(const GpuMat& prevImg, const GpuMat& nextI if (err) err->create(prevImg.size(), CV_32FC1); - + // build the image pyramids. // we pad each level with +/-winSize.{width|height} // pixels to simplify the further patch extraction. @@ -248,12 +260,12 @@ void cv::gpu::PyrLKOpticalFlow::dense(const GpuMat& prevImg, const GpuMat& nextI buildImagePyramid(nextImg, nextPyr_, true); buildImagePyramid(u, uPyr_, false); buildImagePyramid(v, vPyr_, false); - + // dI/dx ~ Ix, dI/dy ~ Iy ensureSizeIsEnough(prevImg.rows + winSize.height * 2, prevImg.cols + winSize.width * 2, CV_16SC1, dx_buf_); ensureSizeIsEnough(prevImg.rows + winSize.height * 2, prevImg.cols + winSize.width * 2, CV_16SC1, dy_buf_); - + loadConstants(1, minEigThreshold, make_int2(winSize.width, winSize.height), iters); DevMem2Df derr = err ? *err : DevMem2Df(); @@ -271,7 +283,7 @@ void cv::gpu::PyrLKOpticalFlow::dense(const GpuMat& prevImg, const GpuMat& nextI calcSharrDeriv(prevPyr_[level], dIdx, dIdy); - lkDense_gpu(prevPyr_[level], nextPyr_[level], dIdx, dIdy, uPyr_[level], vPyr_[level], + lkDense_gpu(prevPyr_[level], nextPyr_[level], dIdx, dIdy, uPyr_[level], vPyr_[level], level == 0 && err ? &derr : 0, getMinEigenVals); if (level == 0) diff --git a/samples/gpu/pyrlk_optical_flow.cpp b/samples/gpu/pyrlk_optical_flow.cpp index cbc1348e2f..26698ce598 100644 --- a/samples/gpu/pyrlk_optical_flow.cpp +++ b/samples/gpu/pyrlk_optical_flow.cpp @@ -124,7 +124,7 @@ void getFlowField(const Mat& u, const Mat& v, Mat& flowField) { float d = max(fabsf(ptr_u[j]), fabsf(ptr_v[j])); - if (d > maxDisplacement) + if (d > maxDisplacement) maxDisplacement = d; } } @@ -152,11 +152,16 @@ void getFlowField(const Mat& u, const Mat& v, Mat& flowField) int main(int argc, const char* argv[]) { const char* keys = - "{ h | help | false | print help message }" - "{ l | left | | specify left image }" - "{ r | right | | specify right image }" - "{ g | gray | false | use grayscale sources [PyrLK Sparse] }" - "{ p | points | 4000 | specify points count [GoodFeatureToTrack] }"; + "{ h | help | false | print help message }" + "{ l | left | | specify left image }" + "{ r | right | | specify right image }" + "{ gray | gray | false | use grayscale sources [PyrLK Sparse] }" + "{ win_size | win_size | 21 | specify windows size [PyrLK] }" + "{ max_level | max_level | 3 | specify max level [PyrLK] }" + "{ iters | iters | 30 | specify iterations count [PyrLK] }" + "{ deriv_lambda | deriv_lambda | 0.5 | specify deriv lambda [PyrLK] }" + "{ points | points | 4000 | specify points count [GoodFeatureToTrack] }" + "{ min_dist | min_dist | 0 | specify minimal distance between points [GoodFeatureToTrack] }"; CommandLineParser cmd(argc, argv, keys); @@ -178,8 +183,13 @@ int main(int argc, const char* argv[]) } bool useGray = cmd.get("gray"); + int winSize = cmd.get("win_size"); + int maxLevel = cmd.get("max_level"); + int iters = cmd.get("iters"); + double derivLambda = cmd.get("deriv_lambda"); int points = cmd.get("points"); - + double minDist = cmd.get("min_dist"); + Mat frame0 = imread(fname0); Mat frame1 = imread(fname1); @@ -210,7 +220,7 @@ int main(int argc, const char* argv[]) // goodFeaturesToTrack - GoodFeaturesToTrackDetector_GPU detector(points, 0.01, 0.0); + GoodFeaturesToTrackDetector_GPU detector(points, 0.01, minDist); GpuMat d_frame0Gray(frame0Gray); GpuMat d_prevPts; @@ -221,6 +231,12 @@ int main(int argc, const char* argv[]) PyrLKOpticalFlow d_pyrLK; + d_pyrLK.winSize.width = winSize; + d_pyrLK.winSize.height = winSize; + d_pyrLK.maxLevel = maxLevel; + d_pyrLK.iters = iters; + d_pyrLK.derivLambda = derivLambda; + GpuMat d_frame0(frame0); GpuMat d_frame1(frame1); GpuMat d_frame1Gray(frame1Gray); @@ -252,13 +268,13 @@ int main(int argc, const char* argv[]) d_pyrLK.dense(d_frame0Gray, d_frame1Gray, d_u, d_v); // Draw flow field - + Mat flowField; getFlowField(Mat(d_u), Mat(d_v), flowField); imshow("PyrLK [Dense] Flow Field", flowField); - #ifdef HAVE_OPENGL + #ifdef HAVE_OPENGL setOpenGlContext("PyrLK [Dense]"); GpuMat d_vertex, d_colors; -- GitLab