Menu

Basic Background Subtractor Producing black screen

Q3Varnam
2014-07-17
2014-07-17
  • Q3Varnam

    Q3Varnam - 2014-07-17
    public class BackgroundSubtractor
    {
        static boolean capture = true;
        public static void main(String args[])
        {
            VideoCapture video;
    
            try
            {
                video = new VideoCapture(640, 480);
                final BasicBackgroundSubtract<MBFImage> background =new BasicBackgroundSubtract<MBFImage>();
    
                VideoDisplay<MBFImage> display = 
    
                VideoDisplay.createVideoDisplay(video);
                display.addVideoListener(new VideoDisplayListener<MBFImage>() {
    
                    @Override
                    public void beforeUpdate(MBFImage frame)
                    {
                        if(capture)
                        {
                            System.out.println("Capturing background in 4 seconds");
                            try
                            {
                                Thread.sleep(4000);
                            }
                            catch (InterruptedException e)
                            {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
    
                            background.setBackground(frame);
                            System.out.println("Captured");
                            capture =false;
                            return;
                        }
                        background.processImage(frame);
                    }
    
                    @Override
                    public void afterUpdate(VideoDisplay<MBFImage> display)
                    {
                        // TODO Auto-generated method stub
    
                    }
                });
            } catch (VideoCaptureException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
    
        }
    }
    

    I am trying out the basic background subtract class as above. But this results in a black screen.

    I try to capture the background when the app starts up and then insert a new object in the frame between the background and the camera expecting the inserted object will be visible.

    Is my use case for this class correct?

     
  • Jonathon Hare

    Jonathon Hare - 2014-07-17

    I think, your code is basically correct, but instead of sleeping for 4000ms, you'll need to capture a few frames to warm up the camera (the first frame will always be black). I'll try and post a code example in a bit.

     
  • Q3Varnam

    Q3Varnam - 2014-07-17

    Thanks for your fast response as usual, in the mean while I tried using it with two static images of same size, it does work but it produces the similar result as a image.subtract(image)

    For video background subtraction, I tried your suggestion, it still didn't work. Then I realised may be frame is a reference.

    Now I am using frame.clone() in

    background.setBackground(frame.clone());

    This works - but the background has lot of noise.

     

Anonymous
Anonymous

Add attachments
Cancel