Skip to content

Instantly share code, notes, and snippets.

@rafapolo
Created October 9, 2011 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rafapolo/1273897 to your computer and use it in GitHub Desktop.
Save rafapolo/1273897 to your computer and use it in GitHub Desktop.
Detecta movimento na camera e cria rastro dinâmico em video de webcam.
package traildentes;
import processing.core.*;
import processing.video.*;
import java.util.ArrayList;
public class Traildentes extends PApplet {
private static final long serialVersionUID = 1L;
int rastroSize = 10;
float threshold = 100;
Capture video;
PImage prevFrame;
ArrayList<ArrayList<Integer>> rastros = new ArrayList<ArrayList<Integer>>();
int t = 0;
public void setup() {
size(800, 600);
video = new Capture(this, width, height, 30);
prevFrame = createImage(video.width, video.height, RGB);
}
public void draw() {
t++;
if (video.available()) {
prevFrame.copy(video, 0, 0, video.width, video.height, 0, 0,
video.width, video.height);
prevFrame.updatePixels();
video.read();
}
loadPixels();
video.loadPixels();
prevFrame.loadPixels();
ArrayList<Integer> motionAtual = new ArrayList<Integer>();
for (int x = 0; x < video.width; x++) {
for (int y = 0; y < video.height; y++) {
int loc = x + y * video.width;
int current = video.pixels[loc];
int previous = prevFrame.pixels[loc];
float r1 = red(current);
float g1 = green(current);
float b1 = blue(current);
float r2 = red(previous);
float g2 = green(previous);
float b2 = blue(previous);
float diff = dist(r1, g1, b1, r2, g2, b2);
// How different are the colors?
if (diff > threshold) {
motionAtual.add(loc);
} else {
pixels[loc] = color(0);
}
}
}
if (t > 10) { // after 50 draws
rastros.add(motionAtual);
if(rastros.size()==rastroSize){ // array completo? excluir rastro 0
rastros = new ArrayList<ArrayList<Integer>>(rastros.subList(1, rastros.size()));
}
// aplicar rastro
for (int x = 0; x < rastros.size(); x++) {
ArrayList<Integer> motion = rastros.get(x);
// para cada motion
for (int y = 0; y < motion.size(); y++) {
// para cada ponto
int ponto = motion.get(y);
Float R = (new Float(-x*x*10 +255) / rastroSize) * 255;
Float G = (new Float(-(x-rastroSize/2)*(x-rastroSize/2)*10+255) / rastroSize) * 105;
Float B = (new Float(-(x-rastroSize)*(x-rastroSize)*20+255) / rastroSize) * 55;
//Float A = R;
pixels[ponto] = color(R,G,B);
}
}
}
updatePixels();
}
}
@rafapolo
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment