I'm quite new to processing, and I needed to make a code to make a video run while people can walk in and see themselves in front of the video. At the same time, as time goes by, the capture has to pixelate. That means there is a backgroud substraction, a pixelation AND a video looping as background.
I somehow maganed to make the code, but i feel it's messier than needed, and also, it runs too slowly. Oh! And the background substraction isn't working well. That makes for three big problems.
So, the question is, HELL, HOW CAN I MAKE IT RUN SMOOTH AND GOOD? I don't know a way to make it better, does anyone have a suggestion? Any help would be greatly appreciated!
Oh, yes,
Code:
import processing.video.*;
PImage imagen;
int hayMovimiento, minMovimiento, pixel, numPixels, cols, fils, counter, lim;
int[] mascara;
color colorActual, colorMascara;
color[] pixelStorage;
boolean iniciar;
Capture video;
Movie fondo;
void setup () {
size ( 640, 480, P2D );
lim = 15;
counter = 0;
minMovimiento = 50000;
iniciar = false;
video = new Capture ( this, width, height, 12 );
fondo = new Movie (this, "formatos2.mov");
numPixels = video.width * video.height;
mascara = new int[numPixels];
pixelStorage = new color[numPixels];
loadPixels ();
}
void draw () {
fondo.loop ();
image ( fondo, 0, 0 );
if ( video.available () ) {
video.read ();
video.loadPixels ();
hayMovimiento = 0;
for ( int i = 0; i < numPixels; i++ ) {
colorMascara = mascara[i];
colorActual = video.pixels[i];
int actR = colorActual >> 16 & 0xFF;
int actG = colorActual >> 8 & 0xFF;
int actB = colorActual & 0xFF;
int mascR = colorMascara >> 16 & 0xFF;
int mascG = colorMascara >> 8 & 0xFF;
int mascB = colorMascara & 0xFF;
int difR = abs ( actR - mascR );
int difG = abs ( actG - mascG );
int difB = abs ( actB - mascB );
if ( difR > lim && difG > lim && difB > lim ) {
pixelStorage[i] = color ( actR, actG, actB );
hayMovimiento = hayMovimiento + 1;
}
}
if ( hayMovimiento > minMovimiento && counter == 0 && iniciar == true) {
counter = millis () + 5000;
fondo.jump ( 0 );
} else if ( hayMovimiento < minMovimiento ) {
if ( fondo.time () > 5 ) {
fondo.jump ( 0 );
}
counter = 0;
pixel = 1;
}
if ( millis () >= counter && millis () <= counter + 55000 && counter != 0 ) {
pixel = ceil ( ( millis () - counter ) / 1000 ) + 1;
} else if ( millis () > counter + 55000 ) {
counter = 0;
pixel = 1;
} else {
pixel = 1;
}
for ( int j = 0; j < width; j = j + pixel ) {
for ( int k = 0; k < height; k = k + pixel ) {
int m = k * width + j;
if ( pixelStorage[m] == video.pixels[m] ) {
color rellenoPixel = pixelStorage[m];
fill ( rellenoPixel );
noStroke ();
rect ( j, k, pixel, pixel );
}
}
}
println(hayMovimiento); //This is for debug only
}
}
void mousePressed () {
iniciar = true;
video.loadPixels ();
arraycopy (video.pixels, mascara);
}
The pixelation starts when somebody steps in front of the camera (hence the "hayMovimiento" variable), and the 60 secondvídeo loops its first secons while there's nobody, and goes further on the opposite case. Also, the video has been rendered to weight 3MB. I think that should not be a problem...
A last note: English is not my natural tongue...