opencv2中的背景/前景分离(类BackgroundSubtractor)
class CV_EXPORTS_W BackgroundSubtractor:public Algorithm
class BackgroundSubtractorMOG : public BackgroundSubtractor
class BackgroundSubtractorMOG2 : public BackgroundSubtractor¶
在智能视频分析中,混合高斯建模算法(GMM)是使用最为广泛的算法之一,近来项目对速度要求较高,原先研发的算法在实时性上无法满足需求,因此转向GMM。
在Opencv中,有GMM算法的三个实现版本,分别是BackgroundSubtractorMOG、BackgroundSubtractorMOG2和BackgroundSubtractorGMG,在多种实际场景测试和比较中,发现BackgroundSubtractorMOG综合性能要优于其他两种,因此采用BackgroundSubtractorMOG为原型进行改写。
#include<opencv2/core/core.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/video/background_segm.hpp> #include <iostream> using namespace std; using namespace cv; int main() { // Open the video file VideoCapture capture("E:/图片/bike.avi"); // check if video successfully opened if (!capture.isOpened()) return 0; // current video frame Mat frame; // foreground binary image Mat foreground; namedWindow("Extracted Foreground"); // The Mixture of Gaussian object // used with all default parameters 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 mog(frame,foreground,0.01); // Complement the image threshold(foreground,foreground,128,255,cv::THRESH_BINARY_INV); // show foreground imshow("Extracted Foreground",foreground); // introduce a delay // or press key to stop if (waitKey(10)>=0) stop= true; } waitKey(); }
运行结果: