Background subtraction is a way of eliminating the background from image. To achieve this we extract the moving foreground from the static background.

In OpenCV we have three algorithms to do this operation:

### 1. BackgroundSubtractorMOG

It is a Gaussian Mixture-based Background/Foreground Segmentation Algorithm.

### 2. BackgroundSubtractorMOG2

It is also a Gaussian Mixture-based Background/Foreground Segmentation Algorithm. It provides better adaptibility to varying scenes due illumination changes etc.

### 3. BackgroundSubtractorGMG

This algorithm combines statistical background image estimation and per-pixel Bayesian segmentation.

How to apply OpenCV in-built functions for background substraction:

1. Create an object to signify the algorithm we are using for background subtraction.
2. Apply backgroundsubtractor.apply() function on image.

#### Implemented code in Python:

#importing libraries
import numpy as np
import cv2

#creating object
fgbg1 = cv2.bgsegm.createBackgroundSubtractorMOG();
fgbg2 = cv2.createBackgroundSubtractorMOG2();
fgbg3 = cv2.bgsegm.createBackgroundSubtractorGMG();

# capture frames from a camera
cap = cv2.VideoCapture(0);
while(1):

cv2.imshow('Original',img);
k = cv2.waitKey(30) & 0xff;
if k == 27:
break;

cap.release();
cv2.destroyAllWindows();

OUTPUT:

We can see that there is a lot of noise in the resultant image for BackgroundSubtractorGMG, hence it is always preferred to use morphological transformation to the result to remove the noises.

#### Implemented code in Python:

#importing libraries
import numpy as np
import cv2

kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3));

#creating object
fgbg = cv2.bgsegm.createBackgroundSubtractorGMG();

# capture frames from a camera
cap = cv2.VideoCapture(0);
while(1):

#with noise frame

#apply transformation to remove noise

#after removing noise

k = cv2.waitKey(30) & 0xff;
if k == 27:
break;

cap.release();
cv2.destroyAllWindows();

OUTPUT:

$${}$$