📄 gowiththeflow.pde
字号:
/**
* GoWithTheFlow
*
* A simple demo of background subtraction and silhouette extraction.
*
* by Jorge Cardoso http://jorgecardoso.eu
* January 2008
*
*/
import processing.opengl.*;
import processing.video.*;
BackgroundSubtraction bs = new BackgroundSubtraction(320, 240);
int bgSubtractionThreshold = 170;
boolean saving = false;
boolean showOriginal = false;
// We downsample the silhouette. these are the downsampled coords.
float x[], y[];
MovieMaker mm;
void setup() {
// Change size to 320 x 240 if too slow at 640 x 480
size(800, 600, OPENGL);
frameRate(15);
bs.setSubtractionThreshold(bgSubtractionThreshold);
x= new float[100];
y = new float[100];
for (int i = 0; i < x.length; i++) {
x[i] = y[i] = 0;
}
}
void draw() {
background(0);
bs.update();
bs.subtract();
drawSilhouette();
// just for adjusting the threshold. its nice to see the images
if (showOriginal) {
image(bs.currentFrame(), 0, 0);
image(bs.getSubtractionImage(), 320, 0);
}
// to save a movie
if (saving) {
mm.addFrame(); // Add window's pixels to movie
}
}
void drawSilhouette() {
colorMode(HSB);
strokeWeight(2);
noFill();
// get the biggest silhouette found
Silhouette s = bs.getSilhouette();
// if we have any lets draw it
if (s != null) {
s.setSize(width, height); // in this case its not necessary since we're drawing it ourselves
// draw the downsampled silhouette
beginShape();
curveVertex(x[0], y[0]);
for (int i = 0; i < x.length; i++) {
stroke((255/x.length)*i, 255, 255);
curveVertex(x[i], y[i]);
}
curveVertex(x[0], y[0]);
curveVertex(x[0], y[0]);
endShape();
// downsample and move the previous closer to the current
// with some randoms...
float inc = s.silhouette.length/(float)x.length;
float xdif, ydif;
for (int i = 0; i < x.length/2; i++) {
int index = (int)(random(x.length));
xdif = s.silhouette[(int)(index*inc)][0]*width - x[index];
ydif = s.silhouette[(int)(index*inc)][1]*height - y[index];
x[index] =(int)( x[index]+xdif/(random(5,10)));
y[index] = (int)(y[index]+ydif/(random(5,10)));
}
}
colorMode(RGB);
}
void keyPressed() {
if (key == 'm') { // start/finish movie recording
if (saving) {
mm.finish();
println("Finished saving video");
}
else {
mm = new MovieMaker(this, width, height, "drawing"+frameCount+".mov", 10, MovieMaker.VIDEO, MovieMaker.HIGH);
println("Started saving video");
}
saving = !saving;
}
else
if (key == ' ') {
bs.update();
bs.setBackground();
println("setting bg");
}
else if (key == '+' ) {
bgSubtractionThreshold = bgSubtractionThreshold + 5;
bs.setSubtractionThreshold(bgSubtractionThreshold);
println("Subtraction Threshold: " + bgSubtractionThreshold);
}
else if (key == '-' ) {
bgSubtractionThreshold = bgSubtractionThreshold - 5;
bs.setSubtractionThreshold(bgSubtractionThreshold);
println("Subtraction Threshold: " + bgSubtractionThreshold);
}
else if(key =='s') {
bs.settings();
}
else if(key =='d') { // debug
showOriginal = !showOriginal;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -