The basics of background substraction
This tutorial explain the basics of background substraction. First of all we need define what is a background and what is a foreground.
We consider a background the pixels of image without motion. And a foreground the pixels with motion. Then the simplest background model assume each background pixel his brightness varies independently with normal distribution. Then we can calculate our statistical model of background by accumulating several dozens of frames and his squares, this is:
Then the average is:
And need the standar derivation for our statistical model:
Then, when we have our statistical background model, we can give a image and divide it in background and foreground, the pixels that are in foreground are the P(x,y) that met this condition:
For optimize a litle the operations we can use this for evaluate the condition:
And is a constant or variable and we can set it to 3, it is the well-known “three sigmas” rule.
Ok, then we now have the theory, then, we go to implement it.
To develop it opencv give us some functions, cvAcc and cvSquareAcc
cvAcc(frame,acc,NULL); cvSquareAcc(frame, sqacc, NULL); N++; cvConvertScale(acc, M, (double)(1.0/N), 0); cvConvertScale(sqacc, sqaccM, (double)(1.0/N),0); cvMul( M, M, M2, 1 ); cvSub( sqaccM, M2, sig2, NULL);//The sig is sig2 //For detect FG Condition cvConvertScale(sig2, lambda_sig2, (double)9, 0); cvSub(M, frame, leftCondition, NULL); cvMul(leftCond, leftCond, leftCond2, NULL); //Compare leftCondition > lambda_sig2 //to detect foreground.
This code is an example and it’s no completed.
In next tutorial a more advanced background substranction.