C#无法使用正确的颜色映射显示RAW16灰度图像

问题描述 投票:1回答:1

修改此链接中提供的代码:

Original code

我写了这个:

    private void btnLoad_Click(object sender, EventArgs e)
    {
        if (System.IO.File.Exists(txtPicture.Text))
        {
            byte[] _data = System.IO.File.ReadAllBytes(txtPicture.Text);

            var _rgbData = Convert16BitGrayScaleToRgb16(_data, 160, 120);
            var _bmp = CreateBitmapFromBytes(_rgbData, 160, 120);

            pbFrame.Image = _bmp;
        }
    }

    private static void Convert16bitGSToRGB(UInt16 color, out byte red, out byte green, out byte blue)
    {
        red = (byte)(color & 0x31);
        green = (byte)((color & 0x7E0) >> 5);
        blue = (byte)((color & 0xF800) >> 11);
    }

    private static byte[] Convert16BitGrayScaleToRgb48(byte[] inBuffer, int width, int height)
    {
        int inBytesPerPixel = 2;
        int outBytesPerPixel = 6;

        byte[] outBuffer = new byte[width * height * outBytesPerPixel];
        int inStride = width * inBytesPerPixel;
        int outStride = width * outBytesPerPixel;

        // Step through the image by row
        for (int y = 0; y < height; y++)
        {
            // Step through the image by column
            for (int x = 0; x < width; x++)
            {
                // Get inbuffer index and outbuffer index
                int inIndex = (y * inStride) + (x * inBytesPerPixel);
                int outIndex = (y * outStride) + (x * outBytesPerPixel);

                byte hibyte = inBuffer[inIndex + 1];
                byte lobyte = inBuffer[inIndex];

                //R
                outBuffer[outIndex] = lobyte;
                outBuffer[outIndex + 1] = hibyte;

                //G
                outBuffer[outIndex + 2] = lobyte;
                outBuffer[outIndex + 3] = hibyte;

                //B
                outBuffer[outIndex + 4] = lobyte;
                outBuffer[outIndex + 5] = hibyte;
            }
        }
        return outBuffer;
    }

    private static byte[] Convert16BitGrayScaleToRgb16(byte[] inBuffer, int width, int height)
    {
        int inBytesPerPixel = 2;
        int outBytesPerPixel = 2;

        byte[] outBuffer = new byte[width * height * outBytesPerPixel];
        int inStride = width * inBytesPerPixel;
        int outStride = width * outBytesPerPixel;

        // Step through the image by row
        for (int y = 0; y < height; y++)
        {
            // Step through the image by column
            for (int x = 0; x < width; x++)
            {
                // Get inbuffer index and outbuffer index
                int inIndex = (y * inStride) + (x * inBytesPerPixel);
                int outIndex = (y * outStride) + (x * outBytesPerPixel);

                byte hibyte = inBuffer[inIndex];
                byte lobyte = inBuffer[inIndex+1];

                outBuffer[outIndex] = lobyte;
                outBuffer[outIndex+1] = hibyte;
            }
        }

        return outBuffer;
    }

    private static byte[] Convert16BitGrayScaleToRgb24(byte[] inBuffer, int width, int height)
    {
        int inBytesPerPixel = 2;
        int outBytesPerPixel = 3;

        byte[] outBuffer = new byte[width * height * outBytesPerPixel];
        int inStride = width * inBytesPerPixel;
        int outStride = width * outBytesPerPixel;

        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                int inIndex = (y * inStride) + (x * inBytesPerPixel);
                int outIndex = (y * outStride) + (x * outBytesPerPixel);

                byte hibyte = inBuffer[inIndex];
                byte lobyte = inBuffer[inIndex + 1];

                byte r, g, b;

                UInt16 color = (UInt16)(hibyte << 8 | lobyte);

                Convert16bitGSToRGB(color, out r, out g, out b);

                outBuffer[outIndex] = r;
                outBuffer[outIndex + 1] = g;
                outBuffer[outIndex + 2] = b;
            }
        }

        return outBuffer;
    }

    private static Bitmap CreateBitmapFromBytes(byte[] pixelValues, int width, int height)
    {
        //Create an image that will hold the image data

        Bitmap bmp = new Bitmap(width, height, PixelFormat.Format16bppRgb565);

        //Get a reference to the images pixel data
        Rectangle dimension = new Rectangle(0, 0, bmp.Width, bmp.Height);
        BitmapData picData = bmp.LockBits(dimension, ImageLockMode.ReadWrite, bmp.PixelFormat);
        IntPtr pixelStartAddress = picData.Scan0;

        //Copy the pixel data into the bitmap structure
        Marshal.Copy(pixelValues, 0, pixelStartAddress, pixelValues.Length);

        bmp.UnlockBits(picData);
        return bmp;
    }

但是转换结果仍然不令人满意/正确。这是我应该得到的图片:

Original picture

转换链接到此处的文件:

Example RAW16 picture file

这是使用Convert16BitGrayScaleToRgb48的结果:

enter image description here

这是使用Convert16BitGrayScaleToRgb16的结果:

GrayScaleToRgb16

这是使用Convert16BitGrayScaleToRgb24的结果:

GrayScaleToRgb24

很明显,颜色重新映射是错误的,但我不明白问题出在哪里。

另外,我还发现picturebox不能完全显示其存储的内容。自上而下的第二张图片(Convert16BitGrayScaleToRgb48结果)是图片框显示的内容,而下一张图片是我以PNG格式保存的图片获得的图片:

Image saved

我RAW16灰度应表示2个字节,其中包含在565或555地图上编码的16位灰度值或RGB灰度值。但是这些假设似乎都不符合真实情况。

有人暗示如何转换源文件中提供的值以获得与第一个图片一样的图片(使用ImageJ从同一来源获得)?

c# .net image picturebox
1个回答
0
投票

我使用GIMP找到了可能的提示。如果我通过该应用程序加载原始文件(更改.data中的扩展名和/或强制将其加载为RAW)并将其设置为160x120 16bpp BigEndian,则会得到近乎黑框(!),但是如果我更改压缩级别图像结果正确的唯一小峰值附近的范围(大约1,0黑色-1,3.0白色)。更改字节序非常简单,压缩动态范围要少一些,但是我正在研究它。

[从这次经历中学到的第一课是“不相信你的眼睛”:-)。

我努力的最终结果是这三种方法:

public static void GetMinMax(byte[] data, out UInt16 min, out UInt16 max, bool big_endian = true)
{
    if (big_endian)
        min = max = (UInt16)((data[0] << 8) | data[1]);
    else
        min = max = (UInt16)((data[1] << 8) | data[0]);

    for (int i = 0; i < (data.Length - 1); i += 2)
    {
        UInt16 _value;

        if (big_endian)
            _value = (UInt16)((data[i] << 8) | data[i + 1]);
        else
            _value = (UInt16)((data[i + 1] << 8) | data[i]);

        if (_value < min)
            min = _value;

        if (_value > max)
            max = _value;
    }
}

public static void CompressRange(byte MSB, byte LSB, UInt16 min, UInt16 max, out byte color, Polarity polarity)
{
    UInt16 _value = (UInt16)((MSB << 8) | LSB);

    _value -= min;

    switch (polarity)
    {
        case Polarity.BlackHot:
            _value = (UInt16)((_value * 255) / (max - min));
            _value = (UInt16)(255 - _value);
            break;

        default:
        case Polarity.WhiteHot:
            _value = (UInt16)((_value * 255) / (max - min));
            break;
    }

    color = (byte)(_value & 0xff);
}

public static byte[] Convert16BitGrayScaleToRgb24(byte[] inBuffer, int width, int height, UInt16 min, UInt16 max, bool big_endian = true, Polarity polarity = Polarity.WhiteHot)
{
    int inBytesPerPixel = 2;
    int outBytesPerPixel = 3;

    byte[] outBuffer = new byte[width * height * outBytesPerPixel];
    int inStride = width * inBytesPerPixel;
    int outStride = width * outBytesPerPixel;

    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            int inIndex = (y * inStride) + (x * inBytesPerPixel);
            int outIndex = (y * outStride) + (x * outBytesPerPixel);

            byte hibyte;
            byte lobyte;

            if (big_endian)
            {
                hibyte = inBuffer[inIndex];
                lobyte = inBuffer[inIndex + 1];
            }
            else
            {
                hibyte = inBuffer[inIndex + 1];
                lobyte = inBuffer[inIndex];
            }

            byte gray;

            CompressRange(hibyte, lobyte, min, max, out gray, polarity);

            outBuffer[outIndex] = gray;
            outBuffer[outIndex + 1] = gray;
            outBuffer[outIndex + 2] = gray;
        }
    }

    return outBuffer;
}

这些允许加载附加到原始问题的文件,并将其显示在标准WindowsForm PictureBox上。使用48bpp格式将导致某些图形卡上的图像质量下降(至少在我的图形卡上)。BTW GetMinMax会计算当前帧的最小值最大值,而不管环境的历史记录如何。这意味着,如果您要使用此功能来显示图片序列(如我所愿),则FOV中平均温度的强烈变化会将整个图像驱动到不同的exposure,从而导致某些细节丢失。图片。在这种情况下,我建议在当前帧上计算最小-最大,但不建议在位图生成方法Convert16BitGrayScaleToRgb24中使用它,而对两个值均使用移动平均值

© www.soinside.com 2019 - 2024. All rights reserved.