【发布时间】:2018-04-25 13:57:08
【问题描述】:
我有 Basler acA3800 USB 相机。
IGrabResult grabResult = camera.StreamGrabber.RetrieveResult(5000, TimeoutHandling.ThrowException);
// Image grabbed successfully?
if (grabResult.GrabSucceeded)
{
byte[] buffer = grabResult.PixelData as byte[];
ImageWindow.DisplayImage(0, grabResult);
pictureBox1.Image = ImageFromRawBgraArray(buffer,3840,2748,PixelFormat.Format8bppIndexed);
MessageBox.Show(grabResult.PixelTypeValue.ToString());
}
此代码部分显示图像自身窗口。
我有捕获图像的像素数据。原始图像很好,但是当我将其转换为图像时,它已损坏。这是我的转换函数。
public Image ImageFromRawBgraArray(byte[] arr, int width, int height, PixelFormat pixelFormat)
{
var output = new Bitmap(width, height, pixelFormat);
var rect = new Rectangle(0, 0, width, height);
var bmpData = output.LockBits(rect, ImageLockMode.ReadWrite, output.PixelFormat);
// Row-by-row copy
var arrRowLength = width * Image.GetPixelFormatSize(output.PixelFormat) / 8;
var ptr = bmpData.Scan0;
for (var i = 0; i < height; i++)
{
Marshal.Copy(arr, i * arrRowLength, ptr, arrRowLength);
ptr += bmpData.Stride;
}
output.UnlockBits(bmpData);
return output;
}
我认为这与像素类型有关。我选择了 PixelFormat.Format8bppIndexed 的像素格式。其他的不工作。
MessageBox.Show(grabResult.PixelTypeValue.ToString());
这个消息框给了我像素类型。上面写着“BayerBG8”。这是什么意思?我应该怎么做才能获得清晰的图像?
【问题讨论】:
-
BayerBG8的快速 google 找到了我 this page。在上面查找BayerBG2BGR,您会发现使用图像上的特定索引作为 R、G 和 B 是一种相当特殊的转换。您需要提前对字节数组进行某种处理。 -
请注意the camera's official manual document,第 7.14 节“颜色创建和增强”,详细说明了数据的保存方式。
标签: c# image image-processing camera bytebuffer