video tracking
in
Core Library Questions
•
2 years ago
Hi,
I'm working on my first Processing project and I'm doing video tracking and I need to do that when person located in a specific areato activate an action such as playing a video or sound.
my code is this:
I'm working on my first Processing project and I'm doing video tracking and I need to do that when person located in a specific areato activate an action such as playing a video or sound.
my code is this:
import processing.video.*;
// Variable para capturar video
Capture video;
// frame Previo
PImage prevFrame;
PImage shipSprite;//variable de la nave
PImage bg;
// variable que define cual debe ser la diferencia de pixel para considerarlo movimiento de pixels
float threshold = 50;
void setup() {
size(1029,683);
video = new Capture(this, width, height, 15);
// crea una imagen con el tamaño del video
prevFrame = createImage(video.width,video.height,RGB);
bg = loadImage("fondo.png");
shipSprite=loadImage("nave.png");//traer imagen al archivo
}
void draw() {
background(bg);//pone la imagen de fondo
image(video,0,0);//borra la captura de la imagen y muestra el background
// Captura video
if (video.available()) {
// guarda el frame previo para comparar el movimiento
prevFrame.copy(video,0,0,video.width,video.height,0,0,video.width,video.height);
prevFrame.updatePixels();
video.read();
}
loadPixels();
video.loadPixels();
prevFrame.loadPixels();
// variables necesarias para encontrar le promedio X y Y de movimientos
float sumX = 0;
float sumY = 0;
int motionCount = 0;
// compara contodos los pixels
for (int x = 0; x < video.width; x++ ) {
for (int y = 0; y < video.height; y++ ) {
// color actual
color current = video.pixels[x+y*video.width];
// color previo
color previous = prevFrame.pixels[x+y*video.width];
// Step 4, compare colors (previous vs. current)
float r1 = red(current);
float g1 = green(current);
float b1 = blue(current);
float r2 = red(previous);
float g2 = green(previous);
float b2 = blue(previous);
// compara la diferencia entre el color actualmy el previo.
float diff = dist(r1,g1,b1,r2,g2,b2);
// si e movimiento de color es mayor al top lo guada
if (diff > threshold) {
sumX += x;
sumY += y;
motionCount++;
}
}
}
// haya el promedio de movimiento.
float avgX = sumX / motionCount;
float avgY = sumY / motionCount;
// dibuja la nave en el promedio de movimiento
image(shipSprite,avgX,600);
}
1