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 & HelpPrograms › Color Detection
Page Index Toggle Pages: 1
Color Detection (Read 763 times)
Color Detection
Feb 11th, 2010, 10:57am
 
Hi Everyone, I'm new to these forums and I haven't searched through them to much so I apologize if this has been asked before.  I've seen a lot of examples of color tracking via the camera, but what I want to know is if there is a way to detect computer generated color.

I ask this because I have been assigned a Frogger project where my teacher is trying to make us detect collision depending on if the edges of the frog are touching the edges of the car.  I want to know if this can be done instead by detecting if the frog's color is touching the car's color. It seems like it would look a lot neater than the edge detection way.

Sorry for the long winded message, any help would be great.  Thanks ahead of time!
Re: Color Detection
Reply #1 - Feb 11th, 2010, 11:31am
 
It can be done. As this block of code shows, you can render the objects to their own off-screen buffers, then examine the pixels in those buffers to see if there are any pixels that are different from the background color in both.

Quote:
boolean touching;
PGraphics g2, g3;

void setup(){
  size(200,200, P2D);
  g2 = createGraphics(200,200, P2D);
  g3 = createGraphics(200,200, P2D);
  noStroke();
}

void draw(){
  pushMatrix();

  g2.beginDraw();
  g2.background(0);
  g2.fill(255);
  g2.noStroke();
  g2.rect(20,30,40,50);
  g2.rect(20,30,100,5);
  g2.ellipse(190,190,40,60);
  g2.loadPixels();
  g2.endDraw();

  g3.beginDraw();
  g3.background(128);
  g3.fill(255);
  g3.noStroke();
  g3.rect(mouseX-5,mouseY-5,10,10);
  g3.loadPixels();
  g3.endDraw();

  touching = false;
  for( int i = 0; i < 200*200; i++){
    touching = touching || (g3.pixels[i] == g2.pixels[i]);
  }

  background(!touching?color(128,0,0):color(0,128,0));
  fill(196,0,196);
  rect(20,30,40,50);
  fill(0,0,0);
  rect(20,30,100,5);
  fill(200,100,50);
  ellipse(190,190,40,60);
  fill(196,196,0);
  rect(mouseX-5, mouseY-5, 10, 10);
  popMatrix();
}

Page Index Toggle Pages: 1