C# 确定位图中的像素在C中是否为绿色

C# 确定位图中的像素在C中是否为绿色,c#,bitmap,pixel,C#,Bitmap,Pixel,我目前有以下代码可以查看位图的像素: 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;

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

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);
    }
一旦从字节中读取一个像素,我希望能够确定该像素是否为绿色。我在计算一个特定的阴影和绿色之间的距离时遇到了一些问题


提前感谢您的帮助。

其中纯绿色为0255,0-您需要做的是获取每个像素的R、G和B分量之间的差值,并将其平均。假设你有一个100200,50的像素,它是一个非绿色的:-R,G和B的差值将是100,55,50,这3个差值的平均值为68除以3。平均值越接近0,它就越接近参考颜色


然后,你需要做的是选择一个“阈值”——你被允许与参考颜色保持足够的距离,然后低于该阈值的任何颜色都被视为绿色,然后你可以随心所欲地使用它。

颜色之间的距离不仅仅是一种,哪一个效果最好取决于你想从中得到什么,那么,目标是什么?使用Color.GetHue方法。定义一个从几乎黄色~70到几乎松软~170的范围,看看gethuegetpixelx,y*256是否在其中。用色调比较是其中的一种方法,但它会考虑一些不饱和的颜色和一些接近黑色的颜色是极其绿色的,因为它们有直角。