当前位置:编程学习 > 网站相关 >>

OPENCV中混合高斯背景模型的实现

混合高斯背景模型理论的论文:点击下载

////////////////////////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; //高斯函数被匹配的次数
            for( m = 0; m < first_frame->nChannels; m++)
            {
                bg_model->g_point[n].g_values[0].variance[m] = var_init;

              //均值赋为当前像素的值
                bg_model->g_point[n].g_values[0].mean[m] = (unsigned char)first_frame->imageData[p + m];
            }

          //除第一以外的高斯分布函数的初始化(均值、权值和匹配次数都置零)
            for( k = 1; k < params.n_gauss; k++)  
            {   
                bg_model->g_point[n].g_values[k].weight = 0;
                bg_model->g_point[n].g_values[k].match_sum = 0;
                for( m = 0; m < first_frame->nChannels; m++){
                    bg_model->g_point[n].g_values[k].variance[m] = var_init;
                    bg_model->g_point[n].g_values[k].mean[m] = 0;
                }
            }
        }
    } //g_point[]:像素,g_values[]:高斯分布函数,mean[]:通道
   
    bg_model->countFrames = 0;
   
    __END__;
   
    if( cvGetErrStatus() < 0 )
    {
        CvBGStatModel* base_ptr = (CvBGStatModel*)bg_model;
       
        if( bg_model && bg_model->release )
            bg_model->release( &base_ptr );
        else
        

补充:综合编程 , 其他综合 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,