Book Image

OpenCV 3.0 Computer Vision with Java

By : Daniel Lelis Baggio
Book Image

OpenCV 3.0 Computer Vision with Java

By: Daniel Lelis Baggio

Overview of this book

Table of Contents (15 chapters)
OpenCV 3.0 Computer Vision with Java
Credits
About the Author
Acknowledgment
About the Reviewers
www.PacktPub.com
Preface
Index

Frame differencing


It should be straightforward to think of a simple background subtraction in order to retrieve foreground objects. A simple solution could look similar to the following line of code:

Core.absdiff(backgroundImage,inputImage , foregroundImage);

This function simply subtracts each pixel of backgroundImage from inputImage and writes its absolute value in foregroundImage. As long as we have initialized the background to backgroundImage and we have that clear from objects, this could work as a simple solution.

Here follows the background subtraction video processor code:

public class AbsDifferenceBackground implements VideoProcessor {
  private Mat backgroundImage;

  public AbsDifferenceBackground(Mat backgroundImage) {
    this.backgroundImage = backgroundImage;
  }

  public Mat process(Mat inputImage) {
    Mat foregroundImage = new Mat();
    Core.absdiff(backgroundImage,inputImage , foregroundImage);
    return foregroundImage;
  }

}

The main method, process, is really simple...