Commit 132b885b authored by peng xiao's avatar peng xiao

Add opencl implementation of Farnback optical flow.

parent 381057ea
......@@ -1410,6 +1410,69 @@ namespace cv
oclMat vPyr_[2];
bool isDeviceArch11_;
};
class CV_EXPORTS FarnebackOpticalFlow
{
public:
FarnebackOpticalFlow()
{
numLevels = 5;
pyrScale = 0.5;
fastPyramids = false;
winSize = 13;
numIters = 10;
polyN = 5;
polySigma = 1.1;
flags = 0;
}
int numLevels;
double pyrScale;
bool fastPyramids;
int winSize;
int numIters;
int polyN;
double polySigma;
int flags;
void operator ()(const oclMat &frame0, const oclMat &frame1, oclMat &flowx, oclMat &flowy);
void releaseMemory()
{
frames_[0].release();
frames_[1].release();
pyrLevel_[0].release();
pyrLevel_[1].release();
M_.release();
bufM_.release();
R_[0].release();
R_[1].release();
blurredFrame_[0].release();
blurredFrame_[1].release();
pyramid0_.clear();
pyramid1_.clear();
}
private:
void prepareGaussian(
int n, double sigma, float *g, float *xg, float *xxg,
double &ig11, double &ig03, double &ig33, double &ig55);
void setPolynomialExpansionConsts(int n, double sigma);
void updateFlow_boxFilter(
const oclMat& R0, const oclMat& R1, oclMat& flowx, oclMat &flowy,
oclMat& M, oclMat &bufM, int blockSize, bool updateMatrices);
void updateFlow_gaussianBlur(
const oclMat& R0, const oclMat& R1, oclMat& flowx, oclMat& flowy,
oclMat& M, oclMat &bufM, int blockSize, bool updateMatrices);
oclMat frames_[2];
oclMat pyrLevel_[2], M_, bufM_, R_[2], blurredFrame_[2];
std::vector<oclMat> pyramid0_, pyramid1_;
};
//////////////// build warping maps ////////////////////
//! builds plane warping maps
CV_EXPORTS void buildWarpPlaneMaps(Size src_size, Rect dst_roi, const Mat &K, const Mat &R, const Mat &T, float scale, oclMat &map_x, oclMat &map_y);
......
......@@ -225,4 +225,133 @@ PERFTEST(tvl1flow)
TestSystem::instance().ExceptedMatSimilar(gold[0], flowx, 3e-3);
TestSystem::instance().ExceptedMatSimilar(gold[1], flowy, 3e-3);
}
\ No newline at end of file
}
///////////// FarnebackOpticalFlow ////////////////////////
PERFTEST(FarnebackOpticalFlow)
{
cv::Mat frame0 = imread("rubberwhale1.png", cv::IMREAD_GRAYSCALE);
ASSERT_FALSE(frame0.empty());
cv::Mat frame1 = imread("rubberwhale2.png", cv::IMREAD_GRAYSCALE);
ASSERT_FALSE(frame1.empty());
cv::ocl::oclMat d_frame0(frame0), d_frame1(frame1);
int polyNs[2] = { 5, 7 };
double polySigmas[2] = { 1.1, 1.5 };
int farneFlags[2] = { 0, cv::OPTFLOW_FARNEBACK_GAUSSIAN };
bool UseInitFlows[2] = { false, true };
double pyrScale = 0.5;
string farneFlagStrs[2] = { "BoxFilter", "GaussianBlur" };
string useInitFlowStrs[2] = { "", "UseInitFlow" };
for ( int i = 0; i < 2; ++i)
{
int polyN = polyNs[i];
double polySigma = polySigmas[i];
for ( int j = 0; j < 2; ++j)
{
int flags = farneFlags[j];
for ( int k = 0; k < 2; ++k)
{
bool useInitFlow = UseInitFlows[k];
SUBTEST << "polyN(" << polyN << "); " << farneFlagStrs[j] << "; " << useInitFlowStrs[k];
cv::ocl::FarnebackOpticalFlow farn;
farn.pyrScale = pyrScale;
farn.polyN = polyN;
farn.polySigma = polySigma;
farn.flags = flags;
cv::ocl::oclMat d_flowx, d_flowy;
cv::Mat flow, flowBuf, flowxBuf, flowyBuf;
WARMUP_ON;
farn(d_frame0, d_frame1, d_flowx, d_flowy);
if (useInitFlow)
{
cv::Mat flowxy[] = {cv::Mat(d_flowx), cv::Mat(d_flowy)};
cv::merge(flowxy, 2, flow);
flow.copyTo(flowBuf);
flowxy[0].copyTo(flowxBuf);
flowxy[1].copyTo(flowyBuf);
farn.flags |= cv::OPTFLOW_USE_INITIAL_FLOW;
farn(d_frame0, d_frame1, d_flowx, d_flowy);
}
WARMUP_OFF;
cv::calcOpticalFlowFarneback(
frame0, frame1, flow, farn.pyrScale, farn.numLevels, farn.winSize,
farn.numIters, farn.polyN, farn.polySigma, farn.flags);
std::vector<cv::Mat> flowxy;
cv::split(flow, flowxy);
double diff0 = 0.0;
TestSystem::instance().setAccurate(ExceptedMatSimilar(flowxy[0], cv::Mat(d_flowx), 0.1, diff0));
TestSystem::instance().setDiff(diff0);
double diff1 = 0.0;
TestSystem::instance().setAccurate(ExceptedMatSimilar(flowxy[1], cv::Mat(d_flowy), 0.1, diff1));
TestSystem::instance().setDiff(diff1);
if (useInitFlow)
{
cv::Mat flowx, flowy;
farn.flags = (flags | cv::OPTFLOW_USE_INITIAL_FLOW);
CPU_ON;
cv::calcOpticalFlowFarneback(
frame0, frame1, flowBuf, farn.pyrScale, farn.numLevels, farn.winSize,
farn.numIters, farn.polyN, farn.polySigma, farn.flags);
CPU_OFF;
GPU_ON;
farn(d_frame0, d_frame1, d_flowx, d_flowy);
GPU_OFF;
GPU_FULL_ON;
d_frame0.upload(frame0);
d_frame1.upload(frame1);
d_flowx.upload(flowxBuf);
d_flowy.upload(flowyBuf);
farn(d_frame0, d_frame1, d_flowx, d_flowy);
d_flowx.download(flowx);
d_flowy.download(flowy);
GPU_FULL_OFF;
}
else
{
cv::Mat flow, flowx, flowy;
cv::ocl::oclMat d_flowx, d_flowy;
farn.flags = flags;
CPU_ON;
cv::calcOpticalFlowFarneback(
frame0, frame1, flow, farn.pyrScale, farn.numLevels, farn.winSize,
farn.numIters, farn.polyN, farn.polySigma, farn.flags);
CPU_OFF;
GPU_ON;
farn(d_frame0, d_frame1, d_flowx, d_flowy);
GPU_OFF;
GPU_FULL_ON;
d_frame0.upload(frame0);
d_frame1.upload(frame1);
farn(d_frame0, d_frame1, d_flowx, d_flowy);
d_flowx.download(flowx);
d_flowy.download(flowy);
GPU_FULL_OFF;
}
}
}
}
}
This diff is collapsed.
This diff is collapsed.
......@@ -272,6 +272,78 @@ TEST_P(Sparse, Mat)
INSTANTIATE_TEST_CASE_P(OCL_Video, Sparse, Combine(
Values(false, true),
Values(false, true)));
//////////////////////////////////////////////////////
// FarnebackOpticalFlow
namespace
{
IMPLEMENT_PARAM_CLASS(PyrScale, double)
IMPLEMENT_PARAM_CLASS(PolyN, int)
CV_FLAGS(FarnebackOptFlowFlags, 0, OPTFLOW_FARNEBACK_GAUSSIAN)
IMPLEMENT_PARAM_CLASS(UseInitFlow, bool)
}
PARAM_TEST_CASE(Farneback, PyrScale, PolyN, FarnebackOptFlowFlags, UseInitFlow)
{
double pyrScale;
int polyN;
int flags;
bool useInitFlow;
virtual void SetUp()
{
pyrScale = GET_PARAM(0);
polyN = GET_PARAM(1);
flags = GET_PARAM(2);
useInitFlow = GET_PARAM(3);
}
};
TEST_P(Farneback, Accuracy)
{
cv::Mat frame0 = imread(workdir + "/rubberwhale1.png", cv::IMREAD_GRAYSCALE);
ASSERT_FALSE(frame0.empty());
cv::Mat frame1 = imread(workdir + "/rubberwhale2.png", cv::IMREAD_GRAYSCALE);
ASSERT_FALSE(frame1.empty());
double polySigma = polyN <= 5 ? 1.1 : 1.5;
cv::ocl::FarnebackOpticalFlow farn;
farn.pyrScale = pyrScale;
farn.polyN = polyN;
farn.polySigma = polySigma;
farn.flags = flags;
cv::ocl::oclMat d_flowx, d_flowy;
farn(oclMat(frame0), oclMat(frame1), d_flowx, d_flowy);
cv::Mat flow;
if (useInitFlow)
{
cv::Mat flowxy[] = {cv::Mat(d_flowx), cv::Mat(d_flowy)};
cv::merge(flowxy, 2, flow);
farn.flags |= cv::OPTFLOW_USE_INITIAL_FLOW;
farn(oclMat(frame0), oclMat(frame1), d_flowx, d_flowy);
}
cv::calcOpticalFlowFarneback(
frame0, frame1, flow, farn.pyrScale, farn.numLevels, farn.winSize,
farn.numIters, farn.polyN, farn.polySigma, farn.flags);
std::vector<cv::Mat> flowxy;
cv::split(flow, flowxy);
EXPECT_MAT_SIMILAR(flowxy[0], d_flowx, 0.1);
EXPECT_MAT_SIMILAR(flowxy[1], d_flowy, 0.1);
}
INSTANTIATE_TEST_CASE_P(OCL_Video, Farneback, testing::Combine(
testing::Values(PyrScale(0.3), PyrScale(0.5), PyrScale(0.8)),
testing::Values(PolyN(5), PolyN(7)),
testing::Values(FarnebackOptFlowFlags(0), FarnebackOptFlowFlags(cv::OPTFLOW_FARNEBACK_GAUSSIAN)),
testing::Values(UseInitFlow(false), UseInitFlow(true))));
#endif // HAVE_OPENCL
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