Skip to content

Instantly share code, notes, and snippets.

@kylemcdonald
Last active December 24, 2015 02:09
Show Gist options
  • Save kylemcdonald/6728319 to your computer and use it in GitHub Desktop.
Save kylemcdonald/6728319 to your computer and use it in GitHub Desktop.
import gab.opencv.*;
import processing.video.*;
Capture video;
OpenCV bg;
OpenCV opencv;
PImage curBg;
PImage colorDiff;
float brightness = 0;
void setup() {
size(640, 480);
video = new Capture(this, width, height);
video.start();
bg = new OpenCV(this, width, height);
opencv = new OpenCV(this, width, height);
curBg = bg.getSnapshot();
updateDiff();
}
void updateDiff() {
opencv.loadImage(video);
opencv.diff(curBg);
colorDiff = opencv.getSnapshot();
}
void draw() {
pushMatrix();
scale(0.5);
image(colorDiff, 0, 0);
image(curBg, width, 0);
popMatrix();
rect(0, 0, brightness, 10);
}
void keyPressed() {
if(key == ' ') {
bg.loadImage(video);
curBg = bg.getSnapshot();
}
}
void captureEvent(Capture c) {
key = ' ';
keyPressed();
c.read();
updateDiff();
brightness = 0;
colorDiff.loadPixels();
for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
brightness += brightness(colorDiff.get(x, y));
}
}
brightness /= (width * height);
println(brightness);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment