cvAbsDiff - background overlaying foreground

Hi guys,

I’m implementing some background subtraction using cvAbsDiff, but as output I get my foreground(web cam feed) with the background kind of overlayed…

Here’s a code snippet:

        } else if (frameCount < 100){  
            cvConvert(bp, frame_mat);  
            cvRunningAvg(frame_mat, background_mat, 0.003, 0);  
        } else if (frameCount > 100){  
            cvConvert(background_mat, background_image);  
            cvAbsDiff(bp, background_image, new_image);  
            cvThreshold(new_image, foreground_image, 254, 255, CV_THRESH_BINARY);  
            test = foreground_image;  

The above is only a small snippet of the code, but should be sufficient to se how I’m trying to do the background subtraction…

Does anyone have an idea of why the background is shown? I might be misunderstanding how to use cvAbsDiff…

Below is an image of the output:

As was once said elsewhere on this forum: welcome to computer vision :slight_smile:

I doubt you’re making any mistakes, this is a regular artifact.

The “problem” is that your hand is more similar to the window frame (I think that’s what it is) than it is to the wall. In other words, compared to the wall background your hand stands out – but not compared to the window frame.

One common trick is to control for the environment or lighting. If you can make the foreground better lit than the background, or vice versa, that will help provide a significant color difference from the perspective of the camera.