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.
Page Index Toggle Pages: 1
Translating pixel motion into color (Read 682 times)
Translating pixel motion into color
Mar 20th, 2007, 5:43pm
 
Hi,
I am trying to translate pixel motion into colors. Such that the resulting motion captured with a webcam is displayed as X color when motion is detected. And when there is no motion the resulting video is black.  
 
It would be interesting if possible to set slow motion as blue and fast motion as red (I am unsure if this is possible).  
 
I would appreciate some help from anyone who can shed some light on my question. Thank you.
-Diego
Re: Translating pixel motion into color
Reply #1 - Mar 22nd, 2007, 12:12am
 
This is what I have gotten so far. Any suggestions to improve the code are welcome.

Quote:


import processing.video.*;

Capture camera;
int[] oldPixels;

void setup()
{
 size(640, 480);
 oldPixels = new int[640*480];
 println(Capture.list());
 camera = new Capture(this, 640, 480, 30);
}

void draw(){
 if (camera.available()) {
   camera.read();
   
   image(camera, 0, 0);

 loadPixels();
 for(int i=0;i<pixels.length;i++)
{
 int c=pixels[i];
 int r=(c>>16)&0x000000FF;
 int g=(c>>8)&0x000000FF;
 int b=c&0x000000FF;
 
 int c2=oldPixels[i];
 int r2=(c2>>16)&0x000000FF;
 int g2=(c2>>8)&0x000000FF;
 int b2=c2&0x000000FF;

 int diffR= r2-r;
 int diffG = g2- g;
 int diffB= b2-b;

 int totalDiff = abs(diffR)+abs(diffG)+abs(diffB);
 float percentDiff = totalDiff/((float) 300.0);
 if(percentDiff>1) percentDiff=1;
 
 int rNew = (int) round(255*percentDiff);
 int gNew = 0;
 int bNew = (int) round( 255 *(((float)1.0) - percentDiff));
 
 oldPixels[i] = pixels[i];
 pixels[i]=0xFF000000|(rNew<<16)|(gNew<<8)|bNew;
}
updatePixels();
}
}


Page Index Toggle Pages: 1