OpenCV中实现了两个版本的高斯混合背景/前景分割方法(Gaussian Mixture-based Background/Foreground Segmentation Algorithm)[1-2],调用接口很明朗,效果也很好。
BackgroundSubtractorMOG 使用示例
int main(){
VideoCapture video("1.avi");
Mat frame,mask,thresholdImage, output;
video>>frame;
BackgroundSubtractorMOG bgSubtractor(20,10,0.5,false);
while(true){
video>>frame;
++frameNum;
bgSubtractor(frame,mask,0.001);
imshow("mask",mask);
waitKey(10);
}
return 0;
}
构造函数可以使用默认构造函数或带形参的构造函数:
BackgroundSubtractorMOG::BackgroundSubtractorMOG()
BackgroundSubtractorMOG::BackgroundSubtractorMOG(int history, int nmixtures,
double backgroundRatio, double noiseSigma=0)
其中history为使用历史帧的数目,nmixtures为混合高斯数量,backgroundRatio为背景比例,noiseSigma为噪声权重。
而调用的接口只有重载操作符():
void BackgroundSubtractorMOG::operator()(InputArray image, OutputArray fgmask, double learningRate=0)
其中image为当前帧图像,fgmask为输出的前景mask,learningRate为背景学习速率。
以下是使用BackgroundSubtractorMOG进行前景/背景检测的一个截图。