test_bgfg.cpp 6.74 KB
Newer Older
wester committed
1 2 3 4 5 6 7 8 9 10 11 12
/*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.
//
//
//                           License Agreement
//                For Open Source Computer Vision Library
//
wester committed
13 14
// Copyright (C) 2010-2013, Multicoreware, Inc., all rights reserved.
// Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved.
wester committed
15 16
// Third party copyrights are property of their respective owners.
//
wester committed
17 18 19
// @Authors
//    Jin Ma, jin@multicorewareinc.com
//
wester committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
// 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 the copyright holders 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"

wester committed
48
#ifdef HAVE_OPENCL
wester committed
49

wester committed
50 51
using namespace cv;
using namespace cv::ocl;
wester committed
52
using namespace cvtest;
wester committed
53 54
using namespace testing;
using namespace std;
wester committed
55

a  
Kai Westerkamp committed
56 57 58 59 60
#if defined(HAVE_XINE)         || \
    defined(HAVE_GSTREAMER)    || \
    defined(HAVE_QUICKTIME)    || \
    defined(HAVE_AVFOUNDATION) || \
    defined(HAVE_FFMPEG)       || \
wester committed
61
    defined(WIN32)
a  
Kai Westerkamp committed
62 63 64 65 66 67 68

#  define BUILD_WITH_VIDEO_INPUT_SUPPORT 1
#else
#  define BUILD_WITH_VIDEO_INPUT_SUPPORT 0
#endif

#if BUILD_WITH_VIDEO_INPUT_SUPPORT
wester committed
69

wester committed
70 71 72
//////////////////////////////////////////////////////
// MOG

wester committed
73
namespace
wester committed
74 75 76 77 78 79 80 81 82 83 84 85
{
    IMPLEMENT_PARAM_CLASS(UseGray, bool)
    IMPLEMENT_PARAM_CLASS(LearningRate, double)
}

PARAM_TEST_CASE(mog, UseGray, LearningRate, bool)
{
    bool useGray;
    double learningRate;
    bool useRoi;

    virtual void SetUp()
wester committed
86
    {
wester committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
        useGray = GET_PARAM(0);
        learningRate = GET_PARAM(1);
        useRoi = GET_PARAM(2);
    }
};

OCL_TEST_P(mog, Update)
{
    std::string inputFile = string(cvtest::TS::ptr()->get_data_path()) + "gpu/video/768x576.avi";
    cv::VideoCapture cap(inputFile);
    ASSERT_TRUE(cap.isOpened());

    cv::Mat frame;
    cap >> frame;
    ASSERT_FALSE(frame.empty());

    cv::ocl::MOG mog;
    cv::ocl::oclMat foreground = createMat_ocl(rng, frame.size(), CV_8UC1, useRoi);

    cv::BackgroundSubtractorMOG mog_gold;
    cv::Mat foreground_gold;

    for (int i = 0; i < 10; ++i)
    {
        cap >> frame;
        ASSERT_FALSE(frame.empty());

        if (useGray)
        {
            cv::Mat temp;
            cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
            cv::swap(temp, frame);
        }

        mog(loadMat_ocl(rng, frame, useRoi), foreground, (float)learningRate);

        mog_gold(frame, foreground_gold, learningRate);

        EXPECT_MAT_NEAR(foreground_gold, foreground, 0.0);
    }
}
INSTANTIATE_TEST_CASE_P(OCL_Video, mog, testing::Combine(
    testing::Values(UseGray(false), UseGray(true)),
    testing::Values(LearningRate(0.0), LearningRate(0.01)),
    Values(true, false)));

//////////////////////////////////////////////////////
// MOG2

namespace
{
wester committed
138 139 140
    IMPLEMENT_PARAM_CLASS(DetectShadow, bool)
}

wester committed
141
PARAM_TEST_CASE(mog2, UseGray, DetectShadow, bool)
wester committed
142 143 144 145 146 147
{
    bool useGray;
    bool detectShadow;
    bool useRoi;
    virtual void SetUp()
    {
wester committed
148 149 150
        useGray = GET_PARAM(0);
        detectShadow = GET_PARAM(1);
        useRoi = GET_PARAM(2);
wester committed
151 152 153
    }
};

wester committed
154
OCL_TEST_P(mog2, Update)
wester committed
155
{
wester committed
156
    std::string inputFile = string(cvtest::TS::ptr()->get_data_path()) + "gpu/video/768x576.avi";
wester committed
157 158 159 160 161 162 163
    cv::VideoCapture cap(inputFile);
    ASSERT_TRUE(cap.isOpened());

    cv::Mat frame;
    cap >> frame;
    ASSERT_FALSE(frame.empty());

wester committed
164 165 166
    cv::ocl::MOG2 mog2;
    mog2.bShadowDetection = detectShadow;
    cv::ocl::oclMat foreground = createMat_ocl(rng, frame.size(), CV_8UC1, useRoi);
wester committed
167

wester committed
168 169
    cv::BackgroundSubtractorMOG2 mog2_gold;
    mog2_gold.set("detectShadows", detectShadow);
wester committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183
    cv::Mat foreground_gold;

    for (int i = 0; i < 10; ++i)
    {
        cap >> frame;
        ASSERT_FALSE(frame.empty());

        if (useGray)
        {
            cv::Mat temp;
            cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
            cv::swap(temp, frame);
        }

wester committed
184
        mog2(loadMat_ocl(rng, frame, useRoi), foreground);
wester committed
185

wester committed
186
        mog2_gold(frame, foreground_gold);
wester committed
187 188

        if (detectShadow)
wester committed
189
            EXPECT_MAT_SIMILAR(foreground_gold, foreground, 15e-3)
wester committed
190
        else
wester committed
191
            EXPECT_MAT_NEAR(foreground_gold, foreground, 0)
wester committed
192 193 194
    }
}

wester committed
195
OCL_TEST_P(mog2, getBackgroundImage)
wester committed
196 197 198 199
{
    if (useGray)
        return;

wester committed
200
    std::string inputFile = string(cvtest::TS::ptr()->get_data_path()) + "gpu/video/768x576.avi";
wester committed
201 202 203 204 205
    cv::VideoCapture cap(inputFile);
    ASSERT_TRUE(cap.isOpened());

    cv::Mat frame;

wester committed
206 207 208
    cv::ocl::MOG2 mog2;
    mog2.bShadowDetection = detectShadow;
    cv::ocl::oclMat foreground;
wester committed
209

wester committed
210 211
    cv::BackgroundSubtractorMOG2 mog2_gold;
    mog2_gold.set("detectShadows", detectShadow);
wester committed
212 213 214 215 216 217 218
    cv::Mat foreground_gold;

    for (int i = 0; i < 10; ++i)
    {
        cap >> frame;
        ASSERT_FALSE(frame.empty());

wester committed
219
        mog2(loadMat_ocl(rng, frame, useRoi), foreground);
wester committed
220

wester committed
221
        mog2_gold(frame, foreground_gold);
wester committed
222 223
    }

wester committed
224 225
    cv::ocl::oclMat background = createMat_ocl(rng, frame.size(), frame.type(), useRoi);
    mog2.getBackgroundImage(background);
wester committed
226 227

    cv::Mat background_gold;
wester committed
228
    mog2_gold.getBackgroundImage(background_gold);
wester committed
229

wester committed
230
    EXPECT_MAT_NEAR(background_gold, background, 1.0);
wester committed
231 232
}

wester committed
233
INSTANTIATE_TEST_CASE_P(OCL_Video, mog2, testing::Combine(
wester committed
234 235
    testing::Values(UseGray(true), UseGray(false)),
    testing::Values(DetectShadow(true), DetectShadow(false)),
wester committed
236
    Values(true, false)));
wester committed
237 238 239

#endif

wester committed
240
#endif