# background subtraction help

Hi,

New to opencv, so if this sounds stupid, please forgive me.

I saw a background subtraction in youtube (http://www.youtube.com/watch?v=rGMqXBvYxog) where the poster states that it is a 50 line code.

Would someone mind helping to create a background subtraction in opencv as shown in that specifid youtube video!

/zenny

edit retag close merge delete

Sort by » oldest newest most voted

The easiest way is to look at segment_objects.cpp in OpenCV samples. It used a BackgroundSubtractorMOG object (doc). MOG stands for Mixture of Gaussian, which is a classical algorithm for this problem. To make a background subtraction, you have to take images of the background, and after you can make the subtraction when new objects appear. It uses BackgroundSubtractorMOG::operator() overloading to perform subtraction. Look at the parameter 'update_bg_model' which is used to stop the background update. If you stand in the foreground, without moving during some time, you become the background if this parameter is on, otherwise, the background isn't update. You can look at the original paper Stauffer & Grimson, CVPR 98.

The sample made a connected components analysis also.

more

Thanks Matthieu for the reply. I am simply trying to capture just eveything except the predefined background. Itwould be nice if anyone share some sample code (if any) to begin with.

Can you tell me the role of connected component analyysis in the sample youtube video? Where can I find relevant literature on connected component analysis? Thanks again.

( 2013-02-07 23:02:14 -0500 )edit

In the sample segment_objects.cpp, comment the line refineSegments and you can see the direct background subtraction. Display the image bgmask instead of out_frame for segmented image. Check the difference!

Connected component is just used to detected the biggest part. It performs some morphomaths to 'clean' the components and use the findcontours to effectively detect them. After, it keeps the biggest. This is done to suppress noise.

As far as I know, the litterature is mainly based on optimization and enhancement for background subtraction (which is made by BackgroundSubtractorMOG). If you are only interested in 'one object' detection by background subtraction, you have nothing more to do, maybe play with the update parameter if you know the number of frame without object.

( 2013-02-08 02:25:20 -0500 )edit