-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHackNights_05BackgroundDifferencing.pde
More file actions
61 lines (47 loc) · 1.35 KB
/
HackNights_05BackgroundDifferencing.pde
File metadata and controls
61 lines (47 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import processing.video.*;
// this is the number of pixels that have to differ from background.
int BACKGROUND_THRESHOLD = 300000;
int numPixels;
boolean debug = false;
Capture video;
PImage frameDiff, backgroundFrame;
void setup() {
size(640, 480);
frameRate(30);
video = new Capture(this, width, height);
video.start();
frameDiff = createImage(width, height, ARGB);
backgroundFrame = createImage(width, height, ARGB);
numPixels = video.width * video.height;
stroke(255, 0, 0);
strokeWeight(4);
noFill();
}
void draw() {
if (video.available()) {
video.read();
video.loadPixels();
frameDiff.loadPixels();
backgroundFrame.loadPixels();
int backgroundSum = 0;
for (int i = 0; i < numPixels; i++) {
int frameDiffB = abs((video.pixels[i]&0xFF) - (backgroundFrame.pixels[i]&0xFF));
backgroundSum += (frameDiffB>64)?frameDiffB:0;
frameDiff.pixels[i] = color(frameDiffB);
}
frameDiff.updatePixels();
// draw stuff
background(0);
image(debug?frameDiff:video, 0, 0);
if (backgroundSum > BACKGROUND_THRESHOLD) {
rect(4, 4, width-8, height-8);
}
}
}
void keyPressed() {
if (key == ' ' || key == 'd' || key == 'D') {
debug = !debug;
} else if (key == 'c' || key == 'C') {
backgroundFrame.copy(video, 0, 0, video.width, video.height, 0, 0, video.width, video.height);
}
}