Files
opencv-MIRROR/modules/video/perf/perf_optflowfarneback.cpp
RS Nikhil Krishna 75ff081cfb video: reuse the polynomial expansion between Farneback calls
Add OPTFLOW_FARNEBACK_REUSE_EXPANSION, an opt-in flag set through
FarnebackOpticalFlow::setFlags() / create(..., flags). When set, calc() keeps
the polynomial expansion of the previous call's next frame and reuses it as
the expansion of the following call's prev frame, which saves roughly 9-14%
on consecutive-frame sequences. Results are bit-identical with reuse on and
off. The flag adds no virtual methods, is ignored by calcOpticalFlowFarneback()
and by the OpenCL path, and clearing it or calling collectGarbage() frees the
cache.

Tests go into test_OF_accuracy.cpp and test_OF_reproducibility.cpp and seed
their data from theRNG(). Includes a perf test and a tutorial section.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-11 22:32:58 -05:00

55 lines
1.7 KiB
C++

// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "perf_precomp.hpp"
namespace opencv_test { namespace {
const int SEQUENCE_LENGTH = 10;
static std::vector<Mat> makeSequence(Size size)
{
Mat base(size.height * 2, size.width * 2, CV_8U);
RNG(0x5eed).fill(base, RNG::UNIFORM, 0, 256);
GaussianBlur(base, base, Size(9, 9), 3, 3);
std::vector<Mat> frames;
for( int i = 0; i < SEQUENCE_LENGTH; i++ )
{
Mat shift = Mat::eye(2, 3, CV_64F);
shift.at<double>(0, 2) = 1.7 * i;
shift.at<double>(1, 2) = -1.1 * i;
Mat warped;
warpAffine(base, warped, shift, base.size(), INTER_LINEAR);
frames.push_back(warped(Rect(size.width / 2, size.height / 2,
size.width, size.height)).clone());
}
return frames;
}
typedef tuple<Size, bool> FarnebackParams;
typedef TestBaseWithParam<FarnebackParams> DenseOpticalFlow_Farneback;
PERF_TEST_P(DenseOpticalFlow_Farneback, perf,
Combine(Values(szVGA, sz720p), testing::Bool()))
{
const Size size = get<0>(GetParam());
const bool reuseExpansion = get<1>(GetParam());
const std::vector<Mat> frames = makeSequence(size);
Mat flow;
Ptr<FarnebackOpticalFlow> algo = FarnebackOpticalFlow::create();
algo->setFlags(reuseExpansion ? OPTFLOW_FARNEBACK_REUSE_EXPANSION : 0);
TEST_CYCLE()
{
for( int i = 0; i + 1 < SEQUENCE_LENGTH; i++ )
algo->calc(frames[i], frames[i + 1], flow);
}
SANITY_CHECK_NOTHING();
}
}} // namespace