CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Installing SQL Server 2008
  • Writing UDFs for Firebird Embedded SQL Server
  • [Updated] Shutdown Manager
  • Building Windows Azure Cloud Service Applications with Azure Storage and the Azure SDK

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > General Discussion > Algorithms & Data Structures
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    Algorithms & Data Structures Discuss algorithms & data structures. Topics are not specific to any programming language.

    Reply
     
    Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
      #1    
    Old August 18th, 2004, 01:33 PM
    vonnegut2004 vonnegut2004 is offline
    Junior Member
     
    Join Date: Aug 2004
    Posts: 2
    vonnegut2004 is an unknown quantity at this point (<10)
    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 ?
    Reply With Quote
      #2    
    Old August 18th, 2004, 09:58 PM
    Kheun Kheun is offline
    Elite Member
     
    Join Date: Oct 2002
    Location: Singapore
    Posts: 3,124
    Kheun is a name known to all (1000+)Kheun is a name known to all (1000+)Kheun is a name known to all (1000+)Kheun is a name known to all (1000+)Kheun is a name known to all (1000+)Kheun is a name known to all (1000+)Kheun is a name known to all (1000+)Kheun is a name known to all (1000+)
    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.
    Reply With Quote
      #3    
    Old August 19th, 2004, 09:19 AM
    yiannakop's Avatar
    yiannakop yiannakop is offline
    Member +
     
    Join Date: Dec 2001
    Location: Greece, Athens
    Posts: 1,015
    yiannakop has a spectacular aura about (150+)yiannakop has a spectacular aura about (150+)
    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
    Reply With Quote
      #4    
    Old August 19th, 2004, 09:26 AM
    yiannakop's Avatar
    yiannakop yiannakop is offline
    Member +
     
    Join Date: Dec 2001
    Location: Greece, Athens
    Posts: 1,015
    yiannakop has a spectacular aura about (150+)yiannakop has a spectacular aura about (150+)
    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
    Reply With Quote
      #5    
    Old August 28th, 2004, 03:31 PM
    RoboTact's Avatar
    RoboTact RoboTact is offline
    Elite Member
     
    Join Date: Jun 2002
    Location: Moscow, Russia.
    Posts: 2,176
    RoboTact is a glorious beacon of light (400+)RoboTact is a glorious beacon of light (400+)RoboTact is a glorious beacon of light (400+)RoboTact is a glorious beacon of light (400+)RoboTact is a glorious beacon of light (400+)RoboTact is a glorious beacon of light (400+)
    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."
    Reply With Quote
      #6    
    Old August 30th, 2004, 10:17 AM
    yiannakop's Avatar
    yiannakop yiannakop is offline
    Member +
     
    Join Date: Dec 2001
    Location: Greece, Athens
    Posts: 1,015
    yiannakop has a spectacular aura about (150+)yiannakop has a spectacular aura about (150+)
    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
    Reply With Quote
      #7    
    Old March 30th, 2005, 12:18 PM
    wsmmi6674 wsmmi6674 is offline
    Junior Member
     
    Join Date: Mar 2005
    Location: West Michigan
    Posts: 20
    wsmmi6674 is an unknown quantity at this point (<10)
    Re: Color Detection Algorithm

    Assuming you receive pixel values in RGB form, another option is to convert the color to HSV (Hue - Saturation - Value). Pure green has (I believe) a hue value of 192.

    This would make it easier to weight saturation and light levels differently than hue.
    --
    Scott
    Reply With Quote
      #8    
    Old March 31st, 2005, 01:28 AM
    barrensoul's Avatar
    barrensoul barrensoul is offline
    Member
     
    Join Date: Mar 2005
    Location: Canada Alberta
    Posts: 80
    barrensoul is an unknown quantity at this point (<10)
    Re: Color Detection Algorithm

    with a screen that is 32 bit you use this (this is how I get colours from a direct draw surface)

    long colour = (*(reinterpret_cast<long*>(ddsd.lpSurface)+(Y*Xres+X)))&0x00FF00

    &0x00FF00 is a bit mask that will extract the green value so all you need to do is this

    if(colour) //if true then there is some green in the pixel


    there ya go be carefull with 16 bit numbers as some systems consider 16 bit to be 15

    (ddsd.lpSurface is a void pointer to the beginning of the screens memory address, which is why it needs to be reinterpreted)

    oh and in a 24bit BMP (just RGB no alpha) you have 16,777,216 different colours
    __________________
    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."
    Reply With Quote
      #9    
    Old April 8th, 2005, 10:38 AM
    yiannakop's Avatar
    yiannakop yiannakop is offline
    Member +
     
    Join Date: Dec 2001
    Location: Greece, Athens
    Posts: 1,015
    yiannakop has a spectacular aura about (150+)yiannakop has a spectacular aura about (150+)
    Re: Color Detection Algorithm

    Quote:
    Originally Posted by wsmmi6674
    Assuming you receive pixel values in RGB form, another option is to convert the color to HSV (Hue - Saturation - Value). Pure green has (I believe) a hue value of 192.

    This would make it easier to weight saturation and light levels differently than hue.
    --
    Scott
    Also: hsv is sometimes better to use for color detection problems. For example, it is prooved that for human skin color detection, hsi (or hsv) model is much better, because it is closer to human color perception.
    __________________
    Theodore
    Personal Web Page (some audio segmentation tools): www.di.uoa.gr/~tyiannak
    Reply With Quote
      #10    
    Old April 8th, 2005, 11:37 PM
    barrensoul's Avatar
    barrensoul barrensoul is offline
    Member
     
    Join Date: Mar 2005
    Location: Canada Alberta
    Posts: 80
    barrensoul is an unknown quantity at this point (<10)
    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."
    Reply With Quote
      #11    
    Old April 11th, 2005, 08:55 AM
    wsmmi6674 wsmmi6674 is offline
    Junior Member
     
    Join Date: Mar 2005
    Location: West Michigan
    Posts: 20
    wsmmi6674 is an unknown quantity at this point (<10)
    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
    Reply With Quote
      #12    
    Old April 11th, 2005, 09:21 AM
    yiannakop's Avatar
    yiannakop yiannakop is offline
    Member +
     
    Join Date: Dec 2001
    Location: Greece, Athens
    Posts: 1,015
    yiannakop has a spectacular aura about (150+)yiannakop has a spectacular aura about (150+)
    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
    Reply With Quote
      #13    
    Old February 27th, 2007, 03:09 PM
    kimohoss kimohoss is offline
    Junior Member
     
    Join Date: Feb 2007
    Posts: 2
    kimohoss is an unknown quantity at this point (<10)
    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.
    Reply With Quote
      #14    
    Old March 5th, 2007, 07:51 AM
    kimohoss kimohoss is offline
    Junior Member
     
    Join Date: Feb 2007
    Posts: 2
    kimohoss is an unknown quantity at this point (<10)
    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
    Reply With Quote
      #15    
    Old March 5th, 2007, 11:27 AM
    MrViggy's Avatar
    MrViggy MrViggy is offline
    Elite Member
     
    Join Date: Feb 2002
    Posts: 3,775
    MrViggy has much to be proud of (1500+)MrViggy has much to be proud of (1500+)MrViggy has much to be proud of (1500+)MrViggy has much to be proud of (1500+)MrViggy has much to be proud of (1500+)MrViggy has much to be proud of (1500+)MrViggy has much to be proud of (1500+)MrViggy has much to be proud of (1500+)MrViggy has much to be proud of (1500+)MrViggy has much to be proud of (1500+)MrViggy has much to be proud of (1500+)
    Re: Color Detection Algorithm

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

    Viggy
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > General Discussion > Algorithms & Data Structures


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 02:52 PM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
    Copyright WebMediaBrands Inc. 2002-2009