2

Trying to find a solution for how to recognize if there is a "green" color on particular screenshot (image below).

The problem is that when using a plain RGB Bitmap, it doesn't work as I would like to because the image is taken from a screenshot of a webpage and it's kind of blurry, so many pixels doesn't look like "green".

I need somehow to understand how to define whether there is "green" color on a particular screenshot

enter image description here

Kit
  • 20,354
  • 4
  • 60
  • 103
user239247
  • 51
  • 3
  • 2
    ye it's maybe a little wrong name for topic, but I need somehow to know whether there is green color, maybe using some kinda rgb ranges or Idk really – user239247 Aug 02 '18 at 11:38
  • You can 1) downscale it to very small size and then go over all pixels and ignore all with a low saturation value. Then put the others in a histogram (dictionary, maybe after shifting all bytes right by 2-4 bits and back . Then the largest count will be the dominant color.. - You may want to staudy [this](https://stackoverflow.com/questions/31612232/color-table-algorithm/31626758#31626758) and [this](https://stackoverflow.com/questions/365935/trying-to-convert-rgb-from-a-net-color-to-a-string-such-as-red-or-blue/39142758#39142758) – TaW Aug 02 '18 at 11:41
  • Related https://stackoverflow.com/questions/25168445/how-to-determine-if-color-is-close-to-other-color – Magnus Aug 02 '18 at 11:49
  • What if get all the pixels hues, then set range of hues for example from 70 to 170 and if any pixel in that hue then there is green color or alike one? – user239247 Aug 02 '18 at 12:08
  • Converting to HSV might be useful: https://stackoverflow.com/a/24024027/1266873 – Koray Aug 10 '18 at 17:25

2 Answers2

3

I need somehow to know whether there is green color

Iterate all pixels and search for the desired color

Color c = Color.Green; //or the color you want to search

Bitmap bmp = new Bitmap(Image.FromFile(@"c:\file.bmp"));
bool containsgreen = false;
for (int w = 0; w < bmp.Width; w++)
    for (int h = 0; h < bmp.Height; h++)
        if (bmp.GetPixel(w, h) == c)
            containsgreen = true;

If you're looking for a color range or similar colors, you could also calculate the color distance to add tolerance

public static double GetColourDistance(Color e1, Color e2)
{
    return Math.Sqrt((e1.R - e2.R) * (e1.R - e2.R) + (e1.G - e2.G) * (e1.G - e2.G) + (e1.B - e2.B) * (e1.B - e2.B));
}
fubo
  • 44,811
  • 17
  • 103
  • 137
2

I am going to assume from your question, that when you say green, you don't mean that any pixel will have some positive value for the G component of the RGB color, but that you mean it looks visually green to a human.

If that is the case, I suggest a modification to @fubo's code that calculates "visually green". That would be when the G component is greater than the other components.

Now, this will return true for some sketchy greens, e.g. a green that is very, very dark or very, very light. If you want to filter those out, use a tolerance value of your choosing.

Here's the code:

bool HasGreen(int tolerance)
{
    using (var bmp = new Bitmap(Image.FromFile(@"c:\file.bmp")))
    {            
        for (int w = 0; w < bmp.Width; w++)
        for (int h = 0; w < bmp.Height; h++)
            if (IsGreenPixel(bmp.GetPixel(w, h), tolerance))
                return true;
    }

    return false;
}

bool IsGreenPixel(Color color, int tolerance) 
    => color.G > color.R + tolerance && color.G > color.B + tolerance;

If you're looking for "what is the main green color in the green colors", you could modify this algorithm further by doing counts of colors and dropping them into buckets (i.e. a histogram).

Kit
  • 20,354
  • 4
  • 60
  • 103