oencv学习(8)--背景消除

如果摄像机是固定的,那么我们可以认为场景(背景)大多数情况下是不变的,而只有前景(被跟踪的目标)会运动,这样就可以建立背景模型。通过比较当前帧和背景模型,就能轻松地跟踪目标运动情况了。这里,最容易想到的比较方式就是当前帧减去背景模型了。背景模型的建立最容易想到的就是选取一张没有目标时的图片作为背景模型,但是很多情况下,背景中往往一直会有目标在运动,而且程序在没有经验的情况下又不会知道哪幅图片是有目标的,哪幅图片是没有目标的,程序只是根据我们人为建立的模型去检测目标而已。于是,不难想到,我们可以截取一段目标出现相对较少的视频段(也可叫做图片序列吧。。。),然后对帧求均值,将均值图片作为背景模型。这时候的背景模型总该靠谱一点了吧。

如opencv2.3.1里的以下函数可计算当前帧与背景之差的绝对值

cv::absdiff(backgroundImage,currentImage,foreground);

 

opencv2.3.1里

 

void accumulateWeighted(InputArray src, InputOutputArray dst, double alpha, InputArray mask=noArray() )
函数实现该功能:

 

其参数介绍如下:

 

Parameters
src – Input image as 1- or 3-channel, 8-bit or 32-bit floating point.
dst – Accumulator image with the same number of channels as input image, 32-bit or 64-bit floating-point.
alpha – Weight of the input image.
mask – Optional operation mask.
The function calculates the weighted sum of the input image src and the accumulator dst so that dst becomes a
running average of a frame sequence:
dst(x, y) ← (1 − alpha) · dst(x, y) + alpha · src(x, y) if mask(x, y) = 0

 

posted on 2013-05-23 16:49  happycaoyue  阅读(355)  评论(0编辑  收藏  举报