从8位索引像素阵列转换为位图对象

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

我正在开发一个从Camera设备获取Image的程序。这是我如何获得图像的事件。

    private void StreamGrabber_ImageGrabbed(object sender, ImageGrabbedEventArgs e)
    {
        IGrabResult res = e.GrabResult;
        byte[] pixels = res.PixelData as byte[];
        pictureBox1.Image = ByteArrayToBitmap(pixels,res.Width,res.Height, PixelFormat.Format8bppIndexed);

        //ImagePersistence.Save(ImageFileFormat.Jpeg, "tmp/tmp" + i + ".jpg", img);

    }

我可以使用上面的代码获得像素数据阵列。我可以使用单行代码直接保存图像。

ImagePersistence.Save(ImageFileFormat.Jpeg, "tmp/tmp" + i + ".jpg", img);

现在我的问题从这里开始。我不想保存它,我只是想从像素数据创建Bitmap对象,只是在我的pictureBox1对象中显示。我有图像的像素数据,但无论如何我无法正确获取图像。

这是ByteArrayToBitmap函数;

    public Bitmap ByteArrayToBitmap(byte[] byteIn, int imwidth, int imheight, PixelFormat pixelformat)     
    {
        Bitmap picOut = new Bitmap(imwidth, imheight, pixelformat);  
        BitmapData bmpData = picOut.LockBits(new Rectangle(0, 0, imwidth, imheight), ImageLockMode.WriteOnly, pixelformat);
        IntPtr ptr = bmpData.Scan0;
        Int32 psize = bmpData.Stride * imheight;
        System.Runtime.InteropServices.Marshal.Copy(byteIn, 0, ptr, psize);
        picOut.UnlockBits(bmpData);
        return picOut;      
    }

这是图像的样子;

GRABBED IMAGE IS HERE

如你所见,图像很奇怪。首先我想是否是相机的问题。但是我已经尝试了它自己的程序,Camera完美无缺。我的代码出了问题。

我从调试屏幕提供有用的信息;

图片尺寸为3840x2748。

像素数据的字节数组的大小是10.552.320

我做了那个计算:3840 * 2748 = 10.552.320图像不是灰度,是RGB图像,

所以我认为像素数据包括8位索引像素。

我遇到了这个问题。我试图向您提供有关我的问题的所有有用信息。

如何正确获取图像并创建位图对象?

编辑

    public Bitmap CopyDataToBitmap(int Width, int Height, byte[] data)
    {
        var b = new Bitmap(Width, Height, PixelFormat.Format8bppIndexed);
        ColorPalette ncp = b.Palette;
        int counter = 0;
        for (int i = 32; i <= 256; i+=32) //for R channel (3 bit)
            for (int j = 32; j <= 256; j+=32)  //for G Channel (3 bit)
                for (int k = 64; k <= 256; k+=64)  //for B Channel (2 bit)
                {
                    ncp.Entries[counter] = Color.FromArgb(255,i-1,j-1,k-1);
                    counter++;
                }
        b.Palette = ncp;

        var BoundsRect = new Rectangle(0, 0, Width, Height);

        BitmapData bmpData = b.LockBits(BoundsRect,
                                        ImageLockMode.WriteOnly,
                                        PixelFormat.Format8bppIndexed);

        IntPtr ptr = bmpData.Scan0;
        int bytes = bmpData.Stride * b.Height;
        var rgbValues = new byte[bytes];
        Marshal.Copy(data, 0, ptr, bytes);

        b.UnlockBits(bmpData);
        Console.WriteLine(b.GetPixel(3648, 1145).ToString());

        return b; 
    }

我用这个函数改变了算法。我正在使用调色板。但结果仍然相同。

        ColorPalette ncp = b.Palette;
        for (int i = 0; i < 257; i++)
            ncp.Entries[i] = Color.FromArgb(255, i, i, i);
        b.Palette = ncp;

当我使用此调色板时,这次,图像变为灰度。

我只想获得清晰的RGB图像。

编辑2

我的相机像素格式是BayerRG8。我不知道它是否有帮助。

c# image image-processing bitmap bmp
1个回答
1
投票

8位位图仅使用256色,在任何现代相机中都没用。颜色必须由制作图像的人提供。每个像素是一个字节(或8位),它是0到256之间的值,它不是指颜色,而是指颜色表中的索引。

for (int i = 0; i < 256; i++)//change to 256
    ncp.Entries[i] = Color.FromArgb(255, i, i, i);

这基本上是一个随机的颜色表。它是正确的颜色表的几率为1 /(256 * 256 * 256)


BayerBG8是原始格式,它与8位位图无关。见description

每个像素用4个颜色检测器表示。每个像素只有3种颜色,因此它们放置了一个额外的绿色探测器。

第一排是蓝绿蓝绿...... 下一行是绿红绿红......

BGBGBGBG...
GRGRGRGR...
BGBGBGBG...
GRGRGRGR...

您必须采用2x2的正方形并获得平均颜色,代表1个像素:

piexel 1: BG    piexel 2: BG ...
          GR              GR ...

从3840x2748开始,最终得到1920x1374像素。

每种颜色可以用3个字节表示。现在你有:

BGR BGR BGR BGR BGR BGR //RGB is stored backward as BGR

这总共是5760 * 1374字节。以下示例使用简单近似。每个像素有两个绿色值,我们取两个绿色值的平均值。

int src_width = 3840;
int src_height = 2748;

int dst_width = src_width / 2;
int dst_height = src_height / 2;

byte[] destination = new byte[dst_width * dst_height * 3];
int pos = 0;
for(int row = 0; row < src_height; row += 2)
{
    for(int col = 0; col < src_width; col += 2)
    {
        int i = row * src_width + col;
        //blue from this line
        destination[pos++] = source[i];

        //green average of two greens from this line and next line:
        destination[pos++] = (source[i + 1] + source[i + src_width])/2;

        //red from the next line
        destination[pos++] = source[i + src_width + 1];
    }
}

使用destinationFormat24bppRgb,尺寸为1920x1374

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