背景消除

BS算法  
    - 图像分割(GMM – 高斯混合模型)  
    - 机器学习(KNN –K个最近邻) 
BackgroundSubtractor (父类)
   -  BackgroundSubtractorMOG2 
   -  BackgroundSubtractorKNN
#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char**) {
    VideoCapture capture;
    capture.open("D:/images/video_004.avi");
    if (!capture.isOpened()) {
        printf("could not find the video file...
");
        return -1;
    }
    // create windows
    Mat frame;
    Mat bsmaskMOG2, bsmaskKNN;
    namedWindow("input video", CV_WINDOW_AUTOSIZE);
    namedWindow("MOG2", CV_WINDOW_AUTOSIZE);

    Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));

    // MOG2 BS
    Ptr<BackgroundSubtractor> pMOG2 = createBackgroundSubtractorMOG2();
    // KNN  BS
    Ptr<BackgroundSubtractor> pKNN = createBackgroundSubtractorKNN();
while (capture.read(frame)) { imshow("input video", frame); pMOG2->apply(frame, bsmaskMOG2); morphologyEx(bsmaskMOG2, bsmaskMOG2, MORPH_OPEN, kernel, Point(-1, -1)); imshow("MOG2", bsmaskMOG2); pKNN->apply(frame, bsmaskKNN); morphologyEx(bsmaskKNN, bsmaskKNN, MORPH_OPEN, kernel, Point(-1, -1)); imshow("KNN", bsmaskKNN); char c = waitKey(100); if (c == 27) { break; } } capture.release(); waitKey(0); return 0; }