We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpVideo Capture,  Movie Playback,  Vision Libraries › Making a capture + movie program run faster.
Page Index Toggle Pages: 1
Making a capture + movie program run faster. (Read 984 times)
Making a capture + movie program run faster.
Jun 26th, 2009, 9:48am
 
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...
Re: Making a capture + movie program run faster.
Reply #1 - Jul 6th, 2009, 9:51pm
 
hola,
que tal utilizar multiples threads,
uno principal para la captura y la pixelizacion y otro para el video de fondo.
Ahora mismo no tengo el tiempo para estudiar bien el codigo y hacer sugerencias específicas pero prometo que en cuanto tenga un ratito lo hago
Re: Making a capture + movie program run faster.
Reply #2 - Jul 22nd, 2009, 7:26am
 
Hice algunas pruebas, pero por ahora no he logrado acelerar la performance. El mayor problema es el pixelado de la captura seun parece, pero nopuedo prescindir de el...
Si es que logras hacerte un tiempo para darme una mano, genial. De todos modos, gracias por la atención!
Page Index Toggle Pages: 1