2012-06-29 00:24:48 +08:00
|
|
|
/*
|
|
|
|
* BackgroundSubtractorGBH_test.cpp
|
|
|
|
*
|
|
|
|
* Created on: Jun 14, 2012
|
|
|
|
* Author: andrewgodbehere
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "test_precomp.hpp"
|
|
|
|
|
|
|
|
using namespace cv;
|
|
|
|
|
|
|
|
class CV_BackgroundSubtractorTest : public cvtest::BaseTest
|
|
|
|
{
|
|
|
|
public:
|
2012-07-01 04:47:09 +08:00
|
|
|
CV_BackgroundSubtractorTest();
|
2012-06-29 00:24:48 +08:00
|
|
|
protected:
|
2012-07-01 04:47:09 +08:00
|
|
|
void run(int);
|
2012-06-29 00:24:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
CV_BackgroundSubtractorTest::CV_BackgroundSubtractorTest()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This test checks the following:
|
|
|
|
* (i) BackgroundSubtractorGMG can operate with matrices of various types and sizes
|
|
|
|
* (ii) Training mode returns empty fgmask
|
|
|
|
* (iii) End of training mode, and anomalous frame yields every pixel detected as FG
|
|
|
|
*/
|
|
|
|
void CV_BackgroundSubtractorTest::run(int)
|
|
|
|
{
|
2012-07-01 04:47:09 +08:00
|
|
|
int code = cvtest::TS::OK;
|
|
|
|
RNG& rng = ts->get_rng();
|
|
|
|
int type = ((unsigned int)rng)%7; //!< pick a random type, 0 - 6, defined in types_c.h
|
|
|
|
int channels = 1 + ((unsigned int)rng)%4; //!< random number of channels from 1 to 4.
|
|
|
|
int channelsAndType = CV_MAKETYPE(type,channels);
|
|
|
|
int width = 2 + ((unsigned int)rng)%98; //!< Mat will be 2 to 100 in width and height
|
|
|
|
int height = 2 + ((unsigned int)rng)%98;
|
|
|
|
|
2013-03-20 23:51:49 +08:00
|
|
|
Ptr<BackgroundSubtractorGMG> fgbg = createBackgroundSubtractorGMG();
|
2012-07-01 04:47:09 +08:00
|
|
|
Mat fgmask;
|
|
|
|
|
2012-12-16 02:09:21 +08:00
|
|
|
if (fgbg.empty())
|
2013-04-11 21:38:33 +08:00
|
|
|
CV_Error(Error::StsError,"Failed to create Algorithm\n");
|
2012-07-01 04:47:09 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a few parameters
|
|
|
|
*/
|
2013-03-20 23:51:49 +08:00
|
|
|
fgbg->setSmoothingRadius(7);
|
|
|
|
fgbg->setDecisionThreshold(0.7);
|
|
|
|
fgbg->setNumFrames(120);
|
2012-07-01 04:47:09 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate bounds for the values in the matrix for each type
|
|
|
|
*/
|
|
|
|
double maxd = 0, mind = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Max value for simulated images picked randomly in upper half of type range
|
|
|
|
* Min value for simulated images picked randomly in lower half of type range
|
|
|
|
*/
|
|
|
|
if (type == CV_8U)
|
|
|
|
{
|
|
|
|
uchar half = UCHAR_MAX/2;
|
2013-03-20 23:51:49 +08:00
|
|
|
maxd = (unsigned char)rng.uniform(half+32, UCHAR_MAX);
|
|
|
|
mind = (unsigned char)rng.uniform(0, half-32);
|
2012-07-01 04:47:09 +08:00
|
|
|
}
|
|
|
|
else if (type == CV_8S)
|
|
|
|
{
|
2013-03-20 23:51:49 +08:00
|
|
|
maxd = (char)rng.uniform(32, CHAR_MAX);
|
|
|
|
mind = (char)rng.uniform(CHAR_MIN, -32);
|
2012-07-01 04:47:09 +08:00
|
|
|
}
|
|
|
|
else if (type == CV_16U)
|
|
|
|
{
|
|
|
|
ushort half = USHRT_MAX/2;
|
2013-03-20 23:51:49 +08:00
|
|
|
maxd = (unsigned int)rng.uniform(half+32, USHRT_MAX);
|
|
|
|
mind = (unsigned int)rng.uniform(0, half-32);
|
2012-07-01 04:47:09 +08:00
|
|
|
}
|
|
|
|
else if (type == CV_16S)
|
|
|
|
{
|
2013-03-20 23:51:49 +08:00
|
|
|
maxd = rng.uniform(32, SHRT_MAX);
|
|
|
|
mind = rng.uniform(SHRT_MIN, -32);
|
2012-07-01 04:47:09 +08:00
|
|
|
}
|
|
|
|
else if (type == CV_32S)
|
|
|
|
{
|
2013-03-20 23:51:49 +08:00
|
|
|
maxd = rng.uniform(32, INT_MAX);
|
|
|
|
mind = rng.uniform(INT_MIN, -32);
|
2012-07-01 04:47:09 +08:00
|
|
|
}
|
|
|
|
else if (type == CV_32F)
|
|
|
|
{
|
2013-03-20 23:51:49 +08:00
|
|
|
maxd = rng.uniform(32.0f, FLT_MAX);
|
|
|
|
mind = rng.uniform(-FLT_MAX, -32.0f);
|
2012-07-01 04:47:09 +08:00
|
|
|
}
|
|
|
|
else if (type == CV_64F)
|
|
|
|
{
|
|
|
|
maxd = rng.uniform(32.0, DBL_MAX);
|
|
|
|
mind = rng.uniform(-DBL_MAX, -32.0);
|
|
|
|
}
|
|
|
|
|
2013-03-20 23:51:49 +08:00
|
|
|
fgbg->setMinVal(mind);
|
|
|
|
fgbg->setMaxVal(maxd);
|
|
|
|
|
2012-07-01 04:47:09 +08:00
|
|
|
Mat simImage = Mat::zeros(height, width, channelsAndType);
|
2013-03-20 23:51:49 +08:00
|
|
|
int numLearningFrames = 120;
|
|
|
|
for (int i = 0; i < numLearningFrames; ++i)
|
2012-07-01 04:47:09 +08:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Genrate simulated "image" for any type. Values always confined to upper half of range.
|
|
|
|
*/
|
2013-03-20 23:51:49 +08:00
|
|
|
rng.fill(simImage, RNG::UNIFORM, (mind + maxd)*0.5, maxd);
|
2012-07-01 04:47:09 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Feed simulated images into background subtractor
|
|
|
|
*/
|
2013-03-20 23:51:49 +08:00
|
|
|
fgbg->apply(simImage,fgmask);
|
2012-07-01 04:47:09 +08:00
|
|
|
Mat fullbg = Mat::zeros(simImage.rows, simImage.cols, CV_8U);
|
|
|
|
|
|
|
|
//! fgmask should be entirely background during training
|
|
|
|
code = cvtest::cmpEps2( ts, fgmask, fullbg, 0, false, "The training foreground mask" );
|
|
|
|
if (code < 0)
|
|
|
|
ts->set_failed_test_info( code );
|
|
|
|
}
|
|
|
|
//! generate last image, distinct from training images
|
2013-03-20 23:51:49 +08:00
|
|
|
rng.fill(simImage, RNG::UNIFORM, mind, maxd);
|
2012-07-01 04:47:09 +08:00
|
|
|
|
2013-03-20 23:51:49 +08:00
|
|
|
fgbg->apply(simImage,fgmask);
|
2012-07-01 04:47:09 +08:00
|
|
|
//! now fgmask should be entirely foreground
|
|
|
|
Mat fullfg = 255*Mat::ones(simImage.rows, simImage.cols, CV_8U);
|
|
|
|
code = cvtest::cmpEps2( ts, fgmask, fullfg, 255, false, "The final foreground mask" );
|
|
|
|
if (code < 0)
|
|
|
|
{
|
|
|
|
ts->set_failed_test_info( code );
|
|
|
|
}
|
2012-06-29 00:24:48 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(VIDEO_BGSUBGMG, accuracy) { CV_BackgroundSubtractorTest test; test.safe_run(); }
|