混合高斯模型实现运动目标检测(OpenCV内置实现)

分享技术,记录生活!

直接贴代码:

void GMM(){
    // 打开视频文件
    VideoCapture capture("E:/LeftBag.mpg");

    if (!capture.isOpened()){
        cout << "Video open fail!" << endl;
        return;
    }
    //当前视频帧
    Mat frame;
    //前景的二值图像
    Mat foreground;
    namedWindow("Extracted Foreground");
    //混合高斯模型类的对象, 全部采用默认参数
    BackgroundSubtractorMOG2 mog;
    bool stop(false);
    //遍历视频中的所有帧
    while (!stop){
        //读取下一帧
        if (!capture.read(frame))
            break;
        // 更新背景并返回前景
        mog(frame, foreground, 0.01);
        // 学习速率
        threshold(foreground, foreground, 128, 255, THRESH_BINARY_INV);
        // 显示前景
        imshow("Extracted Foreground", foreground);
        if (waitKey(10) >= 0)
            stop = true;

    }

}

猜你喜欢

转载自my.oschina.net/u/3702502/blog/1633136