Results 1 to 6 of 6

Thread: How to detect similar colors in a picture?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    290

    How to detect similar colors in a picture?

    I was just trying to detect similar colors in a picture.
    First I load a picture in a picturebox and then I select some hundreds of similar colors in the picture using the mouse and getpixel api.

    In another picturebox (it's empty, so no picture has been loaded), the program sets the pixel to blue if that color has been selected from the original picture.

    The problem is I've to select tons of different pixels from the original picture to get a good result. In case that I only select a few ones, then I will see some small blue pixels in the second picturebox.

    I remember that I've use a software where you can select just a pixel and the program detects all the similar colors.

    How can I do it?

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How to detect similar colors in a picture?

    I guess it depends on how you define similar colors. There are probably several techniques and a couple of them may be:

    -- Take a color and add/subtract a percentage (threshold) from each of the R,G,B components. Any color that falls within that range is 'similar'
    -- Similar to #1 above, but using properties of the color: just focusing on the R, G, or B component, not all, or maybe using Hue vs RGB

    When messing with colors, there are usually many ways to address the problem. Every solution has pros and cons
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    290

    Re: How to detect similar colors in a picture?

    Quote Originally Posted by LaVolpe View Post
    I guess it depends on how you define similar colors. There are probably several techniques and a couple of them may be:

    -- Take a color and add/subtract a percentage (threshold) from each of the R,G,B components. Any color that falls within that range is 'similar'
    -- Similar to #1 above, but using properties of the color: just focusing on the R, G, or B component, not all, or maybe using Hue vs RGB

    When messing with colors, there are usually many ways to address the problem. Every solution has pros and cons
    I don't fully understand what you're trying to explain to me.
    So, you suggest that I've to change (add or substract) a percentaje from each of the R,G,B components... so if I've a color, for example 45,123,31. I've to add (or substract) a percentaje... so

    45,123,31 is:


    And I add 45 units to each of the R,G,B values:
    90,168,76


    For me both are green colors and I can think both are similars.

    So I don't fully understand what you mean.

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How to detect similar colors in a picture?

    So in your example, any color that falls in that range is 'similar'
    Code:
    ' Values below are the range +/- from the selected color
    If R => 45 And R <= 90 Then
        If G => 123 And G <= 168 Then
            If B => 31 And B <= 76 Then 
                ' similar color to the selected color
            End If
        End If
    End If
    If you are already using logic like this, then I don't think I fully understand the question or the logic is too generic and won't work in this case?

    Note: If using this logic, I would suggest adding AND subtracting a percentage from the selected color. That way a 'similar' color would match if it was a bit lighter or a bit darker than the selected color
    Last edited by LaVolpe; Apr 6th, 2015 at 09:34 AM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: How to detect similar colors in a picture?

    I think I have an algorithm somewhere that I used to convert a 32 bit colour image to an 8 bit coloured image. I had to use a nearest match scheme to achieve this as 8 bit images only has 256 colours to choose from as opposed to millions of colours. Would you like me to get that for you?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  6. #6
    Fanatic Member
    Join Date
    Aug 2013
    Posts
    806

    Re: How to detect similar colors in a picture?

    For RGB colors, the easiest way to compare colors is to treat them as three-dimensional vectors. So to calculate similarity, you use a standard distance formula, e.g.:

    Code:
    Similarity = Sqr( (R2 - R1) ^ 2 + (G2 - G1) ^ 2 + (B2 - B1) ^ 2)
    If you only need relative distances, you can omit the Sqr() for a performance boost. Detecting similar colors is very simple:

    Code:
    If Similarity < Threshold Then 
      (colors are similar)
    Else
      (colors are not similar)
    End If
    The best Threshold value will depend greatly on the source images. Low-quality JPEGs will need a higher threshold than lossless formats like BMP or PNG. It will probably take some experimentation to find the best number. I'd start at 10 (if not using Sqr()) and work your way up from there.

    <Begin potentially pedantic comments>

    One of the problems with the RGB color space is that color distance is not uniformly perceptual. This is a fancy way of saying that color distance is not a very meaningful measurement for RGB colors.

    For example, using the formula above, two dark colors with a similarity of 10 could look pretty much identical. However, two bright colors with a similarity of 10 may look very different.

    Scientists have developed a number of color models that are perceptually uniform. These color models are the result of thousands of tests, where participants of all ages and genders would try to match differing colors, and scientists would use the resulting data to figure out exactly how humans perceive color. (Pretty mind-numbing work!) These experiments made it possible to develop color models that correlate very well with human perception.

    CIELAB is probably the most famous perceptually uniform color space. If you need very accurate results, I'd recommend using it instead of RGB. Converting between RGB and CIELAB is complicated, but you can find well-known formulas here.

    <End pedantry>
    Check out PhotoDemon, a pro-grade photo editor written completely in VB6. (Full source available at GitHub.)

Tags for this Thread

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