Mixed Gaussian model to achieve moving target detection (OpenCV built-in implementation)

Share technology, record life!

Paste the code directly:

void GMM(){
    // Open the video file
    VideoCapture capture("E:/LeftBag.mpg");

    if (!capture.isOpened()){
        cout << "Video open fail!" << endl;
        return;
    }
    //Current video frame
    Mat frame;
    //Binary image of
    foreground Mat foreground;
    namedWindow("Extracted Foreground" );
    //Objects of the mixed Gaussian model class, all use the default parameters
    BackgroundSubtractorMOG2 mog;
    bool stop(false);
    //Travel through all frames in the video
    while (!stop){
        //Read the next frame
        if (!capture. read(frame))
            break;
        // update background and return foreground
        mog(frame, foreground, 0.01);
        // learning rate
        threshold(foreground, foreground, 128, 255, THRESH_BINARY_INV);
        // show foreground
        imshow("Extracted Foreground", foreground);
        if (waitKey(10) >= 0)
            stop = true;

    }

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324431976&siteId=291194637