2

I currently have the following code in place to look through the pixels of a bitmap:

public struct Pixel
{
    public byte Blue;
    public byte Green;
    public byte Red;
    public byte Alpha;

    public Pixel(byte blue, byte green, byte red, byte alpha)
    {
        Blue = blue;
        Green = green;
        Red = red;
        Alpha = alpha;
    }
}

    public unsafe void Change(ref Bitmap inputBitmap)
    {
        Rectangle imageSize = new Rectangle(0, 0, inputBitmap.Width, inputBitmap.Height);
        BitmapData imageData = inputBitmap.LockBits(imageSize, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);

        for (int indexY = 0; indexY < imageData.Height; indexY++)
        {
            byte* imageDataBytes = (byte*)imageData.Scan0 + (indexY * imageData.Stride);

            for (int indexX = 0; indexX < imageData.Width; indexX++)
            {
                Pixel pixel = GetPixelColour(imageDataBytes, indexX);
            }
        }

        inputBitmap.UnlockBits(imageData);
    }

Once a pixel from the bytes is read, I want to be able to determine if the pixel is any shade of green. I'm having some problems figuring out what the math should be to determine the distance between a specific shade and green and the one being looked at.

Thank you in advance for the help.

Siyual
  • 16,415
  • 8
  • 44
  • 58
William Troup
  • 12,739
  • 21
  • 70
  • 98
  • There isn't just one type of distance between colours, and which one works best depends on what you want from it, so, what is the goal? – harold Mar 10 '17 at 14:27
  • 1
    Use the Color.GetHue() method. – Hans Passant Mar 10 '17 at 14:37
  • define a range from almost yellow (~70) to almost turquiose (~170) and see if gethue(getpixel(x,y))*256 falls in it. See [here](http://stackoverflow.com/questions/27374550/how-to-compare-color-object-and-get-closest-color-in-an-color/27375621#27375621), [here](http://stackoverflow.com/questions/365935/trying-to-convert-rgb-from-a-net-color-to-a-string-such-as-red-or-blue/39142758#39142758) and [here](http://stackoverflow.com/questions/31612232/color-table-algorithm/31626758#31626758) for more.. – TaW Mar 10 '17 at 15:33
  • Comparing by hue is one of those ways but it will consider some unsaturated colours and some near-black colours to be extremely green, because they have the right angle. – harold Mar 10 '17 at 15:54

1 Answers1

5

Where pure green is 0,255,0 - what you need to do is take the difference between the R, G and B components of each pixel and average them. So say you had a pixel that was 100,200,50 (which is a off-green: https://www.colorcodehex.com/64c832/) - the R, G and B differences would be 100,55,50 for an average of 68 (sum of those 3 differences divided by 3). The closer to 0 that average, the closer it is to your reference color.

What you then need to do is pick a 'threshold' -- how far away from your reference color you're allowed to be and still be considered close enough, and then anything below that threshold you treat as being green and you then do whatever you like with it.

PhonicUK
  • 13,486
  • 4
  • 43
  • 62