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
identifying the color in opencv (Read 3560 times)
identifying the color in opencv
May 20th, 2009, 8:16pm
 
hi,
  i am the beginner in opencv. i need to find the percentage of green, yellow and brown  color in an image.
  the problem is  there is variation of rgb values  of these 3 colors.  

currently i am working on an leaf image and i need to find the amount of yellow,green and brown color present in it.....
 
 please help me.....

    thanks.......
Re: identifying the color in opencv
Reply #1 - May 22nd, 2009, 1:07am
 
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
Re: identifying the color in opencv
Reply #2 - May 24th, 2009, 6:57am
 
i am an beginner in computer vision. i know only OpenCV, so i didn't understand the code. i pretty sure its in C but what library have you used?

Thank you..
Re: identifying the color in opencv
Reply #3 - May 24th, 2009, 8:03am
 
the code is pure processing: you don't need a specific library to track colors.

what exactly you don't understand?
Page Index Toggle Pages: 1