非动态的背景建模与前景检测算法
2020-06-21王若宇
王若宇



摘 要:为了从一些复杂的场景中提取出人像,用到的开源代码有很多,其均假设若一个像素的颜色在较长时间内变化不大,则认为它是背景。但是,这些开源代码在用于一些自拍摄像头时效果较差,因为当一个人端坐在摄像头前,他的像素不会发生很大变化。因此,本文提出了另外一种算法,该算法基于如下假设:在背景建模期间前景不会出现,且背景建模结束后,背景的变化始终为建模期间背景变化的子集。这种非动态的算法适用于提取距离摄像头距离更近的对象。
关键词:前景提取;前景检测;背景建模
Abstract: In order to extract human images from some complex scenes, many open source codes are used, which assume that if the color of a pixel does not change much in a long time, it is considered as the background. However, these open source codes are not very effective for some self timer cameras, because when a person sits in front of the camera, his pixels will not change greatly. Therefore, this paper proposed another algorithm, which was based on the following assumption: the foreground would not appear during the background modeling, and after the background modeling, the background change was always a subset of the background change during the modeling. This non dynamic algorithm is suitable for extracting objects closer to the camera.
Keywords: foreground extraction;foreground detection;background modeling
1 算法設计
1.1 算法思路
由于是从摄像头采集的图像,因此其有BGR三个通道,每个通道的值为[0,255]。为每一个像素的每一个通道创建一个编码本(CodeBook[1]),编码本中储存有若干个码元(CodeElement),一个码元中储存一个区间。在前景检测阶段,若某一像素的三个通道都落在对应编码本的任一码元里,则认为它是背景;否则,认为它是前景。据此得到掩模,然后利用形态学开闭操作、轮廓检测、凸包运算等消除噪声,优化掩模效果。
创建编码本和码元的思路在数据结构介绍中解释。
1.2 数据结构介绍
1.2.1 码元(CodeElement)。创建码元的代码如下:
1.2.2 编码本(CodeBook)。创建编码本的代码如下:
其中,learnHigh和learnLow表示一个学习阈值。在背景建模阶段,对输入图像的每一个像素的每一通道,遍历其编码本中的码元,如果该通道的值在某个码元中,则跳过;如果该通道的值虽然不在某个码元中,但它在(Min-learnLow)~(Max+learnHigh)中,则表示该码元应该学习这个像素,因此应扩大码元的区间使之包含这个像素。……
