Drawing over body in real time
in
Contributed Library Questions
•
1 year ago
Hi There!
I'm
trying to create
a program where
the user (
captured in
real time through a
web
cam)
can draw
over
his
body.
I tried to use
background subtraction
to delineate
between
the white
and black area
so you can
tell the program to
draw
only
in the white
(which would be
the body area)
...
but failed to
do so. I would very much appreciate if
someone could
help me with
the code. Thanks!
---------Here's my code------------
import codeanticode.gsvideo.*;
import processing.video.*;
Capture camara;
color trackColor; // variable del rastreo del color
int ancho = 640;
int alto = 480;
int posx, posy;
int[] posX = new int[10000];
int[] posY = new int[10000];
int prof = 2;
int j = 0;
void setup() {
size(640, 480, P3D);
camara = new Capture(this, width, height, 30);
for (int i = 0; i < posX.length; i++) { // Inicializa los valores para que no de errores después
posX[i] = -1;
posY[i] = -1;
}
// ---------------------------------------Empieza a rastrear el color rojo
trackColor = color(255, 0, 0);
smooth();
}
void movieEvent(GSMovie movie) {
movie.read();
}
void captureEvent(Capture camara) {
camara.read();
}
void draw() {
camara.loadPixels();//rastrea pixeles
image(camara, 0, 0);
float worldRecord = 100; //tolerancia
// Coordenada X e Y del color más cercano al rastreado
int closestX = 0;
int closestY = 0;
// Inicia un loop para ir por cada pixel.
for (int x = 0; x < camara.width; x ++ ) {
for (int y = 0; y < camara.height; y ++ ) {
int loc = x + y*camara.width;
// El color actual
color currentColor = camara.pixels[loc];
float r1 = red(currentColor);
float g1 = green(currentColor);
float b1 = blue(currentColor);
float r2 = red(trackColor);
float g2 = green(trackColor);
float b2 = blue(trackColor);
float d = dist(r1, g1, b1, r2, g2, b2); // Comparar el color actual con el color que estamos rastreando y da la diferencia
// Si el color actual es más similar (al rastreado) que el color más cercano, se guarda su ubicación y su diferencia de color.
if (d < worldRecord) {
worldRecord = d;
closestX = x;
closestY = y;
}
}
}
/*// Sólo se considera el color encontrado si su distancia en color es menor a 10.
if (worldRecord < 10) {
// Dibuja un círculo en el pixel rastreado
fill(trackColor);
strokeWeight(0.0);
stroke(0);
ellipse(closestX, closestY, 18, 18);
}*/
//----------------------------------------------------------------------------------------------
for (int i = 0; i < posX.length; i++) {
stroke (255, 0, 0);
strokeWeight (3);
// Le da la posición del COLOR a posX y posY
posX[j%posX.length] = closestX;
posY[j%posX.length] = closestY;
if (posX[i] > 0 && i > 50) { // es para que no dibuje las líneas que no tienen la posición del mouse
line (posX[i-1], posY[i-1], posX[i], posY[i]);
}
}
j++;
if (camara.available()) { //pregunto si hay un nuevo fotograma
camara.read();
}
}
void mousePressed() {
// Guarda el color del lugar en donde el mouse haga click
int loc = mouseX + mouseY*camara.width;
trackColor = camara.pixels[loc];
}
1