merged bug fix from r8188 (PyrLKOpticalFlow patch size calculation)

pull/13383/head
Vladislav Vinogradov 13 years ago
parent e467ece19c
commit a6c261bebd
  1. 78
      modules/gpu/src/pyrlk.cpp
  2. 36
      samples/gpu/pyrlk_optical_flow.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_<short> dx_buf, DevMem2D_<short> dy_buf, DevMem2D_<short> dIdx, DevMem2D_<short> dIdy, int cn,
void calcSharrDeriv_gpu(DevMem2Db src, DevMem2D_<short> dx_buf, DevMem2D_<short> dy_buf, DevMem2D_<short> dIdx, DevMem2D_<short> dIdy, int cn,
cudaStream_t stream = 0);
void lkSparse_gpu(DevMem2Db I, DevMem2Db J, DevMem2D_<short> dIdx, DevMem2D_<short> 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_<short> dIdx, DevMem2D_<short> dIdy,
void lkDense_gpu(DevMem2Db I, DevMem2Db J, DevMem2D_<short> dIdx, DevMem2D_<short> 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<Gpu
pyr.resize(maxLevel + 1);
Size sz = img0.size();
for (int level = 0; level <= maxLevel; ++level)
{
GpuMat temp;
@ -124,10 +124,33 @@ void cv::gpu::PyrLKOpticalFlow::buildImagePyramid(const GpuMat& img0, vector<Gpu
}
}
namespace
{
void calcPatchSize(cv::Size winSize, int cn, dim3& block, dim3& patch)
{
winSize.width *= cn;
if (winSize.width > 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<float2>(), nextPts.ptr<float2>(), status.ptr(), level == 0 && err ? err->ptr<float>() : 0, getMinEigenVals, prevPts.cols,
lkSparse_gpu(prevPyr_[level], nextPyr_[level], dIdx, dIdy,
prevPts.ptr<float2>(), nextPts.ptr<float2>(), status.ptr(), level == 0 && err ? err->ptr<float>() : 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)

@ -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<bool>("gray");
int winSize = cmd.get<int>("win_size");
int maxLevel = cmd.get<int>("max_level");
int iters = cmd.get<int>("iters");
double derivLambda = cmd.get<double>("deriv_lambda");
int points = cmd.get<int>("points");
double minDist = cmd.get<double>("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;

Loading…
Cancel
Save