vendor: OpenCV 5.0.0 snapshot at 755e50675d97db9b7d449d8bd6b09888646f6c6e
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
// 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.
|
||||
//
|
||||
// Author: andrewgodbehere
|
||||
#include "test_precomp.hpp"
|
||||
|
||||
namespace opencv_test { namespace {
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
typedef testing::TestWithParam<std::tuple<perf::MatDepth,int>> bgsubgmg_allTypes;
|
||||
TEST_P(bgsubgmg_allTypes, accuracy)
|
||||
{
|
||||
const int depth = get<0>(GetParam());
|
||||
const int ncn = get<1>(GetParam());
|
||||
const int mtype = CV_MAKETYPE(depth, ncn);
|
||||
const int width = 64;
|
||||
const int height = 64;
|
||||
RNG& rng = TS::ptr()->get_rng();
|
||||
|
||||
Ptr<BackgroundSubtractorGMG> fgbg = createBackgroundSubtractorGMG();
|
||||
ASSERT_TRUE(fgbg != nullptr) << "Failed to call createBackgroundSubtractorGMG()";
|
||||
|
||||
/**
|
||||
* Set a few parameters
|
||||
*/
|
||||
fgbg->setSmoothingRadius(7);
|
||||
fgbg->setDecisionThreshold(0.7);
|
||||
fgbg->setNumFrames(120);
|
||||
|
||||
/**
|
||||
* 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 (depth == CV_8U)
|
||||
{
|
||||
uchar half = UCHAR_MAX/2;
|
||||
maxd = (unsigned char)rng.uniform(half+32, UCHAR_MAX);
|
||||
mind = (unsigned char)rng.uniform(0, half-32);
|
||||
}
|
||||
else if (depth == CV_8S)
|
||||
{
|
||||
maxd = (char)rng.uniform(32, CHAR_MAX);
|
||||
mind = (char)rng.uniform(CHAR_MIN, -32);
|
||||
}
|
||||
else if (depth == CV_16U)
|
||||
{
|
||||
ushort half = USHRT_MAX/2;
|
||||
maxd = (unsigned int)rng.uniform(half+32, USHRT_MAX);
|
||||
mind = (unsigned int)rng.uniform(0, half-32);
|
||||
}
|
||||
else if (depth == CV_16S)
|
||||
{
|
||||
maxd = rng.uniform(32, SHRT_MAX);
|
||||
mind = rng.uniform(SHRT_MIN, -32);
|
||||
}
|
||||
else if (depth == CV_32S)
|
||||
{
|
||||
maxd = rng.uniform(32, INT_MAX);
|
||||
mind = rng.uniform(INT_MIN, -32);
|
||||
}
|
||||
else
|
||||
{
|
||||
ASSERT_TRUE( (depth == CV_32F)||(depth == CV_64F) ) << "Unsupported depth";
|
||||
const double harf = 0.5;
|
||||
const double bias = 0.125; // = 32/256 (Like CV_8U)
|
||||
maxd = rng.uniform(harf + bias, 1.0);
|
||||
mind = rng.uniform(0.0, harf - bias );
|
||||
}
|
||||
|
||||
fgbg->setMinVal(mind);
|
||||
fgbg->setMaxVal(maxd);
|
||||
|
||||
Mat simImage(height, width, mtype);
|
||||
Mat fgmask;
|
||||
|
||||
const Mat fullbg(height, width, CV_8UC1, cv::Scalar(0)); // all background.
|
||||
|
||||
const int numLearningFrames = 120;
|
||||
for (int i = 0; i < numLearningFrames; ++i)
|
||||
{
|
||||
/**
|
||||
* Genrate simulated "image" for any type. Values always confined to upper half of range.
|
||||
*/
|
||||
rng.fill(simImage, RNG::UNIFORM, (mind + maxd)*0.5, maxd);
|
||||
|
||||
/**
|
||||
* Feed simulated images into background subtractor
|
||||
*/
|
||||
fgbg->apply(simImage,fgmask);
|
||||
|
||||
EXPECT_EQ(cv::norm(fgmask, fullbg, NORM_INF), 0) << "foreground mask should be entirely background during training";
|
||||
}
|
||||
//! generate last image, distinct from training images
|
||||
rng.fill(simImage, RNG::UNIFORM, mind, maxd);
|
||||
fgbg->apply(simImage,fgmask);
|
||||
|
||||
const Mat fullfg(height, width, CV_8UC1, cv::Scalar(255)); // all foreground.
|
||||
EXPECT_EQ(cv::norm(fgmask, fullfg, NORM_INF), 0) << "foreground mask should be entirely foreground finally";
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(/**/,
|
||||
bgsubgmg_allTypes,
|
||||
testing::Combine(
|
||||
testing::Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F),
|
||||
testing::Values(1,2,3,4)));
|
||||
|
||||
}} // namespace
|
||||
@@ -0,0 +1,140 @@
|
||||
// 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 "test_precomp.hpp"
|
||||
#include <set>
|
||||
|
||||
namespace opencv_test { namespace {
|
||||
|
||||
static string getDataDir() { return TS::ptr()->get_data_path(); }
|
||||
|
||||
static string getLenaImagePath() { return getDataDir() + "shared/lena.png"; }
|
||||
|
||||
// Simple synthetic illumination invariance test
|
||||
TEST(BackgroundSubtractor_LSBP, IlluminationInvariance)
|
||||
{
|
||||
RNG rng;
|
||||
Mat input(100, 100, CV_32FC3);
|
||||
|
||||
rng.fill(input, RNG::UNIFORM, 0.0f, 0.1f);
|
||||
|
||||
Mat lsv1, lsv2;
|
||||
cv::bgsegm::BackgroundSubtractorLSBPDesc::calcLocalSVDValues(lsv1, input);
|
||||
input *= 10;
|
||||
cv::bgsegm::BackgroundSubtractorLSBPDesc::calcLocalSVDValues(lsv2, input);
|
||||
|
||||
ASSERT_LE(cv::norm(lsv1, lsv2), 0.04f);
|
||||
}
|
||||
|
||||
TEST(BackgroundSubtractor_LSBP, Correctness)
|
||||
{
|
||||
Mat input(3, 3, CV_32FC3);
|
||||
|
||||
float n = 0;
|
||||
for (int i = 0; i < 3; ++i)
|
||||
for (int j = 0; j < 3; ++j) {
|
||||
input.at<Point3f>(i, j) = Point3f(n, n, n);
|
||||
++n;
|
||||
}
|
||||
|
||||
Mat lsv;
|
||||
bgsegm::BackgroundSubtractorLSBPDesc::calcLocalSVDValues(lsv, input);
|
||||
|
||||
EXPECT_LE(std::abs(lsv.at<float>(1, 1) - 0.0903614f), 0.001f);
|
||||
|
||||
input = 1;
|
||||
bgsegm::BackgroundSubtractorLSBPDesc::calcLocalSVDValues(lsv, input);
|
||||
|
||||
EXPECT_LE(std::abs(lsv.at<float>(1, 1) - 0.0f), 0.001f);
|
||||
}
|
||||
|
||||
TEST(BackgroundSubtractor_LSBP, Discrimination)
|
||||
{
|
||||
Point2i LSBPSamplePoints[32];
|
||||
for (int i = 0; i < 32; ++i) {
|
||||
const double phi = i * CV_2PI / 32.0;
|
||||
LSBPSamplePoints[i] = Point2i(int(4 * std::cos(phi)), int(4 * std::sin(phi)));
|
||||
}
|
||||
|
||||
Mat lena = imread(getLenaImagePath());
|
||||
Mat lsv;
|
||||
|
||||
lena.convertTo(lena, CV_32FC3);
|
||||
|
||||
bgsegm::BackgroundSubtractorLSBPDesc::calcLocalSVDValues(lsv, lena);
|
||||
|
||||
Scalar mean, var;
|
||||
meanStdDev(lsv, mean, var);
|
||||
|
||||
EXPECT_GE(mean[0], 0.02);
|
||||
EXPECT_LE(mean[0], 0.04);
|
||||
EXPECT_GE(var[0], 0.03);
|
||||
|
||||
Mat desc;
|
||||
bgsegm::BackgroundSubtractorLSBPDesc::computeFromLocalSVDValues(desc, lsv, LSBPSamplePoints);
|
||||
Size sz = desc.size();
|
||||
std::set<int> distinctive_elements;
|
||||
|
||||
for (int i = 0; i < sz.height; ++i)
|
||||
for (int j = 0; j < sz.width; ++j)
|
||||
distinctive_elements.insert(desc.at<int>(i, j));
|
||||
|
||||
EXPECT_GE(distinctive_elements.size(), 35000U);
|
||||
}
|
||||
|
||||
static double scoreBitwiseReduce(const Mat& mask, const Mat& gtMask, uchar v1, uchar v2) {
|
||||
Mat result;
|
||||
cv::bitwise_and(mask == v1, gtMask == v2, result);
|
||||
return cv::countNonZero(result);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static double evaluateBGSAlgorithm(Ptr<T> bgs) {
|
||||
Mat background = imread(getDataDir() + "shared/fruits.png");
|
||||
Mat object = imread(getDataDir() + "shared/baboon.png");
|
||||
cv::resize(object, object, Size(100, 100), 0, 0, INTER_LINEAR_EXACT);
|
||||
Ptr<bgsegm::SyntheticSequenceGenerator> generator = bgsegm::createSyntheticSequenceGenerator(background, object);
|
||||
|
||||
double f1_mean = 0;
|
||||
unsigned total = 0;
|
||||
|
||||
for (int frameNum = 1; frameNum <= 400; ++frameNum) {
|
||||
Mat frame, gtMask;
|
||||
generator->getNextFrame(frame, gtMask);
|
||||
|
||||
Mat mask;
|
||||
bgs->apply(frame, mask);
|
||||
|
||||
Size sz = frame.size();
|
||||
EXPECT_EQ(sz, gtMask.size());
|
||||
EXPECT_EQ(gtMask.size(), mask.size());
|
||||
EXPECT_EQ(mask.type(), gtMask.type());
|
||||
EXPECT_EQ(mask.type(), CV_8U);
|
||||
|
||||
// We will give the algorithm some time for the proper background model inference.
|
||||
// Almost all background subtraction algorithms have a problem with cold start and require some time for background model initialization.
|
||||
// So we will not count first part of the frames in the score.
|
||||
if (frameNum > 300) {
|
||||
const double tp = scoreBitwiseReduce(mask, gtMask, 255, 255);
|
||||
const double fp = scoreBitwiseReduce(mask, gtMask, 255, 0);
|
||||
const double fn = scoreBitwiseReduce(mask, gtMask, 0, 255);
|
||||
|
||||
if (tp + fn + fp > 0) {
|
||||
const double f1_score = 2.0 * tp / (2.0 * tp + fn + fp);
|
||||
f1_mean += f1_score;
|
||||
++total;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
f1_mean /= total;
|
||||
return f1_mean;
|
||||
}
|
||||
|
||||
TEST(BackgroundSubtractor_LSBP, Accuracy)
|
||||
{
|
||||
EXPECT_GE(evaluateBGSAlgorithm(bgsegm::createBackgroundSubtractorGSOC()), 0.9);
|
||||
EXPECT_GE(evaluateBGSAlgorithm(bgsegm::createBackgroundSubtractorLSBP()), 0.25);
|
||||
}
|
||||
|
||||
}} // namespace
|
||||
@@ -0,0 +1,6 @@
|
||||
// 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 "test_precomp.hpp"
|
||||
|
||||
CV_TEST_MAIN("cv")
|
||||
@@ -0,0 +1,16 @@
|
||||
// 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.
|
||||
#ifndef __OPENCV_TEST_PRECOMP_HPP__
|
||||
#define __OPENCV_TEST_PRECOMP_HPP__
|
||||
|
||||
#include "opencv2/ts.hpp"
|
||||
#include "opencv2/imgproc.hpp"
|
||||
#include "opencv2/imgcodecs.hpp"
|
||||
#include "opencv2/bgsegm.hpp"
|
||||
|
||||
namespace opencv_test {
|
||||
using namespace cv::bgsegm;
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user