////////////////////////cvCreateGaussianBGModel///////////////////////////////////////////
CV_IMPL CvBGStatModel *cvCreateGaussianBGModel( IplImage*first_frame,CvGaussBGStatModelParams* parameters )
{
CvGaussBGModel* bg_model = 0;
CV_FUNCNAME( "cvCreateGaussianBGModel" );
__BEGIN__;
double var_init;
CvGaussBGStatModelParams params;
int i, j, k, m, n;
// init parameters
if( parameters == NULL )
{
params.win_size = CV_BGFG_MOG_WINDOW_SIZE; // 初始化階段的幀數;用戶自定義模型學 習率a=1/win_size;
params.bg_threshold = CV_BGFG_MOG_BACKGROUND_THRESHOLD;
params.std_threshold = CV_BGFG_MOG_STD_THRESHOLD;
params.weight_init = CV_BGFG_MOG_WEIGHT_INIT;
params.variance_init = CV_BGFG_MOG_SIGMA_INIT*CV_BGFG_MOG_SIGMA_INIT; //方差
params.minArea = CV_BGFG_MOG_MINAREA;
params.n_gauss = CV_BGFG_MOG_NGAUSSIANS; //高斯分佈函數的個數
}
else
{
params = *parameters; //用戶自定義參數
}
if( !CV_IS_IMAGE(first_frame) )
CV_ERROR( CV_StsBadArg, "Invalid or NULL first_frame parameter" );
CV_CALL( bg_model = (CvGaussBGModel*)cvAlloc( sizeof(*bg_model) ));
memset( bg_model, 0, sizeof(*bg_model) );
bg_model->type = CV_BG_MODEL_MOG; //CV_BG_MODEL_MOG爲高斯背景模型
bg_model->release = (CvReleaseBGStatModel)icvReleaseGaussianBGModel;
bg_model->update = (CvUpdateBGStatModel)icvUpdateGaussianBGModel;
bg_model->params = params;
//prepare storages
CV_CALL( bg_model->g_point = (CvGaussBGPoint*)cvAlloc(sizeof(CvGaussBGPoint)*
((first_frame->width*first_frame->height) + 256)));
CV_CALL( bg_model->background = cvCreateImage(cvSize(first_frame->width,
first_frame->height), IPL_DEPTH_8U, first_frame->nChannels));
CV_CALL( bg_model->foreground = cvCreateImage(cvSize(first_frame->width,
first_frame->height), IPL_DEPTH_8U, 1));
CV_CALL( bg_model->storage = cvCreateMemStorage());
//initializing
var_init = 2 * params.std_threshold * params.std_threshold; //初始化方差
CV_CALL( bg_model->g_point[0].g_values =
(CvGaussBGValues*)cvAlloc( sizeof(CvGaussBGValues)*params.n_gauss*
(first_frame->width*first_frame->height + 128)));
for( i = 0, n = 0; i < first_frame->height; i++ ) //行
{
for( j = 0; j < first_frame->width; j++, n++ ) //列
{
const int p = i*first_frame->widthStep+j*first_frame->nChannels;
//以下幾步是對第一個高斯函數做初始化
bg_model->g_point[n].g_values = bg_model->g_point[0].g_values + n*params.n_gauss;
bg_model->g_point[n].g_values[0].weight = 1; //權值賦爲1
bg_model->g_point[n].g_values[0].match_sum = 1; //高斯函數被匹配的次數