C#中位图图像的灰度变暗问题

时间:2022-05-18 20:54:32

I'm loading a 8bppIndexed greyscale image into memory and reading the pixel values. The problem is that the values I am getting from the pixels do not seem to match the actual image, they are always darker. My image is a simple grey gradient like this:

我正在将8bppIndexed灰度图像加载到内存中并读取像素值。问题是我从像素获得的值似乎与实际图像不匹配,它们总是更暗。我的图像是一个简单的灰色渐变,如下所示:

C#中位图图像的灰度变暗问题

The bottom right pixel is returning 191 and the top left 0. The top left is actually 64 and bottom right is 255.

右下角像素返回191,左上角为0.左上角实际为64,右下角为255。

Here is how I am loading my image:

这是我加载图像的方式:

Bitmap threshImg = new Bitmap(@"C:\grey.bmp");

Checking the PixelFormat confirms it is in Format8bppIndexed. So I read the bottom right pixel and top left like so:

检查PixelFormat确认它在Format8bppIndexed中。所以我读了右下角的像素和左上角,如下所示:

BitmapData data = threshImg.LockBits(new Rectangle(0, 0, rectWidth, rectHeight), ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
unsafe
{
    byte* pixel = (byte*)data.Scan0.ToPointer();
    int topVal = (int)(byte)pixel[0];
    int bottomVal = (int)(byte)pixel[((threshImg.Height * threshImg.Width)) - 1];
}
threshImg.UnlockBits(data);

If I convert the image to 24bppRbg (and adjust the code accordingly) I see the correct colour values in the respective corners.

如果我将图像转换为24bppRbg(并相应地调整代码),我会在相应的角落中看到正确的颜色值。

Anyone know why I'm getting darker values when using an 8bppIndexed image?

任何人都知道为什么我在使用8bppIndexed图像时会变暗颜色值?

2 个解决方案

#1


The value in the 8bpp indexed image isn't the color itself (or gray value) but the index. Try to look up the color value in the palette.

8bpp索引图像中的值不是颜色本身(或灰度值)而是索引。尝试在调色板中查找颜色值。

#2


With an indexed image there are only a certain number of colours (or in this case shades of grey) available - usually 256. It's probable that there aren't enough to get the full range of shades in the original image.

对于索引图像,只有一定数量的颜色(或者在这种情况下为灰色阴影)可用 - 通常为256.可能没有足够的颜色来获得原始图像中的全部阴影。

As having the exact shades is important I'd shift to a 24bpp image.

由于具有确切的阴影很重要,我将转向24bpp图像。

#1


The value in the 8bpp indexed image isn't the color itself (or gray value) but the index. Try to look up the color value in the palette.

8bpp索引图像中的值不是颜色本身(或灰度值)而是索引。尝试在调色板中查找颜色值。

#2


With an indexed image there are only a certain number of colours (or in this case shades of grey) available - usually 256. It's probable that there aren't enough to get the full range of shades in the original image.

对于索引图像,只有一定数量的颜色(或者在这种情况下为灰色阴影)可用 - 通常为256.可能没有足够的颜色来获得原始图像中的全部阴影。

As having the exact shades is important I'd shift to a 24bpp image.

由于具有确切的阴影很重要,我将转向24bpp图像。