Commit a3a09cf4 authored by Vladislav Vinogradov's avatar Vladislav Vinogradov

refactored OpticalFlowDual_TVL1:

* added DenseOpticalFlow interface
* moved OpticalFlowDual_TVL1 to src folder
parent 2181a41a
......@@ -431,13 +431,13 @@ PERF_TEST_P(ImagePair, Video_OpticalFlowDual_TVL1,
{
cv::Mat flow;
cv::OpticalFlowDual_TVL1 alg;
cv::Ptr<cv::DenseOpticalFlow> alg = cv::createOptFlow_DualTVL1();
alg(frame0, frame1, flow);
alg->calc(frame0, frame1, flow);
TEST_CYCLE()
{
alg(frame0, frame1, flow);
alg->calc(frame0, frame1, flow);
}
CPU_SANITY_CHECK(flow);
......
......@@ -431,9 +431,9 @@ GPU_TEST_P(OpticalFlowDual_TVL1, Accuracy)
cv::gpu::GpuMat d_flowy = createMat(frame0.size(), CV_32FC1, useRoi);
d_alg(loadMat(frame0, useRoi), loadMat(frame1, useRoi), d_flowx, d_flowy);
cv::OpticalFlowDual_TVL1 alg;
cv::Ptr<cv::DenseOpticalFlow> alg = cv::createOptFlow_DualTVL1();
cv::Mat flow;
alg(frame0, frame1, flow);
alg->calc(frame0, frame1, flow);
cv::Mat gold[2];
cv::split(flow, gold);
......
......@@ -643,11 +643,11 @@ See [Tao2012]_. And site of project - http://graphics.berkeley.edu/papers/Tao-SA
OpticalFlowDual_TVL1
--------------------
createOptFlow_DualTVL1
----------------------
"Dual TV L1" Optical Flow Algorithm.
.. ocv:class:: OpticalFlowDual_TVL12
.. ocv:function:: Ptr<DenseOpticalFlow> createOptFlow_DualTVL1()
The class implements the "Dual TV L1" optical flow algorithm described in [Zach2007]_ and [Javier2012]_ .
......@@ -685,11 +685,11 @@ Here are important members of the class that control the algorithm, which you ca
OpticalFlowDual_TVL1::operator()
--------------------------------
DenseOpticalFlow::calc
--------------------------
Calculates an optical flow.
.. ocv:function:: void OpticalFlowDual_TVL1::operator ()(InputArray I0, InputArray I1, InputOutputArray flow)
.. ocv:function:: void DenseOpticalFlow::calc(InputArray I0, InputArray I1, InputOutputArray flow)
:param prev: first 8-bit single-channel input image.
......@@ -699,11 +699,11 @@ Calculates an optical flow.
OpticalFlowDual_TVL1::collectGarbage
------------------------------------
DenseOpticalFlow::collectGarbage
--------------------------------
Releases all inner buffers.
.. ocv:function:: void OpticalFlowDual_TVL1::collectGarbage()
.. ocv:function:: void DenseOpticalFlow::collectGarbage()
......
......@@ -352,104 +352,19 @@ CV_EXPORTS_W void calcOpticalFlowSF(Mat& from,
double upscale_sigma_color,
double speed_up_thr);
class CV_EXPORTS DenseOpticalFlow : public Algorithm
{
public:
virtual void calc(InputArray I0, InputArray I1, InputOutputArray flow) = 0;
virtual void collectGarbage() = 0;
};
// Implementation of the Zach, Pock and Bischof Dual TV-L1 Optical Flow method
//
// see reference:
// [1] C. Zach, T. Pock and H. Bischof, "A Duality Based Approach for Realtime TV-L1 Optical Flow".
// [2] Javier Sanchez, Enric Meinhardt-Llopis and Gabriele Facciolo. "TV-L1 Optical Flow Estimation".
class CV_EXPORTS OpticalFlowDual_TVL1
{
public:
OpticalFlowDual_TVL1();
void operator ()(InputArray I0, InputArray I1, InputOutputArray flow);
void collectGarbage();
/**
* Time step of the numerical scheme.
*/
double tau;
/**
* Weight parameter for the data term, attachment parameter.
* This is the most relevant parameter, which determines the smoothness of the output.
* The smaller this parameter is, the smoother the solutions we obtain.
* It depends on the range of motions of the images, so its value should be adapted to each image sequence.
*/
double lambda;
/**
* Weight parameter for (u - v)^2, tightness parameter.
* It serves as a link between the attachment and the regularization terms.
* In theory, it should have a small value in order to maintain both parts in correspondence.
* The method is stable for a large range of values of this parameter.
*/
double theta;
/**
* Number of scales used to create the pyramid of images.
*/
int nscales;
/**
* Number of warpings per scale.
* Represents the number of times that I1(x+u0) and grad( I1(x+u0) ) are computed per scale.
* This is a parameter that assures the stability of the method.
* It also affects the running time, so it is a compromise between speed and accuracy.
*/
int warps;
/**
* Stopping criterion threshold used in the numerical scheme, which is a trade-off between precision and running time.
* A small value will yield more accurate solutions at the expense of a slower convergence.
*/
double epsilon;
/**
* Stopping criterion iterations number used in the numerical scheme.
*/
int iterations;
bool useInitialFlow;
private:
void procOneScale(const Mat_<float>& I0, const Mat_<float>& I1, Mat_<float>& u1, Mat_<float>& u2);
std::vector<Mat_<float> > I0s;
std::vector<Mat_<float> > I1s;
std::vector<Mat_<float> > u1s;
std::vector<Mat_<float> > u2s;
Mat_<float> I1x_buf;
Mat_<float> I1y_buf;
Mat_<float> flowMap1_buf;
Mat_<float> flowMap2_buf;
Mat_<float> I1w_buf;
Mat_<float> I1wx_buf;
Mat_<float> I1wy_buf;
Mat_<float> grad_buf;
Mat_<float> rho_c_buf;
Mat_<float> v1_buf;
Mat_<float> v2_buf;
Mat_<float> p11_buf;
Mat_<float> p12_buf;
Mat_<float> p21_buf;
Mat_<float> p22_buf;
Mat_<float> div_p1_buf;
Mat_<float> div_p2_buf;
Mat_<float> u1x_buf;
Mat_<float> u1y_buf;
Mat_<float> u2x_buf;
Mat_<float> u2y_buf;
};
CV_EXPORTS Ptr<DenseOpticalFlow> createOptFlow_DualTVL1();
}
......
......@@ -22,12 +22,9 @@ PERF_TEST_P(ImagePair, OpticalFlowDual_TVL1, testing::Values(impair("cv/optflow/
Mat flow;
OpticalFlowDual_TVL1 tvl1;
Ptr<DenseOpticalFlow> tvl1 = createOptFlow_DualTVL1();
TEST_CYCLE()
{
tvl1(frame1, frame2, flow);
}
TEST_CYCLE_N(10) tvl1->calc(frame1, frame2, flow);
SANITY_CHECK(flow, 0.5);
}
......@@ -77,7 +77,67 @@
using namespace std;
using namespace cv;
cv::OpticalFlowDual_TVL1::OpticalFlowDual_TVL1()
namespace {
class OpticalFlowDual_TVL1 : public DenseOpticalFlow
{
public:
OpticalFlowDual_TVL1();
void calc(InputArray I0, InputArray I1, InputOutputArray flow);
void collectGarbage();
AlgorithmInfo* info() const;
protected:
double tau;
double lambda;
double theta;
int nscales;
int warps;
double epsilon;
int iterations;
bool useInitialFlow;
private:
void procOneScale(const Mat_<float>& I0, const Mat_<float>& I1, Mat_<float>& u1, Mat_<float>& u2);
std::vector<Mat_<float> > I0s;
std::vector<Mat_<float> > I1s;
std::vector<Mat_<float> > u1s;
std::vector<Mat_<float> > u2s;
Mat_<float> I1x_buf;
Mat_<float> I1y_buf;
Mat_<float> flowMap1_buf;
Mat_<float> flowMap2_buf;
Mat_<float> I1w_buf;
Mat_<float> I1wx_buf;
Mat_<float> I1wy_buf;
Mat_<float> grad_buf;
Mat_<float> rho_c_buf;
Mat_<float> v1_buf;
Mat_<float> v2_buf;
Mat_<float> p11_buf;
Mat_<float> p12_buf;
Mat_<float> p21_buf;
Mat_<float> p22_buf;
Mat_<float> div_p1_buf;
Mat_<float> div_p2_buf;
Mat_<float> u1x_buf;
Mat_<float> u1y_buf;
Mat_<float> u2x_buf;
Mat_<float> u2y_buf;
};
OpticalFlowDual_TVL1::OpticalFlowDual_TVL1()
{
tau = 0.25;
lambda = 0.15;
......@@ -89,7 +149,7 @@ cv::OpticalFlowDual_TVL1::OpticalFlowDual_TVL1()
useInitialFlow = false;
}
void cv::OpticalFlowDual_TVL1::operator ()(InputArray _I0, InputArray _I1, InputOutputArray _flow)
void OpticalFlowDual_TVL1::calc(InputArray _I0, InputArray _I1, InputOutputArray _flow)
{
Mat I0 = _I0.getMat();
Mat I1 = _I1.getMat();
......@@ -195,23 +255,21 @@ void cv::OpticalFlowDual_TVL1::operator ()(InputArray _I0, InputArray _I1, Input
merge(uxy, 2, _flow);
}
namespace
{
////////////////////////////////////////////////////////////
// buildFlowMap
////////////////////////////////////////////////////////////
// buildFlowMap
struct BuildFlowMapBody : ParallelLoopBody
{
struct BuildFlowMapBody : ParallelLoopBody
{
void operator() (const Range& range) const;
Mat_<float> u1;
Mat_<float> u2;
mutable Mat_<float> map1;
mutable Mat_<float> map2;
};
};
void BuildFlowMapBody::operator() (const Range& range) const
{
void BuildFlowMapBody::operator() (const Range& range) const
{
for (int y = range.start; y < range.end; ++y)
{
const float* u1Row = u1[y];
......@@ -226,10 +284,10 @@ namespace
map2Row[x] = y + u2Row[x];
}
}
}
}
void buildFlowMap(const Mat_<float>& u1, const Mat_<float>& u2, Mat_<float>& map1, Mat_<float>& map2)
{
void buildFlowMap(const Mat_<float>& u1, const Mat_<float>& u2, Mat_<float>& map1, Mat_<float>& map2)
{
CV_DbgAssert( u2.size() == u1.size() );
CV_DbgAssert( map1.size() == u1.size() );
CV_DbgAssert( map2.size() == u1.size() );
......@@ -242,22 +300,22 @@ namespace
body.map2 = map2;
parallel_for_(Range(0, u1.rows), body);
}
}
////////////////////////////////////////////////////////////
// centeredGradient
////////////////////////////////////////////////////////////
// centeredGradient
struct CenteredGradientBody : ParallelLoopBody
{
struct CenteredGradientBody : ParallelLoopBody
{
void operator() (const Range& range) const;
Mat_<float> src;
mutable Mat_<float> dx;
mutable Mat_<float> dy;
};
};
void CenteredGradientBody::operator() (const Range& range) const
{
void CenteredGradientBody::operator() (const Range& range) const
{
const int last_col = src.cols - 1;
for (int y = range.start; y < range.end; ++y)
......@@ -275,10 +333,10 @@ namespace
dyRow[x] = 0.5f * (srcNextRow[x] - srcPrevRow[x]);
}
}
}
}
void centeredGradient(const Mat_<float>& src, Mat_<float>& dx, Mat_<float>& dy)
{
void centeredGradient(const Mat_<float>& src, Mat_<float>& dx, Mat_<float>& dy)
{
CV_DbgAssert( src.rows > 2 && src.cols > 2 );
CV_DbgAssert( dx.size() == src.size() );
CV_DbgAssert( dy.size() == src.size() );
......@@ -329,22 +387,22 @@ namespace
dx(last_row, last_col) = 0.5f * (src(last_row, last_col) - src(last_row, last_col - 1));
dy(last_row, last_col) = 0.5f * (src(last_row, last_col) - src(last_row - 1, last_col));
}
}
////////////////////////////////////////////////////////////
// forwardGradient
////////////////////////////////////////////////////////////
// forwardGradient
struct ForwardGradientBody : ParallelLoopBody
{
struct ForwardGradientBody : ParallelLoopBody
{
void operator() (const Range& range) const;
Mat_<float> src;
mutable Mat_<float> dx;
mutable Mat_<float> dy;
};
};
void ForwardGradientBody::operator() (const Range& range) const
{
void ForwardGradientBody::operator() (const Range& range) const
{
const int last_col = src.cols - 1;
for (int y = range.start; y < range.end; ++y)
......@@ -361,10 +419,10 @@ namespace
dyRow[x] = srcNextRow[x] - srcCurRow[x];
}
}
}
}
void forwardGradient(const Mat_<float>& src, Mat_<float>& dx, Mat_<float>& dy)
{
void forwardGradient(const Mat_<float>& src, Mat_<float>& dx, Mat_<float>& dy)
{
CV_DbgAssert( src.rows > 2 && src.cols > 2 );
CV_DbgAssert( dx.size() == src.size() );
CV_DbgAssert( dy.size() == src.size() );
......@@ -399,22 +457,22 @@ namespace
dx(last_row, last_col) = 0.0f;
dy(last_row, last_col) = 0.0f;
}
}
////////////////////////////////////////////////////////////
// divergence
////////////////////////////////////////////////////////////
// divergence
struct DivergenceBody : ParallelLoopBody
{
struct DivergenceBody : ParallelLoopBody
{
void operator() (const Range& range) const;
Mat_<float> v1;
Mat_<float> v2;
mutable Mat_<float> div;
};
};
void DivergenceBody::operator() (const Range& range) const
{
void DivergenceBody::operator() (const Range& range) const
{
for (int y = range.start; y < range.end; ++y)
{
const float* v1Row = v1[y];
......@@ -431,10 +489,10 @@ namespace
divRow[x] = v1x + v2y;
}
}
}
}
void divergence(const Mat_<float>& v1, const Mat_<float>& v2, Mat_<float>& div)
{
void divergence(const Mat_<float>& v1, const Mat_<float>& v2, Mat_<float>& div)
{
CV_DbgAssert( v1.rows > 2 && v1.cols > 2 );
CV_DbgAssert( v2.size() == v1.size() );
CV_DbgAssert( div.size() == v1.size() );
......@@ -458,13 +516,13 @@ namespace
div(y, 0) = v1(y, 0) + v2(y, 0) - v2(y - 1, 0);
div(0, 0) = v1(0, 0) + v2(0, 0);
}
}
////////////////////////////////////////////////////////////
// calcGradRho
////////////////////////////////////////////////////////////
// calcGradRho
struct CalcGradRhoBody : ParallelLoopBody
{
struct CalcGradRhoBody : ParallelLoopBody
{
void operator() (const Range& range) const;
Mat_<float> I0;
......@@ -475,10 +533,10 @@ namespace
Mat_<float> u2;
mutable Mat_<float> grad;
mutable Mat_<float> rho_c;
};
};
void CalcGradRhoBody::operator() (const Range& range) const
{
void CalcGradRhoBody::operator() (const Range& range) const
{
for (int y = range.start; y < range.end; ++y)
{
const float* I0Row = I0[y];
......@@ -503,11 +561,11 @@ namespace
rhoRow[x] = (I1wRow[x] - I1wxRow[x] * u1Row[x] - I1wyRow[x] * u2Row[x] - I0Row[x]);
}
}
}
}
void calcGradRho(const Mat_<float>& I0, const Mat_<float>& I1w, const Mat_<float>& I1wx, const Mat_<float>& I1wy, const Mat_<float>& u1, const Mat_<float>& u2,
void calcGradRho(const Mat_<float>& I0, const Mat_<float>& I1w, const Mat_<float>& I1wx, const Mat_<float>& I1wy, const Mat_<float>& u1, const Mat_<float>& u2,
Mat_<float>& grad, Mat_<float>& rho_c)
{
{
CV_DbgAssert( I1w.size() == I0.size() );
CV_DbgAssert( I1wx.size() == I0.size() );
CV_DbgAssert( I1wy.size() == I0.size() );
......@@ -528,13 +586,13 @@ namespace
body.rho_c = rho_c;
parallel_for_(Range(0, I0.rows), body);
}
}
////////////////////////////////////////////////////////////
// estimateV
////////////////////////////////////////////////////////////
// estimateV
struct EstimateVBody : ParallelLoopBody
{
struct EstimateVBody : ParallelLoopBody
{
void operator() (const Range& range) const;
Mat_<float> I1wx;
......@@ -546,10 +604,10 @@ namespace
mutable Mat_<float> v1;
mutable Mat_<float> v2;
float l_t;
};
};
void EstimateVBody::operator() (const Range& range) const
{
void EstimateVBody::operator() (const Range& range) const
{
for (int y = range.start; y < range.end; ++y)
{
const float* I1wxRow = I1wx[y];
......@@ -590,11 +648,11 @@ namespace
v2Row[x] = u2Row[x] + d2;
}
}
}
}
void estimateV(const Mat_<float>& I1wx, const Mat_<float>& I1wy, const Mat_<float>& u1, const Mat_<float>& u2, const Mat_<float>& grad, const Mat_<float>& rho_c,
void estimateV(const Mat_<float>& I1wx, const Mat_<float>& I1wy, const Mat_<float>& u1, const Mat_<float>& u2, const Mat_<float>& grad, const Mat_<float>& rho_c,
Mat_<float>& v1, Mat_<float>& v2, float l_t)
{
{
CV_DbgAssert( I1wy.size() == I1wx.size() );
CV_DbgAssert( u1.size() == I1wx.size() );
CV_DbgAssert( u2.size() == I1wx.size() );
......@@ -616,13 +674,13 @@ namespace
body.l_t = l_t;
parallel_for_(Range(0, I1wx.rows), body);
}
}
////////////////////////////////////////////////////////////
// estimateU
////////////////////////////////////////////////////////////
// estimateU
float estimateU(const Mat_<float>& v1, const Mat_<float>& v2, const Mat_<float>& div_p1, const Mat_<float>& div_p2, Mat_<float>& u1, Mat_<float>& u2, float theta)
{
float estimateU(const Mat_<float>& v1, const Mat_<float>& v2, const Mat_<float>& div_p1, const Mat_<float>& div_p2, Mat_<float>& u1, Mat_<float>& u2, float theta)
{
CV_DbgAssert( v2.size() == v1.size() );
CV_DbgAssert( div_p1.size() == v1.size() );
CV_DbgAssert( div_p2.size() == v1.size() );
......@@ -653,13 +711,13 @@ namespace
}
return error;
}
}
////////////////////////////////////////////////////////////
// estimateDualVariables
////////////////////////////////////////////////////////////
// estimateDualVariables
struct EstimateDualVariablesBody : ParallelLoopBody
{
struct EstimateDualVariablesBody : ParallelLoopBody
{
void operator() (const Range& range) const;
Mat_<float> u1x;
......@@ -671,10 +729,10 @@ namespace
mutable Mat_<float> p21;
mutable Mat_<float> p22;
float taut;
};
};
void EstimateDualVariablesBody::operator() (const Range& range) const
{
void EstimateDualVariablesBody::operator() (const Range& range) const
{
for (int y = range.start; y < range.end; ++y)
{
const float* u1xRow = u1x[y];
......@@ -701,11 +759,11 @@ namespace
p22Row[x] = (p22Row[x] + taut * u2yRow[x]) / ng2;
}
}
}
}
void estimateDualVariables(const Mat_<float>& u1x, const Mat_<float>& u1y, const Mat_<float>& u2x, const Mat_<float>& u2y,
void estimateDualVariables(const Mat_<float>& u1x, const Mat_<float>& u1y, const Mat_<float>& u2x, const Mat_<float>& u2y,
Mat_<float>& p11, Mat_<float>& p12, Mat_<float>& p21, Mat_<float>& p22, float taut)
{
{
CV_DbgAssert( u1y.size() == u1x.size() );
CV_DbgAssert( u2x.size() == u1x.size() );
CV_DbgAssert( u2y.size() == u1x.size() );
......@@ -727,10 +785,9 @@ namespace
body.taut = taut;
parallel_for_(Range(0, u1x.rows), body);
}
}
void cv::OpticalFlowDual_TVL1::procOneScale(const Mat_<float>& I0, const Mat_<float>& I1, Mat_<float>& u1, Mat_<float>& u2)
void OpticalFlowDual_TVL1::procOneScale(const Mat_<float>& I0, const Mat_<float>& I1, Mat_<float>& u1, Mat_<float>& u2)
{
const float scaledEpsilon = static_cast<float>(epsilon * epsilon * I0.size().area());
......@@ -818,21 +875,12 @@ void cv::OpticalFlowDual_TVL1::procOneScale(const Mat_<float>& I0, const Mat_<fl
}
}
namespace
{
template <typename T> void releaseVector(vector<T>& v)
{
vector<T> empty;
empty.swap(v);
}
}
void cv::OpticalFlowDual_TVL1::collectGarbage()
void OpticalFlowDual_TVL1::collectGarbage()
{
releaseVector(I0s);
releaseVector(I1s);
releaseVector(u1s);
releaseVector(u2s);
I0s.clear();
I1s.clear();
u1s.clear();
u2s.clear();
I1x_buf.release();
I1y_buf.release();
......@@ -863,3 +911,27 @@ void cv::OpticalFlowDual_TVL1::collectGarbage()
u2x_buf.release();
u2y_buf.release();
}
CV_INIT_ALGORITHM(OpticalFlowDual_TVL1, "DenseOpticalFlow.DualTVL1",
obj.info()->addParam(obj, "tau", obj.tau, false, 0, 0,
"Time step of the numerical scheme");
obj.info()->addParam(obj, "lambda", obj.lambda, false, 0, 0,
"Weight parameter for the data term, attachment parameter");
obj.info()->addParam(obj, "theta", obj.theta, false, 0, 0,
"Weight parameter for (u - v)^2, tightness parameter");
obj.info()->addParam(obj, "nscales", obj.nscales, false, 0, 0,
"Number of scales used to create the pyramid of images");
obj.info()->addParam(obj, "warps", obj.warps, false, 0, 0,
"Number of warpings per scale");
obj.info()->addParam(obj, "epsilon", obj.epsilon, false, 0, 0,
"Stopping criterion threshold used in the numerical scheme, which is a trade-off between precision and running time");
obj.info()->addParam(obj, "iterations", obj.iterations, false, 0, 0,
"Stopping criterion iterations number used in the numerical scheme");
obj.info()->addParam(obj, "useInitialFlow", obj.useInitialFlow));
} // namespace
Ptr<DenseOpticalFlow> cv::createOptFlow_DualTVL1()
{
return new OpticalFlowDual_TVL1;
}
......@@ -152,9 +152,9 @@ TEST(Video_calcOpticalFlowDual_TVL1, Regression)
ASSERT_FALSE(frame2.empty());
Mat_<Point2f> flow;
OpticalFlowDual_TVL1 tvl1;
Ptr<DenseOpticalFlow> tvl1 = createOptFlow_DualTVL1();
tvl1(frame1, frame2, flow);
tvl1->calc(frame1, frame2, flow);
#ifdef DUMP
writeOpticalFlowToFile(flow, gold_flow_path);
......
......@@ -173,10 +173,10 @@ int main(int argc, const char* argv[])
}
Mat_<Point2f> flow;
OpticalFlowDual_TVL1 tvl1;
Ptr<DenseOpticalFlow> tvl1 = createOptFlow_DualTVL1();
const double start = (double)getTickCount();
tvl1(frame0, frame1, flow);
tvl1->calc(frame0, frame1, flow);
const double timeSec = (getTickCount() - start) / getTickFrequency();
cout << "calcOpticalFlowDual_TVL1 : " << timeSec << " sec" << endl;
......
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