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
Color Range Tracking/Recognition? (Read 1900 times)
Color Range Tracking/Recognition?
Mar 13th, 2007, 2:26am
 
Hi,
I am trying to achieve a specific effect with video capture, such that I can isolate color ranges and the resulting video frames are only composed of RGB.

To further explain what I am trying to achieve: I want a webcam to recognize a range of Reds, Greens, and Blues and assign that range to Pure Red, Pure Green or Pure Blue. Such that the resulting video capture would only be comprised of 3 colors.

I am a Processing beginner, so any help regarding this would be appreciated. I am working on a graphic design color project for my MA.

thanks - diego
Re: Color Range Tracking/Recognition?
Reply #1 - Mar 13th, 2007, 11:05am
 
If you just want each pixel to be the colour of the most dominant component, you could use something along the lines of:

Code:
captureImage.loadPixels();
for(int i=0;i<captureImage.pixels.length;i++)
{
int c=captureImage.pixels[i];
int r=(c>>16)&0x000000FF;
int g=(c>>8)&0x000000FF;
int b=c&0x000000FF;
if(r>g && r>b)
{
r=255;
g=0;
b=0;
}
else if(g>r && g>b)
{
r=0;
g=255;
b=0;
}
else if(b>r && b>g)
{
r=0;
g=0;
b=255;
}
else
{
//red green and blue are equal... what do you do?
//lets go for white.. maybe a bad idea..
r=255;
g=255;
b=255;
}
captureImage.pixels[i]=(r<<16)|(g<<8)|b;
//I can't remember if the alpha needs to be set... if the
//above is transaprent, try:
//captureImage.pixels[i]=0xFF000000|(r<<16)|(g<<8)|b;
}
captureImage.updatePixels();
Re: Color Range Tracking/Recognition?
Reply #2 - Mar 13th, 2007, 8:20pm
 
JohnG,
Thanks for you help! Your code helped me solve my problem.
Here is the final code:

import processing.video.*;

Capture camera;

void setup()
{
 size(320, 240);
 println(Capture.list());
 camera = new Capture(this, 320, 240, 12);
}
void captureEvent(Capture camera)
{
camera.read();
}

void draw()
{
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;
 if(r>g && r>b)
 {
   r=255;
   g=0;
   b=0;
 }
 else if(g>r && g>b)
 {
   r=0;
   g=255;
   b=0;
 }
 else if(b>r && b>g)
 {
   r=0;
   g=0;
   b=255;
 }
 else
 {
   r=255;
   g=255;
   b=255;
 }
 pixels[i]=0xFF000000|(r<<16)|(g<<8)|b;
}
updatePixels();
}
Re: Color Range Tracking/Recognition?
Reply #3 - Mar 13th, 2007, 11:54pm
 
to avoid synchronization problems, use:

Code:

void draw() {
if (camera.available()) {
camera.read();

// your former draw() code here
}
}


and remove captureEvent(), because captureEvent() can be called at any time, so if you're doing heavy lifting on the pixels inside draw(), you'll get a collision where read() hasn't finished inside captureEvent(), but draw is running simultaneously.

i realize this may be wrong in some of the examples, we're getting this sorted out.
Re: Color Range Tracking/Recognition?
Reply #4 - Mar 14th, 2007, 2:43am
 
Thanks fry, I made the appropriate changes. Cheers.
Page Index Toggle Pages: 1