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

分享技术,记录生活!ide

直接贴代码:学习

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;对象

    }it

}遍历

相关文章
相关标签/搜索