Skip to content

Instantly share code, notes, and snippets.

@eldog eldog/BackgroundMask.pde
Last active Feb 6, 2017

Embed
What would you like to do?
// Import Libraries - install these if necessary.
import processing.video.*;
import gab.opencv.*;
//create Capture
Capture liveCam;
//create PImages
PImage camCapture, foregroundMask, backgroundMask, screenshot;
float comparVal = 0.25;
OpenCV opencv;
int width = 640;
int height = 480;
void setup() {
//scene setup
fullScreen();
colorMode(HSB,1,1,1);
liveCam = new Capture(this,width,height);
camCapture = createImage(width,height,HSB);
screenshot = createImage(width,height,HSB);
foregroundMask = createImage(width,height,HSB);
backgroundMask = createImage(width,height,HSB);
// Create the opencv object in setup, not in the draw call.
opencv = new OpenCV(this, foregroundMask);
//live Camera start
liveCam.start();
}
void draw() {
if (liveCam.available()) {
liveCam.read();
}
//set camCapture PImage to be current Frame
camCapture.set(0,0,liveCam);
camCapture.loadPixels();
opencv.loadImage(screenshot);
// Don't do the processing in processing (Java), let opencv do the heavy lifting (save the CPU and the frames!).
opencv.diff(camCapture);
opencv.blur(6);
opencv.threshold(50);
opencv.erode();
opencv.erode();
opencv.erode();
opencv.dilate();
opencv.dilate();
opencv.dilate();
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
You can’t perform that action at this time.