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进行前景/背景检测的一个截图。
BackgroundSubtractorMOG2 使用示例
int main(){
VideoCapture video("1.avi");
Mat frame,mask,thresholdImage, output;
//video>>frame;
BackgroundSubtractorMOG2 bgSubtractor(20,16,true);
while(true){
video>>frame;
++frameNum;
bgSubtractor(frame,mask,0.001);
cout<<frameNum<<endl;
//imshow("mask",mask);
//waitKey(10);
}
return 0;
}
同样的,构造函数可以使用默认构造函数和带形参的构造函数
BackgroundSubtractorMOG2::BackgroundSubtractorMOG2()
BackgroundSubtractorMOG2::BackgroundSubtractorMOG2(int history,
float varThreshold, bool bShadowDetection=true )
history同上,varThreshold表示马氏平方距离上使用的来判断是否为背景的阈值(此值不影响背景更新速率),bShadowDetection表示是否使用阴影检测(如果开启阴影检测,则mask中使用127表示阴影)。
使用重载操作符()调用每帧检测函数:
void BackgroundSubtractorMOG2::operator()(InputArray image, OutputArray fgmask, double learningRate=-1)
参数意义同BackgroundSubtractorMOG中的operator()函数。
同时BackgroundSubtractorMOG2提供了getBackgroundImage()函数用以返回背景图像:
void BackgroundSubtractorMOG2::getBackgroundImage(OutputArray backgroundImage)
另外OpenCV的refman中说新建对象以后还有其他和模型油有关的参数可以修改,不过比较坑的是opencv把这个这些函数参数声明为protected,同时没有提供访问接口,所以要修改的话还是要自己修改源文件提供访问接口。
protected:
Size frameSize;
int frameType;
Mat bgmodel;
Mat bgmodelUsedModes;//keep track of number of modes per pixel
int nframes;
int history;
int nmixtures;
//! here it is the maximum allowed number of mixture components.
//! Actual number is determined dynamically per pixel
double varThreshold;
// threshold on the squared Mahalanobis distance to decide if it is well described
// by the background model or not. Related to Cthr from the paper.
// This does not influence the update of the background. A typical value could be 4 sigma
// and that is varThreshold=4*4=16; Corresponds to Tb in the paper.
/////////////////////////
// less important parameters - things you might change but be carefull
////////////////////////
float backgroundRatio;
// corresponds to fTB=1-cf from the paper
// TB - threshold when the component becomes significant enough to be included into
// the background model. It is the TB=1-cf from the paper. So I use cf=0.1 => TB=0.
// For alpha=0.001 it means that the mode should exist for approximately 105 frames before
// it is considered foreground
// float noiseSigma;
float varThresholdGen;
//correspondts to Tg - threshold on the squared Mahalan. dist. to decide
//when a sample is close to the existing components. If it is not close
//to any a new component will be generated. I use 3 sigma => Tg=3*3=9.
//Smaller Tg leads to more generated components and higher Tg might make
//lead to small number of components but they can grow too large
float fVarInit;
float fVarMin;
float fVarMax;
//initial variance for the newly generated components.
//It will will influence the speed of adaptation. A good guess should be made.
//A simple way is to estimate the typical standard deviation from the images.
//I used here 10 as a reasonable value
// min and max can be used to further control the variance
float fCT;//CT - complexity reduction prior
//this is related to the number of samples needed to accept that a component
//actually exists. We use CT=0.05 of all the samples. By setting CT=0 you get
//the standard Stauffer&Grimson algorithm (maybe not exact but very similar)
//shadow detection parameters
bool bShadowDetection;//default 1 - do shadow detection
unsigned char nShadowDetection;//do shadow detection - insert this value as the detection result - 127 default value
float fTau;
// Tau - shadow threshold. The shadow is detected if the pixel is darker
//version of the background. Tau is a threshold on how much darker the shadow can be.
//Tau= 0.5 means that if pixel is more than 2 times darker then it is not shadow
//See: Prati,Mikic,Trivedi,Cucchiarra,"Detecting Moving Shadows...",IEEE PAMI,2003.
以下是使用BackgroundSubtractorMOG2检测的前景和背景:
参考文献:
[1] KaewTraKulPong, Pakorn, and Richard Bowden. "An improved adaptive background mixture model for real-time tracking with shadow detection." Video-Based Surveillance Systems. Springer US, 2002. 135-144.
[2] Zivkovic, Zoran. "Improved adaptive Gaussian mixture model for background subtraction." Pattern Recognition, 2004. ICPR 2004. Proceedings of the 17th International Conference on. Vol. 2. IEEE, 2004.
分享到:
相关推荐
利用opencv进行高斯背景建模的例子,visual c++ 6.0开发
提取监控视频中的车辆,以便于数出车辆数目。。。。。。。。。。。
实现了改进的高斯混合背景建模算法,开发环境为VS2013+opencv,主要用C语言,opencv实现
一个混合高斯模型的domo vs2008+openCV2.3.1测试没有问题 使用的时候要传进去视频的路径 win32控制台参数不知道怎么传的童鞋可以百度 高斯函数的数量设置为3就可以应付大部分场景了
基于混合高斯模型背景建模法来检测运动目标算法(matlab).zip资源matlab opencv运动目标检测程序资料基于混合高斯模型背景建模法来检测运动目标算法(matlab).zip资源matlab opencv运动目标检测程序资料基于混合高斯...
高斯混合滤波建模,基于opencv,用于背景建模,前景检测
在matlab中编译c++文件并调用opencv的混合高斯模型背景建模函数
混合高斯背景建模,使用opencv,CS2012编写
自己写的基于混合高斯背景建模的运动目标检测算法,没有利用opencv本身封装的GMM函数。
实现了混合高斯背景建模的代码,建立工程后配置opencv库运行
OPENCV目标跟踪_opencv_目标检测_混合高斯背景模型-背景差法Opencv_混合高斯模型_背景差法_VC++
这是opencv实现的高斯混合模型,已经运行成功
使用Opencv实现的混合高斯背景建模,能够对图像的前景和背景进行分割。
是opencv 混合高斯模型的原函数,并进行了修改可以实现前景提取并进行背景建模
用python编写的,结合了混合高斯背景建模和轮廓算法的camshift目标跟踪算法
使用vibe背景建模算法完成对运动目标的前景提取,这是一种2011年才提出的一种新的背景建模算法,相对于opencv里面的经典混合高斯背景建模速度快了一倍,其算法比混合高斯背景建模跟简单,同时前景的提取效果也好一点...
本例的开发环境是vs2008,用的是基于opencv的开发工具包,可以实现效果较好的前景提取技术
基于混合高斯背景建模的目标运动意图分析,通过混合高斯背景建模进行前景提取,自己写高斯模型,并非通过OPENCV函数库
可以有效的建立混合高斯模型,基于opencv开发,已经调试好,需要改一下文件路径,初学者可以看一下