学习opencv高斯背景建模的改进Word下载.docx
《学习opencv高斯背景建模的改进Word下载.docx》由会员分享,可在线阅读,更多相关《学习opencv高斯背景建模的改进Word下载.docx(11页珍藏版)》请在冰豆网上搜索。
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;
//如果parameters非空,则将其参数赋给params
if(!
CV_IS_IMAGE(first_frame))//如果第一帧不是图像,报错
CV_ERROR(CV_StsBadArg,"
InvalidorNULLfirst_frameparameter"
CV_CALL(bg_model=(CvGaussBGModel*)cvAlloc(sizeof(*bg_model)));
//为bg_model申请内存
memset(bg_model,0,sizeof(*bg_model));
//初始化刚申请的内存
bg_model->
type=CV_BG_MODEL_MOG;
//背景模型类型是:
CV_BG_MODEL_MOG
release=(CvReleaseBGStatModel)icvReleaseGaussianBGModel;
//释放调用icvReleaseGaussianBGModel
update=(CvUpdateBGStatModel)icvUpdateGaussianBGModel;
// 更新调用icvUpdateGaussianBGModel
params=params;
//参数为params
//preparestorages
CV_CALL(bg_model->
g_point=(CvGaussBGPoint*)cvAlloc(sizeof(CvGaussBGPoint)*
((first_frame->
width*first_frame->
height)+256)));
//为背景模型bg_model的高斯背景点g_point分配内存,
background=cvCreateImage(cvSize(first_frame>
width,
first_frame->
height),IPL_DEPTH_8U,first_frame->
nChannels));
foreground=cvCreateImage(cvSize(first_frame->
width
first_frame->
height),IPL_DEPTH_8U,1));
storage=cvCreateMemStorage());
//initializing
var_init=2*params.std_threshold*params.std_threshold;
//初始化方差
g_point[0].g_values=
(CvGaussBGValues*)cvAlloc(sizeof(CvGaussBGValues)*params.n_gauss*(first_frame->
height+128)));
for(i=0,p=0,n=0;
i<
first_frame->
height;
i++)
for(j=0;
j<
width;
j++,n++)//n
=i*first_frame->
width+j
g_point[n].g_values=
bg_model->
g_point[0].g_values+n*params.n_gauss;
g_point[n].g_values[0].weight=1;
//thefirstvalueseenhasweightone
//首个高斯模型,权值赋予1
g_point[n].g_values[0].match_sum=1;
//
thesumofmatchesforaparticulargaussian
for(m=0;
m<
nChannels;
m++)//对每个通道
g_point[n].g_values[0].variance[m]=var_init;
//第0个高斯模型的第M个通道的方差,
g_point[n].g_values[0].mean[m]=(unsignedchar)first_frame->
imageData[p+m];
//均值,第M通道的值
for(k=1;
k<
params.n_gauss;
k++)//其他高斯模型,
g_point[n].g_values[k].weight=0;
//第K个高斯模型的权值
0
g_point[n].g_values[k].match_sum=0;
//第K个高斯模型的match_sum
m++)
g_point[n].g_values[k].variance[m]=var_init;
//第K个高斯模型的第m通道的方差
g_point[n].g_values[k].mean[m]=0;
//第K个高斯模型的第m通道的均值0
p+=first_frame->
countFrames=0;
//帧=0
__END__;
if(cvGetErrStatus()<
0)//如果有错误
CvBGStatModel*base_ptr=(CvBGStatModel*)bg_model;
if(bg_model&
&
release)
release(&
base_ptr);
//释放模型
cvFree(&
bg_model);
bg_model=0;
return(CvBGStatModel*)bg_model;
//返回创建的背景模型
}
CV_IMPLvoidCV_CDECL
icvReleaseGaussianBGModel(CvGaussBGModel**_bg_model)
//返回背景模型
icvReleaseGaussianBGModel"
_bg_model)
CV_ERROR(CV_StsNullPtr,"
"
if(*_bg_model)
CvGaussBGModel*bg_model=*_bg_model;
if(bg_model->
g_point)
g_point[0].g_values);
//释放背景点中的值
g_point);
//释放背景点
cvReleaseImage(&
background);
//释放背景模型中的前景
foreground);
//释放背景模型中的背景
cvReleaseMemStorage(&
storage);
//释放背景模型中的存储器
cvFree(_bg_model);
//释放背景模型
CV_IMPLint
CV_CDECL
icvUpdateGaussianBGModel(IplImage*curr_frame,CvGaussBGModel*
bg_model)
inti,j,k;
intregion_count=0;
CvSeq*first_seq=NULL,*prev_seq=NULL,*seq=NULL;
countFrames++;
//每执行一次更新,帧数++
for(i=0;
curr_frame->
j++)//对每个像素点逐点进行运算
intmatch[CV_BGFG_MOG_MAX_NGAUSSIANS];
//CV_BGFG_MOG_MAX_NGAUSSIANS=500,最大高斯模型数目:
match[500]
doublesort_key[CV_BGFG_MOG_MAX_NGAUSSIANS];
//排序:
sort_key[500]
constintnChannels=curr_frame->
//当前帧的通道数
constintn=i*curr_frame->
width+j;
//正在处理第几个像素,
constintp=n*curr_frame->
//第几个通道,这与图像(BGR,BGR,BGR....)的交叉存储格式有关
//Afewshortcuts
CvGaussBGPoint*g_point=&
g_point[n];
constCvGaussBGStatModelParamsbg_model_params=bg_model->
params;
doublepixel[4];
intno_match;
for(k=0;
nChannels;
k++)//获得某个像素的
第K通道的值
pixel[k]=(uchar)curr_frame->
imageData[p+k];
no_match=icvMatchTest(pixel,nChannels,match,g_point,&
bg_model_params);
//判断该像素值是否与背景模型匹配
countFrames>
=bg_model->
params.win_size)//判断已经处理的帧数是否等于初始化阶段帧长,如果是则:
?
icvUpdateFullWindow(pixel,nChannels,match,g_point,&
params);
//调用正常阶段更新函数进行更新
if(no_match==-1)
//如果没有找到匹配的,则调用正常阶段NoMatch情况的更新函数
icvUpdateFullNoMatch(curr_frame,p,match,g_point,&
else//初始化阶段
icvUpdatePartialWindow(pixel,nChannels,match,g_point,&
if(no_match==-1)
icvUpdatePartialNoMatch(pixel,nChannels,match,g_point,&
icvGetSortKey(nChannels,sort_key,g_point,&
//获得模型的适应度值
icvInsertionSortGaussians(g_point,sort_key,(CvGaussBGStatModelParams*)&
//进行排序
icvBackgroundTest(nChannels,n,p,match,bg_model);
//判断是否是背景
//下面这段是前景滤波,滤掉小块区域。
cvClearMemStorage(bg_model->
//cvMorphologyEx(bg_model->
foreground,bg_model->
foreground,0,0,CV_MOP_OPEN,1);
foreground,0,0,CV_MOP_CLOSE,1);
cvFindContours(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.height<
params.minArea)//去掉小的区域
//deletesmallcontour
prev_seq=seq->
h_prev;
if(prev_seq)
prev_seq->
h_next=seq->
h_next;
if(seq->
seq->
h_next->
h_prev=prev_seq;
first_seq=seq->
h_prev=NULL;
}//endofif
region_count++;
//否则,区域数++
}//endoffor
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;
//返回轮廓数
staticvoidicvInsertionSortGaussians(CvGaussBGPoint*g_point,double*sort_key,CvGaussBGStatModelParams*bg_model_params)
inti,j;
for(i=1;
bg_model_params->
n_gauss;
i++)//对每个高斯背景模型
doubleindex=sort_key[i];
//获得适应度值
for(j=i;
j>
0&
sort_key[j-1]<
index;
j--)//sortdecendingorder降序排列,
doubletemp_sort_key=sort_key[j];
sort_key[j]=sort_key[j-1];
sort_key[j-1]=temp_sort_key;
CvGaussBGValuestemp_gauss_values=g_point->
g_values[j];
g_point->
g_values[j]=g_point->
g_values[j-1];
g_values[j-1]=temp_gauss_values;
//
sort_key[j]=index;
staticinticvMatchTest(double*src_pixel,intnChannels,int*match,
constCvGaussBGPoint*g_point,