【问题标题】:Convert from 8-bit indexed pixel array to Bitmap Object从 8 位索引像素数组转换为位图对象
【发布时间】:2018-12-04 09:19:29
【问题描述】:

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

    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;      
    }

这是图像的外观;

如您所见,图像很奇怪。首先我想是不是相机的问题。但我已经用它自己的程序试过了,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

我的相机的像素格式是 BayerBG8。不知道有没有用。

【问题讨论】:

  • GDI+ 使用 BGR 顺序。也许你只需要交换蓝色和红色?
  • 不确定,但看起来颜色反转了。
  • @DanByström 我不知道该怎么做。我只有像素数据。
  • 8 位位图有一个调色板。调色板大小应该是 256 * 4。不过话说回来,高分辨率相机使用 8 位位图是不寻常的。也许它是 24 位 size = (width * 3 + padding) * height - 显示真实图像应该是什么样子。
  • @BarmakShemirani 我有一个大小为 256 的调色板。在您发表评论后,我尝试将其扩展到 4*256=1024,它抛出 ArgumentException 并且异常说,调色板不能大于 255 并且不能小于 0。相机自己的库使用 IGrabResult.Display() 正确显示具有相同数据的图像;代码。但我做不到。我真的被困在这里了。

标签: c# image image-processing bitmap bmp


【解决方案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

【讨论】:

  • 终于得到了 RGB 图像,非常感谢。当我从它自己的程序中保存图像时,它以 3840x2748 尺寸保存。我用谷歌搜索发现baslerweb.com/en/sales-support/knowledge-base/… 我认为您的算法正在丢失一半的像素。你有什么建议?
  • 我更新了算法以使用原始图像中的所有数据。我不知道最终的图像是什么样的。它可能需要额外的颜色校正、抗锯齿等,您可以在 .net 中进行这些操作
  • @FurkanKESKIN 是的,拜耳使用滑动窗口,而不仅仅是 2x2 块。它在 X 和 Y 中移动一个字节,这意味着您的最终结果仅比原始字节数少一个像素。我之前回答过一个类似的问题,结果也与拜耳有关;你可以查看答案here
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-18
  • 2020-09-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多