1.opencv中的GMM算法
GMM(Gaussian Mixture Model)是一种经典的背景提取算法,opencv中也把它引入并封装为算法类。使用opencv2413版本时,通过BackgroundSubtractorMOG类即可调用。这里先给出一个调用例子和算法效果,代码如下所示。
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/background_segm.hpp>
int main()
{
// Open the video file
cv::VideoCapture capture("768X576.avi");
// check if video successfully opened
if (!capture.isOpened())
return 0;
// current video frame
cv::Mat frame, frameGray;
// foreground binary image
cv::Mat foreground;
cv::namedWindow("Extracted Foreground");
// The Mixture of Gaussian object
// used with all default parameters
cv::BackgroundSubtractorMOG mog;
bool stop(false);
// for all frames in video
while (!stop) {
// read next frame if any
if (!capture.read(frame))
break;
// update the background
// and return the foreground
cvtColor(frame, frameGray, CV_BGR2GRAY);
mog(frameGray,foreground,0.01);
// show foreground
cv::imshow("Extracted Foreground",foreground);
cv::imshow("image",frame);
// introduce a delay
// or press key to stop
if (cv::waitKey(10)>=0)
stop= true;
}
return 0;
}
其中“768X576.avi”是opencv源码文件中的一个包含多个行人的视频文件,算法效果如下图所示,可以看到行人被很好的用白色标注出来。
2.算法原理
BackgroundSubtractorMOG的实现在2413版本的opencv_2.4.13\opencv\sources\modules\video\src\bgfg_gaussmix.cpp中,cpp中讲到算法原理参考《An Improved Adaptive Background Mixture Model for Real-time Tracki