Files
opencv-MIRROR/modules/video/test/test_OF_accuracy.cpp
Rahul Kumar 5f335351c9 Merge pull request #30859 from pctablet505:fix-video-dis-collectgarbage-crash
video: fix DISOpticalFlow::calc() crash after collectGarbage() 🤖🤖🤖 - #30859

Fixes #30858

`DISOpticalFlow::calc()` crashed after `collectGarbage()`, which cleared the vector of `VariationalRefinement` objects that only the constructor creates.

### Change

- Keep the objects and call `collectGarbage()` on each of them instead.
- Also release the buffers that `collectGarbage()` missed: `I0x_buf`, `I0y_buf` and their `_aux` copies, the initial-flow pyramids, and their OpenCL counterparts.

### Tests

`DenseOpticalFlow_DIS.calc_after_collectGarbage` calls `collectGarbage()` between `calc()` calls, after a call with an initial flow, before the first call and after parameter changes, on `Mat` and `UMat`. Each result must equal that of a fresh object.

| Check (Linux x86-64, GCC 15.2, with opencv_extra) | 4.x | This PR |
|---|---|---|
| new test | segfault | passes, 3 seeds |
| `calc()` after `collectGarbage()` under AddressSanitizer | heap-use-after-free in `prepareBuffers()` | no report |
| `opencv_test_video`, 178 existing tests | 8 failures (GOTURN model, data) | same 8 |

The `UMat` part also passed with OpenCL on an NVIDIA RTX PRO 6000. 5.x has the same code; the test sits before `regression_20185_patch_larger_than_border` so that the regular 4.x to 5.x merge is clean. Not run on Windows.

Prepared with an AI assistant; every result was reproduced by running the code.

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
2026-11-17 16:46:45 +03:00

334 lines
12 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, StaleInitialFlow)
{
Mat frame1(240, 320, CV_8UC1), frame2(240, 320, CV_8UC1);
theRNG().fill(frame1, RNG::UNIFORM, 0, 256);
GaussianBlur(frame1, frame1, Size(5, 5), 1.0);
Mat M = (Mat_<double>(2, 3) << 1, 0, 2.0, 0, 1, -1.0);
warpAffine(frame1, frame2, M, frame1.size(), INTER_LINEAR, BORDER_REFLECT);
Ptr<DISOpticalFlow> dis = DISOpticalFlow::create(DISOpticalFlow::PRESET_MEDIUM);
Mat initial(frame1.size(), CV_32FC2, Scalar(5.f, -3.f));
dis->calc(frame1, frame2, initial);
Mat flow, reference;
dis->calc(frame1, frame2, flow);
DISOpticalFlow::create(DISOpticalFlow::PRESET_MEDIUM)->calc(frame1, frame2, reference);
EXPECT_EQ(0.0, cvtest::norm(flow, reference, NORM_INF));
Mat small1 = frame1(Rect(0, 0, 160, 120)).clone();
Mat small2 = frame2(Rect(0, 0, 160, 120)).clone();
Ptr<DISOpticalFlow> dis_grow = DISOpticalFlow::create(DISOpticalFlow::PRESET_MEDIUM);
Mat small_initial(small1.size(), CV_32FC2, Scalar(5.f, -3.f));
dis_grow->calc(small1, small2, small_initial);
Mat grown_flow;
dis_grow->calc(frame1, frame2, grown_flow);
EXPECT_EQ(0.0, cvtest::norm(grown_flow, reference, NORM_INF));
}
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, calc_after_collectGarbage)
{
Mat frame1(240, 320, CV_8UC1), frame2(240, 320, CV_8UC1);
theRNG().fill(frame1, RNG::UNIFORM, 0, 256);
GaussianBlur(frame1, frame1, Size(5, 5), 1.0);
Mat warp = (Mat_<double>(2, 3) << 1, 0, 2.0, 0, 1, -1.0);
warpAffine(frame1, frame2, warp, frame1.size(), INTER_LINEAR, BORDER_REFLECT);
Mat reference;
DISOpticalFlow::create(DISOpticalFlow::PRESET_MEDIUM)->calc(frame1, frame2, reference);
// collectGarbage() between two calc() calls and after an initial-flow call
{
Ptr<DISOpticalFlow> dis = DISOpticalFlow::create(DISOpticalFlow::PRESET_MEDIUM);
Mat flow1, flow2;
dis->calc(frame1, frame2, flow1);
dis->collectGarbage();
dis->calc(frame1, frame2, flow2);
EXPECT_EQ(0, cvtest::norm(flow1, reference, NORM_INF));
EXPECT_EQ(0, cvtest::norm(flow2, reference, NORM_INF));
Mat initial(frame1.size(), CV_32FC2, Scalar(5.f, -3.f));
dis->calc(frame1, frame2, initial);
dis->collectGarbage();
Mat flowAfterInitial;
dis->calc(frame1, frame2, flowAfterInitial);
EXPECT_EQ(0, cvtest::norm(flowAfterInitial, reference, NORM_INF));
}
// collectGarbage() before the first calc() and after parameter changes
{
Ptr<DISOpticalFlow> dis = DISOpticalFlow::create(DISOpticalFlow::PRESET_MEDIUM);
dis->collectGarbage();
Mat flow1;
dis->calc(frame1, frame2, flow1);
EXPECT_EQ(0, cvtest::norm(flow1, reference, NORM_INF));
dis->setFinestScale(2);
dis->setVariationalRefinementIterations(0);
dis->collectGarbage();
Mat flowNoVarRef, refNoVarRef;
dis->calc(frame1, frame2, flowNoVarRef);
Ptr<DISOpticalFlow> refDis = DISOpticalFlow::create(DISOpticalFlow::PRESET_MEDIUM);
refDis->setFinestScale(2);
refDis->setVariationalRefinementIterations(0);
refDis->calc(frame1, frame2, refNoVarRef);
EXPECT_EQ(0, cvtest::norm(flowNoVarRef, refNoVarRef, NORM_INF));
}
// UMat / OpenCL path
{
UMat uFrame1, uFrame2, uFlow1, uFlow2, uRef;
frame1.copyTo(uFrame1);
frame2.copyTo(uFrame2);
Ptr<DISOpticalFlow> dis = DISOpticalFlow::create(DISOpticalFlow::PRESET_FAST);
dis->collectGarbage();
dis->calc(uFrame1, uFrame2, uFlow1);
dis->collectGarbage();
dis->calc(uFrame1, uFrame2, uFlow2);
DISOpticalFlow::create(DISOpticalFlow::PRESET_FAST)->calc(uFrame1, uFrame2, uRef);
ASSERT_EQ(uFrame1.size(), uFlow1.size());
ASSERT_EQ(uFrame1.size(), uFlow2.size());
EXPECT_EQ(0, cvtest::norm(uFlow1, uRef, NORM_INF));
EXPECT_EQ(0, cvtest::norm(uFlow2, uRef, NORM_INF));
}
}
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());
}
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));
}
}} // namespace