commit
6bbac2a7d9
55 changed files with 4252 additions and 218 deletions
After Width: | Height: | Size: 49 KiB |
@ -0,0 +1,282 @@ |
||||
/*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
|
||||
//
|
||||
// Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
|
||||
// Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
|
||||
// Third party copyrights are property of their respective owners.
|
||||
//
|
||||
// @Authors
|
||||
// Fangfang Bai, fangfang@multicorewareinc.com
|
||||
// Jin Ma, jin@multicorewareinc.com
|
||||
//
|
||||
// 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 oclMaterials 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 "perf_precomp.hpp" |
||||
using namespace perf; |
||||
using namespace std; |
||||
using namespace cv::ocl; |
||||
using namespace cv; |
||||
using std::tr1::tuple; |
||||
using std::tr1::get; |
||||
#if defined(HAVE_XINE) || \ |
||||
defined(HAVE_GSTREAMER) || \
|
||||
defined(HAVE_QUICKTIME) || \
|
||||
defined(HAVE_AVFOUNDATION) || \
|
||||
defined(HAVE_FFMPEG) || \
|
||||
defined(WIN32) |
||||
|
||||
# define BUILD_WITH_VIDEO_INPUT_SUPPORT 1 |
||||
#else |
||||
# define BUILD_WITH_VIDEO_INPUT_SUPPORT 0 |
||||
#endif |
||||
|
||||
#if BUILD_WITH_VIDEO_INPUT_SUPPORT |
||||
static void cvtFrameFmt(vector<Mat>& input, vector<Mat>& output) |
||||
{ |
||||
for(int i = 0; i< (int)(input.size()); i++) |
||||
{ |
||||
cvtColor(input[i], output[i], COLOR_RGB2GRAY); |
||||
} |
||||
} |
||||
//prepare data for CPU
|
||||
static void prepareData(VideoCapture& cap, int cn, vector<Mat>& frame_buffer) |
||||
{ |
||||
cv::Mat frame; |
||||
std::vector<Mat> frame_buffer_init; |
||||
int nFrame = (int)frame_buffer.size(); |
||||
for(int i = 0; i < nFrame; i++) |
||||
{ |
||||
cap >> frame; |
||||
ASSERT_FALSE(frame.empty()); |
||||
frame_buffer_init.push_back(frame); |
||||
} |
||||
|
||||
if(cn == 1) |
||||
cvtFrameFmt(frame_buffer_init, frame_buffer); |
||||
else |
||||
frame_buffer = frame_buffer_init; |
||||
} |
||||
//copy CPU data to GPU
|
||||
static void prepareData(vector<Mat>& frame_buffer, vector<oclMat>& frame_buffer_ocl) |
||||
{ |
||||
for(int i = 0; i < (int)frame_buffer.size(); i++) |
||||
frame_buffer_ocl.push_back(cv::ocl::oclMat(frame_buffer[i])); |
||||
} |
||||
#endif |
||||
///////////// MOG ////////////////////////
|
||||
#if BUILD_WITH_VIDEO_INPUT_SUPPORT |
||||
|
||||
typedef tuple<string, int, double> VideoMOGParamType; |
||||
typedef TestBaseWithParam<VideoMOGParamType> VideoMOGFixture; |
||||
|
||||
PERF_TEST_P(VideoMOGFixture, MOG, |
||||
::testing::Combine(::testing::Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"), |
||||
::testing::Values(1, 3), |
||||
::testing::Values(0.0, 0.01))) |
||||
{ |
||||
VideoMOGParamType params = GetParam(); |
||||
|
||||
const string inputFile = perf::TestBase::getDataPath(get<0>(params)); |
||||
const int cn = get<1>(params); |
||||
const float learningRate = static_cast<float>(get<2>(params)); |
||||
|
||||
const int nFrame = 5; |
||||
|
||||
Mat foreground_cpu; |
||||
std::vector<Mat> frame_buffer(nFrame); |
||||
std::vector<oclMat> frame_buffer_ocl; |
||||
|
||||
cv::VideoCapture cap(inputFile); |
||||
ASSERT_TRUE(cap.isOpened()); |
||||
|
||||
prepareData(cap, cn, frame_buffer); |
||||
|
||||
cv::Mat foreground; |
||||
cv::ocl::oclMat foreground_d; |
||||
if(RUN_PLAIN_IMPL) |
||||
{ |
||||
TEST_CYCLE() |
||||
{ |
||||
cv::Ptr<cv::BackgroundSubtractorMOG> mog = createBackgroundSubtractorMOG(); |
||||
foreground.release(); |
||||
for (int i = 0; i < nFrame; i++) |
||||
{ |
||||
mog->apply(frame_buffer[i], foreground, learningRate); |
||||
} |
||||
} |
||||
SANITY_CHECK(foreground); |
||||
}else if(RUN_OCL_IMPL) |
||||
{ |
||||
prepareData(frame_buffer, frame_buffer_ocl); |
||||
CV_Assert((int)(frame_buffer_ocl.size()) == nFrame); |
||||
OCL_TEST_CYCLE() |
||||
{ |
||||
cv::ocl::MOG d_mog; |
||||
foreground_d.release(); |
||||
for (int i = 0; i < nFrame; ++i) |
||||
{ |
||||
d_mog(frame_buffer_ocl[i], foreground_d, learningRate); |
||||
} |
||||
} |
||||
foreground_d.download(foreground); |
||||
SANITY_CHECK(foreground); |
||||
}else |
||||
OCL_PERF_ELSE |
||||
} |
||||
#endif |
||||
|
||||
///////////// MOG2 ////////////////////////
|
||||
#if BUILD_WITH_VIDEO_INPUT_SUPPORT |
||||
|
||||
typedef tuple<string, int> VideoMOG2ParamType; |
||||
typedef TestBaseWithParam<VideoMOG2ParamType> VideoMOG2Fixture; |
||||
|
||||
PERF_TEST_P(VideoMOG2Fixture, MOG2, |
||||
::testing::Combine(::testing::Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"), |
||||
::testing::Values(1, 3))) |
||||
{ |
||||
VideoMOG2ParamType params = GetParam(); |
||||
|
||||
const string inputFile = perf::TestBase::getDataPath(get<0>(params)); |
||||
const int cn = get<1>(params); |
||||
int nFrame = 5; |
||||
|
||||
std::vector<cv::Mat> frame_buffer(nFrame); |
||||
std::vector<cv::ocl::oclMat> frame_buffer_ocl; |
||||
|
||||
cv::VideoCapture cap(inputFile); |
||||
ASSERT_TRUE(cap.isOpened()); |
||||
prepareData(cap, cn, frame_buffer); |
||||
cv::Mat foreground; |
||||
cv::ocl::oclMat foreground_d; |
||||
|
||||
if(RUN_PLAIN_IMPL) |
||||
{ |
||||
TEST_CYCLE() |
||||
{ |
||||
cv::Ptr<cv::BackgroundSubtractorMOG2> mog2 = createBackgroundSubtractorMOG2(); |
||||
mog2->set("detectShadows", false); |
||||
foreground.release(); |
||||
|
||||
for (int i = 0; i < nFrame; i++) |
||||
{ |
||||
mog2->apply(frame_buffer[i], foreground); |
||||
} |
||||
} |
||||
SANITY_CHECK(foreground); |
||||
}else if(RUN_OCL_IMPL) |
||||
{ |
||||
prepareData(frame_buffer, frame_buffer_ocl); |
||||
CV_Assert((int)(frame_buffer_ocl.size()) == nFrame); |
||||
OCL_TEST_CYCLE() |
||||
{ |
||||
cv::ocl::MOG2 d_mog2; |
||||
foreground_d.release(); |
||||
for (int i = 0; i < nFrame; i++) |
||||
{ |
||||
d_mog2(frame_buffer_ocl[i], foreground_d); |
||||
} |
||||
} |
||||
foreground_d.download(foreground); |
||||
SANITY_CHECK(foreground); |
||||
}else |
||||
OCL_PERF_ELSE |
||||
} |
||||
#endif |
||||
|
||||
///////////// MOG2_GetBackgroundImage //////////////////
|
||||
#if BUILD_WITH_VIDEO_INPUT_SUPPORT |
||||
|
||||
typedef TestBaseWithParam<VideoMOG2ParamType> Video_MOG2GetBackgroundImage; |
||||
|
||||
PERF_TEST_P(Video_MOG2GetBackgroundImage, MOG2, |
||||
::testing::Combine(::testing::Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"), |
||||
::testing::Values(3))) |
||||
{ |
||||
VideoMOG2ParamType params = GetParam(); |
||||
|
||||
const string inputFile = perf::TestBase::getDataPath(get<0>(params)); |
||||
const int cn = get<1>(params); |
||||
int nFrame = 5; |
||||
|
||||
std::vector<cv::Mat> frame_buffer(nFrame); |
||||
std::vector<cv::ocl::oclMat> frame_buffer_ocl; |
||||
|
||||
cv::VideoCapture cap(inputFile); |
||||
ASSERT_TRUE(cap.isOpened()); |
||||
|
||||
prepareData(cap, cn, frame_buffer); |
||||
|
||||
cv::Mat foreground; |
||||
cv::Mat background; |
||||
cv::ocl::oclMat foreground_d; |
||||
cv::ocl::oclMat background_d; |
||||
|
||||
if(RUN_PLAIN_IMPL) |
||||
{ |
||||
TEST_CYCLE() |
||||
{ |
||||
cv::Ptr<cv::BackgroundSubtractorMOG2> mog2 = createBackgroundSubtractorMOG2(); |
||||
mog2->set("detectShadows", false); |
||||
foreground.release(); |
||||
background.release(); |
||||
for (int i = 0; i < nFrame; i++) |
||||
{ |
||||
mog2->apply(frame_buffer[i], foreground); |
||||
} |
||||
mog2->getBackgroundImage(background); |
||||
} |
||||
SANITY_CHECK(background); |
||||
}else if(RUN_OCL_IMPL) |
||||
{ |
||||
prepareData(frame_buffer, frame_buffer_ocl); |
||||
CV_Assert((int)(frame_buffer_ocl.size()) == nFrame); |
||||
OCL_TEST_CYCLE() |
||||
{ |
||||
cv::ocl::MOG2 d_mog2; |
||||
foreground_d.release(); |
||||
background_d.release(); |
||||
for (int i = 0; i < nFrame; i++) |
||||
{ |
||||
d_mog2(frame_buffer_ocl[i], foreground_d); |
||||
} |
||||
d_mog2.getBackgroundImage(background_d); |
||||
} |
||||
background_d.download(background); |
||||
SANITY_CHECK(background); |
||||
}else |
||||
OCL_PERF_ELSE |
||||
} |
||||
#endif |
@ -0,0 +1,638 @@ |
||||
/*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
|
||||
//
|
||||
// Copyright (C) 2010-2013, Multicoreware, Inc., all rights reserved.
|
||||
// Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved.
|
||||
// Third party copyrights are property of their respective owners.
|
||||
//
|
||||
// @Authors
|
||||
// Jin Ma, jin@multicorewareinc.com
|
||||
//
|
||||
// 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 oclMaterials 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 "precomp.hpp" |
||||
using namespace cv; |
||||
using namespace cv::ocl; |
||||
namespace cv |
||||
{ |
||||
namespace ocl |
||||
{ |
||||
extern const char* bgfg_mog; |
||||
|
||||
typedef struct _contant_struct |
||||
{ |
||||
cl_float c_Tb; |
||||
cl_float c_TB; |
||||
cl_float c_Tg; |
||||
cl_float c_varInit; |
||||
cl_float c_varMin; |
||||
cl_float c_varMax; |
||||
cl_float c_tau; |
||||
cl_uchar c_shadowVal; |
||||
}contant_struct; |
||||
|
||||
cl_mem cl_constants = NULL; |
||||
float c_TB; |
||||
} |
||||
} |
||||
|
||||
#if defined _MSC_VER |
||||
#define snprintf sprintf_s |
||||
#endif |
||||
|
||||
namespace cv { namespace ocl { namespace device |
||||
{ |
||||
namespace mog |
||||
{ |
||||
void mog_ocl(const oclMat& frame, int cn, oclMat& fgmask, oclMat& weight, oclMat& sortKey, oclMat& mean, oclMat& var, |
||||
int nmixtures, float varThreshold, float learningRate, float backgroundRatio, float noiseSigma); |
||||
|
||||
void getBackgroundImage_ocl(int cn, const oclMat& weight, const oclMat& mean, oclMat& dst, int nmixtures, float backgroundRatio); |
||||
|
||||
void loadConstants(float Tb, float TB, float Tg, float varInit, float varMin, float varMax, float tau, |
||||
unsigned char shadowVal); |
||||
|
||||
void mog2_ocl(const oclMat& frame, int cn, oclMat& fgmask, oclMat& modesUsed, oclMat& weight, oclMat& variance, oclMat& mean, |
||||
float alphaT, float prune, bool detectShadows, int nmixtures); |
||||
|
||||
void getBackgroundImage2_ocl(int cn, const oclMat& modesUsed, const oclMat& weight, const oclMat& mean, oclMat& dst, int nmixtures); |
||||
} |
||||
}}} |
||||
|
||||
namespace mog |
||||
{ |
||||
const int defaultNMixtures = 5; |
||||
const int defaultHistory = 200; |
||||
const float defaultBackgroundRatio = 0.7f; |
||||
const float defaultVarThreshold = 2.5f * 2.5f; |
||||
const float defaultNoiseSigma = 30.0f * 0.5f; |
||||
const float defaultInitialWeight = 0.05f; |
||||
} |
||||
void cv::ocl::BackgroundSubtractor::operator()(const oclMat&, oclMat&, float) |
||||
{ |
||||
|
||||
} |
||||
cv::ocl::BackgroundSubtractor::~BackgroundSubtractor() |
||||
{ |
||||
|
||||
} |
||||
|
||||
cv::ocl::MOG::MOG(int nmixtures) : |
||||
frameSize_(0, 0), frameType_(0), nframes_(0) |
||||
{ |
||||
nmixtures_ = std::min(nmixtures > 0 ? nmixtures : mog::defaultNMixtures, 8); |
||||
history = mog::defaultHistory; |
||||
varThreshold = mog::defaultVarThreshold; |
||||
backgroundRatio = mog::defaultBackgroundRatio; |
||||
noiseSigma = mog::defaultNoiseSigma; |
||||
} |
||||
|
||||
void cv::ocl::MOG::initialize(cv::Size frameSize, int frameType) |
||||
{ |
||||
CV_Assert(frameType == CV_8UC1 || frameType == CV_8UC3 || frameType == CV_8UC4); |
||||
|
||||
frameSize_ = frameSize; |
||||
frameType_ = frameType; |
||||
|
||||
int ch = CV_MAT_CN(frameType); |
||||
int work_ch = ch; |
||||
|
||||
// for each gaussian mixture of each pixel bg model we store
|
||||
// the mixture sort key (w/sum_of_variances), the mixture weight (w),
|
||||
// the mean (nchannels values) and
|
||||
// the diagonal covariance matrix (another nchannels values)
|
||||
|
||||
weight_.create(frameSize.height * nmixtures_, frameSize_.width, CV_32FC1); |
||||
sortKey_.create(frameSize.height * nmixtures_, frameSize_.width, CV_32FC1); |
||||
mean_.create(frameSize.height * nmixtures_, frameSize_.width, CV_32FC(work_ch)); |
||||
var_.create(frameSize.height * nmixtures_, frameSize_.width, CV_32FC(work_ch)); |
||||
|
||||
weight_.setTo(cv::Scalar::all(0)); |
||||
sortKey_.setTo(cv::Scalar::all(0)); |
||||
mean_.setTo(cv::Scalar::all(0)); |
||||
var_.setTo(cv::Scalar::all(0)); |
||||
|
||||
nframes_ = 0; |
||||
} |
||||
|
||||
void cv::ocl::MOG::operator()(const cv::ocl::oclMat& frame, cv::ocl::oclMat& fgmask, float learningRate) |
||||
{ |
||||
using namespace cv::ocl::device::mog; |
||||
|
||||
CV_Assert(frame.depth() == CV_8U); |
||||
|
||||
int ch = frame.oclchannels(); |
||||
int work_ch = ch; |
||||
|
||||
if (nframes_ == 0 || learningRate >= 1.0 || frame.size() != frameSize_ || work_ch != mean_.oclchannels()) |
||||
initialize(frame.size(), frame.type()); |
||||
|
||||
fgmask.create(frameSize_, CV_8UC1); |
||||
|
||||
++nframes_; |
||||
learningRate = learningRate >= 0.0f && nframes_ > 1 ? learningRate : 1.0f / std::min(nframes_, history); |
||||
CV_Assert(learningRate >= 0.0f); |
||||
|
||||
mog_ocl(frame, ch, fgmask, weight_, sortKey_, mean_, var_, nmixtures_, |
||||
varThreshold, learningRate, backgroundRatio, noiseSigma); |
||||
} |
||||
|
||||
void cv::ocl::MOG::getBackgroundImage(oclMat& backgroundImage) const |
||||
{ |
||||
using namespace cv::ocl::device::mog; |
||||
|
||||
backgroundImage.create(frameSize_, frameType_); |
||||
|
||||
cv::ocl::device::mog::getBackgroundImage_ocl(backgroundImage.oclchannels(), weight_, mean_, backgroundImage, nmixtures_, backgroundRatio); |
||||
} |
||||
|
||||
void cv::ocl::MOG::release() |
||||
{ |
||||
frameSize_ = Size(0, 0); |
||||
frameType_ = 0; |
||||
nframes_ = 0; |
||||
|
||||
weight_.release(); |
||||
sortKey_.release(); |
||||
mean_.release(); |
||||
var_.release(); |
||||
clReleaseMemObject(cl_constants); |
||||
} |
||||
|
||||
static void mog_withoutLearning(const oclMat& frame, int cn, oclMat& fgmask, oclMat& weight, oclMat& mean, oclMat& var, |
||||
int nmixtures, float varThreshold, float backgroundRatio) |
||||
{ |
||||
Context* clCxt = Context::getContext(); |
||||
|
||||
size_t local_thread[] = {32, 8, 1}; |
||||
size_t global_thread[] = {frame.cols, frame.rows, 1}; |
||||
|
||||
int frame_step = (int)(frame.step/frame.elemSize()); |
||||
int fgmask_step = (int)(fgmask.step/fgmask.elemSize()); |
||||
int weight_step = (int)(weight.step/weight.elemSize()); |
||||
int mean_step = (int)(mean.step/mean.elemSize()); |
||||
int var_step = (int)(var.step/var.elemSize()); |
||||
|
||||
int fgmask_offset_y = (int)(fgmask.offset/fgmask.step); |
||||
int fgmask_offset_x = (int)(fgmask.offset%fgmask.step); |
||||
fgmask_offset_x = fgmask_offset_x/(int)fgmask.elemSize(); |
||||
|
||||
int frame_offset_y = (int)(frame.offset/frame.step); |
||||
int frame_offset_x = (int)(frame.offset%frame.step); |
||||
frame_offset_x = frame_offset_x/(int)frame.elemSize(); |
||||
|
||||
char build_option[50]; |
||||
if(cn == 1) |
||||
{ |
||||
snprintf(build_option, 50, "-D CN1 -D NMIXTURES=%d", nmixtures); |
||||
}else |
||||
{ |
||||
snprintf(build_option, 50, "-D NMIXTURES=%d", nmixtures); |
||||
} |
||||
|
||||
String kernel_name = "mog_withoutLearning_kernel"; |
||||
std::vector<std::pair<size_t, const void*> > args; |
||||
|
||||
args.push_back(std::make_pair(sizeof(cl_mem), (void*)&frame.data)); |
||||
args.push_back(std::make_pair(sizeof(cl_mem), (void*)&fgmask.data)); |
||||
args.push_back(std::make_pair(sizeof(cl_mem), (void*)&weight.data)); |
||||
args.push_back(std::make_pair(sizeof(cl_mem), (void*)&mean.data)); |
||||
args.push_back(std::make_pair(sizeof(cl_mem), (void*)&var.data)); |
||||
|
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&frame.rows)); |
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&frame.cols)); |
||||
|
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&frame_step)); |
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&fgmask_step)); |
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&weight_step)); |
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&mean_step)); |
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&var_step)); |
||||
|
||||
args.push_back(std::make_pair(sizeof(cl_float), (void*)&varThreshold)); |
||||
args.push_back(std::make_pair(sizeof(cl_float), (void*)&backgroundRatio)); |
||||
|
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&fgmask_offset_x)); |
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&fgmask_offset_y)); |
||||
|
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&frame_offset_x)); |
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&frame_offset_y)); |
||||
|
||||
openCLExecuteKernel(clCxt, &bgfg_mog, kernel_name, global_thread, local_thread, args, -1, -1, build_option); |
||||
} |
||||
|
||||
|
||||
static void mog_withLearning(const oclMat& frame, int cn, oclMat& fgmask_raw, oclMat& weight, oclMat& sortKey, oclMat& mean, oclMat& var, |
||||
int nmixtures, float varThreshold, float backgroundRatio, float learningRate, float minVar) |
||||
{ |
||||
Context* clCxt = Context::getContext(); |
||||
|
||||
size_t local_thread[] = {32, 8, 1}; |
||||
size_t global_thread[] = {frame.cols, frame.rows, 1}; |
||||
|
||||
oclMat fgmask(fgmask_raw.size(), CV_32SC1); |
||||
|
||||
int frame_step = (int)(frame.step/frame.elemSize()); |
||||
int fgmask_step = (int)(fgmask.step/fgmask.elemSize()); |
||||
int weight_step = (int)(weight.step/weight.elemSize()); |
||||
int sortKey_step = (int)(sortKey.step/sortKey.elemSize()); |
||||
int mean_step = (int)(mean.step/mean.elemSize()); |
||||
int var_step = (int)(var.step/var.elemSize()); |
||||
|
||||
int fgmask_offset_y = (int)(fgmask.offset/fgmask.step); |
||||
int fgmask_offset_x = (int)(fgmask.offset%fgmask.step); |
||||
fgmask_offset_x = fgmask_offset_x/(int)fgmask.elemSize(); |
||||
|
||||
int frame_offset_y = (int)(frame.offset/frame.step); |
||||
int frame_offset_x = (int)(frame.offset%frame.step); |
||||
frame_offset_x = frame_offset_x/(int)frame.elemSize(); |
||||
|
||||
char build_option[50]; |
||||
if(cn == 1) |
||||
{ |
||||
snprintf(build_option, 50, "-D CN1 -D NMIXTURES=%d", nmixtures); |
||||
}else |
||||
{ |
||||
snprintf(build_option, 50, "-D NMIXTURES=%d", nmixtures); |
||||
} |
||||
|
||||
String kernel_name = "mog_withLearning_kernel"; |
||||
std::vector<std::pair<size_t, const void*> > args; |
||||
|
||||
args.push_back(std::make_pair(sizeof(cl_mem), (void*)&frame.data)); |
||||
args.push_back(std::make_pair(sizeof(cl_mem), (void*)&fgmask.data)); |
||||
args.push_back(std::make_pair(sizeof(cl_mem), (void*)&weight.data)); |
||||
args.push_back(std::make_pair(sizeof(cl_mem), (void*)&sortKey.data)); |
||||
args.push_back(std::make_pair(sizeof(cl_mem), (void*)&mean.data)); |
||||
args.push_back(std::make_pair(sizeof(cl_mem), (void*)&var.data)); |
||||
|
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&frame.rows)); |
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&frame.cols)); |
||||
|
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&frame_step)); |
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&fgmask_step)); |
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&weight_step)); |
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&sortKey_step)); |
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&mean_step)); |
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&var_step)); |
||||
|
||||
args.push_back(std::make_pair(sizeof(cl_float), (void*)&varThreshold)); |
||||
args.push_back(std::make_pair(sizeof(cl_float), (void*)&backgroundRatio)); |
||||
args.push_back(std::make_pair(sizeof(cl_float), (void*)&learningRate)); |
||||
args.push_back(std::make_pair(sizeof(cl_float), (void*)&minVar)); |
||||
|
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&fgmask_offset_x)); |
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&fgmask_offset_y)); |
||||
|
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&frame_offset_x)); |
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&frame_offset_y)); |
||||
|
||||
openCLExecuteKernel(clCxt, &bgfg_mog, kernel_name, global_thread, local_thread, args, -1, -1, build_option); |
||||
fgmask.convertTo(fgmask, CV_8U); |
||||
fgmask.copyTo(fgmask_raw); |
||||
} |
||||
|
||||
void cv::ocl::device::mog::mog_ocl(const oclMat& frame, int cn, oclMat& fgmask, oclMat& weight, oclMat& sortKey, oclMat& mean, oclMat& var, |
||||
int nmixtures, float varThreshold, float learningRate, float backgroundRatio, float noiseSigma) |
||||
{ |
||||
const float minVar = noiseSigma * noiseSigma; |
||||
|
||||
if(learningRate > 0.0f) |
||||
mog_withLearning(frame, cn, fgmask, weight, sortKey, mean, var, nmixtures, |
||||
varThreshold, backgroundRatio, learningRate, minVar); |
||||
else |
||||
mog_withoutLearning(frame, cn, fgmask, weight, mean, var, nmixtures, varThreshold, backgroundRatio); |
||||
} |
||||
|
||||
void cv::ocl::device::mog::getBackgroundImage_ocl(int cn, const oclMat& weight, const oclMat& mean, oclMat& dst, int nmixtures, float backgroundRatio) |
||||
{ |
||||
Context* clCxt = Context::getContext(); |
||||
|
||||
size_t local_thread[] = {32, 8, 1}; |
||||
size_t global_thread[] = {dst.cols, dst.rows, 1}; |
||||
|
||||
int weight_step = (int)(weight.step/weight.elemSize()); |
||||
int mean_step = (int)(mean.step/mean.elemSize()); |
||||
int dst_step = (int)(dst.step/dst.elemSize()); |
||||
|
||||
char build_option[50]; |
||||
if(cn == 1) |
||||
{ |
||||
snprintf(build_option, 50, "-D CN1 -D NMIXTURES=%d", nmixtures); |
||||
}else |
||||
{ |
||||
snprintf(build_option, 50, "-D NMIXTURES=%d", nmixtures); |
||||
} |
||||
|
||||
String kernel_name = "getBackgroundImage_kernel"; |
||||
std::vector<std::pair<size_t, const void*> > args; |
||||
|
||||
args.push_back(std::make_pair(sizeof(cl_mem), (void*)&weight.data)); |
||||
args.push_back(std::make_pair(sizeof(cl_mem), (void*)&mean.data)); |
||||
args.push_back(std::make_pair(sizeof(cl_mem), (void*)&dst.data)); |
||||
|
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&dst.rows)); |
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&dst.cols)); |
||||
|
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&weight_step)); |
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&mean_step)); |
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&dst_step)); |
||||
|
||||
args.push_back(std::make_pair(sizeof(cl_float), (void*)&backgroundRatio)); |
||||
|
||||
openCLExecuteKernel(clCxt, &bgfg_mog, kernel_name, global_thread, local_thread, args, -1, -1, build_option); |
||||
} |
||||
|
||||
void cv::ocl::device::mog::loadConstants(float Tb, float TB, float Tg, float varInit, float varMin, float varMax, float tau, unsigned char shadowVal) |
||||
{ |
||||
varMin = cv::min(varMin, varMax); |
||||
varMax = cv::max(varMin, varMax); |
||||
|
||||
c_TB = TB; |
||||
|
||||
_contant_struct *constants = new _contant_struct; |
||||
constants->c_Tb = Tb; |
||||
constants->c_TB = TB; |
||||
constants->c_Tg = Tg; |
||||
constants->c_varInit = varInit; |
||||
constants->c_varMin = varMin; |
||||
constants->c_varMax = varMax; |
||||
constants->c_tau = tau; |
||||
constants->c_shadowVal = shadowVal; |
||||
|
||||
cl_constants = load_constant(*((cl_context*)getoclContext()), *((cl_command_queue*)getoclCommandQueue()), |
||||
(void *)constants, sizeof(_contant_struct)); |
||||
} |
||||
|
||||
void cv::ocl::device::mog::mog2_ocl(const oclMat& frame, int cn, oclMat& fgmaskRaw, oclMat& modesUsed, oclMat& weight, oclMat& variance, |
||||
oclMat& mean, float alphaT, float prune, bool detectShadows, int nmixtures) |
||||
{ |
||||
oclMat fgmask(fgmaskRaw.size(), CV_32SC1); |
||||
|
||||
Context* clCxt = Context::getContext(); |
||||
|
||||
const float alpha1 = 1.0f - alphaT; |
||||
|
||||
cl_int detectShadows_flag = 0; |
||||
if(detectShadows) |
||||
detectShadows_flag = 1; |
||||
|
||||
size_t local_thread[] = {32, 8, 1}; |
||||
size_t global_thread[] = {frame.cols, frame.rows, 1}; |
||||
|
||||
int frame_step = (int)(frame.step/frame.elemSize()); |
||||
int fgmask_step = (int)(fgmask.step/fgmask.elemSize()); |
||||
int weight_step = (int)(weight.step/weight.elemSize()); |
||||
int modesUsed_step = (int)(modesUsed.step/modesUsed.elemSize()); |
||||
int mean_step = (int)(mean.step/mean.elemSize()); |
||||
int var_step = (int)(variance.step/variance.elemSize()); |
||||
|
||||
int fgmask_offset_y = (int)(fgmask.offset/fgmask.step); |
||||
int fgmask_offset_x = (int)(fgmask.offset%fgmask.step); |
||||
fgmask_offset_x = fgmask_offset_x/(int)fgmask.elemSize(); |
||||
|
||||
int frame_offset_y = (int)(frame.offset/frame.step); |
||||
int frame_offset_x = (int)(frame.offset%frame.step); |
||||
frame_offset_x = frame_offset_x/(int)frame.elemSize(); |
||||
|
||||
String kernel_name = "mog2_kernel"; |
||||
std::vector<std::pair<size_t, const void*> > args; |
||||
|
||||
char build_option[50]; |
||||
if(cn == 1) |
||||
{ |
||||
snprintf(build_option, 50, "-D CN1 -D NMIXTURES=%d", nmixtures); |
||||
}else |
||||
{ |
||||
snprintf(build_option, 50, "-D NMIXTURES=%d", nmixtures); |
||||
} |
||||
|
||||
args.push_back(std::make_pair(sizeof(cl_mem), (void*)&frame.data)); |
||||
args.push_back(std::make_pair(sizeof(cl_mem), (void*)&fgmask.data)); |
||||
args.push_back(std::make_pair(sizeof(cl_mem), (void*)&weight.data)); |
||||
args.push_back(std::make_pair(sizeof(cl_mem), (void*)&mean.data)); |
||||
args.push_back(std::make_pair(sizeof(cl_mem), (void*)&modesUsed.data)); |
||||
args.push_back(std::make_pair(sizeof(cl_mem), (void*)&variance.data)); |
||||
|
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&frame.rows)); |
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&frame.cols)); |
||||
|
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&frame_step)); |
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&fgmask_step)); |
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&weight_step)); |
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&mean_step)); |
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&modesUsed_step)); |
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&var_step)); |
||||
|
||||
args.push_back(std::make_pair(sizeof(cl_float), (void*)&alphaT)); |
||||
args.push_back(std::make_pair(sizeof(cl_float), (void*)&alpha1)); |
||||
args.push_back(std::make_pair(sizeof(cl_float), (void*)&prune)); |
||||
|
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&detectShadows_flag)); |
||||
|
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&fgmask_offset_x)); |
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&fgmask_offset_y)); |
||||
|
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&frame_offset_x)); |
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&frame_offset_y)); |
||||
args.push_back(std::make_pair(sizeof(cl_mem), (void*)&cl_constants)); |
||||
|
||||
openCLExecuteKernel(clCxt, &bgfg_mog, kernel_name, global_thread, local_thread, args, -1, -1, build_option); |
||||
|
||||
fgmask.convertTo(fgmask, CV_8U); |
||||
fgmask.copyTo(fgmaskRaw); |
||||
} |
||||
|
||||
void cv::ocl::device::mog::getBackgroundImage2_ocl(int cn, const oclMat& modesUsed, const oclMat& weight, const oclMat& mean, oclMat& dst, int nmixtures) |
||||
{ |
||||
Context* clCxt = Context::getContext(); |
||||
|
||||
size_t local_thread[] = {32, 8, 1}; |
||||
size_t global_thread[] = {modesUsed.cols, modesUsed.rows, 1}; |
||||
|
||||
int weight_step = (int)(weight.step/weight.elemSize()); |
||||
int modesUsed_step = (int)(modesUsed.step/modesUsed.elemSize()); |
||||
int mean_step = (int)(mean.step/mean.elemSize()); |
||||
int dst_step = (int)(dst.step/dst.elemSize()); |
||||
|
||||
int dst_y = (int)(dst.offset/dst.step); |
||||
int dst_x = (int)(dst.offset%dst.step); |
||||
dst_x = dst_x/(int)dst.elemSize(); |
||||
|
||||
String kernel_name = "getBackgroundImage2_kernel"; |
||||
std::vector<std::pair<size_t, const void*> > args; |
||||
|
||||
char build_option[50]; |
||||
if(cn == 1) |
||||
{ |
||||
snprintf(build_option, 50, "-D CN1 -D NMIXTURES=%d", nmixtures); |
||||
}else |
||||
{ |
||||
snprintf(build_option, 50, "-D NMIXTURES=%d", nmixtures); |
||||
} |
||||
|
||||
args.push_back(std::make_pair(sizeof(cl_mem), (void*)&modesUsed.data)); |
||||
args.push_back(std::make_pair(sizeof(cl_mem), (void*)&weight.data)); |
||||
args.push_back(std::make_pair(sizeof(cl_mem), (void*)&mean.data)); |
||||
args.push_back(std::make_pair(sizeof(cl_mem), (void*)&dst.data)); |
||||
args.push_back(std::make_pair(sizeof(cl_float), (void*)&c_TB)); |
||||
|
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&modesUsed.rows)); |
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&modesUsed.cols)); |
||||
|
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&modesUsed_step)); |
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&weight_step)); |
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&mean_step)); |
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&dst_step)); |
||||
|
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&dst_x)); |
||||
args.push_back(std::make_pair(sizeof(cl_int), (void*)&dst_y)); |
||||
|
||||
openCLExecuteKernel(clCxt, &bgfg_mog, kernel_name, global_thread, local_thread, args, -1, -1, build_option); |
||||
} |
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
// MOG2
|
||||
|
||||
namespace mog2 |
||||
{ |
||||
// default parameters of gaussian background detection algorithm
|
||||
const int defaultHistory = 500; // Learning rate; alpha = 1/defaultHistory2
|
||||
const float defaultVarThreshold = 4.0f * 4.0f; |
||||
const int defaultNMixtures = 5; // maximal number of Gaussians in mixture
|
||||
const float defaultBackgroundRatio = 0.9f; // threshold sum of weights for background test
|
||||
const float defaultVarThresholdGen = 3.0f * 3.0f; |
||||
const float defaultVarInit = 15.0f; // initial variance for new components
|
||||
const float defaultVarMax = 5.0f * defaultVarInit; |
||||
const float defaultVarMin = 4.0f; |
||||
|
||||
// additional parameters
|
||||
const float defaultfCT = 0.05f; // complexity reduction prior constant 0 - no reduction of number of components
|
||||
const unsigned char defaultnShadowDetection = 127; // value to use in the segmentation mask for shadows, set 0 not to do shadow detection
|
||||
const float defaultfTau = 0.5f; // Tau - shadow threshold, see the paper for explanation
|
||||
} |
||||
|
||||
cv::ocl::MOG2::MOG2(int nmixtures) : frameSize_(0, 0), frameType_(0), nframes_(0) |
||||
{ |
||||
nmixtures_ = nmixtures > 0 ? nmixtures : mog2::defaultNMixtures; |
||||
|
||||
history = mog2::defaultHistory; |
||||
varThreshold = mog2::defaultVarThreshold; |
||||
bShadowDetection = true; |
||||
|
||||
backgroundRatio = mog2::defaultBackgroundRatio; |
||||
fVarInit = mog2::defaultVarInit; |
||||
fVarMax = mog2::defaultVarMax; |
||||
fVarMin = mog2::defaultVarMin; |
||||
|
||||
varThresholdGen = mog2::defaultVarThresholdGen; |
||||
fCT = mog2::defaultfCT; |
||||
nShadowDetection = mog2::defaultnShadowDetection; |
||||
fTau = mog2::defaultfTau; |
||||
} |
||||
|
||||
void cv::ocl::MOG2::initialize(cv::Size frameSize, int frameType) |
||||
{ |
||||
using namespace cv::ocl::device::mog; |
||||
CV_Assert(frameType == CV_8UC1 || frameType == CV_8UC3 || frameType == CV_8UC4); |
||||
|
||||
frameSize_ = frameSize; |
||||
frameType_ = frameType; |
||||
nframes_ = 0; |
||||
|
||||
int ch = CV_MAT_CN(frameType); |
||||
int work_ch = ch; |
||||
|
||||
// for each gaussian mixture of each pixel bg model we store ...
|
||||
// the mixture weight (w),
|
||||
// the mean (nchannels values) and
|
||||
// the covariance
|
||||
weight_.create(frameSize.height * nmixtures_, frameSize_.width, CV_32FC1); |
||||
weight_.setTo(Scalar::all(0)); |
||||
|
||||
variance_.create(frameSize.height * nmixtures_, frameSize_.width, CV_32FC1); |
||||
variance_.setTo(Scalar::all(0)); |
||||
|
||||
mean_.create(frameSize.height * nmixtures_, frameSize_.width, CV_32FC(work_ch)); //4 channels
|
||||
mean_.setTo(Scalar::all(0)); |
||||
|
||||
//make the array for keeping track of the used modes per pixel - all zeros at start
|
||||
bgmodelUsedModes_.create(frameSize_, CV_32FC1); |
||||
bgmodelUsedModes_.setTo(cv::Scalar::all(0)); |
||||
|
||||
loadConstants(varThreshold, backgroundRatio, varThresholdGen, fVarInit, fVarMin, fVarMax, fTau, nShadowDetection); |
||||
} |
||||
|
||||
void cv::ocl::MOG2::operator()(const oclMat& frame, oclMat& fgmask, float learningRate) |
||||
{ |
||||
using namespace cv::ocl::device::mog; |
||||
|
||||
int ch = frame.oclchannels(); |
||||
int work_ch = ch; |
||||
|
||||
if (nframes_ == 0 || learningRate >= 1.0f || frame.size() != frameSize_ || work_ch != mean_.oclchannels()) |
||||
initialize(frame.size(), frame.type()); |
||||
|
||||
fgmask.create(frameSize_, CV_8UC1); |
||||
fgmask.setTo(cv::Scalar::all(0)); |
||||
|
||||
++nframes_; |
||||
learningRate = learningRate >= 0.0f && nframes_ > 1 ? learningRate : 1.0f / std::min(2 * nframes_, history); |
||||
CV_Assert(learningRate >= 0.0f); |
||||
|
||||
mog2_ocl(frame, frame.oclchannels(), fgmask, bgmodelUsedModes_, weight_, variance_, mean_, learningRate, -learningRate * fCT, bShadowDetection, nmixtures_); |
||||
} |
||||
|
||||
void cv::ocl::MOG2::getBackgroundImage(oclMat& backgroundImage) const |
||||
{ |
||||
using namespace cv::ocl::device::mog; |
||||
|
||||
backgroundImage.create(frameSize_, frameType_); |
||||
|
||||
cv::ocl::device::mog::getBackgroundImage2_ocl(backgroundImage.oclchannels(), bgmodelUsedModes_, weight_, mean_, backgroundImage, nmixtures_); |
||||
} |
||||
|
||||
void cv::ocl::MOG2::release() |
||||
{ |
||||
frameSize_ = Size(0, 0); |
||||
frameType_ = 0; |
||||
nframes_ = 0; |
||||
|
||||
weight_.release(); |
||||
variance_.release(); |
||||
mean_.release(); |
||||
|
||||
bgmodelUsedModes_.release(); |
||||
} |
@ -0,0 +1,535 @@ |
||||
/*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 |
||||
// |
||||
// Copyright (C) 2010-2013, Multicoreware, Inc., all rights reserved. |
||||
// Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved. |
||||
// Third party copyrights are property of their respective owners. |
||||
// |
||||
// @Authors |
||||
// Jin Ma jin@multicorewareinc.com |
||||
// |
||||
// 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 oclMaterials 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*/ |
||||
|
||||
#if defined (CN1) |
||||
#define T_FRAME uchar |
||||
#define T_MEAN_VAR float |
||||
#define CONVERT_TYPE convert_uchar_sat |
||||
#define F_ZERO (0.0f) |
||||
float cvt(uchar val) |
||||
{ |
||||
return val; |
||||
} |
||||
|
||||
float sqr(float val) |
||||
{ |
||||
return val * val; |
||||
} |
||||
|
||||
float sum(float val) |
||||
{ |
||||
return val; |
||||
} |
||||
|
||||
float clamp1(float var, float learningRate, float diff, float minVar) |
||||
{ |
||||
return fmax(var + learningRate * (diff * diff - var), minVar); |
||||
} |
||||
#else |
||||
#define T_FRAME uchar4 |
||||
#define T_MEAN_VAR float4 |
||||
#define CONVERT_TYPE convert_uchar4_sat |
||||
#define F_ZERO (0.0f, 0.0f, 0.0f, 0.0f) |
||||
float4 cvt(const uchar4 val) |
||||
{ |
||||
float4 result; |
||||
result.x = val.x; |
||||
result.y = val.y; |
||||
result.z = val.z; |
||||
result.w = val.w; |
||||
|
||||
return result; |
||||
} |
||||
|
||||
float sqr(const float4 val) |
||||
{ |
||||
return val.x * val.x + val.y * val.y + val.z * val.z; |
||||
} |
||||
|
||||
float sum(const float4 val) |
||||
{ |
||||
return (val.x + val.y + val.z); |
||||
} |
||||
|
||||
float4 clamp1(const float4 var, float learningRate, const float4 diff, float minVar) |
||||
{ |
||||
float4 result; |
||||
result.x = fmax(var.x + learningRate * (diff.x * diff.x - var.x), minVar); |
||||
result.y = fmax(var.y + learningRate * (diff.y * diff.y - var.y), minVar); |
||||
result.z = fmax(var.z + learningRate * (diff.z * diff.z - var.z), minVar); |
||||
result.w = 0.0f; |
||||
return result; |
||||
} |
||||
#endif |
||||
|
||||
typedef struct |
||||
{ |
||||
float c_Tb; |
||||
float c_TB; |
||||
float c_Tg; |
||||
float c_varInit; |
||||
float c_varMin; |
||||
float c_varMax; |
||||
float c_tau; |
||||
uchar c_shadowVal; |
||||
}con_srtuct_t; |
||||
|
||||
void swap(__global float* ptr, int x, int y, int k, int rows, int ptr_step) |
||||
{ |
||||
float val = ptr[(k * rows + y) * ptr_step + x]; |
||||
ptr[(k * rows + y) * ptr_step + x] = ptr[((k + 1) * rows + y) * ptr_step + x]; |
||||
ptr[((k + 1) * rows + y) * ptr_step + x] = val; |
||||
} |
||||
|
||||
void swap4(__global float4* ptr, int x, int y, int k, int rows, int ptr_step) |
||||
{ |
||||
float4 val = ptr[(k * rows + y) * ptr_step + x]; |
||||
ptr[(k * rows + y) * ptr_step + x] = ptr[((k + 1) * rows + y) * ptr_step + x]; |
||||
ptr[((k + 1) * rows + y) * ptr_step + x] = val; |
||||
} |
||||
|
||||
__kernel void mog_withoutLearning_kernel(__global T_FRAME* frame, __global uchar* fgmask, |
||||
__global float* weight, __global T_MEAN_VAR* mean, __global T_MEAN_VAR* var, |
||||
int frame_row, int frame_col, int frame_step, int fgmask_step, |
||||
int weight_step, int mean_step, int var_step, |
||||
float varThreshold, float backgroundRatio, int fgmask_offset_x, |
||||
int fgmask_offset_y, int frame_offset_x, int frame_offset_y) |
||||
{ |
||||
int x = get_global_id(0); |
||||
int y = get_global_id(1); |
||||
|
||||
if (x < frame_col && y < frame_row) |
||||
{ |
||||
T_MEAN_VAR pix = cvt(frame[(y + frame_offset_y) * frame_step + (x + frame_offset_x)]); |
||||
|
||||
int kHit = -1; |
||||
int kForeground = -1; |
||||
|
||||
for (int k = 0; k < (NMIXTURES); ++k) |
||||
{ |
||||
if (weight[(k * frame_row + y) * weight_step + x] < 1.192092896e-07f) |
||||
break; |
||||
|
||||
T_MEAN_VAR mu = mean[(k * frame_row + y) * mean_step + x]; |
||||
T_MEAN_VAR _var = var[(k * frame_row + y) + var_step + x]; |
||||
|
||||
T_MEAN_VAR diff = pix - mu; |
||||
|
||||
if (sqr(diff) < varThreshold * sum(_var)) |
||||
{ |
||||
kHit = k; |
||||
break; |
||||
} |
||||
} |
||||
|
||||
if (kHit >= 0) |
||||
{ |
||||
float wsum = 0.0f; |
||||
for (int k = 0; k < (NMIXTURES); ++k) |
||||
{ |
||||
wsum += weight[(k * frame_row + y) * weight_step + x]; |
||||
|
||||
if (wsum > backgroundRatio) |
||||
{ |
||||
kForeground = k + 1; |
||||
break; |
||||
} |
||||
} |
||||
} |
||||
if(kHit < 0 || kHit >= kForeground) |
||||
fgmask[(y + fgmask_offset_y) * fgmask_step + (x + fgmask_offset_x)] = (uchar) (-1); |
||||
else |
||||
fgmask[(y + fgmask_offset_y) * fgmask_step + (x + fgmask_offset_x)] = (uchar) (0); |
||||
} |
||||
} |
||||
|
||||
__kernel void mog_withLearning_kernel(__global T_FRAME* frame, __global int* fgmask, |
||||
__global float* weight, __global float* sortKey, __global T_MEAN_VAR* mean, |
||||
__global T_MEAN_VAR* var, int frame_row, int frame_col, int frame_step, int fgmask_step, |
||||
int weight_step, int sortKey_step, int mean_step, int var_step, |
||||
float varThreshold, float backgroundRatio, float learningRate, float minVar, |
||||
int fgmask_offset_x, int fgmask_offset_y, int frame_offset_x, int frame_offset_y) |
||||
{ |
||||
const float w0 = 0.05f; |
||||
const float sk0 = w0 / 30.0f; |
||||
const float var0 = 900.f; |
||||
|
||||
int x = get_global_id(0); |
||||
int y = get_global_id(1); |
||||
|
||||
if(x >= frame_col || y >= frame_row) return; |
||||
float wsum = 0.0f; |
||||
int kHit = -1; |
||||
int kForeground = -1; |
||||
int k = 0; |
||||
|
||||
T_MEAN_VAR pix = cvt(frame[(y + frame_offset_y) * frame_step + (x + frame_offset_x)]); |
||||
|
||||
for (; k < (NMIXTURES); ++k) |
||||
{ |
||||
float w = weight[(k * frame_row + y) * weight_step + x]; |
||||
wsum += w; |
||||
|
||||
if (w < 1.192092896e-07f) |
||||
break; |
||||
|
||||
T_MEAN_VAR mu = mean[(k * frame_row + y) * mean_step + x]; |
||||
T_MEAN_VAR _var = var[(k * frame_row + y) * var_step + x]; |
||||
|
||||
float sortKey_prev, weight_prev; |
||||
T_MEAN_VAR mean_prev, var_prev; |
||||
if (sqr(pix - mu) < varThreshold * sum(_var)) |
||||
{ |
||||
wsum -= w; |
||||
float dw = learningRate * (1.0f - w); |
||||
|
||||
_var = clamp1(_var, learningRate, pix - mu, minVar); |
||||
|
||||
sortKey_prev = w / sqr(sum(_var)); |
||||
sortKey[(k * frame_row + y) * sortKey_step + x] = sortKey_prev; |
||||
|
||||
weight_prev = w + dw; |
||||
weight[(k * frame_row + y) * weight_step + x] = weight_prev; |
||||
|
||||
mean_prev = mu + learningRate * (pix - mu); |
||||
mean[(k * frame_row + y) * mean_step + x] = mean_prev; |
||||
|
||||
var_prev = _var; |
||||
var[(k * frame_row + y) * var_step + x] = var_prev; |
||||
} |
||||
|
||||
int k1 = k - 1; |
||||
|
||||
if (k1 >= 0 && sqr(pix - mu) < varThreshold * sum(_var)) |
||||
{ |
||||
float sortKey_next = sortKey[(k1 * frame_row + y) * sortKey_step + x]; |
||||
float weight_next = weight[(k1 * frame_row + y) * weight_step + x]; |
||||
T_MEAN_VAR mean_next = mean[(k1 * frame_row + y) * mean_step + x]; |
||||
T_MEAN_VAR var_next = var[(k1 * frame_row + y) * var_step + x]; |
||||
|
||||
for (; sortKey_next < sortKey_prev && k1 >= 0; --k1) |
||||
{ |
||||
sortKey[(k1 * frame_row + y) * sortKey_step + x] = sortKey_prev; |
||||
sortKey[((k1 + 1) * frame_row + y) * sortKey_step + x] = sortKey_next; |
||||
|
||||
weight[(k1 * frame_row + y) * weight_step + x] = weight_prev; |
||||
weight[((k1 + 1) * frame_row + y) * weight_step + x] = weight_next; |
||||
|
||||
mean[(k1 * frame_row + y) * mean_step + x] = mean_prev; |
||||
mean[((k1 + 1) * frame_row + y) * mean_step + x] = mean_next; |
||||
|
||||
var[(k1 * frame_row + y) * var_step + x] = var_prev; |
||||
var[((k1 + 1) * frame_row + y) * var_step + x] = var_next; |
||||
|
||||
sortKey_prev = sortKey_next; |
||||
sortKey_next = k1 > 0 ? sortKey[((k1 - 1) * frame_row + y) * sortKey_step + x] : 0.0f; |
||||
|
||||
weight_prev = weight_next; |
||||
weight_next = k1 > 0 ? weight[((k1 - 1) * frame_row + y) * weight_step + x] : 0.0f; |
||||
|
||||
mean_prev = mean_next; |
||||
mean_next = k1 > 0 ? mean[((k1 - 1) * frame_row + y) * mean_step + x] : (T_MEAN_VAR)F_ZERO; |
||||
|
||||
var_prev = var_next; |
||||
var_next = k1 > 0 ? var[((k1 - 1) * frame_row + y) * var_step + x] : (T_MEAN_VAR)F_ZERO; |
||||
} |
||||
} |
||||
|
||||
kHit = k1 + 1; |
||||
break; |
||||
} |
||||
|
||||
if (kHit < 0) |
||||
{ |
||||
kHit = k = k < ((NMIXTURES) - 1) ? k : ((NMIXTURES) - 1); |
||||
wsum += w0 - weight[(k * frame_row + y) * weight_step + x]; |
||||
|
||||
weight[(k * frame_row + y) * weight_step + x] = w0; |
||||
mean[(k * frame_row + y) * mean_step + x] = pix; |
||||
#if defined (CN1) |
||||
var[(k * frame_row + y) * var_step + x] = (T_MEAN_VAR)(var0); |
||||
#else |
||||
var[(k * frame_row + y) * var_step + x] = (T_MEAN_VAR)(var0, var0, var0, var0); |
||||
#endif |
||||
sortKey[(k * frame_row + y) * sortKey_step + x] = sk0; |
||||
} |
||||
else |
||||
{ |
||||
for( ; k < (NMIXTURES); k++) |
||||
wsum += weight[(k * frame_row + y) * weight_step + x]; |
||||
} |
||||
|
||||
float wscale = 1.0f / wsum; |
||||
wsum = 0; |
||||
for (k = 0; k < (NMIXTURES); ++k) |
||||
{ |
||||
float w = weight[(k * frame_row + y) * weight_step + x]; |
||||
w *= wscale; |
||||
wsum += w; |
||||
|
||||
weight[(k * frame_row + y) * weight_step + x] = w; |
||||
sortKey[(k * frame_row + y) * sortKey_step + x] *= wscale; |
||||
|
||||
kForeground = select(kForeground, k + 1, wsum > backgroundRatio && kForeground < 0); |
||||
} |
||||
fgmask[(y + fgmask_offset_y) * fgmask_step + (x + fgmask_offset_x)] = (uchar)(-(kHit >= kForeground)); |
||||
} |
||||
|
||||
|
||||
__kernel void getBackgroundImage_kernel(__global float* weight, __global T_MEAN_VAR* mean, __global T_FRAME* dst, |
||||
int dst_row, int dst_col, int weight_step, int mean_step, int dst_step, |
||||
float backgroundRatio) |
||||
{ |
||||
int x = get_global_id(0); |
||||
int y = get_global_id(1); |
||||
|
||||
if(x < dst_col && y < dst_row) |
||||
{ |
||||
T_MEAN_VAR meanVal = (T_MEAN_VAR)F_ZERO; |
||||
float totalWeight = 0.0f; |
||||
|
||||
for (int mode = 0; mode < (NMIXTURES); ++mode) |
||||
{ |
||||
float _weight = weight[(mode * dst_row + y) * weight_step + x]; |
||||
|
||||
T_MEAN_VAR _mean = mean[(mode * dst_row + y) * mean_step + x]; |
||||
meanVal = meanVal + _weight * _mean; |
||||
|
||||
totalWeight += _weight; |
||||
|
||||
if(totalWeight > backgroundRatio) |
||||
break; |
||||
} |
||||
meanVal = meanVal * (1.f / totalWeight); |
||||
dst[y * dst_step + x] = CONVERT_TYPE(meanVal); |
||||
} |
||||
} |
||||
|
||||
__kernel void mog2_kernel(__global T_FRAME * frame, __global int* fgmask, __global float* weight, __global T_MEAN_VAR * mean, |
||||
__global int* modesUsed, __global float* variance, int frame_row, int frame_col, int frame_step, |
||||
int fgmask_step, int weight_step, int mean_step, int modesUsed_step, int var_step, float alphaT, float alpha1, float prune, |
||||
int detectShadows_flag, int fgmask_offset_x, int fgmask_offset_y, int frame_offset_x, int frame_offset_y, __constant con_srtuct_t* constants) |
||||
{ |
||||
int x = get_global_id(0); |
||||
int y = get_global_id(1); |
||||
|
||||
if(x < frame_col && y < frame_row) |
||||
{ |
||||
T_MEAN_VAR pix = cvt(frame[(y + frame_offset_y) * frame_step + x + frame_offset_x]); |
||||
|
||||
bool background = false; // true - the pixel classified as background |
||||
|
||||
bool fitsPDF = false; //if it remains zero a new GMM mode will be added |
||||
|
||||
int nmodes = modesUsed[y * modesUsed_step + x]; |
||||
int nNewModes = nmodes; //current number of modes in GMM |
||||
|
||||
float totalWeight = 0.0f; |
||||
|
||||
for (int mode = 0; mode < nmodes; ++mode) |
||||
{ |
||||
float _weight = alpha1 * weight[(mode * frame_row + y) * weight_step + x] + prune; |
||||
|
||||
if (!fitsPDF) |
||||
{ |
||||
float var = variance[(mode * frame_row + y) * var_step + x]; |
||||
|
||||
T_MEAN_VAR _mean = mean[(mode * frame_row + y) * mean_step + x]; |
||||
|
||||
T_MEAN_VAR diff = _mean - pix; |
||||
float dist2 = sqr(diff); |
||||
|
||||
if (totalWeight < constants -> c_TB && dist2 < constants -> c_Tb * var) |
||||
background = true; |
||||
|
||||
if (dist2 < constants -> c_Tg * var) |
||||
{ |
||||
fitsPDF = true; |
||||
_weight += alphaT; |
||||
float k = alphaT / _weight; |
||||
mean[(mode * frame_row + y) * mean_step + x] = _mean - k * diff; |
||||
float varnew = var + k * (dist2 - var); |
||||
varnew = fmax(varnew, constants -> c_varMin); |
||||
varnew = fmin(varnew, constants -> c_varMax); |
||||
|
||||
variance[(mode * frame_row + y) * var_step + x] = varnew; |
||||
for (int i = mode; i > 0; --i) |
||||
{ |
||||
if (_weight < weight[((i - 1) * frame_row + y) * weight_step + x]) |
||||
break; |
||||
swap(weight, x, y, i - 1, frame_row, weight_step); |
||||
swap(variance, x, y, i - 1, frame_row, var_step); |
||||
#if defined (CN1) |
||||
swap(mean, x, y, i - 1, frame_row, mean_step); |
||||
#else |
||||
swap4(mean, x, y, i - 1, frame_row, mean_step); |
||||
#endif |
||||
} |
||||
} |
||||
} // !fitsPDF |
||||
|
||||
if (_weight < -prune) |
||||
{ |
||||
_weight = 0.0; |
||||
nmodes--; |
||||
} |
||||
|
||||
weight[(mode * frame_row + y) * weight_step + x] = _weight; //update weight by the calculated value |
||||
totalWeight += _weight; |
||||
} |
||||
|
||||
totalWeight = 1.f / totalWeight; |
||||
for (int mode = 0; mode < nmodes; ++mode) |
||||
weight[(mode * frame_row + y) * weight_step + x] *= totalWeight; |
||||
|
||||
nmodes = nNewModes; |
||||
|
||||
if (!fitsPDF) |
||||
{ |
||||
int mode = nmodes == (NMIXTURES) ? (NMIXTURES) - 1 : nmodes++; |
||||
|
||||
if (nmodes == 1) |
||||
weight[(mode * frame_row + y) * weight_step + x] = 1.f; |
||||
else |
||||
{ |
||||
weight[(mode * frame_row + y) * weight_step + x] = alphaT; |
||||
|
||||
for (int i = 0; i < nmodes - 1; ++i) |
||||
weight[(i * frame_row + y) * weight_step + x] *= alpha1; |
||||
} |
||||
|
||||
mean[(mode * frame_row + y) * mean_step + x] = pix; |
||||
variance[(mode * frame_row + y) * var_step + x] = constants -> c_varInit; |
||||
|
||||
for (int i = nmodes - 1; i > 0; --i) |
||||
{ |
||||
// check one up |
||||
if (alphaT < weight[((i - 1) * frame_row + y) * weight_step + x]) |
||||
break; |
||||
|
||||
swap(weight, x, y, i - 1, frame_row, weight_step); |
||||
swap(variance, x, y, i - 1, frame_row, var_step); |
||||
#if defined (CN1) |
||||
swap(mean, x, y, i - 1, frame_row, mean_step); |
||||
#else |
||||
swap4(mean, x, y, i - 1, frame_row, mean_step); |
||||
#endif |
||||
} |
||||
} |
||||
|
||||
modesUsed[y * modesUsed_step + x] = nmodes; |
||||
|
||||
bool isShadow = false; |
||||
if (detectShadows_flag && !background) |
||||
{ |
||||
float tWeight = 0.0f; |
||||
|
||||
for (int mode = 0; mode < nmodes; ++mode) |
||||
{ |
||||
T_MEAN_VAR _mean = mean[(mode * frame_row + y) * mean_step + x]; |
||||
|
||||
T_MEAN_VAR pix_mean = pix * _mean; |
||||
|
||||
float numerator = sum(pix_mean); |
||||
float denominator = sqr(_mean); |
||||
|
||||
if (denominator == 0) |
||||
break; |
||||
|
||||
if (numerator <= denominator && numerator >= constants -> c_tau * denominator) |
||||
{ |
||||
float a = numerator / denominator; |
||||
|
||||
T_MEAN_VAR dD = a * _mean - pix; |
||||
|
||||
if (sqr(dD) < constants -> c_Tb * variance[(mode * frame_row + y) * var_step + x] * a * a) |
||||
{ |
||||
isShadow = true; |
||||
break; |
||||
} |
||||
} |
||||
|
||||
tWeight += weight[(mode * frame_row + y) * weight_step + x]; |
||||
if (tWeight > constants -> c_TB) |
||||
break; |
||||
} |
||||
} |
||||
|
||||
fgmask[(y + fgmask_offset_y) * fgmask_step + x + fgmask_offset_x] = background ? 0 : isShadow ? constants -> c_shadowVal : 255; |
||||
} |
||||
} |
||||
|
||||
__kernel void getBackgroundImage2_kernel(__global int* modesUsed, __global float* weight, __global T_MEAN_VAR* mean, |
||||
__global T_FRAME* dst, float c_TB, int modesUsed_row, int modesUsed_col, int modesUsed_step, int weight_step, |
||||
int mean_step, int dst_step, int dst_x, int dst_y) |
||||
{ |
||||
int x = get_global_id(0); |
||||
int y = get_global_id(1); |
||||
|
||||
if(x < modesUsed_col && y < modesUsed_row) |
||||
{ |
||||
int nmodes = modesUsed[y * modesUsed_step + x]; |
||||
|
||||
T_MEAN_VAR meanVal = (T_MEAN_VAR)F_ZERO; |
||||
|
||||
float totalWeight = 0.0f; |
||||
|
||||
for (int mode = 0; mode < nmodes; ++mode) |
||||
{ |
||||
float _weight = weight[(mode * modesUsed_row + y) * weight_step + x]; |
||||
|
||||
T_MEAN_VAR _mean = mean[(mode * modesUsed_row + y) * mean_step + x]; |
||||
meanVal = meanVal + _weight * _mean; |
||||
|
||||
totalWeight += _weight; |
||||
|
||||
if(totalWeight > c_TB) |
||||
break; |
||||
} |
||||
|
||||
meanVal = meanVal * (1.f / totalWeight); |
||||
dst[(y + dst_y) * dst_step + x + dst_x] = CONVERT_TYPE(meanVal); |
||||
} |
||||
} |
@ -0,0 +1,424 @@ |
||||
/*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 |
||||
// |
||||
// Copyright (C) 2010-2013, Multicoreware, Inc., all rights reserved. |
||||
// Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved. |
||||
// Third party copyrights are property of their respective owners. |
||||
// |
||||
// @Authors |
||||
// Harris Gasparakis, harris.gasparakis@amd.com |
||||
// Xiaopeng Fu, fuxiaopeng2222@163.com |
||||
// Yao Wang, bitwangyaoyao@gmail.com |
||||
// |
||||
// 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 oclMaterials 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*/ |
||||
|
||||
|
||||
#ifdef BORDER_REPLICATE |
||||
//BORDER_REPLICATE: aaaaaa|abcdefgh|hhhhhhh |
||||
#define ADDR_L(i, l_edge, r_edge) ((i) < (l_edge) ? (l_edge) : (i)) |
||||
#define ADDR_R(i, r_edge, addr) ((i) >= (r_edge) ? (r_edge)-1 : (addr)) |
||||
#define ADDR_H(i, t_edge, b_edge) ((i) < (t_edge) ? (t_edge) :(i)) |
||||
#define ADDR_B(i, b_edge, addr) ((i) >= (b_edge) ? (b_edge)-1 :(addr)) |
||||
#endif |
||||
|
||||
#ifdef BORDER_REFLECT |
||||
//BORDER_REFLECT: fedcba|abcdefgh|hgfedcb |
||||
#define ADDR_L(i, l_edge, r_edge) ((i) < (l_edge) ? -(i)-1 : (i)) |
||||
#define ADDR_R(i, r_edge, addr) ((i) >= (r_edge) ? -(i)-1+((r_edge)<<1) : (addr)) |
||||
#define ADDR_H(i, t_edge, b_edge) ((i) < (t_edge) ? -(i)-1 : (i)) |
||||
#define ADDR_B(i, b_edge, addr) ((i) >= (b_edge) ? -(i)-1+((b_edge)<<1) : (addr)) |
||||
#endif |
||||
|
||||
#ifdef BORDER_REFLECT_101 |
||||
//BORDER_REFLECT_101: gfedcb|abcdefgh|gfedcba |
||||
#define ADDR_L(i, l_edge, r_edge) ((i) < (l_edge) ? -(i) : (i)) |
||||
#define ADDR_R(i, r_edge, addr) ((i) >= (r_edge) ? -(i)-2+((r_edge)<<1) : (addr)) |
||||
#define ADDR_H(i, t_edge, b_edge) ((i) < (t_edge) ? -(i) : (i)) |
||||
#define ADDR_B(i, b_edge, addr) ((i) >= (b_edge) ? -(i)-2+((b_edge)<<1) : (addr)) |
||||
#endif |
||||
|
||||
//blur function does not support BORDER_WRAP |
||||
#ifdef BORDER_WRAP |
||||
//BORDER_WRAP: cdefgh|abcdefgh|abcdefg |
||||
#define ADDR_L(i, l_edge, r_edge) ((i) < (l_edge) ? (i)+(r_edge) : (i)) |
||||
#define ADDR_R(i, r_edge, addr) ((i) >= (r_edge) ? (i)-(r_edge) : (addr)) |
||||
#define ADDR_H(i, t_edge, b_edge) ((i) < (t_edge) ? (i)+(b_edge) : (i)) |
||||
#define ADDR_B(i, b_edge, addr) ((i) >= (b_edge) ? (i)-(b_edge) : (addr)) |
||||
#endif |
||||
|
||||
__kernel void |
||||
edgeEnhancingFilter_C4_D0( |
||||
__global const uchar4 * restrict src, |
||||
__global uchar4 *dst, |
||||
float alpha, |
||||
int src_offset, |
||||
int src_whole_rows, |
||||
int src_whole_cols, |
||||
int src_step, |
||||
int dst_offset, |
||||
int dst_rows, |
||||
int dst_cols, |
||||
int dst_step, |
||||
__global const float* lut, |
||||
int lut_step) |
||||
{ |
||||
int col = get_local_id(0); |
||||
const int gX = get_group_id(0); |
||||
const int gY = get_group_id(1); |
||||
|
||||
int src_x_off = (src_offset % src_step) >> 2; |
||||
int src_y_off = src_offset / src_step; |
||||
int dst_x_off = (dst_offset % dst_step) >> 2; |
||||
int dst_y_off = dst_offset / dst_step; |
||||
|
||||
int startX = gX * (THREADS-ksX+1) - anX + src_x_off; |
||||
int startY = (gY * (1+EXTRA)) - anY + src_y_off; |
||||
|
||||
int dst_startX = gX * (THREADS-ksX+1) + dst_x_off; |
||||
int dst_startY = (gY * (1+EXTRA)) + dst_y_off; |
||||
|
||||
int posX = dst_startX - dst_x_off + col; |
||||
int posY = (gY * (1+EXTRA)) ; |
||||
|
||||
__local uchar4 data[ksY+EXTRA][THREADS]; |
||||
|
||||
float4 tmp_sum[1+EXTRA]; |
||||
for(int tmpint = 0; tmpint < 1+EXTRA; tmpint++) |
||||
{ |
||||
tmp_sum[tmpint] = (float4)(0,0,0,0); |
||||
} |
||||
|
||||
#ifdef BORDER_CONSTANT |
||||
bool con; |
||||
uchar4 ss; |
||||
for(int j = 0; j < ksY+EXTRA; j++) |
||||
{ |
||||
con = (startX+col >= 0 && startX+col < src_whole_cols && startY+j >= 0 && startY+j < src_whole_rows); |
||||
|
||||
int cur_col = clamp(startX + col, 0, src_whole_cols); |
||||
if(con) |
||||
{ |
||||
ss = src[(startY+j)*(src_step>>2) + cur_col]; |
||||
} |
||||
|
||||
data[j][col] = con ? ss : (uchar4)0; |
||||
} |
||||
#else |
||||
for(int j= 0; j < ksY+EXTRA; j++) |
||||
{ |
||||
int selected_row; |
||||
int selected_col; |
||||
selected_row = ADDR_H(startY+j, 0, src_whole_rows); |
||||
selected_row = ADDR_B(startY+j, src_whole_rows, selected_row); |
||||
|
||||
selected_col = ADDR_L(startX+col, 0, src_whole_cols); |
||||
selected_col = ADDR_R(startX+col, src_whole_cols, selected_col); |
||||
|
||||
data[j][col] = src[selected_row * (src_step>>2) + selected_col]; |
||||
} |
||||
#endif |
||||
|
||||
barrier(CLK_LOCAL_MEM_FENCE); |
||||
|
||||
float4 var[1+EXTRA]; |
||||
|
||||
#if VAR_PER_CHANNEL |
||||
float4 weight; |
||||
float4 totalWeight = (float4)(0,0,0,0); |
||||
#else |
||||
float weight; |
||||
float totalWeight = 0; |
||||
#endif |
||||
|
||||
int4 currValCenter; |
||||
int4 currWRTCenter; |
||||
|
||||
int4 sumVal = 0; |
||||
int4 sumValSqr = 0; |
||||
|
||||
if(col < (THREADS-(ksX-1))) |
||||
{ |
||||
int4 currVal; |
||||
|
||||
int howManyAll = (2*anX+1)*(ksY); |
||||
|
||||
//find variance of all data |
||||
int startLMj; |
||||
int endLMj ; |
||||
#if CALCVAR |
||||
// Top row: don't sum the very last element |
||||
for(int extraCnt = 0; extraCnt <=EXTRA; extraCnt++) |
||||
{ |
||||
startLMj = extraCnt; |
||||
endLMj = ksY+extraCnt-1; |
||||
sumVal =0; |
||||
sumValSqr=0; |
||||
for(int j = startLMj; j < endLMj; j++) |
||||
{ |
||||
for(int i=-anX; i<=anX; i++) |
||||
{ |
||||
currVal = convert_int4(data[j][col+anX+i]) ; |
||||
|
||||
sumVal += currVal; |
||||
sumValSqr += mul24(currVal, currVal); |
||||
} |
||||
} |
||||
var[extraCnt] = convert_float4( ( (sumValSqr * howManyAll)- mul24(sumVal , sumVal) ) ) / ( (float)(howManyAll*howManyAll) ) ; |
||||
#else |
||||
var[extraCnt] = (float4)(900.0, 900.0, 900.0, 0.0); |
||||
#endif |
||||
} |
||||
|
||||
for(int extraCnt = 0; extraCnt <= EXTRA; extraCnt++) |
||||
{ |
||||
|
||||
// top row: include the very first element, even on first time |
||||
startLMj = extraCnt; |
||||
// go all the way, unless this is the last local mem chunk, |
||||
// then stay within limits - 1 |
||||
endLMj = extraCnt + ksY; |
||||
|
||||
// Top row: don't sum the very last element |
||||
currValCenter = convert_int4( data[ (startLMj + endLMj)/2][col+anX] ); |
||||
|
||||
for(int j = startLMj, lut_j = 0; j < endLMj; j++, lut_j++) |
||||
{ |
||||
for(int i=-anX; i<=anX; i++) |
||||
{ |
||||
#if FIXED_WEIGHT |
||||
#if VAR_PER_CHANNEL |
||||
weight.x = 1.0f; |
||||
weight.y = 1.0f; |
||||
weight.z = 1.0f; |
||||
weight.w = 1.0f; |
||||
#else |
||||
weight = 1.0f; |
||||
#endif |
||||
#else |
||||
currVal = convert_int4(data[j][col+anX+i]) ; |
||||
currWRTCenter = currVal-currValCenter; |
||||
|
||||
#if VAR_PER_CHANNEL |
||||
weight = var[extraCnt] / (var[extraCnt] + convert_float4(currWRTCenter * currWRTCenter)) * (float4)(lut[lut_j*lut_step+anX+i]); |
||||
//weight.x = var[extraCnt].x / ( var[extraCnt].x + (float) mul24(currWRTCenter.x , currWRTCenter.x) ) ; |
||||
//weight.y = var[extraCnt].y / ( var[extraCnt].y + (float) mul24(currWRTCenter.y , currWRTCenter.y) ) ; |
||||
//weight.z = var[extraCnt].z / ( var[extraCnt].z + (float) mul24(currWRTCenter.z , currWRTCenter.z) ) ; |
||||
//weight.w = 0; |
||||
#else |
||||
weight = 1.0f/(1.0f+( mul24(currWRTCenter.x, currWRTCenter.x) + mul24(currWRTCenter.y, currWRTCenter.y) + mul24(currWRTCenter.z, currWRTCenter.z))/(var.x+var.y+var.z)); |
||||
#endif |
||||
#endif |
||||
tmp_sum[extraCnt] += convert_float4(data[j][col+anX+i]) * weight; |
||||
totalWeight += weight; |
||||
} |
||||
} |
||||
|
||||
tmp_sum[extraCnt] /= totalWeight; |
||||
|
||||
if(posX >= 0 && posX < dst_cols && (posY+extraCnt) >= 0 && (posY+extraCnt) < dst_rows) |
||||
{ |
||||
dst[(dst_startY+extraCnt) * (dst_step>>2)+ dst_startX + col] = convert_uchar4(tmp_sum[extraCnt]); |
||||
} |
||||
|
||||
#if VAR_PER_CHANNEL |
||||
totalWeight = (float4)(0,0,0,0); |
||||
#else |
||||
totalWeight = 0; |
||||
#endif |
||||
} |
||||
} |
||||
} |
||||
|
||||
|
||||
__kernel void |
||||
edgeEnhancingFilter_C1_D0( |
||||
__global const uchar * restrict src, |
||||
__global uchar *dst, |
||||
float alpha, |
||||
int src_offset, |
||||
int src_whole_rows, |
||||
int src_whole_cols, |
||||
int src_step, |
||||
int dst_offset, |
||||
int dst_rows, |
||||
int dst_cols, |
||||
int dst_step, |
||||
__global const float * lut, |
||||
int lut_step) |
||||
{ |
||||
int col = get_local_id(0); |
||||
const int gX = get_group_id(0); |
||||
const int gY = get_group_id(1); |
||||
|
||||
int src_x_off = (src_offset % src_step); |
||||
int src_y_off = src_offset / src_step; |
||||
int dst_x_off = (dst_offset % dst_step); |
||||
int dst_y_off = dst_offset / dst_step; |
||||
|
||||
int startX = gX * (THREADS-ksX+1) - anX + src_x_off; |
||||
int startY = (gY * (1+EXTRA)) - anY + src_y_off; |
||||
|
||||
int dst_startX = gX * (THREADS-ksX+1) + dst_x_off; |
||||
int dst_startY = (gY * (1+EXTRA)) + dst_y_off; |
||||
|
||||
int posX = dst_startX - dst_x_off + col; |
||||
int posY = (gY * (1+EXTRA)) ; |
||||
|
||||
__local uchar data[ksY+EXTRA][THREADS]; |
||||
|
||||
float tmp_sum[1+EXTRA]; |
||||
for(int tmpint = 0; tmpint < 1+EXTRA; tmpint++) |
||||
{ |
||||
tmp_sum[tmpint] = (float)(0); |
||||
} |
||||
|
||||
#ifdef BORDER_CONSTANT |
||||
bool con; |
||||
uchar ss; |
||||
for(int j = 0; j < ksY+EXTRA; j++) |
||||
{ |
||||
con = (startX+col >= 0 && startX+col < src_whole_cols && startY+j >= 0 && startY+j < src_whole_rows); |
||||
|
||||
int cur_col = clamp(startX + col, 0, src_whole_cols); |
||||
if(con) |
||||
{ |
||||
ss = src[(startY+j)*(src_step) + cur_col]; |
||||
} |
||||
|
||||
data[j][col] = con ? ss : 0; |
||||
} |
||||
#else |
||||
for(int j= 0; j < ksY+EXTRA; j++) |
||||
{ |
||||
int selected_row; |
||||
int selected_col; |
||||
selected_row = ADDR_H(startY+j, 0, src_whole_rows); |
||||
selected_row = ADDR_B(startY+j, src_whole_rows, selected_row); |
||||
|
||||
selected_col = ADDR_L(startX+col, 0, src_whole_cols); |
||||
selected_col = ADDR_R(startX+col, src_whole_cols, selected_col); |
||||
|
||||
data[j][col] = src[selected_row * (src_step) + selected_col]; |
||||
} |
||||
#endif |
||||
|
||||
barrier(CLK_LOCAL_MEM_FENCE); |
||||
|
||||
float var[1+EXTRA]; |
||||
|
||||
float weight; |
||||
float totalWeight = 0; |
||||
|
||||
int currValCenter; |
||||
int currWRTCenter; |
||||
|
||||
int sumVal = 0; |
||||
int sumValSqr = 0; |
||||
|
||||
if(col < (THREADS-(ksX-1))) |
||||
{ |
||||
int currVal; |
||||
|
||||
int howManyAll = (2*anX+1)*(ksY); |
||||
|
||||
//find variance of all data |
||||
int startLMj; |
||||
int endLMj; |
||||
#if CALCVAR |
||||
// Top row: don't sum the very last element |
||||
for(int extraCnt=0; extraCnt<=EXTRA; extraCnt++) |
||||
{ |
||||
startLMj = extraCnt; |
||||
endLMj = ksY+extraCnt-1; |
||||
sumVal = 0; |
||||
sumValSqr =0; |
||||
for(int j = startLMj; j < endLMj; j++) |
||||
{ |
||||
for(int i=-anX; i<=anX; i++) |
||||
{ |
||||
currVal = (uint)(data[j][col+anX+i]) ; |
||||
|
||||
sumVal += currVal; |
||||
sumValSqr += mul24(currVal, currVal); |
||||
} |
||||
} |
||||
var[extraCnt] = (float)( ( (sumValSqr * howManyAll)- mul24(sumVal , sumVal) ) ) / ( (float)(howManyAll*howManyAll) ) ; |
||||
#else |
||||
var[extraCnt] = (float)(900.0); |
||||
#endif |
||||
} |
||||
|
||||
for(int extraCnt = 0; extraCnt <= EXTRA; extraCnt++) |
||||
{ |
||||
|
||||
// top row: include the very first element, even on first time |
||||
startLMj = extraCnt; |
||||
// go all the way, unless this is the last local mem chunk, |
||||
// then stay within limits - 1 |
||||
endLMj = extraCnt + ksY; |
||||
|
||||
// Top row: don't sum the very last element |
||||
currValCenter = (int)( data[ (startLMj + endLMj)/2][col+anX] ); |
||||
|
||||
for(int j = startLMj, lut_j = 0; j < endLMj; j++, lut_j++) |
||||
{ |
||||
for(int i=-anX; i<=anX; i++) |
||||
{ |
||||
#if FIXED_WEIGHT |
||||
weight = 1.0f; |
||||
#else |
||||
currVal = (int)(data[j][col+anX+i]) ; |
||||
currWRTCenter = currVal-currValCenter; |
||||
|
||||
weight = var[extraCnt] / (var[extraCnt] + (float)mul24(currWRTCenter,currWRTCenter)) * lut[lut_j*lut_step+anX+i] ; |
||||
#endif |
||||
tmp_sum[extraCnt] += (float)(data[j][col+anX+i] * weight); |
||||
totalWeight += weight; |
||||
} |
||||
} |
||||
|
||||
tmp_sum[extraCnt] /= totalWeight; |
||||
|
||||
|
||||
if(posX >= 0 && posX < dst_cols && (posY+extraCnt) >= 0 && (posY+extraCnt) < dst_rows) |
||||
{ |
||||
dst[(dst_startY+extraCnt) * (dst_step)+ dst_startX + col] = (uchar)(tmp_sum[extraCnt]); |
||||
} |
||||
|
||||
totalWeight = 0; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,227 @@ |
||||
/*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
|
||||
//
|
||||
// Copyright (C) 2010-2013, Multicoreware, Inc., all rights reserved.
|
||||
// Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved.
|
||||
// Third party copyrights are property of their respective owners.
|
||||
//
|
||||
// @Authors
|
||||
// Jin Ma, jin@multicorewareinc.com
|
||||
//
|
||||
// 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 oclMaterials 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" |
||||
|
||||
#ifdef HAVE_OPENCL |
||||
|
||||
using namespace cv; |
||||
using namespace cv::ocl; |
||||
using namespace cvtest; |
||||
using namespace testing; |
||||
using namespace std; |
||||
|
||||
extern string workdir; |
||||
//////////////////////////////////////////////////////
|
||||
// MOG
|
||||
|
||||
namespace |
||||
{ |
||||
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() |
||||
{ |
||||
useGray = GET_PARAM(0); |
||||
|
||||
learningRate = GET_PARAM(1); |
||||
|
||||
useRoi = GET_PARAM(2); |
||||
} |
||||
}; |
||||
|
||||
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(frame.size(), CV_8UC1, useRoi); |
||||
|
||||
Ptr<cv::BackgroundSubtractorMOG> mog_gold = createBackgroundSubtractorMOG(); |
||||
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(frame, useRoi), foreground, (float)learningRate); |
||||
|
||||
mog_gold->apply(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 |
||||
{ |
||||
IMPLEMENT_PARAM_CLASS(DetectShadow, bool) |
||||
} |
||||
|
||||
PARAM_TEST_CASE(mog2, UseGray, DetectShadow, bool) |
||||
{ |
||||
bool useGray; |
||||
bool detectShadow; |
||||
bool useRoi; |
||||
virtual void SetUp() |
||||
{ |
||||
useGray = GET_PARAM(0); |
||||
detectShadow = GET_PARAM(1); |
||||
useRoi = GET_PARAM(2); |
||||
} |
||||
}; |
||||
|
||||
TEST_P(mog2, 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::MOG2 mog2; |
||||
mog2.bShadowDetection = detectShadow; |
||||
cv::ocl::oclMat foreground = createMat_ocl(frame.size(), CV_8UC1, useRoi); |
||||
|
||||
cv::Ptr<cv::BackgroundSubtractorMOG2> mog2_gold = createBackgroundSubtractorMOG2(); |
||||
mog2_gold->set("detectShadows", detectShadow); |
||||
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); |
||||
} |
||||
|
||||
mog2(loadMat_ocl(frame, useRoi), foreground); |
||||
|
||||
mog2_gold->apply(frame, foreground_gold); |
||||
|
||||
if (detectShadow) |
||||
EXPECT_MAT_SIMILAR(foreground_gold, foreground, 15e-3) |
||||
else |
||||
EXPECT_MAT_NEAR(foreground_gold, foreground, 0) |
||||
} |
||||
} |
||||
|
||||
TEST_P(mog2, getBackgroundImage) |
||||
{ |
||||
if (useGray) |
||||
return; |
||||
|
||||
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; |
||||
|
||||
cv::ocl::MOG2 mog2; |
||||
mog2.bShadowDetection = detectShadow; |
||||
cv::ocl::oclMat foreground; |
||||
|
||||
cv::Ptr<cv::BackgroundSubtractorMOG2> mog2_gold = createBackgroundSubtractorMOG2(); |
||||
mog2_gold->set("detectShadows", detectShadow); |
||||
cv::Mat foreground_gold; |
||||
|
||||
for (int i = 0; i < 10; ++i) |
||||
{ |
||||
cap >> frame; |
||||
ASSERT_FALSE(frame.empty()); |
||||
|
||||
mog2(loadMat_ocl(frame, useRoi), foreground); |
||||
|
||||
mog2_gold->apply(frame, foreground_gold); |
||||
} |
||||
|
||||
cv::ocl::oclMat background = createMat_ocl(frame.size(), frame.type(), useRoi); |
||||
mog2.getBackgroundImage(background); |
||||
|
||||
cv::Mat background_gold; |
||||
mog2_gold->getBackgroundImage(background_gold); |
||||
|
||||
EXPECT_MAT_NEAR(background_gold, background, 1.0); |
||||
} |
||||
|
||||
INSTANTIATE_TEST_CASE_P(OCL_Video, mog2, testing::Combine( |
||||
testing::Values(UseGray(true), UseGray(false)), |
||||
testing::Values(DetectShadow(true), DetectShadow(false)), |
||||
Values(true, false))); |
||||
|
||||
#endif |
@ -0,0 +1,161 @@ |
||||
Downhill Simplex Method |
||||
======================= |
||||
|
||||
.. highlight:: cpp |
||||
|
||||
optim::DownhillSolver |
||||
--------------------------------- |
||||
|
||||
.. ocv:class:: optim::DownhillSolver |
||||
|
||||
This class is used to perform the non-linear non-constrained *minimization* of a function, given on an *n*-dimensional Euclidean space, |
||||
using the **Nelder-Mead method**, also known as **downhill simplex method**. The basic idea about the method can be obtained from |
||||
(`http://en.wikipedia.org/wiki/Nelder-Mead\_method <http://en.wikipedia.org/wiki/Nelder-Mead_method>`_). It should be noted, that |
||||
this method, although deterministic, is rather a heuristic and therefore may converge to a local minima, not necessary a global one. |
||||
It is iterative optimization technique, which at each step uses an information about the values of a function evaluated only at |
||||
*n+1* points, arranged as a *simplex* in *n*-dimensional space (hence the second name of the method). At each step new point is |
||||
chosen to evaluate function at, obtained value is compared with previous ones and based on this information simplex changes it's shape |
||||
, slowly moving to the local minimum. |
||||
|
||||
Algorithm stops when the number of function evaluations done exceeds ``termcrit.maxCount``, when the function values at the |
||||
vertices of simplex are within ``termcrit.epsilon`` range or simplex becomes so small that it |
||||
can enclosed in a box with ``termcrit.epsilon`` sides, whatever comes first, for some defined by user |
||||
positive integer ``termcrit.maxCount`` and positive non-integer ``termcrit.epsilon``. |
||||
|
||||
:: |
||||
|
||||
class CV_EXPORTS Solver : public Algorithm |
||||
{ |
||||
public: |
||||
class CV_EXPORTS Function |
||||
{ |
||||
public: |
||||
virtual ~Function() {} |
||||
//! ndim - dimensionality |
||||
virtual double calc(const double* x) const = 0; |
||||
}; |
||||
|
||||
virtual Ptr<Function> getFunction() const = 0; |
||||
virtual void setFunction(const Ptr<Function>& f) = 0; |
||||
|
||||
virtual TermCriteria getTermCriteria() const = 0; |
||||
virtual void setTermCriteria(const TermCriteria& termcrit) = 0; |
||||
|
||||
// x contain the initial point before the call and the minima position (if algorithm converged) after. x is assumed to be (something that |
||||
// after getMat() will return) row-vector or column-vector. *It's size and should |
||||
// be consisted with previous dimensionality data given, if any (otherwise, it determines dimensionality)* |
||||
virtual double minimize(InputOutputArray x) = 0; |
||||
}; |
||||
|
||||
class CV_EXPORTS DownhillSolver : public Solver |
||||
{ |
||||
public: |
||||
//! returns row-vector, even if the column-vector was given |
||||
virtual void getInitStep(OutputArray step) const=0; |
||||
//!This should be called at least once before the first call to minimize() and step is assumed to be (something that |
||||
//! after getMat() will return) row-vector or column-vector. *It's dimensionality determines the dimensionality of a problem.* |
||||
virtual void setInitStep(InputArray step)=0; |
||||
}; |
||||
|
||||
It should be noted, that ``optim::DownhillSolver`` is a derivative of the abstract interface ``optim::Solver``, which in |
||||
turn is derived from the ``Algorithm`` interface and is used to encapsulate the functionality, common to all non-linear optimization |
||||
algorithms in the ``optim`` module. |
||||
|
||||
optim::DownhillSolver::getFunction |
||||
-------------------------------------------- |
||||
|
||||
Getter for the optimized function. The optimized function is represented by ``Solver::Function`` interface, which requires |
||||
derivatives to implement the sole method ``calc(double*)`` to evaluate the function. |
||||
|
||||
.. ocv:function:: Ptr<Solver::Function> optim::DownhillSolver::getFunction() |
||||
|
||||
:return: Smart-pointer to an object that implements ``Solver::Function`` interface - it represents the function that is being optimized. It can be empty, if no function was given so far. |
||||
|
||||
optim::DownhillSolver::setFunction |
||||
----------------------------------------------- |
||||
|
||||
Setter for the optimized function. *It should be called at least once before the call to* ``DownhillSolver::minimize()``, as |
||||
default value is not usable. |
||||
|
||||
.. ocv:function:: void optim::DownhillSolver::setFunction(const Ptr<Solver::Function>& f) |
||||
|
||||
:param f: The new function to optimize. |
||||
|
||||
optim::DownhillSolver::getTermCriteria |
||||
---------------------------------------------------- |
||||
|
||||
Getter for the previously set terminal criteria for this algorithm. |
||||
|
||||
.. ocv:function:: TermCriteria optim::DownhillSolver::getTermCriteria() |
||||
|
||||
:return: Deep copy of the terminal criteria used at the moment. |
||||
|
||||
optim::DownhillSolver::setTermCriteria |
||||
------------------------------------------ |
||||
|
||||
Set terminal criteria for downhill simplex method. Two things should be noted. First, this method *is not necessary* to be called |
||||
before the first call to ``DownhillSolver::minimize()``, as the default value is sensible. Second, the method will raise an error |
||||
if ``termcrit.type!=(TermCriteria::MAX_ITER+TermCriteria::EPS)``, ``termcrit.epsilon<=0`` or ``termcrit.maxCount<=0``. That is, |
||||
both ``epsilon`` and ``maxCount`` should be set to positive values (non-integer and integer respectively) and they represent |
||||
tolerance and maximal number of function evaluations that is allowed. |
||||
|
||||
Algorithm stops when the number of function evaluations done exceeds ``termcrit.maxCount``, when the function values at the |
||||
vertices of simplex are within ``termcrit.epsilon`` range or simplex becomes so small that it |
||||
can enclosed in a box with ``termcrit.epsilon`` sides, whatever comes first. |
||||
|
||||
.. ocv:function:: void optim::DownhillSolver::setTermCriteria(const TermCriteria& termcrit) |
||||
|
||||
:param termcrit: Terminal criteria to be used, represented as ``TermCriteria`` structure (defined elsewhere in openCV). Mind you, that it should meet ``(termcrit.type==(TermCriteria::MAX_ITER+TermCriteria::EPS) && termcrit.epsilon>0 && termcrit.maxCount>0)``, otherwise the error will be raised. |
||||
|
||||
optim::DownhillSolver::getInitStep |
||||
----------------------------------- |
||||
|
||||
Returns the initial step that will be used in downhill simplex algorithm. See the description |
||||
of corresponding setter (follows next) for the meaning of this parameter. |
||||
|
||||
.. ocv:function:: void optim::getInitStep(OutputArray step) |
||||
|
||||
:param step: Initial step that will be used in algorithm. Note, that although corresponding setter accepts column-vectors as well as row-vectors, this method will return a row-vector. |
||||
|
||||
optim::DownhillSolver::setInitStep |
||||
---------------------------------- |
||||
|
||||
Sets the initial step that will be used in downhill simplex algorithm. Step, together with initial point (givin in ``DownhillSolver::minimize``) |
||||
are two *n*-dimensional vectors that are used to determine the shape of initial simplex. Roughly said, initial point determines the position |
||||
of a simplex (it will become simplex's centroid), while step determines the spread (size in each dimension) of a simplex. To be more precise, |
||||
if :math:`s,x_0\in\mathbb{R}^n` are the initial step and initial point respectively, the vertices of a simplex will be: :math:`v_0:=x_0-\frac{1}{2} |
||||
s` and :math:`v_i:=x_0+s_i` for :math:`i=1,2,\dots,n` where :math:`s_i` denotes projections of the initial step of *n*-th coordinate (the result |
||||
of projection is treated to be vector given by :math:`s_i:=e_i\cdot\left<e_i\cdot s\right>`, where :math:`e_i` form canonical basis) |
||||
|
||||
.. ocv:function:: void optim::setInitStep(InputArray step) |
||||
|
||||
:param step: Initial step that will be used in algorithm. Roughly said, it determines the spread (size in each dimension) of an initial simplex. |
||||
|
||||
optim::DownhillSolver::minimize |
||||
----------------------------------- |
||||
|
||||
The main method of the ``DownhillSolver``. It actually runs the algorithm and performs the minimization. The sole input parameter determines the |
||||
centroid of the starting simplex (roughly, it tells where to start), all the others (terminal criteria, initial step, function to be minimized) |
||||
are supposed to be set via the setters before the call to this method or the default values (not always sensible) will be used. |
||||
|
||||
.. ocv:function:: double optim::DownhillSolver::minimize(InputOutputArray x) |
||||
|
||||
:param x: The initial point, that will become a centroid of an initial simplex. After the algorithm will terminate, it will be setted to the point where the algorithm stops, the point of possible minimum. |
||||
|
||||
:return: The value of a function at the point found. |
||||
|
||||
optim::createDownhillSolver |
||||
------------------------------------ |
||||
|
||||
This function returns the reference to the ready-to-use ``DownhillSolver`` object. All the parameters are optional, so this procedure can be called |
||||
even without parameters at all. In this case, the default values will be used. As default value for terminal criteria are the only sensible ones, |
||||
``DownhillSolver::setFunction()`` and ``DownhillSolver::setInitStep()`` should be called upon the obtained object, if the respective parameters |
||||
were not given to ``createDownhillSolver()``. Otherwise, the two ways (give parameters to ``createDownhillSolver()`` or miss the out and call the |
||||
``DownhillSolver::setFunction()`` and ``DownhillSolver::setInitStep()``) are absolutely equivalent (and will drop the same errors in the same way, |
||||
should invalid input be detected). |
||||
|
||||
.. ocv:function:: Ptr<optim::DownhillSolver> optim::createDownhillSolver(const Ptr<Solver::Function>& f,InputArray initStep, TermCriteria termcrit) |
||||
|
||||
:param f: Pointer to the function that will be minimized, similarly to the one you submit via ``DownhillSolver::setFunction``. |
||||
:param step: Initial step, that will be used to construct the initial simplex, similarly to the one you submit via ``DownhillSolver::setInitStep``. |
||||
:param termcrit: Terminal criteria to the algorithm, similarly to the one you submit via ``DownhillSolver::setTermCriteria``. |
@ -0,0 +1,18 @@ |
||||
namespace cv{namespace optim{ |
||||
#ifdef ALEX_DEBUG |
||||
#define dprintf(x) printf x |
||||
static void print_matrix(const Mat& x){ |
||||
printf("\ttype:%d vs %d,\tsize: %d-on-%d\n",x.type(),CV_64FC1,x.rows,x.cols); |
||||
for(int i=0;i<x.rows;i++){ |
||||
printf("\t["); |
||||
for(int j=0;j<x.cols;j++){ |
||||
printf("%g, ",x.at<double>(i,j)); |
||||
} |
||||
printf("]\n"); |
||||
} |
||||
} |
||||
#else |
||||
#define dprintf(x) |
||||
#define print_matrix(x) |
||||
#endif |
||||
}} |
@ -0,0 +1,273 @@ |
||||
#include "precomp.hpp" |
||||
#include "debug.hpp" |
||||
#include "opencv2/core/core_c.h" |
||||
|
||||
namespace cv{namespace optim{ |
||||
|
||||
class DownhillSolverImpl : public DownhillSolver |
||||
{ |
||||
public: |
||||
void getInitStep(OutputArray step) const; |
||||
void setInitStep(InputArray step); |
||||
Ptr<Function> getFunction() const; |
||||
void setFunction(const Ptr<Function>& f); |
||||
TermCriteria getTermCriteria() const; |
||||
DownhillSolverImpl(); |
||||
void setTermCriteria(const TermCriteria& termcrit); |
||||
double minimize(InputOutputArray x); |
||||
protected: |
||||
Ptr<Solver::Function> _Function; |
||||
TermCriteria _termcrit; |
||||
Mat _step; |
||||
private: |
||||
inline void createInitialSimplex(Mat_<double>& simplex,Mat& step); |
||||
inline double innerDownhillSimplex(cv::Mat_<double>& p,double MinRange,double MinError,int& nfunk, |
||||
const Ptr<Solver::Function>& f,int nmax); |
||||
inline double tryNewPoint(Mat_<double>& p,Mat_<double>& y,Mat_<double>& coord_sum,const Ptr<Solver::Function>& f,int ihi, |
||||
double fac,Mat_<double>& ptry); |
||||
}; |
||||
|
||||
double DownhillSolverImpl::tryNewPoint( |
||||
Mat_<double>& p, |
||||
Mat_<double>& y, |
||||
Mat_<double>& coord_sum, |
||||
const Ptr<Solver::Function>& f, |
||||
int ihi, |
||||
double fac, |
||||
Mat_<double>& ptry |
||||
) |
||||
{ |
||||
int ndim=p.cols; |
||||
int j; |
||||
double fac1,fac2,ytry; |
||||
|
||||
fac1=(1.0-fac)/ndim; |
||||
fac2=fac1-fac; |
||||
for (j=0;j<ndim;j++) |
||||
{ |
||||
ptry(j)=coord_sum(j)*fac1-p(ihi,j)*fac2; |
||||
} |
||||
ytry=f->calc((double*)ptry.data); |
||||
if (ytry < y(ihi)) |
||||
{ |
||||
y(ihi)=ytry; |
||||
for (j=0;j<ndim;j++) |
||||
{ |
||||
coord_sum(j) += ptry(j)-p(ihi,j); |
||||
p(ihi,j)=ptry(j); |
||||
} |
||||
} |
||||
|
||||
return ytry; |
||||
} |
||||
|
||||
/*
|
||||
Performs the actual minimization of Solver::Function f (after the initialization was done) |
||||
|
||||
The matrix p[ndim+1][1..ndim] represents ndim+1 vertices that |
||||
form a simplex - each row is an ndim vector. |
||||
On output, nfunk gives the number of function evaluations taken. |
||||
*/ |
||||
double DownhillSolverImpl::innerDownhillSimplex( |
||||
cv::Mat_<double>& p, |
||||
double MinRange, |
||||
double MinError, |
||||
int& nfunk, |
||||
const Ptr<Solver::Function>& f, |
||||
int nmax |
||||
) |
||||
{ |
||||
int ndim=p.cols; |
||||
double res; |
||||
int i,ihi,ilo,inhi,j,mpts=ndim+1; |
||||
double error, range,ysave,ytry; |
||||
Mat_<double> coord_sum(1,ndim,0.0),buf(1,ndim,0.0),y(1,ndim,0.0); |
||||
|
||||
nfunk = 0; |
||||
|
||||
for(i=0;i<ndim+1;++i) |
||||
{ |
||||
y(i) = f->calc(p[i]); |
||||
} |
||||
|
||||
nfunk = ndim+1; |
||||
|
||||
reduce(p,coord_sum,0,CV_REDUCE_SUM); |
||||
|
||||
for (;;) |
||||
{ |
||||
ilo=0; |
||||
/* find highest (worst), next-to-worst, and lowest
|
||||
(best) points by going through all of them. */ |
||||
ihi = y(0)>y(1) ? (inhi=1,0) : (inhi=0,1); |
||||
for (i=0;i<mpts;i++) |
||||
{ |
||||
if (y(i) <= y(ilo)) |
||||
ilo=i; |
||||
if (y(i) > y(ihi)) |
||||
{ |
||||
inhi=ihi; |
||||
ihi=i; |
||||
} |
||||
else if (y(i) > y(inhi) && i != ihi) |
||||
inhi=i; |
||||
} |
||||
|
||||
/* check stop criterion */ |
||||
error=fabs(y(ihi)-y(ilo)); |
||||
range=0; |
||||
for(i=0;i<ndim;++i) |
||||
{ |
||||
double min = p(0,i); |
||||
double max = p(0,i); |
||||
double d; |
||||
for(j=1;j<=ndim;++j) |
||||
{ |
||||
if( min > p(j,i) ) min = p(j,i); |
||||
if( max < p(j,i) ) max = p(j,i); |
||||
} |
||||
d = fabs(max-min); |
||||
if(range < d) range = d; |
||||
} |
||||
|
||||
if(range <= MinRange || error <= MinError) |
||||
{ /* Put best point and value in first slot. */ |
||||
std::swap(y(0),y(ilo)); |
||||
for (i=0;i<ndim;i++) |
||||
{ |
||||
std::swap(p(0,i),p(ilo,i)); |
||||
} |
||||
break; |
||||
} |
||||
|
||||
if (nfunk >= nmax){ |
||||
dprintf(("nmax exceeded\n")); |
||||
return y(ilo); |
||||
} |
||||
nfunk += 2; |
||||
/*Begin a new iteration. First, reflect the worst point about the centroid of others */ |
||||
ytry = tryNewPoint(p,y,coord_sum,f,ihi,-1.0,buf); |
||||
if (ytry <= y(ilo)) |
||||
{ /*If that's better than the best point, go twice as far in that direction*/ |
||||
ytry = tryNewPoint(p,y,coord_sum,f,ihi,2.0,buf); |
||||
} |
||||
else if (ytry >= y(inhi)) |
||||
{ /* The new point is worse than the second-highest, but better
|
||||
than the worst so do not go so far in that direction */ |
||||
ysave = y(ihi); |
||||
ytry = tryNewPoint(p,y,coord_sum,f,ihi,0.5,buf); |
||||
if (ytry >= ysave) |
||||
{ /* Can't seem to improve things. Contract the simplex to good point
|
||||
in hope to find a simplex landscape. */ |
||||
for (i=0;i<mpts;i++) |
||||
{ |
||||
if (i != ilo) |
||||
{ |
||||
for (j=0;j<ndim;j++) |
||||
{ |
||||
p(i,j) = coord_sum(j) = 0.5*(p(i,j)+p(ilo,j)); |
||||
} |
||||
y(i)=f->calc((double*)coord_sum.data); |
||||
} |
||||
} |
||||
nfunk += ndim; |
||||
reduce(p,coord_sum,0,CV_REDUCE_SUM); |
||||
} |
||||
} else --(nfunk); /* correct nfunk */ |
||||
dprintf(("this is simplex on iteration %d\n",nfunk)); |
||||
print_matrix(p); |
||||
} /* go to next iteration. */ |
||||
res = y(0); |
||||
|
||||
return res; |
||||
} |
||||
|
||||
void DownhillSolverImpl::createInitialSimplex(Mat_<double>& simplex,Mat& step){ |
||||
for(int i=1;i<=step.cols;++i) |
||||
{ |
||||
simplex.row(0).copyTo(simplex.row(i)); |
||||
simplex(i,i-1)+= 0.5*step.at<double>(0,i-1); |
||||
} |
||||
simplex.row(0) -= 0.5*step; |
||||
|
||||
dprintf(("this is simplex\n")); |
||||
print_matrix(simplex); |
||||
} |
||||
|
||||
double DownhillSolverImpl::minimize(InputOutputArray x){ |
||||
dprintf(("hi from minimize\n")); |
||||
CV_Assert(_Function.empty()==false); |
||||
dprintf(("termcrit:\n\ttype: %d\n\tmaxCount: %d\n\tEPS: %g\n",_termcrit.type,_termcrit.maxCount,_termcrit.epsilon)); |
||||
dprintf(("step\n")); |
||||
print_matrix(_step); |
||||
|
||||
Mat x_mat=x.getMat(); |
||||
CV_Assert(MIN(x_mat.rows,x_mat.cols)==1); |
||||
CV_Assert(MAX(x_mat.rows,x_mat.cols)==_step.cols); |
||||
CV_Assert(x_mat.type()==CV_64FC1); |
||||
|
||||
Mat_<double> proxy_x; |
||||
|
||||
if(x_mat.rows>1){ |
||||
proxy_x=x_mat.t(); |
||||
}else{ |
||||
proxy_x=x_mat; |
||||
} |
||||
|
||||
int count=0; |
||||
int ndim=_step.cols; |
||||
Mat_<double> simplex=Mat_<double>(ndim+1,ndim,0.0); |
||||
|
||||
simplex.row(0).copyTo(proxy_x); |
||||
createInitialSimplex(simplex,_step); |
||||
double res = innerDownhillSimplex( |
||||
simplex,_termcrit.epsilon, _termcrit.epsilon, count,_Function,_termcrit.maxCount); |
||||
simplex.row(0).copyTo(proxy_x); |
||||
|
||||
dprintf(("%d iterations done\n",count)); |
||||
|
||||
if(x_mat.rows>1){ |
||||
Mat(x_mat.rows, 1, CV_64F, (double*)proxy_x.data).copyTo(x); |
||||
} |
||||
return res; |
||||
} |
||||
DownhillSolverImpl::DownhillSolverImpl(){ |
||||
_Function=Ptr<Function>(); |
||||
_step=Mat_<double>(); |
||||
} |
||||
Ptr<Solver::Function> DownhillSolverImpl::getFunction()const{ |
||||
return _Function; |
||||
} |
||||
void DownhillSolverImpl::setFunction(const Ptr<Function>& f){ |
||||
_Function=f; |
||||
} |
||||
TermCriteria DownhillSolverImpl::getTermCriteria()const{ |
||||
return _termcrit; |
||||
} |
||||
void DownhillSolverImpl::setTermCriteria(const TermCriteria& termcrit){ |
||||
CV_Assert(termcrit.type==(TermCriteria::MAX_ITER+TermCriteria::EPS) && termcrit.epsilon>0 && termcrit.maxCount>0); |
||||
_termcrit=termcrit; |
||||
} |
||||
// both minRange & minError are specified by termcrit.epsilon; In addition, user may specify the number of iterations that the algorithm does.
|
||||
Ptr<DownhillSolver> createDownhillSolver(const Ptr<Solver::Function>& f, InputArray initStep, TermCriteria termcrit){ |
||||
DownhillSolver *DS=new DownhillSolverImpl(); |
||||
DS->setFunction(f); |
||||
DS->setInitStep(initStep); |
||||
DS->setTermCriteria(termcrit); |
||||
return Ptr<DownhillSolver>(DS); |
||||
} |
||||
void DownhillSolverImpl::getInitStep(OutputArray step)const{ |
||||
_step.copyTo(step); |
||||
} |
||||
void DownhillSolverImpl::setInitStep(InputArray step){ |
||||
//set dimensionality and make a deep copy of step
|
||||
Mat m=step.getMat(); |
||||
dprintf(("m.cols=%d\nm.rows=%d\n",m.cols,m.rows)); |
||||
CV_Assert(MIN(m.cols,m.rows)==1 && m.type()==CV_64FC1); |
||||
if(m.rows==1){ |
||||
m.copyTo(_step); |
||||
}else{ |
||||
transpose(m,_step); |
||||
} |
||||
} |
||||
}} |
@ -0,0 +1,63 @@ |
||||
#include "test_precomp.hpp" |
||||
#include <cstdlib> |
||||
#include <cmath> |
||||
#include <algorithm> |
||||
|
||||
static void mytest(cv::Ptr<cv::optim::DownhillSolver> solver,cv::Ptr<cv::optim::Solver::Function> ptr_F,cv::Mat& x,cv::Mat& step, |
||||
cv::Mat& etalon_x,double etalon_res){ |
||||
solver->setFunction(ptr_F); |
||||
int ndim=MAX(step.cols,step.rows); |
||||
solver->setInitStep(step); |
||||
cv::Mat settedStep; |
||||
solver->getInitStep(settedStep); |
||||
ASSERT_TRUE(settedStep.rows==1 && settedStep.cols==ndim); |
||||
ASSERT_TRUE(std::equal(step.begin<double>(),step.end<double>(),settedStep.begin<double>())); |
||||
std::cout<<"step setted:\n\t"<<step<<std::endl; |
||||
double res=solver->minimize(x); |
||||
std::cout<<"res:\n\t"<<res<<std::endl; |
||||
std::cout<<"x:\n\t"<<x<<std::endl; |
||||
std::cout<<"etalon_res:\n\t"<<etalon_res<<std::endl; |
||||
std::cout<<"etalon_x:\n\t"<<etalon_x<<std::endl; |
||||
double tol=solver->getTermCriteria().epsilon; |
||||
ASSERT_TRUE(std::abs(res-etalon_res)<tol); |
||||
/*for(cv::Mat_<double>::iterator it1=x.begin<double>(),it2=etalon_x.begin<double>();it1!=x.end<double>();it1++,it2++){
|
||||
ASSERT_TRUE(std::abs((*it1)-(*it2))<tol); |
||||
}*/ |
||||
std::cout<<"--------------------------\n"; |
||||
} |
||||
|
||||
class SphereF:public cv::optim::Solver::Function{ |
||||
public: |
||||
double calc(const double* x)const{ |
||||
return x[0]*x[0]+x[1]*x[1]; |
||||
} |
||||
}; |
||||
class RosenbrockF:public cv::optim::Solver::Function{ |
||||
double calc(const double* x)const{ |
||||
return 100*(x[1]-x[0]*x[0])*(x[1]-x[0]*x[0])+(1-x[0])*(1-x[0]); |
||||
} |
||||
}; |
||||
|
||||
TEST(Optim_Downhill, regression_basic){ |
||||
cv::Ptr<cv::optim::DownhillSolver> solver=cv::optim::createDownhillSolver(); |
||||
#if 1 |
||||
{ |
||||
cv::Ptr<cv::optim::Solver::Function> ptr_F(new SphereF()); |
||||
cv::Mat x=(cv::Mat_<double>(1,2)<<1.0,1.0), |
||||
step=(cv::Mat_<double>(2,1)<<-0.5,-0.5), |
||||
etalon_x=(cv::Mat_<double>(1,2)<<-0.0,0.0); |
||||
double etalon_res=0.0; |
||||
mytest(solver,ptr_F,x,step,etalon_x,etalon_res); |
||||
} |
||||
#endif |
||||
#if 1 |
||||
{ |
||||
cv::Ptr<cv::optim::Solver::Function> ptr_F(new RosenbrockF()); |
||||
cv::Mat x=(cv::Mat_<double>(2,1)<<0.0,0.0), |
||||
step=(cv::Mat_<double>(2,1)<<0.5,+0.5), |
||||
etalon_x=(cv::Mat_<double>(2,1)<<1.0,1.0); |
||||
double etalon_res=0.0; |
||||
mytest(solver,ptr_F,x,step,etalon_x,etalon_res); |
||||
} |
||||
#endif |
||||
} |
@ -0,0 +1,52 @@ |
||||
// This sample shows the difference of adaptive bilateral filter and bilateral filter.
|
||||
#include "opencv2/core.hpp" |
||||
#include "opencv2/core/utility.hpp" |
||||
#include "opencv2/imgproc.hpp" |
||||
#include "opencv2/highgui.hpp" |
||||
#include "opencv2/ocl.hpp" |
||||
|
||||
using namespace cv; |
||||
using namespace std; |
||||
|
||||
|
||||
int main( int argc, const char** argv ) |
||||
{ |
||||
const char* keys = |
||||
"{ i input | | specify input image }" |
||||
"{ k ksize | 5 | specify kernel size }"; |
||||
CommandLineParser cmd(argc, argv, keys); |
||||
string src_path = cmd.get<string>("i"); |
||||
int ks = cmd.get<int>("k"); |
||||
const char * winName[] = {"input", "adaptive bilateral CPU", "adaptive bilateral OpenCL", "bilateralFilter OpenCL"}; |
||||
|
||||
Mat src = imread(src_path); |
||||
Mat abFilterCPU; |
||||
if(src.empty()){ |
||||
//cout << "error read image: " << src_path << endl;
|
||||
return -1; |
||||
} |
||||
|
||||
std::vector<ocl::Info> infos; |
||||
ocl::getDevice(infos); |
||||
|
||||
ocl::oclMat dsrc(src), dABFilter, dBFilter; |
||||
|
||||
Size ksize(ks, ks); |
||||
adaptiveBilateralFilter(src,abFilterCPU, ksize, 10); |
||||
ocl::adaptiveBilateralFilter(dsrc, dABFilter, ksize, 10); |
||||
ocl::bilateralFilter(dsrc, dBFilter, ks, 30, 9); |
||||
|
||||
Mat abFilter = dABFilter; |
||||
Mat bFilter = dBFilter; |
||||
imshow(winName[0], src); |
||||
|
||||
imshow(winName[1], abFilterCPU); |
||||
|
||||
imshow(winName[2], abFilter); |
||||
|
||||
imshow(winName[3], bFilter); |
||||
|
||||
waitKey(); |
||||
return 0; |
||||
|
||||
} |
@ -0,0 +1,136 @@ |
||||
#include <iostream> |
||||
#include <string> |
||||
|
||||
#include "opencv2/core.hpp" |
||||
#include "opencv2/core/utility.hpp" |
||||
#include "opencv2/ocl.hpp" |
||||
#include "opencv2/highgui.hpp" |
||||
|
||||
using namespace std; |
||||
using namespace cv; |
||||
using namespace cv::ocl; |
||||
|
||||
#define M_MOG 1 |
||||
#define M_MOG2 2 |
||||
|
||||
int main(int argc, const char** argv) |
||||
{ |
||||
|
||||
cv::CommandLineParser cmd(argc, argv, |
||||
"{ c camera | false | use camera }" |
||||
"{ f file | 768x576.avi | input video file }" |
||||
"{ m method | mog | method (mog, mog2) }" |
||||
"{ h help | false | print help message }"); |
||||
|
||||
if (cmd.get<bool>("help")) |
||||
{ |
||||
cout << "Usage : bgfg_segm [options]" << endl; |
||||
cout << "Avaible options:" << endl; |
||||
cmd.printMessage(); |
||||
return 0; |
||||
} |
||||
|
||||
bool useCamera = cmd.get<bool>("camera"); |
||||
string file = cmd.get<string>("file"); |
||||
string method = cmd.get<string>("method"); |
||||
|
||||
if (method != "mog" && method != "mog2") |
||||
{ |
||||
cerr << "Incorrect method" << endl; |
||||
return -1; |
||||
} |
||||
|
||||
int m = method == "mog" ? M_MOG : M_MOG2; |
||||
|
||||
VideoCapture cap; |
||||
|
||||
if (useCamera) |
||||
cap.open(0); |
||||
else |
||||
cap.open(file); |
||||
|
||||
if (!cap.isOpened()) |
||||
{ |
||||
cerr << "can not open camera or video file" << endl; |
||||
return -1; |
||||
} |
||||
|
||||
std::vector<cv::ocl::Info>info; |
||||
cv::ocl::getDevice(info); |
||||
|
||||
Mat frame; |
||||
cap >> frame; |
||||
|
||||
oclMat d_frame(frame); |
||||
|
||||
cv::ocl::MOG mog; |
||||
cv::ocl::MOG2 mog2; |
||||
|
||||
oclMat d_fgmask; |
||||
oclMat d_fgimg; |
||||
oclMat d_bgimg; |
||||
|
||||
d_fgimg.create(d_frame.size(), d_frame.type()); |
||||
|
||||
Mat fgmask; |
||||
Mat fgimg; |
||||
Mat bgimg; |
||||
|
||||
switch (m) |
||||
{ |
||||
case M_MOG: |
||||
mog(d_frame, d_fgmask, 0.01f); |
||||
break; |
||||
|
||||
case M_MOG2: |
||||
mog2(d_frame, d_fgmask); |
||||
break; |
||||
} |
||||
|
||||
for(;;) |
||||
{ |
||||
cap >> frame; |
||||
if (frame.empty()) |
||||
break; |
||||
d_frame.upload(frame); |
||||
|
||||
int64 start = cv::getTickCount(); |
||||
|
||||
//update the model
|
||||
switch (m) |
||||
{ |
||||
case M_MOG: |
||||
mog(d_frame, d_fgmask, 0.01f); |
||||
mog.getBackgroundImage(d_bgimg); |
||||
break; |
||||
|
||||
case M_MOG2: |
||||
mog2(d_frame, d_fgmask); |
||||
mog2.getBackgroundImage(d_bgimg); |
||||
break; |
||||
} |
||||
|
||||
double fps = cv::getTickFrequency() / (cv::getTickCount() - start); |
||||
std::cout << "FPS : " << fps << std::endl; |
||||
|
||||
d_fgimg.setTo(Scalar::all(0)); |
||||
d_frame.copyTo(d_fgimg, d_fgmask); |
||||
|
||||
d_fgmask.download(fgmask); |
||||
d_fgimg.download(fgimg); |
||||
if (!d_bgimg.empty()) |
||||
d_bgimg.download(bgimg); |
||||
|
||||
imshow("image", frame); |
||||
imshow("foreground mask", fgmask); |
||||
imshow("foreground image", fgimg); |
||||
if (!bgimg.empty()) |
||||
imshow("mean background image", bgimg); |
||||
|
||||
int key = waitKey(30); |
||||
if (key == 27) |
||||
break; |
||||
} |
||||
|
||||
return 0; |
||||
} |
Loading…
Reference in new issue