Commit 59ce0a9f authored by Vladislav Vinogradov's avatar Vladislav Vinogradov

Merged revision(s) 8679 from trunk:

new implementation of gpu::PyrLKOpticalFlow::sparse (1.5 - 2x faster)
........
parent 5c19c6cb
...@@ -8,13 +8,12 @@ ...@@ -8,13 +8,12 @@
GPU_PERF_TEST_1(BroxOpticalFlow, cv::gpu::DeviceInfo) GPU_PERF_TEST_1(BroxOpticalFlow, cv::gpu::DeviceInfo)
{ {
cv::gpu::DeviceInfo devInfo = GetParam(); cv::gpu::DeviceInfo devInfo = GetParam();
cv::gpu::setDevice(devInfo.deviceID()); cv::gpu::setDevice(devInfo.deviceID());
cv::Mat frame0_host = readImage("gpu/opticalflow/frame0.png", cv::IMREAD_GRAYSCALE); cv::Mat frame0_host = readImage("gpu/opticalflow/frame0.png", cv::IMREAD_GRAYSCALE);
cv::Mat frame1_host = readImage("gpu/opticalflow/frame1.png", cv::IMREAD_GRAYSCALE);
ASSERT_FALSE(frame0_host.empty()); ASSERT_FALSE(frame0_host.empty());
cv::Mat frame1_host = readImage("gpu/opticalflow/frame1.png", cv::IMREAD_GRAYSCALE);
ASSERT_FALSE(frame1_host.empty()); ASSERT_FALSE(frame1_host.empty());
frame0_host.convertTo(frame0_host, CV_32FC1, 1.0 / 255.0); frame0_host.convertTo(frame0_host, CV_32FC1, 1.0 / 255.0);
...@@ -28,6 +27,8 @@ GPU_PERF_TEST_1(BroxOpticalFlow, cv::gpu::DeviceInfo) ...@@ -28,6 +27,8 @@ GPU_PERF_TEST_1(BroxOpticalFlow, cv::gpu::DeviceInfo)
cv::gpu::BroxOpticalFlow d_flow(0.197f /*alpha*/, 50.0f /*gamma*/, 0.8f /*scale_factor*/, cv::gpu::BroxOpticalFlow d_flow(0.197f /*alpha*/, 50.0f /*gamma*/, 0.8f /*scale_factor*/,
10 /*inner_iterations*/, 77 /*outer_iterations*/, 10 /*solver_iterations*/); 10 /*inner_iterations*/, 77 /*outer_iterations*/, 10 /*solver_iterations*/);
d_flow(frame0, frame1, u, v);
declare.time(10); declare.time(10);
TEST_CYCLE() TEST_CYCLE()
...@@ -44,13 +45,12 @@ INSTANTIATE_TEST_CASE_P(Video, BroxOpticalFlow, ALL_DEVICES); ...@@ -44,13 +45,12 @@ INSTANTIATE_TEST_CASE_P(Video, BroxOpticalFlow, ALL_DEVICES);
GPU_PERF_TEST_1(InterpolateFrames, cv::gpu::DeviceInfo) GPU_PERF_TEST_1(InterpolateFrames, cv::gpu::DeviceInfo)
{ {
cv::gpu::DeviceInfo devInfo = GetParam(); cv::gpu::DeviceInfo devInfo = GetParam();
cv::gpu::setDevice(devInfo.deviceID()); cv::gpu::setDevice(devInfo.deviceID());
cv::Mat frame0_host = readImage("gpu/perf/aloe.jpg", cv::IMREAD_GRAYSCALE); cv::Mat frame0_host = readImage("gpu/perf/aloe.jpg", cv::IMREAD_GRAYSCALE);
cv::Mat frame1_host = readImage("gpu/perf/aloeR.jpg", cv::IMREAD_GRAYSCALE);
ASSERT_FALSE(frame0_host.empty()); ASSERT_FALSE(frame0_host.empty());
cv::Mat frame1_host = readImage("gpu/perf/aloeR.jpg", cv::IMREAD_GRAYSCALE);
ASSERT_FALSE(frame1_host.empty()); ASSERT_FALSE(frame1_host.empty());
frame0_host.convertTo(frame0_host, CV_32FC1, 1.0 / 255.0); frame0_host.convertTo(frame0_host, CV_32FC1, 1.0 / 255.0);
...@@ -70,6 +70,8 @@ GPU_PERF_TEST_1(InterpolateFrames, cv::gpu::DeviceInfo) ...@@ -70,6 +70,8 @@ GPU_PERF_TEST_1(InterpolateFrames, cv::gpu::DeviceInfo)
cv::gpu::GpuMat newFrame; cv::gpu::GpuMat newFrame;
cv::gpu::GpuMat buf; cv::gpu::GpuMat buf;
cv::gpu::interpolateFrames(frame0, frame1, fu, fv, bu, bv, 0.5f, newFrame, buf);
TEST_CYCLE() TEST_CYCLE()
{ {
cv::gpu::interpolateFrames(frame0, frame1, fu, fv, bu, bv, 0.5f, newFrame, buf); cv::gpu::interpolateFrames(frame0, frame1, fu, fv, bu, bv, 0.5f, newFrame, buf);
...@@ -84,13 +86,12 @@ INSTANTIATE_TEST_CASE_P(Video, InterpolateFrames, ALL_DEVICES); ...@@ -84,13 +86,12 @@ INSTANTIATE_TEST_CASE_P(Video, InterpolateFrames, ALL_DEVICES);
GPU_PERF_TEST_1(CreateOpticalFlowNeedleMap, cv::gpu::DeviceInfo) GPU_PERF_TEST_1(CreateOpticalFlowNeedleMap, cv::gpu::DeviceInfo)
{ {
cv::gpu::DeviceInfo devInfo = GetParam(); cv::gpu::DeviceInfo devInfo = GetParam();
cv::gpu::setDevice(devInfo.deviceID()); cv::gpu::setDevice(devInfo.deviceID());
cv::Mat frame0_host = readImage("gpu/perf/aloe.jpg", cv::IMREAD_GRAYSCALE); cv::Mat frame0_host = readImage("gpu/perf/aloe.jpg", cv::IMREAD_GRAYSCALE);
cv::Mat frame1_host = readImage("gpu/perf/aloeR.jpg", cv::IMREAD_GRAYSCALE);
ASSERT_FALSE(frame0_host.empty()); ASSERT_FALSE(frame0_host.empty());
cv::Mat frame1_host = readImage("gpu/perf/aloeR.jpg", cv::IMREAD_GRAYSCALE);
ASSERT_FALSE(frame1_host.empty()); ASSERT_FALSE(frame1_host.empty());
frame0_host.convertTo(frame0_host, CV_32FC1, 1.0 / 255.0); frame0_host.convertTo(frame0_host, CV_32FC1, 1.0 / 255.0);
...@@ -107,6 +108,8 @@ GPU_PERF_TEST_1(CreateOpticalFlowNeedleMap, cv::gpu::DeviceInfo) ...@@ -107,6 +108,8 @@ GPU_PERF_TEST_1(CreateOpticalFlowNeedleMap, cv::gpu::DeviceInfo)
cv::gpu::GpuMat vertex, colors; cv::gpu::GpuMat vertex, colors;
cv::gpu::createOpticalFlowNeedleMap(u, v, vertex, colors);
TEST_CYCLE() TEST_CYCLE()
{ {
cv::gpu::createOpticalFlowNeedleMap(u, v, vertex, colors); cv::gpu::createOpticalFlowNeedleMap(u, v, vertex, colors);
...@@ -118,15 +121,16 @@ INSTANTIATE_TEST_CASE_P(Video, CreateOpticalFlowNeedleMap, ALL_DEVICES); ...@@ -118,15 +121,16 @@ INSTANTIATE_TEST_CASE_P(Video, CreateOpticalFlowNeedleMap, ALL_DEVICES);
////////////////////////////////////////////////////// //////////////////////////////////////////////////////
// GoodFeaturesToTrack // GoodFeaturesToTrack
GPU_PERF_TEST(GoodFeaturesToTrack, cv::gpu::DeviceInfo, double) IMPLEMENT_PARAM_CLASS(MinDistance, double)
GPU_PERF_TEST(GoodFeaturesToTrack, cv::gpu::DeviceInfo, MinDistance)
{ {
cv::gpu::DeviceInfo devInfo = GET_PARAM(0); cv::gpu::DeviceInfo devInfo = GET_PARAM(0);
double minDistance = GET_PARAM(1);
cv::gpu::setDevice(devInfo.deviceID()); cv::gpu::setDevice(devInfo.deviceID());
cv::Mat image_host = readImage("gpu/perf/aloe.jpg", cv::IMREAD_GRAYSCALE); double minDistance = GET_PARAM(1);
cv::Mat image_host = readImage("gpu/perf/aloe.jpg", cv::IMREAD_GRAYSCALE);
ASSERT_FALSE(image_host.empty()); ASSERT_FALSE(image_host.empty());
cv::gpu::GoodFeaturesToTrackDetector_GPU detector(8000, 0.01, minDistance); cv::gpu::GoodFeaturesToTrackDetector_GPU detector(8000, 0.01, minDistance);
...@@ -134,32 +138,42 @@ GPU_PERF_TEST(GoodFeaturesToTrack, cv::gpu::DeviceInfo, double) ...@@ -134,32 +138,42 @@ GPU_PERF_TEST(GoodFeaturesToTrack, cv::gpu::DeviceInfo, double)
cv::gpu::GpuMat image(image_host); cv::gpu::GpuMat image(image_host);
cv::gpu::GpuMat pts; cv::gpu::GpuMat pts;
detector(image, pts);
TEST_CYCLE() TEST_CYCLE()
{ {
detector(image, pts); detector(image, pts);
} }
} }
INSTANTIATE_TEST_CASE_P(Video, GoodFeaturesToTrack, testing::Combine(ALL_DEVICES, testing::Values(0.0, 3.0))); INSTANTIATE_TEST_CASE_P(Video, GoodFeaturesToTrack, testing::Combine(
ALL_DEVICES,
testing::Values(MinDistance(0.0), MinDistance(3.0))));
////////////////////////////////////////////////////// //////////////////////////////////////////////////////
// PyrLKOpticalFlowSparse // PyrLKOpticalFlowSparse
IMPLEMENT_PARAM_CLASS(GraySource, bool)
IMPLEMENT_PARAM_CLASS(Points, int)
IMPLEMENT_PARAM_CLASS(WinSize, int) IMPLEMENT_PARAM_CLASS(WinSize, int)
IMPLEMENT_PARAM_CLASS(Levels, int)
IMPLEMENT_PARAM_CLASS(Iters, int)
GPU_PERF_TEST(PyrLKOpticalFlowSparse, cv::gpu::DeviceInfo, bool, int, int) GPU_PERF_TEST(PyrLKOpticalFlowSparse, cv::gpu::DeviceInfo, GraySource, Points, WinSize, Levels, Iters)
{ {
cv::gpu::DeviceInfo devInfo = GET_PARAM(0); cv::gpu::DeviceInfo devInfo = GET_PARAM(0);
cv::gpu::setDevice(devInfo.deviceID());
bool useGray = GET_PARAM(1); bool useGray = GET_PARAM(1);
int points = GET_PARAM(2); int points = GET_PARAM(2);
int win_size = GET_PARAM(3); int winSize = GET_PARAM(3);
int levels = GET_PARAM(4);
int iters = GET_PARAM(5);
cv::gpu::setDevice(devInfo.deviceID());
cv::Mat frame0_host = readImage("gpu/opticalflow/frame0.png", useGray ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR); cv::Mat frame0_host = readImage("gpu/opticalflow/frame0.png", useGray ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR);
cv::Mat frame1_host = readImage("gpu/opticalflow/frame1.png", useGray ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR);
ASSERT_FALSE(frame0_host.empty()); ASSERT_FALSE(frame0_host.empty());
cv::Mat frame1_host = readImage("gpu/opticalflow/frame1.png", useGray ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR);
ASSERT_FALSE(frame1_host.empty()); ASSERT_FALSE(frame1_host.empty());
cv::Mat gray_frame; cv::Mat gray_frame;
...@@ -174,37 +188,37 @@ GPU_PERF_TEST(PyrLKOpticalFlowSparse, cv::gpu::DeviceInfo, bool, int, int) ...@@ -174,37 +188,37 @@ GPU_PERF_TEST(PyrLKOpticalFlowSparse, cv::gpu::DeviceInfo, bool, int, int)
detector(cv::gpu::GpuMat(gray_frame), pts); detector(cv::gpu::GpuMat(gray_frame), pts);
cv::gpu::PyrLKOpticalFlow pyrLK; cv::gpu::PyrLKOpticalFlow pyrLK;
pyrLK.winSize = cv::Size(win_size, win_size); pyrLK.winSize = cv::Size(winSize, winSize);
pyrLK.maxLevel = levels - 1;
pyrLK.iters = iters;
cv::gpu::GpuMat frame0(frame0_host); cv::gpu::GpuMat frame0(frame0_host);
cv::gpu::GpuMat frame1(frame1_host); cv::gpu::GpuMat frame1(frame1_host);
cv::gpu::GpuMat nextPts; cv::gpu::GpuMat nextPts;
cv::gpu::GpuMat status; cv::gpu::GpuMat status;
pyrLK.sparse(frame0, frame1, pts, nextPts, status);
TEST_CYCLE() TEST_CYCLE()
{ {
pyrLK.sparse(frame0, frame1, pts, nextPts, status); pyrLK.sparse(frame0, frame1, pts, nextPts, status);
} }
} }
INSTANTIATE_TEST_CASE_P(Video, PyrLKOpticalFlowSparse, testing::Combine INSTANTIATE_TEST_CASE_P(Video, PyrLKOpticalFlowSparse, testing::Combine(
( ALL_DEVICES,
ALL_DEVICES, testing::Values(GraySource(true), GraySource(false)),
testing::Bool(), testing::Values(Points(1000), Points(2000), Points(4000), Points(8000)),
testing::Values(1000, 2000, 4000, 8000), testing::Values(WinSize(9), WinSize(13), WinSize(17), WinSize(21)),
testing::Values(17, 21) testing::Values(Levels(1), Levels(2), Levels(3)),
)); testing::Values(Iters(1), Iters(10), Iters(30))));
////////////////////////////////////////////////////// //////////////////////////////////////////////////////
// PyrLKOpticalFlowDense // PyrLKOpticalFlowDense
IMPLEMENT_PARAM_CLASS(Levels, int)
IMPLEMENT_PARAM_CLASS(Iters, int)
GPU_PERF_TEST(PyrLKOpticalFlowDense, cv::gpu::DeviceInfo, WinSize, Levels, Iters) GPU_PERF_TEST(PyrLKOpticalFlowDense, cv::gpu::DeviceInfo, WinSize, Levels, Iters)
{ {
cv::gpu::DeviceInfo devInfo = GET_PARAM(0); cv::gpu::DeviceInfo devInfo = GET_PARAM(0);
cv::gpu::setDevice(devInfo.deviceID()); cv::gpu::setDevice(devInfo.deviceID());
int winSize = GET_PARAM(1); int winSize = GET_PARAM(1);
...@@ -212,9 +226,9 @@ GPU_PERF_TEST(PyrLKOpticalFlowDense, cv::gpu::DeviceInfo, WinSize, Levels, Iters ...@@ -212,9 +226,9 @@ GPU_PERF_TEST(PyrLKOpticalFlowDense, cv::gpu::DeviceInfo, WinSize, Levels, Iters
int iters = GET_PARAM(3); int iters = GET_PARAM(3);
cv::Mat frame0_host = readImage("gpu/opticalflow/frame0.png", cv::IMREAD_GRAYSCALE); cv::Mat frame0_host = readImage("gpu/opticalflow/frame0.png", cv::IMREAD_GRAYSCALE);
cv::Mat frame1_host = readImage("gpu/opticalflow/frame1.png", cv::IMREAD_GRAYSCALE);
ASSERT_FALSE(frame0_host.empty()); ASSERT_FALSE(frame0_host.empty());
cv::Mat frame1_host = readImage("gpu/opticalflow/frame1.png", cv::IMREAD_GRAYSCALE);
ASSERT_FALSE(frame1_host.empty()); ASSERT_FALSE(frame1_host.empty());
cv::gpu::GpuMat frame0(frame0_host); cv::gpu::GpuMat frame0(frame0_host);
...@@ -244,20 +258,18 @@ INSTANTIATE_TEST_CASE_P(Video, PyrLKOpticalFlowDense, testing::Combine( ...@@ -244,20 +258,18 @@ INSTANTIATE_TEST_CASE_P(Video, PyrLKOpticalFlowDense, testing::Combine(
testing::Values(Levels(1), Levels(2), Levels(3)), testing::Values(Levels(1), Levels(2), Levels(3)),
testing::Values(Iters(1), Iters(10)))); testing::Values(Iters(1), Iters(10))));
////////////////////////////////////////////////////// //////////////////////////////////////////////////////
// FarnebackOpticalFlowTest // FarnebackOpticalFlowTest
GPU_PERF_TEST_1(FarnebackOpticalFlowTest, cv::gpu::DeviceInfo) GPU_PERF_TEST_1(FarnebackOpticalFlowTest, cv::gpu::DeviceInfo)
{ {
cv::gpu::DeviceInfo devInfo = GetParam(); cv::gpu::DeviceInfo devInfo = GetParam();
cv::gpu::setDevice(devInfo.deviceID()); cv::gpu::setDevice(devInfo.deviceID());
cv::Mat frame0_host = readImage("gpu/opticalflow/frame0.png", cv::IMREAD_GRAYSCALE); cv::Mat frame0_host = readImage("gpu/opticalflow/frame0.png", cv::IMREAD_GRAYSCALE);
cv::Mat frame1_host = readImage("gpu/opticalflow/frame1.png", cv::IMREAD_GRAYSCALE);
ASSERT_FALSE(frame0_host.empty()); ASSERT_FALSE(frame0_host.empty());
cv::Mat frame1_host = readImage("gpu/opticalflow/frame1.png", cv::IMREAD_GRAYSCALE);
ASSERT_FALSE(frame1_host.empty()); ASSERT_FALSE(frame1_host.empty());
cv::gpu::GpuMat frame0(frame0_host); cv::gpu::GpuMat frame0(frame0_host);
...@@ -265,13 +277,15 @@ GPU_PERF_TEST_1(FarnebackOpticalFlowTest, cv::gpu::DeviceInfo) ...@@ -265,13 +277,15 @@ GPU_PERF_TEST_1(FarnebackOpticalFlowTest, cv::gpu::DeviceInfo)
cv::gpu::GpuMat u; cv::gpu::GpuMat u;
cv::gpu::GpuMat v; cv::gpu::GpuMat v;
cv::gpu::FarnebackOpticalFlow calc; cv::gpu::FarnebackOpticalFlow farneback;
farneback(frame0, frame1, u, v);
declare.time(10); declare.time(10);
TEST_CYCLE() TEST_CYCLE()
{ {
calc(frame0, frame1, u, v); farneback(frame0, frame1, u, v);
} }
} }
......
This diff is collapsed.
...@@ -57,13 +57,11 @@ namespace cv { namespace gpu { namespace device ...@@ -57,13 +57,11 @@ namespace cv { namespace gpu { namespace device
{ {
namespace pyrlk namespace pyrlk
{ {
void loadConstants(int cn, float minEigThreshold, int2 winSize, int iters); void loadConstants(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 lkSparse1_gpu(DevMem2Df I, DevMem2Df J, const float2* prevPts, float2* nextPts, uchar* status, float* err, int ptcount,
cudaStream_t stream = 0); int level, dim3 block, dim3 patch, cudaStream_t stream = 0);
void lkSparse4_gpu(DevMem2D_<float4> I, DevMem2D_<float4> J, const float2* prevPts, float2* nextPts, uchar* status, float* err, int ptcount,
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,
int level, dim3 block, dim3 patch, cudaStream_t stream = 0); int level, dim3 block, dim3 patch, cudaStream_t stream = 0);
void lkDense_gpu(DevMem2Db I, DevMem2Df J, DevMem2Df u, DevMem2Df v, DevMem2Df prevU, DevMem2Df prevV, void lkDense_gpu(DevMem2Db I, DevMem2Df J, DevMem2Df u, DevMem2Df v, DevMem2Df prevU, DevMem2Df prevV,
...@@ -71,65 +69,10 @@ namespace cv { namespace gpu { namespace device ...@@ -71,65 +69,10 @@ namespace cv { namespace gpu { namespace device
} }
}}} }}}
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();
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;
}
}
}
namespace namespace
{ {
void calcPatchSize(cv::Size winSize, int cn, dim3& block, dim3& patch, bool isDeviceArch11) void calcPatchSize(cv::Size winSize, dim3& block, dim3& patch, bool isDeviceArch11)
{ {
winSize.width *= cn;
if (winSize.width > 32 && winSize.width > 2 * winSize.height) if (winSize.width > 32 && winSize.width > 2 * winSize.height)
{ {
block.x = isDeviceArch11 ? 16 : 32; block.x = isDeviceArch11 ? 16 : 32;
...@@ -160,13 +103,13 @@ void cv::gpu::PyrLKOpticalFlow::sparse(const GpuMat& prevImg, const GpuMat& next ...@@ -160,13 +103,13 @@ void cv::gpu::PyrLKOpticalFlow::sparse(const GpuMat& prevImg, const GpuMat& next
return; return;
} }
const int cn = prevImg.channels();
dim3 block, patch; dim3 block, patch;
calcPatchSize(winSize, cn, block, patch, isDeviceArch11_); calcPatchSize(winSize, block, patch, isDeviceArch11_);
CV_Assert(maxLevel >= 0 && winSize.width > 2 && winSize.height > 2); CV_Assert(prevImg.type() == CV_8UC1 || prevImg.type() == CV_8UC3 || prevImg.type() == CV_8UC4);
CV_Assert(prevImg.size() == nextImg.size() && prevImg.type() == nextImg.type()); CV_Assert(prevImg.size() == nextImg.size() && prevImg.type() == nextImg.type());
CV_Assert(maxLevel >= 0);
CV_Assert(winSize.width > 2 && winSize.height > 2);
CV_Assert(patch.x > 0 && patch.x < 6 && patch.y > 0 && patch.y < 6); CV_Assert(patch.x > 0 && patch.x < 6 && patch.y > 0 && patch.y < 6);
CV_Assert(prevPts.rows == 1 && prevPts.type() == CV_32FC2); CV_Assert(prevPts.rows == 1 && prevPts.type() == CV_32FC2);
...@@ -186,35 +129,48 @@ void cv::gpu::PyrLKOpticalFlow::sparse(const GpuMat& prevImg, const GpuMat& next ...@@ -186,35 +129,48 @@ void cv::gpu::PyrLKOpticalFlow::sparse(const GpuMat& prevImg, const GpuMat& next
ensureSizeIsEnough(1, prevPts.cols, CV_32FC1, *err); ensureSizeIsEnough(1, prevPts.cols, CV_32FC1, *err);
// build the image pyramids. // build the image pyramids.
// we pad each level with +/-winSize.{width|height}
// pixels to simplify the further patch extraction.
buildImagePyramid(prevImg, prevPyr_, true); prevPyr_.resize(maxLevel + 1);
buildImagePyramid(nextImg, nextPyr_, true); nextPyr_.resize(maxLevel + 1);
// dI/dx ~ Ix, dI/dy ~ Iy int cn = prevImg.channels();
ensureSizeIsEnough(prevImg.rows + winSize.height * 2, prevImg.cols + winSize.width * 2, CV_MAKETYPE(CV_16S, cn), dx_buf_); if (cn == 1 || cn == 4)
ensureSizeIsEnough(prevImg.rows + winSize.height * 2, prevImg.cols + winSize.width * 2, CV_MAKETYPE(CV_16S, cn), dy_buf_); {
prevImg.convertTo(prevPyr_[0], CV_32F);
nextImg.convertTo(nextPyr_[0], CV_32F);
}
else
{
cvtColor(prevImg, dx_calcBuf_, COLOR_BGR2BGRA);
dx_calcBuf_.convertTo(prevPyr_[0], CV_32F);
loadConstants(cn, minEigThreshold, make_int2(winSize.width, winSize.height), iters); cvtColor(nextImg, dx_calcBuf_, COLOR_BGR2BGRA);
dx_calcBuf_.convertTo(nextPyr_[0], CV_32F);
}
for (int level = maxLevel; level >= 0; level--) for (int level = 1; level <= maxLevel; ++level)
{ {
Size imgSize = prevPyr_[level].size(); pyrDown(prevPyr_[level - 1], prevPyr_[level]);
pyrDown(nextPyr_[level - 1], nextPyr_[level]);
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); loadConstants(make_int2(winSize.width, winSize.height), iters);
lkSparse_gpu(prevPyr_[level], nextPyr_[level], dIdx, dIdy, for (int level = maxLevel; level >= 0; level--)
prevPts.ptr<float2>(), nextPts.ptr<float2>(), status.ptr(), level == 0 && err ? err->ptr<float>() : 0, getMinEigenVals, prevPts.cols, {
level, block, patch); if (cn == 1)
{
lkSparse1_gpu(prevPyr_[level], nextPyr_[level],
prevPts.ptr<float2>(), nextPts.ptr<float2>(), status.ptr(), level == 0 && err ? err->ptr<float>() : 0, prevPts.cols,
level, block, patch);
}
else
{
lkSparse4_gpu(prevPyr_[level], nextPyr_[level],
prevPts.ptr<float2>(), nextPts.ptr<float2>(), status.ptr(), level == 0 && err ? err->ptr<float>() : 0, prevPts.cols,
level, block, patch);
}
} }
} }
...@@ -232,12 +188,17 @@ void cv::gpu::PyrLKOpticalFlow::dense(const GpuMat& prevImg, const GpuMat& nextI ...@@ -232,12 +188,17 @@ void cv::gpu::PyrLKOpticalFlow::dense(const GpuMat& prevImg, const GpuMat& nextI
// build the image pyramids. // build the image pyramids.
buildImagePyramid(prevImg, prevPyr_, false); prevPyr_.resize(maxLevel + 1);
nextPyr_.resize(maxLevel + 1); nextPyr_.resize(maxLevel + 1);
prevPyr_[0] = prevImg;
nextImg.convertTo(nextPyr_[0], CV_32F); nextImg.convertTo(nextPyr_[0], CV_32F);
for (int level = 1; level <= maxLevel; ++level) for (int level = 1; level <= maxLevel; ++level)
{
pyrDown(prevPyr_[level - 1], prevPyr_[level]);
pyrDown(nextPyr_[level - 1], nextPyr_[level]); pyrDown(nextPyr_[level - 1], nextPyr_[level]);
}
uPyr_.resize(2); uPyr_.resize(2);
vPyr_.resize(2); vPyr_.resize(2);
...@@ -250,7 +211,7 @@ void cv::gpu::PyrLKOpticalFlow::dense(const GpuMat& prevImg, const GpuMat& nextI ...@@ -250,7 +211,7 @@ void cv::gpu::PyrLKOpticalFlow::dense(const GpuMat& prevImg, const GpuMat& nextI
vPyr_[1].setTo(Scalar::all(0)); vPyr_[1].setTo(Scalar::all(0));
int2 winSize2i = make_int2(winSize.width, winSize.height); int2 winSize2i = make_int2(winSize.width, winSize.height);
loadConstants(1, minEigThreshold, winSize2i, iters); loadConstants(winSize2i, iters);
DevMem2Df derr = err ? *err : DevMem2Df(); DevMem2Df derr = err ? *err : DevMem2Df();
......
...@@ -41,11 +41,8 @@ ...@@ -41,11 +41,8 @@
#include "precomp.hpp" #include "precomp.hpp"
namespace {
//#define DUMP //#define DUMP
/////////////////////////////////////////////////////////////////////////////////////////////////
// BroxOpticalFlow // BroxOpticalFlow
#define BROX_OPTICAL_FLOW_DUMP_FILE "opticalflow/brox_optical_flow.bin" #define BROX_OPTICAL_FLOW_DUMP_FILE "opticalflow/brox_optical_flow.bin"
...@@ -130,7 +127,6 @@ TEST_P(BroxOpticalFlow, Regression) ...@@ -130,7 +127,6 @@ TEST_P(BroxOpticalFlow, Regression)
INSTANTIATE_TEST_CASE_P(GPU_Video, BroxOpticalFlow, ALL_DEVICES); INSTANTIATE_TEST_CASE_P(GPU_Video, BroxOpticalFlow, ALL_DEVICES);
/////////////////////////////////////////////////////////////////////////////////////////////////
// GoodFeaturesToTrack // GoodFeaturesToTrack
IMPLEMENT_PARAM_CLASS(MinDistance, double) IMPLEMENT_PARAM_CLASS(MinDistance, double)
...@@ -207,7 +203,6 @@ INSTANTIATE_TEST_CASE_P(GPU_Video, GoodFeaturesToTrack, testing::Combine( ...@@ -207,7 +203,6 @@ INSTANTIATE_TEST_CASE_P(GPU_Video, GoodFeaturesToTrack, testing::Combine(
ALL_DEVICES, ALL_DEVICES,
testing::Values(MinDistance(0.0), MinDistance(3.0)))); testing::Values(MinDistance(0.0), MinDistance(3.0))));
/////////////////////////////////////////////////////////////////////////////////////////////////
// PyrLKOpticalFlow // PyrLKOpticalFlow
IMPLEMENT_PARAM_CLASS(UseGray, bool) IMPLEMENT_PARAM_CLASS(UseGray, bool)
...@@ -251,8 +246,7 @@ TEST_P(PyrLKOpticalFlow, Sparse) ...@@ -251,8 +246,7 @@ TEST_P(PyrLKOpticalFlow, Sparse)
cv::gpu::GpuMat d_nextPts; cv::gpu::GpuMat d_nextPts;
cv::gpu::GpuMat d_status; cv::gpu::GpuMat d_status;
cv::gpu::GpuMat d_err; pyrLK.sparse(loadMat(frame0), loadMat(frame1), d_pts, d_nextPts, d_status);
pyrLK.sparse(loadMat(frame0), loadMat(frame1), d_pts, d_nextPts, d_status, &d_err);
std::vector<cv::Point2f> nextPts(d_nextPts.cols); std::vector<cv::Point2f> nextPts(d_nextPts.cols);
cv::Mat nextPts_mat(1, d_nextPts.cols, CV_32FC2, (void*)&nextPts[0]); cv::Mat nextPts_mat(1, d_nextPts.cols, CV_32FC2, (void*)&nextPts[0]);
...@@ -262,22 +256,19 @@ TEST_P(PyrLKOpticalFlow, Sparse) ...@@ -262,22 +256,19 @@ TEST_P(PyrLKOpticalFlow, Sparse)
cv::Mat status_mat(1, d_status.cols, CV_8UC1, (void*)&status[0]); cv::Mat status_mat(1, d_status.cols, CV_8UC1, (void*)&status[0]);
d_status.download(status_mat); d_status.download(status_mat);
std::vector<float> err(d_err.cols);
cv::Mat err_mat(1, d_err.cols, CV_32FC1, (void*)&err[0]);
d_err.download(err_mat);
std::vector<cv::Point2f> nextPts_gold; std::vector<cv::Point2f> nextPts_gold;
std::vector<unsigned char> status_gold; std::vector<unsigned char> status_gold;
std::vector<float> err_gold; cv::calcOpticalFlowPyrLK(frame0, frame1, pts, nextPts_gold, status_gold, cv::noArray());
cv::calcOpticalFlowPyrLK(frame0, frame1, pts, nextPts_gold, status_gold, err_gold);
ASSERT_EQ(nextPts_gold.size(), nextPts.size()); ASSERT_EQ(nextPts_gold.size(), nextPts.size());
ASSERT_EQ(status_gold.size(), status.size()); ASSERT_EQ(status_gold.size(), status.size());
ASSERT_EQ(err_gold.size(), err.size());
size_t mistmatch = 0; size_t mistmatch = 0;
for (size_t i = 0; i < nextPts.size(); ++i) for (size_t i = 0; i < nextPts.size(); ++i)
{ {
cv::Point2i a = nextPts[i];
cv::Point2i b = nextPts_gold[i];
if (status[i] != status_gold[i]) if (status[i] != status_gold[i])
{ {
++mistmatch; ++mistmatch;
...@@ -286,13 +277,9 @@ TEST_P(PyrLKOpticalFlow, Sparse) ...@@ -286,13 +277,9 @@ TEST_P(PyrLKOpticalFlow, Sparse)
if (status[i]) if (status[i])
{ {
cv::Point2i a = nextPts[i]; bool eq = std::abs(a.x - b.x) <= 1 && std::abs(a.y - b.y) <= 1;
cv::Point2i b = nextPts_gold[i];
if (!eq)
bool eq = std::abs(a.x - b.x) < 1 && std::abs(a.y - b.y) < 1;
float errdiff = std::abs(err[i] - err_gold[i]);
if (!eq || errdiff > 1e-1)
++mistmatch; ++mistmatch;
} }
} }
...@@ -306,7 +293,6 @@ INSTANTIATE_TEST_CASE_P(GPU_Video, PyrLKOpticalFlow, testing::Combine( ...@@ -306,7 +293,6 @@ INSTANTIATE_TEST_CASE_P(GPU_Video, PyrLKOpticalFlow, testing::Combine(
ALL_DEVICES, ALL_DEVICES,
testing::Values(UseGray(true), UseGray(false)))); testing::Values(UseGray(true), UseGray(false))));
/////////////////////////////////////////////////////////////////////////////////////////////////
// FarnebackOpticalFlow // FarnebackOpticalFlow
IMPLEMENT_PARAM_CLASS(PyrScale, double) IMPLEMENT_PARAM_CLASS(PyrScale, double)
...@@ -413,4 +399,3 @@ TEST_P(OpticalFlowNan, Regression) ...@@ -413,4 +399,3 @@ TEST_P(OpticalFlowNan, Regression)
INSTANTIATE_TEST_CASE_P(GPU_Video, OpticalFlowNan, ALL_DEVICES); INSTANTIATE_TEST_CASE_P(GPU_Video, OpticalFlowNan, ALL_DEVICES);
} // namespace
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