Files
opencv-MIRROR/modules/video/test/test_OF_accuracy.cpp
velonica0 07b31f1e2e video: dis: vectorize Densification_ParBody with scalable intrinsics (#30546)
* video: dis: vectorize Densification_ParBody with scalable intrinsics

* video: dis: address review comments: braces, parameterized test on all platforms

* video: dis: restore useOptimized with a scope guard in the DIS reference test
2026-11-13 15:38:44 +05:30

458 lines
17 KiB
C++

/*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.
//
//
// Intel License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, 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 materials provided with the distribution.
//
// * The name of Intel Corporation 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 implied warranties, including, but not limited to, the implied
// 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 "test_precomp.hpp"
namespace opencv_test { namespace {
static string getDataDir() { return TS::ptr()->get_data_path(); }
static string getRubberWhaleFrame1() { return getDataDir() + "optflow/RubberWhale1.png"; }
static string getRubberWhaleFrame2() { return getDataDir() + "optflow/RubberWhale2.png"; }
static string getRubberWhaleGroundTruth() { return getDataDir() + "optflow/RubberWhale.flo"; }
static bool isFlowCorrect(float u) { return !cvIsNaN(u) && (fabs(u) < 1e9); }
static float calcRMSE(Mat flow1, Mat flow2)
{
float sum = 0;
int counter = 0;
const int rows = flow1.rows;
const int cols = flow1.cols;
for (int y = 0; y < rows; ++y)
{
for (int x = 0; x < cols; ++x)
{
Vec2f flow1_at_point = flow1.at<Vec2f>(y, x);
Vec2f flow2_at_point = flow2.at<Vec2f>(y, x);
float u1 = flow1_at_point[0];
float v1 = flow1_at_point[1];
float u2 = flow2_at_point[0];
float v2 = flow2_at_point[1];
if (isFlowCorrect(u1) && isFlowCorrect(u2) && isFlowCorrect(v1) && isFlowCorrect(v2))
{
sum += (u1 - u2) * (u1 - u2) + (v1 - v2) * (v1 - v2);
counter++;
}
}
}
return (float)sqrt(sum / (1e-9 + counter));
}
bool readRubberWhale(Mat &dst_frame_1, Mat &dst_frame_2, Mat &dst_GT)
{
const string frame1_path = getRubberWhaleFrame1();
const string frame2_path = getRubberWhaleFrame2();
const string gt_flow_path = getRubberWhaleGroundTruth();
dst_frame_1 = imread(frame1_path);
dst_frame_2 = imread(frame2_path);
dst_GT = readOpticalFlow(gt_flow_path);
if (dst_frame_1.empty() || dst_frame_2.empty() || dst_GT.empty())
return false;
else
return true;
}
TEST(DenseOpticalFlow_DIS, ReferenceAccuracy)
{
Mat frame1, frame2, GT;
ASSERT_TRUE(readRubberWhale(frame1, frame2, GT));
int presets[] = {DISOpticalFlow::PRESET_ULTRAFAST, DISOpticalFlow::PRESET_FAST, DISOpticalFlow::PRESET_MEDIUM};
float target_RMSE[] = {0.86f, 0.74f, 0.49f};
cvtColor(frame1, frame1, COLOR_BGR2GRAY);
cvtColor(frame2, frame2, COLOR_BGR2GRAY);
Ptr<DenseOpticalFlow> algo;
// iterate over presets:
for (int i = 0; i < 3; i++)
{
Mat flow;
algo = DISOpticalFlow::create(presets[i]);
algo->calc(frame1, frame2, flow);
ASSERT_EQ(GT.rows, flow.rows);
ASSERT_EQ(GT.cols, flow.cols);
EXPECT_LE(calcRMSE(GT, flow), target_RMSE[i]);
}
}
TEST(DenseOpticalFlow_DIS, InvalidImgSize_CoarsestLevelLessThanZero)
{
cv::Ptr<cv::DISOpticalFlow> of = cv::DISOpticalFlow::create();
const int mat_size = 10;
cv::Mat x(mat_size, mat_size, CV_8UC1, 42);
cv::Mat y(mat_size, mat_size, CV_8UC1, 42);
cv::Mat flow;
ASSERT_THROW(of->calc(x, y, flow), cv::Exception);
}
// make sure that autoSelectPatchSizeAndScales() works properly.
TEST(DenseOpticalFlow_DIS, InvalidImgSize_CoarsestLevelLessThanFinestLevel)
{
cv::Ptr<cv::DISOpticalFlow> of = cv::DISOpticalFlow::create();
const int mat_size = 80;
cv::Mat x(mat_size, mat_size, CV_8UC1, 42);
cv::Mat y(mat_size, mat_size, CV_8UC1, 42);
cv::Mat flow;
of->calc(x, y, flow);
ASSERT_EQ(flow.rows, mat_size);
ASSERT_EQ(flow.cols, mat_size);
}
TEST(DenseOpticalFlow_VariationalRefinement, ReferenceAccuracy)
{
Mat frame1, frame2, GT;
ASSERT_TRUE(readRubberWhale(frame1, frame2, GT));
float target_RMSE = 0.86f;
cvtColor(frame1, frame1, COLOR_BGR2GRAY);
cvtColor(frame2, frame2, COLOR_BGR2GRAY);
Ptr<VariationalRefinement> var_ref;
var_ref = VariationalRefinement::create();
var_ref->setAlpha(20.0f);
var_ref->setDelta(5.0f);
var_ref->setGamma(10.0f);
var_ref->setSorIterations(25);
var_ref->setFixedPointIterations(25);
Mat flow(frame1.size(), CV_32FC2);
flow.setTo(0.0f);
var_ref->calc(frame1, frame2, flow);
ASSERT_EQ(GT.rows, flow.rows);
ASSERT_EQ(GT.cols, flow.cols);
EXPECT_LE(calcRMSE(GT, flow), target_RMSE);
}
TEST(DenseOpticalFlow_DIS, ManualCoarsestScale)
{
Mat prev = Mat::zeros(Size(320, 240), CV_8UC1);
Mat next = Mat::zeros(Size(320, 240), CV_8UC1);
randu(prev, 0, 255);
randu(next, 0, 255);
Ptr<DISOpticalFlow> dis = DISOpticalFlow::create(DISOpticalFlow::PRESET_FAST);
EXPECT_EQ(dis->getCoarsestScale(), -1);
Mat flow;
dis->calc(prev, next, flow);
EXPECT_FALSE(flow.empty());
int manual_scale = 3;
dis->setCoarsestScale(manual_scale);
EXPECT_EQ(dis->getCoarsestScale(), manual_scale);
dis->calc(prev, next, flow);
EXPECT_FALSE(flow.empty());
dis->setCoarsestScale(-1);
EXPECT_EQ(dis->getCoarsestScale(), -1);
}
// See https://github.com/opencv/opencv/issues/20185
TEST(DenseOpticalFlow_DIS, regression_20185_patch_larger_than_border)
{
Mat prev(240, 320, CV_8UC1), next(240, 320, CV_8UC1);
theRNG().fill(prev, RNG::UNIFORM, 0, 256);
theRNG().fill(next, RNG::UNIFORM, 0, 256);
Ptr<DISOpticalFlow> dis = DISOpticalFlow::create(DISOpticalFlow::PRESET_MEDIUM);
dis->setPatchStride(10);
Mat flow(prev.size(), CV_32FC2, Scalar(60.f, 60.f));
ASSERT_NO_THROW(dis->calc(prev, next, flow));
EXPECT_EQ(flow.size(), prev.size());
dis->setPatchSize(25);
flow.setTo(Scalar(60.f, 60.f));
ASSERT_NO_THROW(dis->calc(prev, next, flow));
EXPECT_EQ(flow.size(), prev.size());
}
typedef tuple<Size, int, bool> DISOptimizedParams; // image size, preset, large initial flow
typedef TestWithParam<DISOptimizedParams> DenseOpticalFlow_DIS_OptimizedMatchesReference;
// The optimized code paths must match the reference selected with setUseOptimized(false)
TEST_P(DenseOpticalFlow_DIS_OptimizedMatchesReference, accuracy)
{
const Size sz = get<0>(GetParam());
const int preset = get<1>(GetParam());
const bool use_initial_flow = get<2>(GetParam());
Mat tmp(sz.height / 4, sz.width / 4, CV_8UC1), frame1, frame2;
theRNG().fill(tmp, RNG::UNIFORM, 0, 256);
resize(tmp, frame1, sz, 0.0, 0.0, INTER_LINEAR_EXACT);
Mat M = getRotationMatrix2D(Point2f(sz.width / 2.f, sz.height / 2.f), 3.0, 1.0);
M.at<double>(0, 2) += 2.5;
M.at<double>(1, 2) -= 1.5;
warpAffine(frame1, frame2, M, sz, INTER_LINEAR, BORDER_REPLICATE);
Mat flow_ref, flow_opt;
if (use_initial_flow)
{
/* a large initial flow pushes the sampling positions to the clamped image borders */
flow_ref.create(sz, CV_32FC2);
flow_ref.setTo(Scalar(60.f, 60.f));
flow_ref.copyTo(flow_opt);
}
{
/* restores the global flag even if calc() throws, so later tests are not affected */
struct UseOptimizedGuard
{
const bool saved;
UseOptimizedGuard() : saved(useOptimized()) {}
~UseOptimizedGuard() { setUseOptimized(saved); }
} guard;
setUseOptimized(false);
DISOpticalFlow::create(preset)->calc(frame1, frame2, flow_ref);
setUseOptimized(true);
DISOpticalFlow::create(preset)->calc(frame1, frame2, flow_opt);
}
EXPECT_LE(cvtest::norm(flow_ref, flow_opt, NORM_INF), 1e-2);
EXPECT_LE(cvtest::norm(flow_ref, flow_opt, NORM_L1), 1e-4 * flow_ref.total());
}
INSTANTIATE_TEST_CASE_P(/**/, DenseOpticalFlow_DIS_OptimizedMatchesReference,
Combine(Values(Size(127, 61), Size(641, 480)),
Values((int)DISOpticalFlow::PRESET_ULTRAFAST, (int)DISOpticalFlow::PRESET_FAST,
(int)DISOpticalFlow::PRESET_MEDIUM),
testing::Bool()));
TEST(Video_OpticalFlowFarneback, InitialFlow_Validation)
{
cv::Mat frame1 = cv::Mat::zeros(100, 100, CV_8UC1);
cv::Mat frame2 = cv::Mat::zeros(100, 100, CV_8UC1);
cv::Mat flow;
// Test 1: Empty flow with OPTFLOW_USE_INITIAL_FLOW flag
EXPECT_ANY_THROW(cv::calcOpticalFlowFarneback(frame1, frame2, flow, 0.5, 3, 15, 3, 5, 1.2, cv::OPTFLOW_USE_INITIAL_FLOW));
// Test 2: Mismatched size (image is 100x100, but flow is 50x50)
cv::Mat wrong_size_flow = cv::Mat::zeros(50, 50, CV_32FC2);
EXPECT_ANY_THROW(cv::calcOpticalFlowFarneback(frame1, frame2, wrong_size_flow, 0.5, 3, 15, 3, 5, 1.2, cv::OPTFLOW_USE_INITIAL_FLOW));
// Test 3: Mismatched type (CV_8UC1 instead of CV_32FC2)
cv::Mat wrong_type_flow = cv::Mat::zeros(100, 100, CV_8UC1);
EXPECT_ANY_THROW(cv::calcOpticalFlowFarneback(frame1, frame2, wrong_type_flow, 0.5, 3, 15, 3, 5, 1.2, cv::OPTFLOW_USE_INITIAL_FLOW));
}
const Point2f FARNEBACK_SHIFT(1.7f, -1.1f);
// A blurred noise texture translated by FARNEBACK_SHIFT per frame.
static std::vector<Mat> makeFarnebackSequence(Size size, int count)
{
Mat base(size.height * 2, size.width * 2, CV_8U);
theRNG().fill(base, RNG::UNIFORM, 0, 256);
GaussianBlur(base, base, Size(9, 9), 3, 3);
std::vector<Mat> frames;
for( int i = 0; i < count; i++ )
{
Mat shift = Mat::eye(2, 3, CV_64F);
shift.at<double>(0, 2) = FARNEBACK_SHIFT.x * i;
shift.at<double>(1, 2) = FARNEBACK_SHIFT.y * 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;
}
static Ptr<FarnebackOpticalFlow> makeFarneback(bool reuse)
{
return FarnebackOpticalFlow::create(3, 0.5, false, 9, 3, 5, 1.1,
reuse ? OPTFLOW_FARNEBACK_REUSE_EXPANSION : 0);
}
static Mat farnebackFlowWithoutReuse(const Mat& prev, const Mat& next)
{
Mat flow;
makeFarneback(false)->calc(prev, next, flow);
return flow;
}
static void expectSameFlow(const Mat& expected, const Mat& actual, const std::string& what)
{
ASSERT_EQ(expected.size(), actual.size()) << what;
ASSERT_EQ(expected.type(), actual.type()) << what;
ASSERT_GT(countNonZero(expected.reshape(1) != 0.0f), 0) << what;
const size_t rowBytes = (size_t)expected.cols * expected.elemSize();
for( int y = 0; y < expected.rows; y++ )
ASSERT_EQ(0, memcmp(expected.ptr(y), actual.ptr(y), rowBytes)) << what << ", row " << y;
}
TEST(DenseOpticalFlow_Farneback, Accuracy)
{
const std::vector<Mat> frames = makeFarnebackSequence(Size(320, 240), 4);
Ptr<FarnebackOpticalFlow> algo = FarnebackOpticalFlow::create();
algo->setFlags(OPTFLOW_FARNEBACK_REUSE_EXPANSION);
for( size_t i = 0; i + 1 < frames.size(); i++ )
{
Mat flow;
algo->calc(frames[i], frames[i + 1], flow);
std::vector<Mat> xy;
split(flow(Rect(16, 16, flow.cols - 32, flow.rows - 32)), xy);
for( int c = 0; c < 2; c++ )
{
Mat v = xy[c].reshape(1, 1).clone();
std::nth_element(v.begin<float>(), v.begin<float>() + v.cols / 2, v.end<float>());
EXPECT_NEAR(c == 0 ? FARNEBACK_SHIFT.x : FARNEBACK_SHIFT.y, v.at<float>(v.cols / 2), 0.1)
<< "pair " << i << ", channel " << c;
}
}
}
TEST(DenseOpticalFlow_Farneback, ReuseMatchesNoReuseOnSequence)
{
EXPECT_FALSE(FarnebackOpticalFlow::create()->getFlags() & OPTFLOW_FARNEBACK_REUSE_EXPANSION);
const std::vector<Mat> frames = makeFarnebackSequence(Size(160, 120), 6);
Ptr<FarnebackOpticalFlow> algo = makeFarneback(true);
for( size_t i = 0; i + 1 < frames.size(); i++ )
{
Mat flow;
algo->calc(frames[i], frames[i + 1], flow);
expectSameFlow(farnebackFlowWithoutReuse(frames[i], frames[i + 1]), flow,
format("pair %d", (int)i));
}
}
// Calls that must not reuse the held expansion: changed content, parameters or frame size.
TEST(DenseOpticalFlow_Farneback, ReuseMissesOnChangedContentOrParameters)
{
const std::vector<Mat> frames = makeFarnebackSequence(Size(160, 120), 3);
const std::vector<Mat> other = makeFarnebackSequence(Size(160, 120), 2);
// Same buffer refilled between calls: only the content differs.
{
Ptr<FarnebackOpticalFlow> algo = makeFarneback(true);
Mat buf = frames[1].clone(), flow;
algo->calc(frames[0], buf, flow);
other[0].copyTo(buf);
algo->calc(buf, other[1], flow);
expectSameFlow(farnebackFlowWithoutReuse(other[0], other[1]), flow, "refilled buffer");
}
typedef std::function<void(const Ptr<FarnebackOpticalFlow>&)> Tweak;
const std::pair<std::string, Tweak> tweaks[] = {
std::make_pair("polyN", Tweak([](const Ptr<FarnebackOpticalFlow>& f) {
f->setPolyN(7); })),
std::make_pair("polySigma", Tweak([](const Ptr<FarnebackOpticalFlow>& f) {
f->setPolySigma(1.3); })),
std::make_pair("pyrScale", Tweak([](const Ptr<FarnebackOpticalFlow>& f) {
f->setPyrScale(0.45); })), // same pyramid depth as 0.5 at this size
};
for( size_t t = 0; t < sizeof(tweaks) / sizeof(tweaks[0]); t++ )
{
Ptr<FarnebackOpticalFlow> algo = makeFarneback(true), fresh = makeFarneback(false);
Mat flow, expected;
algo->calc(frames[0], frames[1], flow);
tweaks[t].second(algo);
tweaks[t].second(fresh);
algo->calc(frames[1], frames[2], flow);
fresh->calc(frames[1], frames[2], expected);
expectSameFlow(expected, flow, tweaks[t].first);
}
// Frame size change.
{
const std::vector<Mat> small = makeFarnebackSequence(Size(96, 72), 2);
Ptr<FarnebackOpticalFlow> algo = makeFarneback(true);
Mat flow;
algo->calc(frames[0], frames[1], flow);
algo->calc(small[0], small[1], flow);
expectSameFlow(farnebackFlowWithoutReuse(small[0], small[1]), flow, "size change");
}
}
// Non-continuous ROIs, and one frame arriving strided as next and then continuous as prev.
TEST(DenseOpticalFlow_Farneback, ReuseWithRoiInput)
{
const Size size(96, 72);
const Rect roi(6, 4, size.width, size.height);
const std::vector<Mat> frames = makeFarnebackSequence(Size(size.width + 19, size.height + 13), 4);
Ptr<FarnebackOpticalFlow> algo = makeFarneback(true);
for( size_t i = 0; i + 1 < frames.size(); i++ )
{
const Mat prev = frames[i](roi), next = frames[i + 1](roi);
ASSERT_FALSE(prev.isContinuous());
Mat flow;
algo->calc(i == 1 ? prev.clone() : prev, next, flow);
expectSameFlow(farnebackFlowWithoutReuse(prev, next), flow, format("pair %d", (int)i));
}
}
TEST(DenseOpticalFlow_Farneback, ReuseReleaseKeepsResult)
{
const std::vector<Mat> frames = makeFarnebackSequence(Size(96, 72), 5);
Ptr<FarnebackOpticalFlow> algo = makeFarneback(true);
Mat flow;
algo->calc(frames[0], frames[1], flow);
algo->collectGarbage();
algo->calc(frames[1], frames[2], flow);
expectSameFlow(farnebackFlowWithoutReuse(frames[1], frames[2]), flow, "after collectGarbage");
algo->setFlags(0);
algo->calc(frames[2], frames[3], flow);
expectSameFlow(farnebackFlowWithoutReuse(frames[2], frames[3]), flow, "reuse off");
algo->setFlags(OPTFLOW_FARNEBACK_REUSE_EXPANSION);
algo->calc(frames[3], frames[4], flow);
expectSameFlow(farnebackFlowWithoutReuse(frames[3], frames[4]), flow, "reuse back on");
}
}} // namespace