Simple Image Subtract

edited March 2015 in Share Your Work

I found a simple way to perform an image subtraction in a Processing sketch. This was to use an image from the background of a scene and an image from the current.

By drawing the current image first, then using the blendMode(SUBTRACT) code, we can draw the image to do the subtraction next. The result is the background removed from the frame and only the new pixels are shown. This can then be sent out via a Syphon Server and further processed.

To note, you will need to snap your own images for this. You can use a webcam or a photo you have already. It is rudimentary, but hey; it's simple.

// Simple Image Subtract

//    This allows one to apply an image subtraction process by simply
//    using a previously taken image of a clean scene and using it
//    under the blendMode(SUBTRACT) code. This removes the background image
//    and lets new movement shine through.

//      By Casey Scalf of www.Sensebellum.com

import codeanticode.syphon.*;

PImage current, remove, syphon;
PGraphics canvas;

// Use with a Syphon Client
// SyphonClient syphonIn;

SyphonServer syphonOut;
int mX;

void setup() {

  size(640, 480, P3D);
  canvas = createGraphics(width, height, P3D);
  //  Use with a Syphon Client
  //  syphonIn = new SyphonClient(this);

  syphonOut = new SyphonServer(this, "Simpl Image Subtraction");

  // Load an image of the background
  remove = loadImage("Your Subtraction Image.jpg");

  // Load an image of the new scene 
  current = loadImage("Your Current Image.jpg");
}

void draw() {
  background(0);

  //  Use with a Syphon Client
  //  syphon = syphonIn.getImage(syphon);

  canvas.beginDraw();
  canvas.background(0);
  canvas.blendMode(NORMAL);
  canvas.image(current, 0, 0);
  canvas.blendMode(SUBTRACT);
  canvas.image(remove, 0, 0);
  canvas.endDraw();

  // This actually draws the image to Processing or else it would only go out
  // via the Syphon Server
  image(canvas, 0, 0);

  // Send image out via our Syphon Server
  syphonOut.sendImage(canvas);
}
Tagged:
Sign In or Register to comment.