Background Subtraction in Opencv C++

(Last Updated On: October 8, 2017)

Change detection or background subtraction is the main and important part of surveillance and vision based applications. These application are mainly used in real time projects like visitor counters in a building while a static camera is taking regular frames and sending back to server similarly traffic control application on the bases of traffic intensity. In all of the above cases the first need to do in application is to subtract vehicle or person from the scene so we can count or take any further decision. If we have just background image like image of road without vehicles or without visitors then we can take that image as background and compare it to the coming frames and find the difference which will tell two things one is there any change occurred in scene second if occurred then is it a visitor or vehicles or something else in the scene. So in this way we just get the change in scene but there are few error chances in this method which can be reduced by reducing the noise. One main error is shadow of the object in foreground. As in background we have empty scene in for ground if we get the vehicle with a shadow then that can create issue.

Background subtraction is mostly used for tracking and detecting moving objects. This is done by finding the difference in current and previous frame also called background model subtraction. Background best support for indoor, static environment because in case of outdoor environment wind, rain whether condition light effect, shadow can affect its results. Background subtraction has various technique like frame differencing, mean filter, Gaussian average and background mixture model. First we discuss with frame differencing technique, frame differencing method first start with segmentation and feature extraction.  We find objects from difference of the background and foreground. This method is implemented by making a background frame from scene that is preserved for further use. After background we take each next scene as foreground and find out difference from background frame. Hence by simple arithmetic operation between pixels of two different frames can give us the differencing objects. Then after difference we set the threshold value to find the exact and real objects not consider any noise or object shadow as object, for that purpose a threshold value is set and if difference is greater than that thresh value than any object detected will be real object without any error. Another technique for filtering is mean filter. In mean filter we make background same as frame differencing technique but our foreground is different and little stronger instead of taking single frame as foreground we take a number of frames and average them for foreground and after formation of foreground we find the difference between background and foreground. For better noise remove thresh hold technique is applied in this technique we ignore few range of pixels falling under a certain value in accumulator array so after thresh hold implementation we are assured that we are getting smooth and best values and noised data is ignored. BackgroundSubtractor MOG and BackgroundSubtractor MOG2 are also famous background subtraction techniques. BackgroundSubtractor MOG is approved background subtractor method for real time tracking and detection. BackgroundSubtractor MOG uses model of background pixels by K Gaussian and then check the weight of mixture represents color proportions. During code we create BackgroundSubtractorMOG by create function and then pass mat objects to that function and start filtering by apply function. BackgroundSubtractor GMG is another famous technique for background subtraction change detection. BackgroundSubtractor GMG algorithms checks both image and pixels segmentations at a time. BackgroundSubtractor GMG takes first few frames for background modeling. After background setting it take foreground and apply probabilistic foreground segmentation algorithm. These were few technique for background subtraction and change detection. Now we will discuss some important thing for background subtraction that is smoothing as for subtraction we must get some smooth and noise free frames. First filtering is averaging. In averaging we take average of all pixels and one by one replace the middle pixels. In this filtering we must declare the kernel size in start so we get specific are or pointed area as smooth and effected and rest area is not changes or effected. Next noise removing technique is median filtering in which we take median and apply its value at center. This is really effective technique for salt and pepper noise which is famous noise in frames. In all other filter central value is newly calculated while in median filtering pixel value is always replaced by some value in image. Now we will see some code for background subtraction.



#include <opencv2/opencv.hpp>

#include <iostream>

#include <cv.h>

#include <highgui.h>

using namespace std;

#include <iostream>

#include "opencv2/opencv.hpp"

#include <windows.h>

using namespace std;

using namespace cv;

int main()


Mat a, b;

String path=”"D:/Video/g.mp4");”

VideoCapture cap(path);



cout<<”error in frame get”;



for(;;) {

cap >> src;

cv::Rect roi(222, 120, 250, 323);




cv::Rect roi1(222, 120, 250, 323);


int c=cap.get(CV_CAP_PROP_FPS);



//cout <<absdiff<<"............";

cout<<"B ...",b;

int na;

imshow("my_window", src);

imshow("Difff", b);

if(cv::waitKey(30) >= 0) break;


return 0;



Write a Reply or Comment