2012-02-13 20:57:27 +08:00
|
|
|
/*M///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
|
|
|
//
|
|
|
|
// By downloading, copying, installing or using the software you agree to this license.
|
|
|
|
// If you do not agree to this license, do not download, install,
|
|
|
|
// copy or use the software.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// License Agreement
|
|
|
|
// For Open Source Computer Vision Library
|
|
|
|
//
|
|
|
|
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
|
|
|
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
|
|
|
|
// Third party copyrights are property of their respective owners.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
// are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistribution's of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// * Redistribution's in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other GpuMaterials provided with the distribution.
|
|
|
|
//
|
|
|
|
// * The name of the copyright holders may not be used to endorse or promote products
|
|
|
|
// derived from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// This software is provided by the copyright holders and contributors "as is" and
|
|
|
|
// any express or bpied warranties, including, but not limited to, the bpied
|
|
|
|
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
|
|
|
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
|
|
|
// indirect, incidental, special, exemplary, or consequential damages
|
|
|
|
// (including, but not limited to, procurement of substitute goods or services;
|
|
|
|
// loss of use, data, or profits; or business interruption) however caused
|
|
|
|
// and on any theory of liability, whether in contract, strict liability,
|
|
|
|
// or tort (including negligence or otherwise) arising in any way out of
|
|
|
|
// the use of this software, even if advised of the possibility of such damage.
|
|
|
|
//
|
|
|
|
//M*/
|
|
|
|
|
|
|
|
#include "precomp.hpp"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace cv;
|
|
|
|
using namespace cv::gpu;
|
|
|
|
|
|
|
|
#if !defined (HAVE_CUDA)
|
|
|
|
|
|
|
|
void cv::gpu::PyrLKOpticalFlow::sparse(const GpuMat&, const GpuMat&, const GpuMat&, GpuMat&, GpuMat&, GpuMat*) { throw_nogpu(); }
|
|
|
|
void cv::gpu::PyrLKOpticalFlow::dense(const GpuMat&, const GpuMat&, GpuMat&, GpuMat&, GpuMat*) { throw_nogpu(); }
|
|
|
|
|
|
|
|
#else /* !defined (HAVE_CUDA) */
|
|
|
|
|
2012-04-25 15:03:32 +08:00
|
|
|
namespace cv { namespace gpu { namespace device
|
2012-02-13 20:57:27 +08:00
|
|
|
{
|
2012-04-25 15:03:32 +08:00
|
|
|
namespace pyrlk
|
2012-02-13 20:57:27 +08:00
|
|
|
{
|
|
|
|
void loadConstants(int cn, float minEigThreshold, int2 winSize, int iters);
|
|
|
|
|
2012-04-25 15:03:32 +08:00
|
|
|
void calcSharrDeriv_gpu(DevMem2Db src, DevMem2D_<short> dx_buf, DevMem2D_<short> dy_buf, DevMem2D_<short> dIdx, DevMem2D_<short> dIdy, int cn,
|
2012-02-13 20:57:27 +08:00
|
|
|
cudaStream_t stream = 0);
|
|
|
|
|
|
|
|
void lkSparse_gpu(DevMem2Db I, DevMem2Db J, DevMem2D_<short> dIdx, DevMem2D_<short> dIdy,
|
2012-04-25 15:03:32 +08:00
|
|
|
const float2* prevPts, float2* nextPts, uchar* status, float* err, bool GET_MIN_EIGENVALS, int ptcount,
|
2012-02-13 20:57:27 +08:00
|
|
|
int level, dim3 block, dim3 patch, cudaStream_t stream = 0);
|
|
|
|
|
2012-06-15 19:02:33 +08:00
|
|
|
void lkDense_gpu(DevMem2Db I, DevMem2Df J, DevMem2Df u, DevMem2Df v, DevMem2Df prevU, DevMem2Df prevV,
|
|
|
|
DevMem2Df err, int2 winSize, cudaStream_t stream = 0);
|
2012-02-13 20:57:27 +08:00
|
|
|
}
|
|
|
|
}}}
|
|
|
|
|
|
|
|
void cv::gpu::PyrLKOpticalFlow::calcSharrDeriv(const GpuMat& src, GpuMat& dIdx, GpuMat& dIdy)
|
|
|
|
{
|
|
|
|
using namespace cv::gpu::device::pyrlk;
|
|
|
|
|
|
|
|
CV_Assert(src.rows > 1 && src.cols > 1);
|
|
|
|
CV_Assert(src.depth() == CV_8U);
|
|
|
|
|
|
|
|
const int cn = src.channels();
|
|
|
|
|
|
|
|
ensureSizeIsEnough(src.size(), CV_MAKETYPE(CV_16S, cn), dx_calcBuf_);
|
|
|
|
ensureSizeIsEnough(src.size(), CV_MAKETYPE(CV_16S, cn), dy_calcBuf_);
|
|
|
|
|
|
|
|
calcSharrDeriv_gpu(src, dx_calcBuf_, dy_calcBuf_, dIdx, dIdy, cn);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cv::gpu::PyrLKOpticalFlow::buildImagePyramid(const GpuMat& img0, vector<GpuMat>& pyr, bool withBorder)
|
|
|
|
{
|
|
|
|
pyr.resize(maxLevel + 1);
|
|
|
|
|
|
|
|
Size sz = img0.size();
|
2012-04-25 15:03:32 +08:00
|
|
|
|
2012-02-13 20:57:27 +08:00
|
|
|
for (int level = 0; level <= maxLevel; ++level)
|
|
|
|
{
|
|
|
|
GpuMat temp;
|
|
|
|
|
|
|
|
if (withBorder)
|
|
|
|
{
|
|
|
|
temp.create(sz.height + winSize.height * 2, sz.width + winSize.width * 2, img0.type());
|
|
|
|
pyr[level] = temp(Rect(winSize.width, winSize.height, sz.width, sz.height));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ensureSizeIsEnough(sz, img0.type(), pyr[level]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (level == 0)
|
|
|
|
img0.copyTo(pyr[level]);
|
|
|
|
else
|
|
|
|
pyrDown(pyr[level - 1], pyr[level]);
|
|
|
|
|
|
|
|
if (withBorder)
|
|
|
|
copyMakeBorder(pyr[level], temp, winSize.height, winSize.height, winSize.width, winSize.width, BORDER_REFLECT_101);
|
|
|
|
|
|
|
|
sz = Size((sz.width + 1) / 2, (sz.height + 1) / 2);
|
|
|
|
|
|
|
|
if (sz.width <= winSize.width || sz.height <= winSize.height)
|
|
|
|
{
|
|
|
|
maxLevel = level;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-25 15:03:32 +08:00
|
|
|
namespace
|
|
|
|
{
|
2012-05-03 04:20:14 +08:00
|
|
|
void calcPatchSize(cv::Size winSize, int cn, dim3& block, dim3& patch, bool isDeviceArch11)
|
2012-04-25 15:03:32 +08:00
|
|
|
{
|
|
|
|
winSize.width *= cn;
|
|
|
|
|
|
|
|
if (winSize.width > 32 && winSize.width > 2 * winSize.height)
|
|
|
|
{
|
2012-05-03 04:20:14 +08:00
|
|
|
block.x = isDeviceArch11 ? 16 : 32;
|
2012-04-25 15:03:32 +08:00
|
|
|
block.y = 8;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-05-03 04:20:14 +08:00
|
|
|
block.x = 16;
|
|
|
|
block.y = isDeviceArch11 ? 8 : 16;
|
2012-04-25 15:03:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
patch.x = (winSize.width + block.x - 1) / block.x;
|
|
|
|
patch.y = (winSize.height + block.y - 1) / block.y;
|
|
|
|
|
|
|
|
block.z = patch.z = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-13 20:57:27 +08:00
|
|
|
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;
|
2012-04-25 15:03:32 +08:00
|
|
|
|
2012-02-13 20:57:27 +08:00
|
|
|
if (prevPts.empty())
|
|
|
|
{
|
|
|
|
nextPts.release();
|
|
|
|
status.release();
|
|
|
|
if (err) err->release();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int cn = prevImg.channels();
|
|
|
|
|
2012-04-25 15:03:32 +08:00
|
|
|
dim3 block, patch;
|
2012-06-15 19:02:33 +08:00
|
|
|
calcPatchSize(winSize, cn, block, patch, isDeviceArch11_);
|
2012-02-13 20:57:27 +08:00
|
|
|
|
|
|
|
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);
|
2012-04-25 15:03:32 +08:00
|
|
|
|
2012-02-13 20:57:27 +08:00
|
|
|
if (useInitialFlow)
|
|
|
|
CV_Assert(nextPts.size() == prevPts.size() && nextPts.type() == CV_32FC2);
|
|
|
|
else
|
|
|
|
ensureSizeIsEnough(1, prevPts.cols, prevPts.type(), nextPts);
|
|
|
|
|
|
|
|
GpuMat temp1 = (useInitialFlow ? nextPts : prevPts).reshape(1);
|
|
|
|
GpuMat temp2 = nextPts.reshape(1);
|
|
|
|
multiply(temp1, Scalar::all(1.0 / (1 << maxLevel) / 2.0), temp2);
|
|
|
|
|
|
|
|
ensureSizeIsEnough(1, prevPts.cols, CV_8UC1, status);
|
|
|
|
status.setTo(Scalar::all(1));
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
ensureSizeIsEnough(1, prevPts.cols, CV_32FC1, *err);
|
2012-04-25 15:03:32 +08:00
|
|
|
|
2012-02-13 20:57:27 +08:00
|
|
|
// 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);
|
2012-04-25 15:03:32 +08:00
|
|
|
|
2012-02-13 20:57:27 +08:00
|
|
|
// 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_);
|
2012-04-25 15:03:32 +08:00
|
|
|
|
2012-02-13 20:57:27 +08:00
|
|
|
loadConstants(cn, minEigThreshold, make_int2(winSize.width, winSize.height), iters);
|
|
|
|
|
|
|
|
for (int level = maxLevel; level >= 0; level--)
|
|
|
|
{
|
|
|
|
Size imgSize = prevPyr_[level].size();
|
|
|
|
|
|
|
|
GpuMat dxWhole(imgSize.height + winSize.height * 2, imgSize.width + winSize.width * 2, dx_buf_.type(), dx_buf_.data, dx_buf_.step);
|
|
|
|
GpuMat dyWhole(imgSize.height + winSize.height * 2, imgSize.width + winSize.width * 2, dy_buf_.type(), dy_buf_.data, dy_buf_.step);
|
|
|
|
dxWhole.setTo(Scalar::all(0));
|
|
|
|
dyWhole.setTo(Scalar::all(0));
|
|
|
|
GpuMat dIdx = dxWhole(Rect(winSize.width, winSize.height, imgSize.width, imgSize.height));
|
|
|
|
GpuMat dIdy = dyWhole(Rect(winSize.width, winSize.height, imgSize.width, imgSize.height));
|
|
|
|
|
|
|
|
calcSharrDeriv(prevPyr_[level], dIdx, dIdy);
|
|
|
|
|
2012-04-25 15:03:32 +08:00
|
|
|
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,
|
2012-02-13 20:57:27 +08:00
|
|
|
level, block, patch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void cv::gpu::PyrLKOpticalFlow::dense(const GpuMat& prevImg, const GpuMat& nextImg, GpuMat& u, GpuMat& v, GpuMat* err)
|
|
|
|
{
|
|
|
|
using namespace cv::gpu::device::pyrlk;
|
|
|
|
|
|
|
|
CV_Assert(prevImg.type() == CV_8UC1);
|
|
|
|
CV_Assert(prevImg.size() == nextImg.size() && prevImg.type() == nextImg.type());
|
2012-06-15 19:02:33 +08:00
|
|
|
CV_Assert(maxLevel >= 0);
|
|
|
|
CV_Assert(winSize.width > 2 && winSize.height > 2);
|
2012-02-13 20:57:27 +08:00
|
|
|
|
|
|
|
if (err)
|
|
|
|
err->create(prevImg.size(), CV_32FC1);
|
2012-04-25 15:03:32 +08:00
|
|
|
|
2012-02-13 20:57:27 +08:00
|
|
|
// build the image pyramids.
|
|
|
|
|
2012-06-15 19:02:33 +08:00
|
|
|
buildImagePyramid(prevImg, prevPyr_, false);
|
2012-04-25 15:03:32 +08:00
|
|
|
|
2012-06-15 19:02:33 +08:00
|
|
|
nextPyr_.resize(maxLevel + 1);
|
|
|
|
nextImg.convertTo(nextPyr_[0], CV_32F);
|
|
|
|
for (int level = 1; level <= maxLevel; ++level)
|
|
|
|
pyrDown(nextPyr_[level - 1], nextPyr_[level]);
|
|
|
|
|
|
|
|
uPyr_.resize(2);
|
|
|
|
vPyr_.resize(2);
|
2012-02-13 20:57:27 +08:00
|
|
|
|
2012-06-15 19:02:33 +08:00
|
|
|
ensureSizeIsEnough(prevImg.size(), CV_32FC1, uPyr_[0]);
|
|
|
|
ensureSizeIsEnough(prevImg.size(), CV_32FC1, vPyr_[0]);
|
|
|
|
ensureSizeIsEnough(prevImg.size(), CV_32FC1, uPyr_[1]);
|
|
|
|
ensureSizeIsEnough(prevImg.size(), CV_32FC1, vPyr_[1]);
|
|
|
|
uPyr_[1].setTo(Scalar::all(0));
|
|
|
|
vPyr_[1].setTo(Scalar::all(0));
|
2012-04-25 15:03:32 +08:00
|
|
|
|
2012-06-15 19:02:33 +08:00
|
|
|
int2 winSize2i = make_int2(winSize.width, winSize.height);
|
|
|
|
loadConstants(1, minEigThreshold, winSize2i, iters);
|
2012-02-13 20:57:27 +08:00
|
|
|
|
|
|
|
DevMem2Df derr = err ? *err : DevMem2Df();
|
|
|
|
|
2012-06-15 19:02:33 +08:00
|
|
|
int idx = 0;
|
|
|
|
|
2012-02-13 20:57:27 +08:00
|
|
|
for (int level = maxLevel; level >= 0; level--)
|
|
|
|
{
|
2012-06-15 19:02:33 +08:00
|
|
|
int idx2 = (idx + 1) & 1;
|
2012-02-13 20:57:27 +08:00
|
|
|
|
2012-06-15 19:02:33 +08:00
|
|
|
lkDense_gpu(prevPyr_[level], nextPyr_[level], uPyr_[idx], vPyr_[idx], uPyr_[idx2], vPyr_[idx2],
|
|
|
|
level == 0 ? derr : DevMem2Df(), winSize2i);
|
2012-02-13 20:57:27 +08:00
|
|
|
|
2012-06-15 19:02:33 +08:00
|
|
|
if (level > 0)
|
|
|
|
idx = idx2;
|
2012-02-13 20:57:27 +08:00
|
|
|
}
|
2012-06-15 19:02:33 +08:00
|
|
|
|
|
|
|
uPyr_[idx].copyTo(u);
|
|
|
|
vPyr_[idx].copyTo(v);
|
2012-02-13 20:57:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* !defined (HAVE_CUDA) */
|