CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 39

Hybrid View

  1. #1
    Join Date
    Aug 2004
    Posts
    2

    Color Detection Algorithm

    I am trying to write an algorithm for detection of the color green. The thing is that if I try detecting the shade of a pixel in a picture, there are more than a million different shades possible. How do I sift thourgh all these different shades and detect which ones are green ? Does anyone know of a simple way to do it or of any prevelant algorithms that does it for you ?

  2. #2
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    Assuming that your picture uses some form of RGB format to store the color for per pixel, you can distinguish if the color of a pixel is green if both red and blue colors are zero.

  3. #3
    Join Date
    Dec 2001
    Location
    Greece, Athens
    Posts
    1,015
    One of the most popular methods used for this puprose is using the vector distance. I've used this in the past for detecting colors and it works fine. Here is a brief explanation:
    Let's say you are searching for "green" pixels. Pure green exists when the values of the pixels are:
    R = 0
    G = 255
    B = 0

    Though, when searching for green you may want to have a "tolerance" meaning that you don't only search for pure green, but also for some other green-like combinations. Suppose that each pixel is a vector:
    pixel = [R G B]
    Also, the color combination you are looking for is another vector:
    green = [0 255 0] (reference color)

    The eucledian distance between those 2 vectors is:
    D = sqrt((R-0)^2 + (G-255)^2 + (B-0)^2)
    (in the place of 0 and 255 you can place whatever you want, according on the color combination you are looking for...)

    So the algorithm goes:
    Code:
    for each pixel 
    {
       1. compute the distance between that pixel and the reference color (D)
       2. if D<Threshold
               then 
                    current pixel is accepted
               else
                    current pixel is NOT accepted               
    }
    PS1: "Threshold" is a constant value and has to bee set manually according on the "tolerance" you want to give to your algorithm (a high value of Threshold means that also less "clear" green pixels will be considered as green, whereas a lower value of Threshold will make the algorithm quite "strict").

    PS2: Threshold can also be computed using addaptive algorithms but this is quite difficult

    PS3: I've tested this simple algorithm using Matlab and it works fine, so if interested tell me to send you the code.

    Hope this helped u.
    Theodore
    Personal Web Page (some audio segmentation tools): www.di.uoa.gr/~tyiannak

  4. #4
    Join Date
    Dec 2001
    Location
    Greece, Athens
    Posts
    1,015
    Also, you could try image segmentation technuiqes like:
    - fuzzy logic
    - k - means vector quantization
    Theodore
    Personal Web Page (some audio segmentation tools): www.di.uoa.gr/~tyiannak

  5. #5
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176

    Re: Color Detection Algorithm

    I think you should give more tolerance to the brightness. To do so, norm the color vectors by 1 and then distance is distance between the resulting vectors+some cooff*difference in length.
    "Programs must be written for people to read, and only incidentally for machines to execute."

  6. #6
    Join Date
    Dec 2001
    Location
    Greece, Athens
    Posts
    1,015

    Re: Color Detection Algorithm

    Nice idea RoboTact. Though, that depends on what the seach is done for. Maybe the user doesn't want to give tolerance to brightness. But if he does, then you are right, that by normalizing the RGB coefficients he achieves that.
    Another way to do this is to execute histogram equalisation in each coefficient (R,G,B), or even better to transform the RGB matrix to HSI and perform histogram equalisation to the I coefficient only.
    Theodore
    Personal Web Page (some audio segmentation tools): www.di.uoa.gr/~tyiannak

  7. #7
    Join Date
    Feb 2007
    Posts
    2

    Re: Color Detection Algorithm

    hello yiannakop
    please i want the matlab code because i need it urgently and as fast as possible in my graduation project thnx a lot.

  8. #8
    Join Date
    Feb 2007
    Posts
    2

    Re: Color Detection Algorithm

    please i want the matlab code ,it's very urgent
    my mail is kimohoss@hotmail.com
    please iu want it as soon as possible

  9. #9
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Color Detection Algorithm

    You realize this post is almost 2 years old. It's possible Theodore isn't reading this forum anymore.

    Viggy

  10. #10
    Join Date
    Dec 2001
    Location
    Greece, Athens
    Posts
    1,015

    Re: Color Detection Algorithm

    Well, that's true, I had not read the thread for a long period of time.
    Anyway, probably it's too late, but here some code I wrote for color detection (though, the pseodo code I had given was, in my opinion quite clear and easy to be transposed to matlab code).

    Anyway, the following code is a matlab function that takes the following arguments:
    1) RGB: a MxNx3 matrix of the RGB values
    2) mode: this argument should take the values 1,2 or 3, depending on the color you want to detect (red, green or blue).
    3) thres: a threshold that is used to detect the desired color in the following way: if the desired color is LARGER than thres * (each of the other two colors), then the current pixel is labelled as 1 (color found).

    Code:
    function I = findColor(RGB, mode, thres)
    % function BIN = findColor(RGB, rgbVal, Thres)
    % Function for detecting a specific rgb value, within acceptable tollerance
    
    % RGB: the rgb image
    % mode: the color to detect:
    %       1: red
    %       2: green
    %       3: blue
    % thres: the distance tollerance
    
    [M,N,t] = size(RGB);
    
    switch (mode)
        case 1
            I1 = zeros(M,N); I2 = zeros(M,N);
            I1( find(RGB(:,:,1) > thres * RGB(:,:,2)) ) = 1;
            I2( find(RGB(:,:,1) > thres * RGB(:,:,3)) ) = 1;
            strTitle = 'Color RED  detected (white areas)';
            I = I1 .* I2;
        case 2
            I1 = zeros(M,N); I2 = zeros(M,N);
            I1( find(RGB(:,:,2) > thres * RGB(:,:,1)) ) = 1;
            I2( find(RGB(:,:,2) > thres * RGB(:,:,3)) ) = 1;
            strTitle = 'Color GREEN  detected (white areas)';
            I = I1 .* I2;        
        case 3
            I1 = zeros(M,N); I2 = zeros(M,N);
            I1( find(RGB(:,:,3) > thres * RGB(:,:,1)) ) = 1;
            I2( find(RGB(:,:,3) > thres * RGB(:,:,2)) ) = 1;
            strTitle = 'Color BLUE  detected (white areas)';
            I = I1 .* I2;        
        otherwise
            fprintf('WRONG ARGUMENTS'\n);
            return;
    end
    
    
    
    subplot(2,1,1),imshow(RGB); title('Original Image');
    subplot(2,1,2),imshow(I,[]); title(strTitle);
    Theodore
    Personal Web Page (some audio segmentation tools): www.di.uoa.gr/~tyiannak

  11. #11
    Join Date
    Mar 2005
    Location
    Canada Alberta
    Posts
    80

    Re: Color Detection Algorithm

    How does HSV work? I've never heard of it
    In C, you merely shoot yourself in the foot.

    In C++, you accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical care is impossible, because you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."

  12. #12
    Join Date
    Mar 2005
    Location
    West Michigan
    Posts
    20

    Re: Color Detection Algorithm

    Whereas RGB represents the Red, Green and Blue components of a color, HSV represents the Hue, Saturation, and Value (light- or dark-ness) components. Some feel it is a better match with the way humans think about color. You can covert RGB colors to HSV using the algorithm found here: http://www.cs.rit.edu/~ncs/color/t_convert.html
    --
    Scott

  13. #13
    Join Date
    Dec 2001
    Location
    Greece, Athens
    Posts
    1,015

    Re: Color Detection Algorithm

    HSV and HSI are 2 very similar color models. HSV stands for:
    • H (hue): Ranges from 0-360. In some cases it is normalized 0-100. Hue represents the COLOR type (e.g. blue, red etc).
    • S (saturation): Ranges from 0-100. It represents the "purity" of the color: the higher saturation value is, the clearer the color is. If saturation is low, the color looks closer to gray.
    • V (value): This is the brightness of the color, and it ranges from 0-100.


    HSV model is sometimes represented as a cone. Try find a tutorial for explanation on this.

    How to convert from RGB to HSV:
    Suppose you have (R,G,B) values:
    MAX = max(R,G,B)
    MIN = min(R,G,B)
    then:
    H = ((G-B)/(MAX-MIN))*60 if MAX = R
    (2+(B-R)/(MAX-MIN))*60 if MAX = G
    (4+(R-G)/(MAX-MIN))*60 if MAX = B
    S = (MAX-MIN)/MIN
    V = MAX

    Hope it helps,
    Theodore
    Theodore
    Personal Web Page (some audio segmentation tools): www.di.uoa.gr/~tyiannak

  14. #14
    Join Date
    Aug 2010
    Posts
    3

    Re: Color Detection Algorithm

    anyone know how to recognize skin color in java~ do help me~very urgent~

  15. #15
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Color Detection Algorithm

    Quote Originally Posted by juana View Post
    anyone know how to recognize skin color in java~ do help me~very urgent~
    Now, more than one week after your post, I'd like to mention that when I pointed you to this thread, I meant you should read it rather than post in it. Considering the age of the thread, I think it's unlikely that the members who wrote it are still monitoring it, at least not on a regular basis.

    Didn't reading the thread give you any useful information?
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured