Background Subtraction is a technique in which background and foreground are segmented so that we can perform our required algorithms (such as face detection, gender classification etc). The basic approach for background subtraction is to store the background image as the reference image, in which there is no movement and then in every other frame subtract the reference image to extract the alien objects in the scene. The alien objects are captured as foreground and these are used for further applications.
Since the background does not always remain constant (mainly because of light changes or continuous movement of leaves) therefore we need to develop an approach in which we update the background continuously so that minute changes in the background are made part of the background. Stauffer & Grimson’s algorithm is one of the most reliable algorithms to facilitate adaptive background subtraction.
Stauffer & Grimson Algorithm
The algorithm models each pixel with a mixture of Gaussians. At every frame, for each pixel, distance of pixel’s color value is calculated from each of the associated K Gaussian distributions. Every new pixel is checked against all existing distributions. The match is the distribution with Mahalanobis distance less than a threshold. The mean and variance of unmatched distributions remain unchanged. The matched distributions are updated by using alpha blending concept. For the unmatched pixel, the lowest weight Gaussian is replaced by the new Gaussian with mean at the new pixel and an initial estimate of covariance matrix. Then we sum up distributions less than some threshold to decide whether the current pixel is part of the background or foreground.
Reference
“Adaptive Background mixture models for real-time tracking” by Chris Stauffer and W.E.L Grimson (The Artificial Intelligence Laboratory, Massachusetts Institute of Technology)
http://www.ai.mit.edu/projects/vsam/
Overview of the algorithm is given at the following link:
http://www-staff.it.uts.edu.au/~massimo/BackgroundSubtractionReview-Piccardi.pdf
The original paper is available at the following link:
http://www.ai.mit.edu/projects/vsam/Publications/stauffer_cvpr98_track.pdf



It’s been about 2 years away from Computer Vision. Now I think it’s never late to come back to Computer Vision world. Will start programming in VC++ and Matlab to implement some algorithms
I have noticed that many people come to this page for finding references related to computer vision……..If your issue is not solved…..feel free to discuss the issue…….I might help you out….
Regards
how to implement stauffer’s algorithm in matlab?
pls help me out……
do u have n code which has implemented dis stauffer grimson algo????
Well, I do not have a code right now with me…….but u can download and install OpenCV 1.0.
After installation, just go to its folder path, and in cv folder you can find implementation of this algorithm.
If you want to use this algorithm, then u can simply include opencv in your project and access the functionality
ok thanks a lot..
i vl try…
n if i hv ne further doubts i will contact u..
thanks a lot once again…
no problemo…………any time
Hello! I want to ask you about that implementation in Opencv 1.0, I have downloaded it but couldn’t find the sources. Can you please add more details about how to use it in opencv? And does this algorithm implemented in latest versions of opencv?
Thank you!
Hey, thanx for the great post – very relevant to what I’m doing now.
I was looking at bg/fg sample in opencv 1.1.0 – it appears to be based on a different algorithm than Stauffer/Grimson.
The code uses the functions cvCreateFGDStatModel and cvUpdateBGStatModel.
These are defined in the cvaux/src/cvbgfg_acmmm2003.cpp
According to the top of that file, the algo is based on:
// Foreground Object Detection from Videos Containing Complex Background
// Li, Huan, Gu, Tian 2003 9p
// http://muq.org/~cynbe/bib/foreground-object-detection-from-videos-containing-complex-background.pdf
Apparently, its not mixture of guassian based.
Can you comment on this algorithm? Is it better than Stauffer/Grimson and MOG?
Thanx!
You can start using this algorithm by passing your reference image or first frame to cvCreateFGDStatModel before going into a loop and then in a loop you can pass the current frame into cvUpdateBGStatModel. Then you can access the background and foreground from the current image.
It was fun using this algorithm, I used to initialize it with a grayscale image, then I used to access the foreground image, convert it into a 3-channel image and AND it with the current 3-channel image to extract the foreground with original colors…………… You can adjust the threshold to acquire a better foreground. If threshold does not help, you can apply some erosion or dilation on the binary image of foreground you get from BGStatModel….it helps a lot
This is probably going to come up in my comp vision exam tomorrow!
Thanks for making it alot clearer
Good luck for your exam matapult. I hope to learn more from you as well. Regards.
I want to work on Gender classification using OpenCV. How i can do it? please give me algorithm or approach for this.
since Computer Vision is based on image processing, therefore Gender classification using openCV will not be 100% perfect.
You might have used Haar Classifiers available in openCV to find frontal faces, profile faces. Actually, they have a neural network which gets trained on hundreds of positive, negative and false images; and generate an XML file containing the calculated feature sets. That feature set is used for further correspondence to detect whether the image contains a face or not.
Similarly, you will need to acquire huge data set of males and females; and train the neural network on those images. Then you can follow the same approach of face recognition to recognize whether the image is of a male or female.
It has been while, I worked in openCV………..but I’ll look into it and get back here as soon as I get hold of some more information.
hi
v r making a project on determining human actions using kinematic features.so the first step is backgroung seperation frm a video. so i need the code of an algorithm which could b implemented in matlab. so can u help?
Hello Evleen,
using OPENCV:
——-
If you are using openCV with your project, you can use BGStatModel with the first frame of video, then it will start returning foreground and background separately.
But before that, are you using a video with static background or there are minimal changes in the background or rapid changes.
If there are no changes, then you will get a nice foreground.
If there are minimal changes, you can use the background subtraction with gaussian model, which ignores minimal changes in the background.
Try to ignore a video with rapid background changes for beginner level.
I hope this might be useful.
Not Using OPENCV:
—————–
Frame Differencing I
——————–
If you are not using openCV, then you can simply take first image of the video as a reference image, and while looping through all frames of the video; subtract the reference image…….theoretically it returns the foreground.
Try to convert the images to grayscale first and then do the image manipulation, and get the result in binary form, perform some erosion/dilation and then use the final binary image as a mask and crop the required area from the original image
FRAME DIFFERENCING II
———————
You can also try to perform frame differencing between 2 consecutive frames, this returns the kinetic changes in the video.
These are some of the possibilities, I tried when I was doing background subtraction. Hope you will find these useful
good luck
Hi!
Thanx for your suggestions.
the problem that we r dealing with is that we do not have frames.
what we r supposed 2 do is that v have 2 divide the video into large
subvideos nd then do the background subtraction. we have around 10 human
actions like walking, jumping, waving, etc.
we have divided each video into 2 sub videos using video clipper. now v have
to do background subtraction for each sub video.
plz let me know about this. v hav a big project nd lot of work is
remaining.after background subtraction, we have to resize the image, then
calculation of optical flow and so on……
regards
evleen
try reading this article…….this might be useful………. I’ll update you with more links as I find them
http://www.dspdesignline.com/howto/210000460
I am working on a image processing project which will actually find a free parking slot. I am working on still picture. Initially,i consider that the background is static and the only task is to determine whether there is a car or not. From the discussion above, i assume that i do not need to use background subtraction algorithm. I don’t know exactly. Can you please tell me about the pseudo algorithm or the sequence of library functions to detect the foreground. I am using opencv.
actaully you need an image of parking lot without any cars, and treat it as a reference image.
Then you will have to mark the places of slots where cars can be parked.(Either you can convert the image into a binary one, where white will indicate the slots and black will indicate non-slots).
To detect whether at particular time, is there any slot available, you have to take current snapshot of parking lot, and take difference from the reference image…… Hopefully this will be useful
@Rana Hammad – please help me with my project ,parking slot vacancy detection. the system needs to display how many parking slots available and i use flash and xml to display parking slots avaible. . Please guide me. i used Opencv and visual c++.net.
Thank you and Godbless you Sir!! ^0^;;;
hi Sharmin Khan, we have same project. have you find any solutions how to detect parking slots? and count how many parking slots available?
Hello Champie
Well, that is quiet an interesting project you have. I think you might have figured out how you will update the XML file which will be parsed in Flash (via action scripting or something – I am not in Flash a lot)
Besides, how many camera streams you are receiving? well, that will not matter a lot but you will need to have a combined repository. I think you might have figured that you don’t need movable cameras; basically for this project. You can use movable cameras but that will require more technicality in camera calibration and object handovers.
But initially, you can install camera in such a way that it covers certain parking slots and keeps monitoring them, and gets stream from that camera and generate results for only that specific camera. I hope you might have decided until now, what you will be doing.
I hope you understand the background subtraction technique. Besides, there are a lot of approaches but I try to stick to the basic techniques. If the environment surrounding the parking slots is static (i.e. the lightness etc does not change, there is no effect of weather conditions) then you can store an image of empty parking slots and store it as a reference image. Then you will need to do just frame differencing between the reference image and the images receiving from the camera; to see the difference. You can also do some statistical calculation about how much area of the image will be used to identify it’s a car; but its not necessary. The difference between 2 images will return a binary result and the size of binary blob will let u identify whether there is any object or not. This will let you decide whether any specific parking slot is empty or not.
If conditions are changing then, you will need to use some gaussian approach to keep your reference image modified. But in most of real-time cases, you need to define some constraints; unless the technique will fail all the time.
I hope my blah blah might be of some use to you. Feel free to ask anything
Good luck
Hi Sir Rana,
I’m done in flash and xml for number of available parking slots and update the xml from c++.
Now , i need to know if I should detect every parking-slot ? or detect and count cars in in a parking area?. I’m confused T_T.
I created a page here for my questions(^_^;;;) :
http://stevedriving.zxq.net/temp/index.html
Thank you Sir Rana,.
Godbless you…
Hi Sir Rana,
I’m done in flash and xml for number of available parking slots and update the xml from c++.
Now , i need to know if I should detect every parking-slot ? or detect and count cars in in a parking area?. I’m confused T_T.
I created a page here for my questions(^_^;;;) :
http://stevedriving.zxq.net/temp/index.html
Thank you Sir Rana,.
hey………
thanx
we r thru wid background seperation
next is resizing the image
hope u can help us out wid this…….
plz
OpenCV provides methods of resizing an image. try looking for CVResizeImage method
Hi Sir Rana,
I’m done in flash and xml for number of available parking slots and update the xml from c++.
Now , i need to know if I should detect every parking-slot ? or detect and count cars in in a parking area?. I’m confused T_T.
I created a page here for my questions(^_^;;;) :
http://stevedriving.zxq.net/temp/index.html
Thank you Sir Rana,.
Sorry about may spam Sir. T_T huhu
Hello Champie,
You have done great so far. According to me, if I have camera installed at a position very similar to yours, I would go for normal keeping track of cars entering/exiting the parking area and tallying it with the total parking slots. Initially, this can be challenging too. You will have to detect that a CAR has entered/exited. A tough task will be to eliminate noises/other objects.
Background Subtraction:
You should google around to find the CVGaussian example of background subtraction in OpenCV. That provides 2 variables namely: background and foreground. These are colored images.
Making Binary Image:
You will have to write a function/procedure yourself. That function will take in the colored image and threshold value (between 0-255). The function will convert the colored image into grayscale to bring the size of image from 3/4 channels to 1 channel. Then use resultant image for parsing. Start parsing the color values in Row X Height format (i.e. using nested loops). Get the value of pixel and if it’s value is less than threshold replace it with 0 else 255 and move on. After parsing, return the resultant image. Besides you can create another 3/4 channel image with the resultant image (i.e. convert 1-channel image to the original image channels) and return it….it’s all up to you
Good luck
THANK YOU THANK YOU SO MUCH SIR RANA!!!
YES, I GOT IT!!
I also use ROI and now it run well. thank you!!.. I will post my code here after i finish it and share it to Everyone who is interested to learn OpenCV thank you again sir!!
“Happiness only real when shared… -Alexander SuperTramp-”
See at the following publication:
http://hal.archives-ouvertes.fr/docs/00/33/82/…/RPCS_2008.pdf
and fuzzy version of the GMM:
http://hal.archives-ouvertes.fr/hal-00336512_v1/
Best regards!
Thierry
Thanx Thierry for sharing your search………. hopefully interested people might find it useful…… thanx again
Free Templates Design
free online website builder
Create a music website
Create a nonprofit organization website
Create an ecommerce website
I am usually not the kind of person to write comments on people’s blogs, but for your write up I simply had to do it. I’ve been digging through your site a lot nowadays and I am really impressed, I think you might really emerge as the main voices for your niche. Not sure what your free time is like in life, but if you started commiting more time to posting on this site, I would bet you would start receiving a lot of traffic soon. With affiliate stuff, it might emerge as a great second income source. Just a concept to ponder. Good luck!
Thanx a lot Ms. Marica Burlison for your thoughts. I may have not been very regular in posting at my blog; but your comment has given me a reason to reconsider and participate actively in this; my second life. Thanx again for this. I think I needed a little push.
Great points…I would note that as someone who really doesn’t comment to blogs much (in fact, this may be my first post), I don’t think the term “lurker” is very flattering to a non-posting reader. It’s not your fault really , but perhaps the blogosphere could come up with a better, non-creepy name for the 90% of us that enjoy just reading the content .
Thanx for posting you first post in my blog. Please suggest me, what kind of posts or information should I share here. That will be a great help. Thanx again
I can’t find how to subscribe to the comments via feedburner. I want to keep up to date on this, how do I do that?
Aloha, have you found a way to subscribe to the comments? Please share it with me. I thought that RSS feeds of comments are available and those can be added easily. But still, share your experience, if you have found something
Just checking out your post on my brand new Blackberry , and I wanted to check if it would allow me comment or if it was going to me go to a full pc to do that. Ill check back later to see if it worked.
I am really honored, that you tried to check my post on your Blackberry & I assure you that it has allowed you to comment here.
Thanx for your time. Please guide me that how can I improve my writing, posts, information here at my blog. That will be just great
Thanx again
I can’t find how to subscribe to the comments via feed . I want to keep on top of this, how do I do that?
Thanks a ton for this, I am greatful for the info
guten tag,
möchte meine erfahrungen mit dem webhoster heihachi (http://heihachi.net) mit anderen sharen.
man kann anonym per paysafecard, webmoney oder liberty reserve bezahlen, ich hab per paysafecard bezahlt. persönliche daten wie addresse interessiert heihachi nicht.
die server stehen in russland und sich abusesicher.
heihachi ist einer der größten szenehoster in deutschland, wenn nicht sogar der größte. zwar steht dieser webhoster häufig in kritik, aber das sind in der regel bloß hater oder pesonen, die keine ahnung haben
die bearbeitungszeit der bestellung (ich hab mir einen webspace gekauft) war ok, habe insgesamt 12 Stunden gewartet.
der webspace hat eine ddos protection inklusive, die auch ganz gut klappt. ich konnte bislang keine größere downtime feststellen.
die geschwindigkeit des servers ist immer akzeptabel bis sehr gut
der support ist freundlich und antwortet immer sehr schnell und ausführlich.
ich erhalte meistens eine antwortet innerhalb von einer stunde.
allerdings dauern einzahlungen (die werden per ticket gemacht) um die 12 stunden, also etwas längert.
kurz:
heihachi / heihachi.net ist ein guter webhoster mit dem ich sehr gute erfahrung gesammelt hab, ich kann diesen provider empfehlen.
die server stehen in russland, man kann anonym hosten, abusemails werden ignoriert, was will man mehr?
mit freundlichen grüßen,
MurMel2009
————————————
The translation of above text is as follow:
good day,
would like my experiences with the Heihachi webhoster (http://heihachi.net) share with others.
You can pay anonymously via paysafecard, or webmoney liberty reserve, I have paid by paysafecard. personal data such as addresse Heihachi not interested.
the servers are in russia and abusesicher.
Heihachi hoster is one of the greatest scene in Germany, if not the largest. Although this is often webhoster in criticism, but those are the only regular hater or Pesonen, have no idea
The delivery time (I bought me a webspace) was ok, waited a total of 12 hours.
the webspace has a ddos protection included, which also works quite well. I could not yet find more down time.
the speed of the servers is always acceptable to very good
the support is friendly and always responds very quickly and in detail.
I usually get a response within one hour.
However, take receipts (which are made by ticket) to the 12 hours, a little extended.
short:
Heihachi / heihachi.net is a good web host with whom I have gained very good experience, I can recommend this provider.
the servers are in russia, you can host anonymous abusemails be ignored, what more could you want?
with friendly greetings,
MurMel2009
Hey people, excellent forum I find It very accessible and it has helped me out a great deal
I hope to be able to give something back & assist other users like this website has helped me
_________________
pwnage unlock
This is a really great website. The layout and design is very easy to navigate and the info is superb. Keep up the good work!!!!
Thanx a lot for your appreciation
take care
Je vous presente un nouveau blog qui vient d’ouvrir sur le theme de la mutuelle vous trouverai toutes les infos utiles
blog mutuelle
Je vous remercie
I’m waiting for the store bought frozen lasagna to cook and I found your article. It’s a decent read. Thanks
Hi sir..! can you please help me.. just want to to know more ideas on how i can recognize sign language using hmm.im really confused.. how can i make an output for example it detected a sign language “B”,how can i make a text beside the detected sign and how can i make an output text “B” . i really need your duide sir. thank you:)
Hello, I think you need to go through the following paper.
“An image processing technique for the translation of ASL finger-spelling to digital audio or text” by Chance M. Glenn , Divya M , Kanthi Sarella , Muhammed Lonon
You can download the paper at the following link:
http://www.rit.edu/~w-tecsym/papers/2005/M1D.pdf
I hope you might find it useful
Thanks for sharing. Share is caring after all.
Hi can able to help in this code its going to infinite loop i am using opencv 1.0
/////////////////////////////////////////////////
#include “cv.h”
#include “cvaux.h”
#include “highgui.h”
#include
int main(int argc,char** argv)
{
IplImage* tmp_frame=NULL;
CvCapture* cap=cvCaptureFromFile(../data/Eye.avi);
//cap=cvCaptureFromFile(argv[1]);
tmp_frame=cvQueryFrame(cap);
if(!tmp_frame)
{
printf(“bad video\n”);
exit(0);
}
cvNamedWindow(“BG”);
cvNamedWindow(“FG”);
CvBGStatModel* bg_model=cvCreateGaussianBGModel(tmp_frame);
for(int fr=1;tmp_frame;fr++)
{
double t=(double)cvGetTickCount();
cvUpdateBGStatModel(tmp_frame,bg_model);
t=(double)cvGetTickCount()-t;
printf(“%.1f\n”,t/(cvGetTickFrequency()*1000));
cvShowImage(“BG”,bg_model->background);
cvShowImage(“FG”,bg_model->foreground);
int k=cvWaitKey(5);
if(k==27)break;
}
cvReleaseBGStatModel(&bg_model);
cvReleaseCapture(&cap);
return 0;
}
//////////////////////////////////////
and one more thing i am dont much about open cv can able to help in this regard
1.if read a video how to extract a frame from that ?
2.after doing process how to make a video file from the processed frames ?
please help in these .
these thing i can bale to do in matlab but i dont know in vc++/opencv
Regards
G.Dorababu
Grand poteau ! Merci pour prendre le temps d’écrire quelque chose qui est réellement en valeur la lecture. Trop souvent je trouve l’information inutile et pas quelque chose qui sont réellement appropriées. Merci pour votre dur labeur.
Je vous remercie d’avoir apprécié mes efforts. J’ai toujours l’impression que si je sais quelque chose, il est préférable de le partager avec d’autres, elle augmente ma connaissance aussi. Merci encore.
I found your blog on google and read a few of your other posts. I just added you to my Google News Reader. Keep up the good work. Look forward to reading more from you in the future.
An fascinating concept this. I’m 1 of those men and women whom tend to wait for things to mature prior to taking action but in this case I’m mindful that inaction leads to only failures so I will heed your comments and begin to do anything about it.
Hi
im working on an image processing project using Matlab.. my project based on tracking objects i already extract the video into grey raw frames and using matlab i saved it into one file in matlab called Main_File….now i’m supouse to apply background subtraction on that frames……if you know any source of code using matlab to do my task
i’m trying to check on the open-cv hopefully i’ll find a matlab code to do background subtraction
thanks alot
by the way i gathered the frames into .y video using matlab
Hi Rana Hammad,
I am final year student in software engineering and I am stuck with my final year project where I am trying to count number of vehicles of a road intersection. So I need clarify loads of things from you. Is there a possible way I can contact you? Please reply as soon as possible.
( My e-mail address ashrox303@hotmail.com)
Thanks.
Did not knew you implemented this.
Mohsen Ali
Hello Mohsen Bhai,
why do you ask?
hello sir……v need matlab code for background subtraction for video analytics
Hello sir, v need matlab code for backgroun subtraction for motion detection
Hello Sir, I m working on an image processing project using Matlab.. my project based on Qualitative Polymer Film casting using image processing. My aim is to detect three defects in film casting i already extract the video into grey raw frames and using matlab i saved it into one file in matlab called Main_File….now i’m supouse to apply background subtraction on that frames……if you know any source of code using matlab to do my task
thanks alot
Sir can u please help me get a cdoe for implementation of running gaussian method for background subtraction…plzzzzzzzzzzz….need it urgently,,,!!!!
Sir can u please help me get a cdoe for implementation of running gaussian method for background subtraction…in a video….plzzzzzzzzzzz….need it urgently,,,!!!!