Skip to content

Instantly share code, notes, and snippets.

@eldog
Created February 6, 2017 21:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eldog/82c67589364ecd2df0a93949bb9433ca to your computer and use it in GitHub Desktop.
Save eldog/82c67589364ecd2df0a93949bb9433ca to your computer and use it in GitHub Desktop.
Removes the background using processing.
import processing.video.*;
import gab.opencv.*;
Capture liveCam;
PImage camCapture, foregroundMask, screenshot;
OpenCV opencv;
int width = 640;
int height = 480;
void setup() {
fullScreen();
liveCam = new Capture(this,width,height);
camCapture = createImage(width,height,HSB);
screenshot = createImage(width,height,HSB);
foregroundMask = createImage(width,height,HSB);
opencv = new OpenCV(this, foregroundMask);
liveCam.start();
}
void draw() {
if (liveCam.available()) {
liveCam.read();
}
// Load current webcam frame.
camCapture.set(0,0,liveCam);
camCapture.loadPixels();
opencv.loadImage(camCapture);
// Take the difference between what was set as the background and the current frame.
opencv.diff(screenshot);
opencv.blur(6);
opencv.threshold(50);
opencv.erode();
opencv.erode();
opencv.erode();
opencv.dilate();
opencv.dilate();
opencv.dilate();
// Invert removes the background, remove this to go the other way
opencv.invert();
foregroundMask = opencv.getSnapshot();
camCapture.blend(foregroundMask, 0, 0, width, height, 0, 0, width, height, ADD);
image(camCapture,0,0);
}
void mouseClicked() {
screenshot.set(0,0,liveCam);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment