The simplest technique is to take a background image without any moving object. Subtract current frame from static background image to find changes in current frame. But it can’t deal with objects being added or removed from static scene, illumination changes and changes in camera position etc. To overcome above mentioned issues frame difference approach is commonly used. I discussed frame differencing approach in my previous article, can be found from here. Average background is an alternative technique is used to incorporate background changes. I discussed averaging method in another article can be found here. Average background can be computed using accumlateWeighted function to determine average background.
accumulateweighted(current_frame, average_frame, 0.01);
0.01 is passed as learning rate. The following code is used to subtract average model from current frame to find difference. Moving objects and some noise is also shown in video output.
Mat model, cFrame, motion;
int fWidth, fHeight;
fWidth = capture.get(CAP_PROP_FRAME_WIDTH);
cvNamedWindow( "video", CV_WINDOW_AUTOSIZE );
while( 1 )
threshold(motion, motion, 30, 255,CV_THRESH_BINARY );
imshow( "video", motion);