混合高斯模型實現運動目標檢測(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

}遍歷

相關文章
相關標籤/搜索