OpenCV混合高斯模型函数说明.docx

上传人:b****6 文档编号:8819123 上传时间:2023-02-01 格式:DOCX 页数:17 大小:36.29KB
下载 相关 举报
OpenCV混合高斯模型函数说明.docx_第1页
第1页 / 共17页
OpenCV混合高斯模型函数说明.docx_第2页
第2页 / 共17页
OpenCV混合高斯模型函数说明.docx_第3页
第3页 / 共17页
OpenCV混合高斯模型函数说明.docx_第4页
第4页 / 共17页
OpenCV混合高斯模型函数说明.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

OpenCV混合高斯模型函数说明.docx

《OpenCV混合高斯模型函数说明.docx》由会员分享,可在线阅读,更多相关《OpenCV混合高斯模型函数说明.docx(17页珍藏版)》请在冰豆网上搜索。

OpenCV混合高斯模型函数说明.docx

OpenCV混合高斯模型函数说明

OpenCV混合高斯模型函数注释说明

一、cvaux.h

#defineCV_BGFG_MOG_MAX_NGAUSSIANS500

//高斯背景检测算法的默认参数设置

#defineCV_BGFG_MOG_BACKGROUND_THRESHOLD0.7//T,背景高斯分布权重之和阈值

#defineCV_BGFG_MOG_STD_THRESHOLD2.5//λ=2.5(99%)

#defineCV_BGFG_MOG_WINDOW_SIZE200//学习率α=1/win_size

#defineCV_BGFG_MOG_NGAUSSIANS5//k=5个混合高斯模型

#defineCV_BGFG_MOG_WEIGHT_INIT0.05//初始权重

#defineCV_BGFG_MOG_SIGMA_INIT30//初始标准差

#defineCV_BGFG_MOG_MINAREA15.f//?

#defineCV_BGFG_MOG_NCOLORS3//颜色通道数

/*************CV_BG_STAT_MODEL_FIELDS()的宏定义**********************/

#defineCV_BG_STAT_MODEL_FIELDS()

inttype;//typeofBGmodel

CvReleaseBGStatModelrelease;//\

CvUpdateBGStatModelupdate;\

IplImage*background;/*8UC3referencebackgroundimage*/\

IplImage*foreground;/*8UC1foregroundimage*/\

IplImage**layers;/*8UC3referencebackgroundimage,canbenull*/\

intlayer_count;/*canbezero*/\

CvMemStorage*storage;/*storageforforeground_regions?

/\

CvSeq*foreground_regions/*foregroundobjectcontours*/

/*************************高斯背景模型参数结构体*************************/

typedefstructCvGaussBGStatModelParams

{

intwin_size;//等于1/alpha

intn_gauss;//高斯模型的个数

doublebg_threshold,std_threshold,minArea;//bg_threshold:

高斯分布权重之和阈值、std_threshold:

2.5、minArea:

doubleweight_init,variance_init;//权重和方差

}CvGaussBGStatModelParams;

/**************************高斯分布模型结构体***************************/

typedefstructCvGaussBGValues

{

intmatch_sum;

doubleweight;

doublevariance[CV_BGFG_MOG_NCOLORS];

doublemean[CV_BGFG_MOG_NCOLORS];

}

CvGaussBGValues;

typedefstructCvGaussBGPoint

{

CvGaussBGValues*g_values;

}

CvGaussBGPoint;

/*************************高斯背景模型结构体*************************/

typedefstructCvGaussBGModel

{

CV_BG_STAT_MODEL_FIELDS();

CvGaussBGStatModelParamsparams;

CvGaussBGPoint*g_point;

intcountFrames;

}

CvGaussBGModel;

二、cvbgfg_gaussmix.cpp

////////////////////////////////////////////////////////////cvCreateGaussianBGModel////////////////////////////////////////////////////////////////

功能:

高斯背景模型变量bg_model初始化赋值

CV_IMPLCvBGStatModel*cvCreateGaussianBGModel(IplImage*first_frame,CvGaussBGStatModelParams*parameters)

{

CvGaussBGModel*bg_model=0;//高斯背景状态模型变量

CV_FUNCNAME("cvCreateGaussianBGModel");

__BEGIN__;

doublevar_init;

CvGaussBGStatModelParamsparams;//高斯背景状态模型参数变量

inti,j,k,n,m,p;

//初始化参数,如果参数为空,则进行初始化赋值

if(parameters==NULL)

{

params.win_size=CV_BGFG_MOG_WINDOW_SIZE;//学习率α=1/200=0.005

params.bg_threshold=CV_BGFG_MOG_BACKGROUND_THRESHOLD;//判断是否为背景点的阈值0.7

params.std_threshold=CV_BGFG_MOG_STD_THRESHOLD;//标准阈值2.5

params.weight_init=CV_BGFG_MOG_WEIGHT_INIT;//权重值0.05

params.variance_init=CV_BGFG_MOG_SIGMA_INIT*CV_BGFG_MOG_SIGMA_INIT;//方差30*30

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,"InvalidorNULLfirst_frameparameter");

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;

//申请内存空间

CV_CALL(bg_model->g_point=(CvGaussBGPoint*)cvAlloc(sizeof(CvGaussBGPoint)*

((first_frame->width*first_frame->height)+256)));//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());

//初始化

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)));//128?

//n:

表示像素点的索引值

//p:

表示当前像素对应颜色通道的首地址

//g_point[]:

对应像素点、g_values[]:

对应高斯模型、variance[]和mean[]:

对应颜色通道的方差和均值

for(i=0,p=0,n=0;iheight;i++)//行

{

for(j=0;jwidth;j++,n++)//列

{

bg_model->g_point[n].g_values=bg_model->g_point[0].g_values+n*params.n_gauss;//每个像素点的第一个高斯模型的地址(每个像素对应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;mnChannels;m++)//对各颜色通道的方差和均值赋值

{

bg_model->g_point[n].g_values[0].variance[m]=var_init;//初始化较大的方差

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

}

//初始化剩下的高斯分布模型的参数

for(k=1;k

{

bg_model->g_point[n].g_values[k].weight=0;//各高斯分布取相等且较小权重值,此处取0

bg_model->g_point[n].g_values[k].match_sum=0;

for(m=0;mnChannels;m++)

{

bg_model->g_point[n].g_values[k].variance[m]=var_init;//初始化较大的方差

bg_model->g_point[n].g_values[k].mean[m]=0;//赋值0

}

}

p+=first_frame->nChannels;

}

}

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

cvFree(&bg_model);

bg_model=0;

}

return(CvBGStatModel*)bg_model;

}

//////////////////////////////////////////////////////////icvUpdateGaussianBGModel///////////////////////////////////////////////////////////////

功能:

对高斯背景模型变量bg_model进行更新

staticintCV_CDECLicvUpdateGaussianBGModel(IplImage*curr_frame,CvGaussBGModel*bg_model)

{

inti,j,k;

intregion_count=0;

CvSeq*first_seq=NULL,*prev_seq=NULL,*seq=NULL;

bg_model->countFrames++;

for(i=0;iheight;i++)//行

{

for(j=0;jwidth;j++)//列

{

intmatch[CV_BGFG_MOG_MAX_NGAUSSIANS];

doublesort_key[CV_BGFG_MOG_MAX_NGAUSSIANS];

constintnChannels=curr_frame->nChannels;//通道数目

constintn=i*curr_frame->width+j;//像素索引值

constintp=n*curr_frame->nChannels;//像素点颜色通道的首地址

//Afewshortcuts

CvGaussBGPoint*g_point=&bg_model->g_point[n];

constCvGaussBGStatModelParamsbg_model_params=bg_model->params;

doublepixel[4];

intno_match;

for(k=0;k

pixel[k]=(uchar)curr_frame->imageData[p+k];

no_match=icvMatchTest(pixel,nChannels,match,g_point,&bg_model_params);

//判断高斯背景模型更新帧数是否达到设置值win_size(?

(初始更新阶段和一般更新阶段在更新处理过程中是不同的,其中定义初始更新阶段为帧数小于win_size)

if(bg_model->countFrames==bg_model->params.win_size)//一般更新阶段

{

icvUpdateFullWindow(pixel,nChannels,match,g_point,&bg_model->params);

if(no_match==-1)

icvUpdateFullNoMatch(curr_frame,p,match,g_point,&bg_model_params);

}

else

{

icvUpdatePartialWindow(pixel,nChannels,match,g_point,&bg_model_params);

if(no_match==-1)

icvUpdatePartialNoMatch(pixel,nChannels,match,g_point,&bg_model_params);

}

icvGetSortKey(nChannels,sort_key,g_point,&bg_model_params);

icvInsertionSortGaussians(g_point,sort_key,(CvGaussBGStatModelParams*)&bg_model_params);

icvBackgroundTest(nChannels,n,p,match,bg_model);

}

}

//foregroundfiltering

//filtersmallregions

cvClearMemStorage(bg_model->storage);

//cvMorphologyEx(bg_model->foreground,bg_model->foreground,0,0,CV_MOP_OPEN,1);

//cvMorphologyEx(bg_model->foreground,bg_model->foreground,0,0,CV_MOP_CLOSE,1);

cvFindContours(bg_model->foreground,bg_model->storage,&first_seq,sizeof(CvContour),CV_RETR_LIST);

for(seq=first_seq;seq;seq=seq->h_next)

{

CvContour*cnt=(CvContour*)seq;

if(cnt->rect.width*cnt->rect.heightparams.minArea)

{

//deletesmallcontour

prev_seq=seq->h_prev;

if(prev_seq)

{

prev_seq->h_next=seq->h_next;

if(seq->h_next)seq->h_next->h_prev=prev_seq;

}

else

{

first_seq=seq->h_next;

if(seq->h_next)seq->h_next->h_prev=NULL;

}

}

else

{

region_count++;

}

}

bg_model->foreground_regions=first_seq;

cvZero(bg_model->foreground);

cvDrawContours(bg_model->foreground,first_seq,CV_RGB(0,0,255),CV_RGB(0,0,255),10,-1);

returnregion_count;

}

////////////////////////////////////////////////////////////icvMatchTest////////////////////////////////////////////////////////////////

功能:

将当前像素与个高斯分布进行匹配判断,如果匹配成功,则返回相应高斯分布的索引值

staticinticvMatchTest(double*src_pixel,intnChannels,int*match,

constCvGaussBGPoint*g_point,

constCvGaussBGStatModelParams*bg_model_params)

{

intk;

intmatchPosition=-1;

for(k=0;kn_gauss;k++)match[k]=0;//高斯分布匹配标识数组初始化置0

for(k=0;kn_gauss;k++)

{

doublesum_d2=0.0;

doublevar_threshold=0.0;

for(intm=0;m

{

doubled=g_point->g_values[k].mean[m]-src_pixel[m];

sum_d2+=(d*d);

var_threshold+=g_point->g_values[k].variance[m];//方差

}//difference

var_threshold=bg_model_params->std_threshold*bg_model_params->std_threshold*var_threshold;//

//匹配方程为:

或者

if(sum_d2

{

match[k]=1;//匹配时标识置1

matchPosition=k;//存储匹配的高斯分布索引值

break;//一旦匹配,就终止与后续高斯分布的匹配

}

}

returnmatchPosition;//返回匹配上的高斯分布索引值,没有匹配的返回-1

}

////////////////////////////////////////////////////icvUpdateFullWindow////////////////////////////////////////////////////////////

功能:

更新各高斯分布的权重值(对于匹配上的高斯分布要增大权值,其余的减小权值),如果存在匹配上的高斯分布,还要更新其均值和方差。

s

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 考试认证 > IT认证

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1