Skip to content

Commit 61b51e1

Browse files
committed
bgsegm: Update test to use EXPECT_/ASSERT_
1 parent 97c0421 commit 61b51e1

File tree

1 file changed

+24
-56
lines changed

1 file changed

+24
-56
lines changed

modules/bgsegm/test/test_backgroundsubtractor_gbh.cpp

Lines changed: 24 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,24 @@
77

88
namespace opencv_test { namespace {
99

10-
class CV_BackgroundSubtractorTest : public cvtest::BaseTest
11-
{
12-
public:
13-
CV_BackgroundSubtractorTest();
14-
void setMatType(int _mtype){ mtype = _mtype; }
15-
protected:
16-
int mtype;
17-
void run(int);
18-
};
19-
20-
CV_BackgroundSubtractorTest::CV_BackgroundSubtractorTest()
21-
{
22-
}
23-
2410
/**
2511
* This test checks the following:
2612
* (i) BackgroundSubtractorGMG can operate with matrices of various types and sizes
2713
* (ii) Training mode returns empty fgmask
2814
* (iii) End of training mode, and anomalous frame yields every pixel detected as FG
2915
*/
30-
void CV_BackgroundSubtractorTest::run(int)
16+
typedef testing::TestWithParam<std::tuple<perf::MatDepth,int>> bgsubgmg_allTypes;
17+
TEST_P(bgsubgmg_allTypes, accuracy)
3118
{
32-
int code = cvtest::TS::OK;
33-
RNG& rng = ts->get_rng();
34-
int depth = CV_MAT_DEPTH(mtype);
35-
int width = 64;
36-
int height = 64;
19+
const int depth = get<0>(GetParam());
20+
const int ncn = get<1>(GetParam());
21+
const int mtype = CV_MAKETYPE(depth, ncn);
22+
const int width = 64;
23+
const int height = 64;
24+
RNG& rng = TS::ptr()->get_rng();
3725

3826
Ptr<BackgroundSubtractorGMG> fgbg = createBackgroundSubtractorGMG();
39-
Mat fgmask;
40-
41-
if (!fgbg)
42-
CV_Error(Error::StsError,"Failed to create Algorithm\n");
27+
ASSERT_TRUE(fgbg != nullptr) << "Failed to call createBackgroundSubtractorGMG()";
4328

4429
/**
4530
* Set a few parameters
@@ -84,22 +69,24 @@ void CV_BackgroundSubtractorTest::run(int)
8469
maxd = rng.uniform(32, INT_MAX);
8570
mind = rng.uniform(INT_MIN, -32);
8671
}
87-
else if (depth == CV_32F)
88-
{
89-
maxd = rng.uniform(32.0f, FLT_MAX);
90-
mind = rng.uniform(-FLT_MAX, -32.0f);
91-
}
92-
else if (depth == CV_64F)
72+
else
9373
{
94-
maxd = rng.uniform(32.0, DBL_MAX);
95-
mind = rng.uniform(-DBL_MAX, -32.0);
74+
ASSERT_TRUE( (depth == CV_32F)||(depth == CV_64F) ) << "Unsupported depth";
75+
const double harf = 0.5;
76+
const double bias = 0.125; // = 32/256 (Like CV_8U)
77+
maxd = rng.uniform(harf + bias, 1.0);
78+
mind = rng.uniform(0.0, harf - bias );
9679
}
9780

9881
fgbg->setMinVal(mind);
9982
fgbg->setMaxVal(maxd);
10083

101-
Mat simImage = Mat::zeros(height, width, mtype);
102-
int numLearningFrames = 120;
84+
Mat simImage(height, width, mtype);
85+
Mat fgmask;
86+
87+
const Mat fullbg(height, width, CV_8UC1, cv::Scalar(0)); // all background.
88+
89+
const int numLearningFrames = 120;
10390
for (int i = 0; i < numLearningFrames; ++i)
10491
{
10592
/**
@@ -111,34 +98,15 @@ void CV_BackgroundSubtractorTest::run(int)
11198
* Feed simulated images into background subtractor
11299
*/
113100
fgbg->apply(simImage,fgmask);
114-
Mat fullbg = Mat::zeros(simImage.rows, simImage.cols, CV_8U);
115101

116-
//! fgmask should be entirely background during training
117-
code = cvtest::cmpEps2( ts, fgmask, fullbg, 0, false, "The training foreground mask" );
118-
if (code < 0)
119-
ts->set_failed_test_info( code );
102+
EXPECT_EQ(cv::norm(fgmask, fullbg, NORM_INF), 0) << "foreground mask should be entirely background during training";
120103
}
121104
//! generate last image, distinct from training images
122105
rng.fill(simImage, RNG::UNIFORM, mind, maxd);
123-
124106
fgbg->apply(simImage,fgmask);
125-
//! now fgmask should be entirely foreground
126-
Mat fullfg = 255*Mat::ones(simImage.rows, simImage.cols, CV_8U);
127-
code = cvtest::cmpEps2( ts, fgmask, fullfg, 255, false, "The final foreground mask" );
128-
if (code < 0)
129-
{
130-
ts->set_failed_test_info( code );
131-
}
132107

133-
}
134-
135-
typedef testing::TestWithParam<std::tuple<perf::MatDepth,int>> bgsubgmg_allTypes;
136-
TEST_P(bgsubgmg_allTypes, accuracy)
137-
{
138-
const int mtype = CV_MAKETYPE(get<0>(GetParam()), get<1>(GetParam()));
139-
CV_BackgroundSubtractorTest test;
140-
test.setMatType(mtype);
141-
test.safe_run();
108+
const Mat fullfg(height, width, CV_8UC1, cv::Scalar(255)); // all foreground.
109+
EXPECT_EQ(cv::norm(fgmask, fullfg, NORM_INF), 0) << "foreground mask should be entirely foreground finally";
142110
}
143111

144112
INSTANTIATE_TEST_CASE_P(/**/,

0 commit comments

Comments
 (0)