-1

我正在尝试对图像进行阈值处理。“tif”类型的图像,我收到这个错误说

具有索引像素格式的图像不支持 SetPixel

这是我的代码,p11是图像名称

for (int r = 0; r < p11.Width; r++)
{
    //  whiteColor = 0;
    //  blackColor = 0;
    for (int c = 0; c < p11.Height; c++)
    {
        num1 = int.Parse(p11.GetPixel(r, c).A.ToString()); // gets the alpha component value of this colout
        num2 = int.Parse(p11.GetPixel(r, c).B.ToString()); // gets the Blue component value of this colout
        num3 = int.Parse(p11.GetPixel(r, c).R.ToString()); // gets the Red component value of this colout
        num4 = int.Parse(p11.GetPixel(r, c).G.ToString()); // gets the green component value of this colout

        if( T <= num1 && T <= num2 && T <= num3 && T <= num4)
        {

        }
        else
        {
            p11.SetPixel(r, c, Color.Black);
        }
    }
}
4

1 回答 1

2

索引像素格式是指图像数据不包含直接颜色但包含调色板条目,它间接引用颜色。8 位或更少的每像素位图像通常是索引图像。要访问实际颜色列表,请Palette参阅Bitmap.

SetPixel不能用于这些图像,因为它需要颜色作为参数。要操作图像内容,您需要BitmapData通过LockBits方法获取。在下面的示例中,TransformColor获取调色板索引并返回另一个。

private unsafe static void ManipulateIndexedBitmap(Bitmap bitmap)
{
    int bpp = Image.GetPixelFormatSize(bitmap.PixelFormat);

    BitmapData bitmapData = bitmap.LockBits(new Rectangle(Point.Empty, bitmap.Size), ImageLockMode.ReadWrite, bitmap.PixelFormat);
    try
    {
        byte* line = (byte*)bitmapData.Scan0;

        // scanning through the lines
        for (int y = 0; y < bitmapData.Height; y++)
        {
            // scanning through the pixels within the line
            for (int x = 0; x < bitmapData.Width; x++)
            {
                switch (bpp)
                {
                    // a pixel is 1 byte - there are up to 256 palette entries 
                    case 8:
                        line[x] = (byte)TransformColor(line[x]);
                        break;
                    // a pixel is 4 bits - there are up to 16 palette entries
                    case 4:
                        // First pixel is the high nibble
                        int pos = x >> 1;
                        byte nibbles = line[pos];
                        if ((x & 1) == 0)
                        {
                            nibbles &= 0x0F;
                            nibbles |= (byte)(TransformColor(nibbles) << 4);
                        }
                        else
                        {
                            nibbles &= 0xF0;
                            nibbles |= (byte)TransformColor(nibbles >> 4);
                        }

                        line[pos] = nibbles;
                        break;
                    // a pixel is 1 bit - there are exactly two palette entries
                    case 1:
                        // First pixel is MSB.
                        pos = x >> 3;
                        byte mask = (byte)(128 >> (x & 7));
                        if (TransformColor(((line[pos] & mask) == 0) ? 0 : 1) == 0)
                            line[pos] &= (byte)~mask;
                        else
                            line[pos] |= mask;
                        break;
                }
            }

            line += bitmapData.Stride;
        }
    }
    finally
    {
        bitmap.UnlockBits(bitmapData);
    }
}
于 2018-12-30T18:16:55.963 回答