Ask Your Question

background subtraction help

asked 2013-02-07 09:25:39 -0500

zenny gravatar image


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

I saw a background subtraction in youtube ( 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!

Thanks in advance!


edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2013-02-07 12:14:07 -0500

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.

edit flag offensive delete link 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.

zenny gravatar imagezenny ( 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.

Mathieu Barnachon gravatar imageMathieu Barnachon ( 2013-02-08 02:25:20 -0500 )edit

answered 2014-04-14 18:37:25 -0500

Yasmin93 gravatar image

check this out link text

edit flag offensive delete link more

Question Tools


Asked: 2013-02-07 09:25:39 -0500

Seen: 2,084 times

Last updated: Apr 14 '14