OpenCV for background detection against static image

Greg Borenstein's new OpenCV library includes an example for background detection:

https://github.com/atduskgreg/opencv-processing/blob/master/examples/BackgroundSubtraction/BackgroundSubtraction.pde

…but it assumes you want to detect foreground objects using live video, which changes frame to frame. I want to take a snapshot of the "background" once, and then compare the current video capture image against that (static) snapshot, like in Golan Levin's library that comes with Processing (see File > Examples > Libraries > Video > Capture > BackgroundSubtraction):

https://github.com/processing/processing/blob/master/java/libraries/video/examples/Capture/BackgroundSubtraction/BackgroundSubtraction.pde

Can I use the new OpenCV library to accomplish what Golan does here manually?

If not, can I use Golan's manual approach to background detection, then pass the pixels of that difference image into OpenCV for contour detection and other processing?

Tagged:

Answers

  • updateBackground() is really cool but you're right, for a static background the approach Golan is using is the easiest. There's a nice utility function for it: absdiff(), which Greg has in there as "public static void diff(Mat mat1, Mat mat2)". Just get the difference between the two images and voila, that's your "foreground".

  • Thanks! This looks like what I want. But how do I define the two Mat arguments and pass them in? I can't find the source for the Mat class.

  • edited March 2014

    Figured it out! The method you mentioned is great if you already have two Mat objects and just want to diff them. It was simpler for me to use "public void diff(PImage img)" to calc the difference between a PImage I pass in and the current OpenCV Mat. I have a PImage called bg, so this worked well for me:

      //Update OpenCV object with latest from camera
      opencv.loadImage(video);
    
      //Whenever you press a key, we take a snapshot and save it as our
      //new background image to compare against.
      if (keyPressed) {
        bg = opencv.getSnapshot();
      }
    
      //Calc the difference between the bg image and the OpenCV image
      //and set the OpenCV image to that difference.
      opencv.diff(bg);
    
  • I'm on the lookout for a similar thing, there is a static reference image and a current video capture. I want to compare the video frames with that static frame. The difficult part being, that the program should give the user the instructions to either pan and/or zoom to get to the reference frame. Also, I'm new to programming.

Sign In or Register to comment.