Remove moving objects to get the background model from multiple images

Issue

I want to find the background in multiple images captured with a fixed camera. Camera detect moving objects(animal) and captured sequential Images. So I need to find a simple background model image by process 5 to 10 captured images with same background.

Can someone help me please??

Solution

Is your eventual goal to find foreground? Can you show some images?

If animals move fast enough they will create a lot of intensity changes while background pixels will remain closely correlated among most of the frames. I won’t write you real code but will give you a pseudo-code in openCV. The main idea is to average only correlated pixels:

Mat Iseq[10];// your sequence
Mat result, Iacc=0, Icnt=0; // Iacc and Icnt are float types
loop through your sequence, i=0; i<N-1; i++
   matchTemplate(Iseg[i], Iseq[i+1], result, CV_TM_CCOEFF_NORMED);
   mask = 1 & (result>0.9); // get correlated part, which is probably background
   Iacc += Iseq[i] & mask + Iseq[i+1] & mask; // accumulate background infer
   Icnt += 2*mask; // keep count
end of loop;
Mat Ibackground = Iacc.mul(1.0/Icnt); // average background (moving parts fade away) 

To improve the result you may reduce mage resolution or apply blur to enhance correlation. You can also clean every mask from small connected components by erosion, for example.

Answered By – Vlad

Answer Checked By – Pedro (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.