Book Image

OpenCV Essentials

Book Image

OpenCV Essentials

Overview of this book

Table of Contents (15 chapters)
OpenCV Essentials
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

The Background subtraction technique


The Background subtraction technique consists of obtaining the important objects over a background.

Now, let's see the methods available in OpenCV for the Background subtraction technique. Currently, the following four important techniques are required for this task:

  • MOG (Mixture-of-Gaussian)

  • MOG2

  • GMG (Geometric MultiGrip)

  • KNN (K-Nearest Neighbors)

Next, we are going to see an example (backgroundSubKNN) using the KNN technique:

#include<opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int backGroundSubKNN()
{
    //1-Set the parameters and initializations
    Mat frame;
    Mat background;
    Mat foreground;
    bool finish = false;
    int history = 500;
    double dist2Threshold = 400.0;
    bool detectShadows = false;
    vector< vector<Point>> contours;
    namedWindow("Frame");
    namedWindow("Background");
    VideoCapture capture(0);

    //Check if the video camera is opened
    if(!capture.isOpened()) return 1...