Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.