first thing you need to do is to tell your sketch what do you mean by green, brown, yellow. you can do this by manually setting the values of each color:
Code:
color green = color(0,255,0); // this is easy :)
color brown = color(80,50,10); //some random brown
color yellow = color(255,255,0);
or by sampling them from an image:
Code:
color mybrown = img.pixels[n];
// where img is the image you're sampling from
// and n is a pointer to a brown area
now that you have your colors you'll have to cycle through your leaf image looking for them. probably you want to label under "green", "brown" and "yellow", not only the pixels that exactly match your base colors, but also their nuances, so that you'll be counting ie. every pixel that you could call "green" "brown" or "yellow" in natural language.
to do so, we'll first split our sampled color (or hardwired one) into its r,g,b components:
Code:
int mybrownR = (mybrown >> 16) & 0xff;
int mybrownG = (mybrown >> 8) & 0xff;
int mybrownB = mybrown & 0xff;
// you can do the same with the green and yellow
then we cycle through the leaf image and check if every pixel looks like one of our 3 colors:
Code:
// let's define a thresholding value
int thr = 20;
// img is the image with the leaf
for(int x=0; x<img.width; x++){
for(int y=0; y<img.height; y++){
color current = pixels[y*img.width+x];
// now we split the pixel color into r,g,b
int curR = (current >> 16) & 0xff;
int curG = (current >> 8) & 0xff;
int curB = current & 0xff;
// if the pixel is more or less (->the thr value) like mybrown
if(curR <= mybrownR+thr && curR >= mybrownR-thr
&& curG <= mybrownG+thr && curG >= mybrownG-thr
&& curB <= mybrownB+thr && curB >= mybrownB-thr){
// it's some kind of brown
brownpixels++;
}
}
}
this is the way i normally handle this kind of problems. you'll probably want to also read somthing about
hsv to get a better idea of what "green", "brown" and "yellow" mean