mirror of
https://github.com/opencv/opencv.git
synced 2026-11-26 03:18:11 -06:00
* video: make clear() reset the MOG2 and KNN background models cv::Algorithm::clear() is documented to clear the algorithm state, but BackgroundSubtractorMOG2 and BackgroundSubtractorKNN inherit the empty base implementation. After clear(), apply() keeps updating the old model and reports foreground against the previous scene. Override clear() in both implementations to drop the model, so the next apply() initializes a new one as it does on a newly created object. * video: clear MOG2 and KNN models without going through initialize() clear() called initialize(Size(), 0). With OpenCL active this builds the apply and getBackgroundImage kernels for frame type 0 and runs create() and setTo() on zero-size UMats; the setTo() call dereferences an empty vector in ocl::checkOptimalVectorWidth() and crashes. Release the model buffers (Mat and UMat) instead and reset the frame size, frame type, frame counter and KNN's update counters to their constructor values. The algorithm parameters are kept. The next apply() initializes the model for the actual frame, as on a new object. Add UMat variants of the clear() regression tests so the OpenCL path is covered when OpenCL is available. * video: do not assign theRNG() in the clear() tests The clear_resets_model tests reseeded theRNG() before each of the two apply() calls they compare. That is not needed: KNN takes its update points from theRNG() only after a frame has been processed, so the first apply() on an empty model gives the same mask and background image whatever the RNG state is. Leave the global RNG as the test system set it.
167 lines
4.9 KiB
C++
167 lines
4.9 KiB
C++
// 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 "opencv2/video/background_segm.hpp"
|
|
#include "opencv2/core/ocl.hpp"
|
|
|
|
namespace opencv_test { namespace {
|
|
|
|
using namespace cv;
|
|
|
|
///////////////////////// MOG2 //////////////////////////////
|
|
TEST(BackgroundSubtractorMOG2, KnownForegroundMaskShadowsTrue)
|
|
{
|
|
Ptr<BackgroundSubtractorMOG2> mog2 = createBackgroundSubtractorMOG2(500, 16, true);
|
|
|
|
//Black Frame
|
|
Mat input = Mat::zeros(480,640 , CV_8UC3);
|
|
|
|
//White Rectangle
|
|
Mat knownFG = Mat::zeros(input.size(), CV_8U);
|
|
|
|
rectangle(knownFG, Rect(3,3,5,5), Scalar(255,255,255), -1);
|
|
|
|
Mat output;
|
|
mog2->apply(input, knownFG, output);
|
|
|
|
for(int y = 3; y < 8; y++)
|
|
{
|
|
for (int x = 3; x < 8; x++){
|
|
EXPECT_EQ(255,output.at<uchar>(y,x)) << "Expected foreground at (" << x << "," << y << ")";
|
|
}
|
|
}
|
|
}
|
|
|
|
TEST(BackgroundSubtractorMOG2, KnownForegroundMaskShadowsFalse)
|
|
{
|
|
Ptr<BackgroundSubtractorMOG2> mog2 = createBackgroundSubtractorMOG2(500, 16, false);
|
|
|
|
//Black Frame
|
|
Mat input = Mat::zeros(480,640 , CV_8UC3);
|
|
|
|
//White Rectangle
|
|
Mat knownFG = Mat::zeros(input.size(), CV_8U);
|
|
|
|
rectangle(knownFG, Rect(3,3,5,5), Scalar(255,255,255), FILLED);
|
|
|
|
Mat output;
|
|
mog2->apply(input, knownFG, output);
|
|
|
|
for(int y = 3; y < 8; y++)
|
|
{
|
|
for (int x = 3; x < 8; x++){
|
|
EXPECT_EQ(255,output.at<uchar>(y,x)) << "Expected foreground at (" << x << "," << y << ")";
|
|
}
|
|
}
|
|
}
|
|
|
|
///////////////////////// KNN //////////////////////////////
|
|
|
|
TEST(BackgroundSubtractorKNN, KnownForegroundMaskShadowsTrue)
|
|
{
|
|
Ptr<BackgroundSubtractorKNN> knn = createBackgroundSubtractorKNN(500, 400.0, true);
|
|
|
|
//Black Frame
|
|
Mat input = Mat::zeros(480,640 , CV_8UC3);
|
|
|
|
//White Rectangle
|
|
Mat knownFG = Mat::zeros(input.size(), CV_8U);
|
|
|
|
rectangle(knownFG, Rect(3,3,5,5), Scalar(255,255,255), FILLED);
|
|
|
|
Mat output;
|
|
knn->apply(input, knownFG, output);
|
|
|
|
for(int y = 3; y < 8; y++)
|
|
{
|
|
for (int x = 3; x < 8; x++){
|
|
EXPECT_EQ(255,output.at<uchar>(y,x)) << "Expected foreground at (" << x << "," << y << ")";
|
|
}
|
|
}
|
|
}
|
|
|
|
TEST(BackgroundSubtractorKNN, KnownForegroundMaskShadowsFalse)
|
|
{
|
|
Ptr<BackgroundSubtractorKNN> knn = createBackgroundSubtractorKNN(500, 400.0, false);
|
|
|
|
//Black Frame
|
|
Mat input = Mat::zeros(480,640 , CV_8UC3);
|
|
|
|
//White Rectangle
|
|
Mat knownFG = Mat::zeros(input.size(), CV_8U);
|
|
|
|
rectangle(knownFG, Rect(3,3,5,5), Scalar(255,255,255), FILLED);
|
|
|
|
Mat output;
|
|
knn->apply(input, knownFG, output);
|
|
|
|
for(int y = 3; y < 8; y++)
|
|
{
|
|
for (int x = 3; x < 8; x++){
|
|
EXPECT_EQ(255,output.at<uchar>(y,x)) << "Expected foreground at (" << x << "," << y << ")";
|
|
}
|
|
}
|
|
}
|
|
|
|
///////////////////////// clear() //////////////////////////////
|
|
|
|
// T is Mat for the CPU path and UMat for the OpenCL path
|
|
template <typename T>
|
|
static void checkClearMatchesNewObject(const Ptr<BackgroundSubtractor>& used, const Ptr<BackgroundSubtractor>& fresh)
|
|
{
|
|
Mat sceneA_(48, 64, CV_8UC3, Scalar::all(40));
|
|
rectangle(sceneA_, Rect(8, 8, 16, 16), Scalar::all(200), FILLED);
|
|
Mat sceneB_(48, 64, CV_8UC3, Scalar::all(160));
|
|
rectangle(sceneB_, Rect(32, 16, 16, 16), Scalar::all(20), FILLED);
|
|
T sceneA, sceneB;
|
|
sceneA_.copyTo(sceneA);
|
|
sceneB_.copyTo(sceneB);
|
|
|
|
T mask;
|
|
for (int i = 0; i < 5; i++)
|
|
used->apply(sceneA, mask);
|
|
used->clear();
|
|
|
|
// One frame is given to each object: KNN takes its update points from theRNG() only after
|
|
// a frame has been processed, so the first apply() on an empty model does not depend on it.
|
|
T maskUsed, maskFresh, bgUsed, bgFresh;
|
|
used->apply(sceneB, maskUsed);
|
|
fresh->apply(sceneB, maskFresh);
|
|
used->getBackgroundImage(bgUsed);
|
|
fresh->getBackgroundImage(bgFresh);
|
|
|
|
EXPECT_EQ(0, cvtest::norm(maskUsed, maskFresh, NORM_INF));
|
|
EXPECT_EQ(0, cvtest::norm(bgUsed, bgFresh, NORM_INF));
|
|
}
|
|
|
|
TEST(BackgroundSubtractorMOG2, clear_resets_model)
|
|
{
|
|
checkClearMatchesNewObject<Mat>(createBackgroundSubtractorMOG2(), createBackgroundSubtractorMOG2());
|
|
}
|
|
|
|
TEST(BackgroundSubtractorKNN, clear_resets_model)
|
|
{
|
|
checkClearMatchesNewObject<Mat>(createBackgroundSubtractorKNN(), createBackgroundSubtractorKNN());
|
|
}
|
|
|
|
#ifdef HAVE_OPENCL
|
|
TEST(BackgroundSubtractorMOG2, clear_resets_model_ocl)
|
|
{
|
|
if (!cv::ocl::useOpenCL())
|
|
throw SkipTestException("OpenCL is not available");
|
|
checkClearMatchesNewObject<UMat>(createBackgroundSubtractorMOG2(), createBackgroundSubtractorMOG2());
|
|
}
|
|
|
|
TEST(BackgroundSubtractorKNN, clear_resets_model_ocl)
|
|
{
|
|
if (!cv::ocl::useOpenCL())
|
|
throw SkipTestException("OpenCL is not available");
|
|
checkClearMatchesNewObject<UMat>(createBackgroundSubtractorKNN(), createBackgroundSubtractorKNN());
|
|
}
|
|
#endif
|
|
|
|
}} // namespace
|
|
/* End of file. */
|