确定位图中的像素是否为 C# 中的任何绿色阴影

Determine if pixel in bitmap is any shade of green in C#

我目前有以下代码来查看位图的像素:

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);
    }

读取字节中的像素后,我希望能够确定该像素是否为任何绿色阴影。我在弄清楚应该用什么数学来确定特定阴影和绿色与被观察者之间的距离时遇到了一些问题。

提前感谢您的帮助。

其中纯绿色为 0,255,0 - 您需要做的是计算每个像素的 R、G 和 B 分量之间的差异并取平均。假设您有一个像素为 100,200,50(这是一个偏绿色:https://www.colorcodehex.com/64c832/)- R、G 和 B 的差异将为 100、55、50,平均为 68(这 3 个的总和差异除以 3)。该平均值越接近 0,它就越接近您的参考颜色。

然后您需要做的是选择一个 'threshold' -- 您允许与参考颜色的距离有多远并且仍然被认为足够接近,然后将低于该阈值的任何东西视为成为绿色,然后你就可以为所欲为。