您的位置:首页 > 其它

高斯背景建模学习 icvUpdateGaussianBGModel()

2009-08-06 21:07 274 查看
*功能:更新高斯背景模块
*参数:pixel, nChannels, match, g_point bg_model->params???
*返回值:整型
*版本:
*作者:
*日期:2008.03.31
*/
static int CV_CDECL//静态全局函数,返回值整型
icvUpdateGaussianBGModel( IplImage* curr_frame, CvGaussBGModel* bg_model )
{
int i, j, k;
int region_count = 0;
CvSeq *first_seq = NULL, *prev_seq = NULL, *seq = NULL;

bg_model->countFrames++;

for( i = 0; i < curr_frame->height; i++ )
{
for( j = 0; j < curr_frame->width; j++ )
{
int match[CV_BGFG_MOG_MAX_NGAUSSIANS];
double sort_key[CV_BGFG_MOG_MAX_NGAUSSIANS];
const int nChannels = curr_frame->nChannels;//const 限定变量不允许被改变
const int n = i*curr_frame->width+j;
const int p = n*curr_frame->nChannels;

// A few short cuts 捷径
CvGaussBGPoint* g_point = &bg_model->g_point
;
const CvGaussBGStatModelParams bg_model_params = bg_model->params;
double pixel[4];
int no_match;

for( k = 0; k < nChannels; k++ )
pixel[k] = (uchar)curr_frame->imageData[p+k];

no_match = icvMatchTest( pixel, nChannels, match, g_point, &bg_model_params );
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 );//计算w/pho,排序用
icvInsertionSortGaussians( g_point, sort_key, (CvGaussBGStatModelParams *)&bg_model_params );//高斯模型排序
icvBackgroundTest( nChannels, n, p, match, bg_model );//背景检测,并将背景像素点置0,前景点置255;
}
}

//foreground filtering 前景过滤

//filter small regions 小区域
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 );

//CV_RETR_LIST 提取所有轮廓,并且放置在 list 中
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.height < bg_model->params.minArea )
{
//delete small contour
//删除小边缘
prev_seq = seq->h_prev;
if( prev_seq )
{
prev_seq->h_next = seq->h_next;// h_prev 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);//绘制边缘

return region_count;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: