我正在使用英特尔实感 SDK 开发 C# 应用程序。我使用了这个能够在彩色框架中跟踪人员的示例:https ://software.intel.com/en-us/articles/diy-pan-tilt-person-tracking-with-intel-realsense-camera
该示例可以显示一个人身体的 X,Y 坐标,但不能显示他的距离,除非它看到一张脸(Z 坐标),而不是这个,我还想记录并显示人的深度数据(灰度图像)在屏幕上。示例程序在能够在屏幕上显示图像之前执行的步骤之一是将 ImageData 从英特尔实感转换为 Format32bppArgb 位图,然后再将其转换为位图图像。
这是将 ImageData(颜色)转换为 Format32bppArgb 位图的代码,用于 RealSense(来自示例)的 ImageData:
public Bitmap ToBitmap(Int32 index, Int32 width, Int32 height)
{
Bitmap bitmap = null;
switch (format)
{
case PixelFormat.PIXEL_FORMAT_RGB32:
bitmap = new Bitmap(width, height, pitches[index], System.Drawing.Imaging.PixelFormat.Format32bppArgb, planes[0]);
break;
case PixelFormat.PIXEL_FORMAT_RGB24:
bitmap = new Bitmap(width, height, pitches[index], System.Drawing.Imaging.PixelFormat.Format24bppRgb, planes[0]);
break;
case PixelFormat.PIXEL_FORMAT_DEPTH:
case PixelFormat.PIXEL_FORMAT_DEPTH_RAW:
bitmap = new Bitmap(width, height, pitches[index], System.Drawing.Imaging.PixelFormat.Format16bppGrayScale, planes[index]);
break;
default:
return null;
}
return bitmap;
}
我创建了一个单独的方法来将(深度)ImageData 转换为 Format16bppGrayScale 位图,在该位图中我预先将格式设置为 PixelFormat.PIXEL_FORMAT_DEPTH_RAW,我将其用于 DepthData。
public Bitmap ToDepthBitmap(Int32 index, Int32 width, Int32 height)
{
Bitmap bitmap = null;
format = PixelFormat.PIXEL_FORMAT_DEPTH;
switch (format)
{
case PixelFormat.PIXEL_FORMAT_RGB32:
bitmap = new Bitmap(width, height, pitches[index], System.Drawing.Imaging.PixelFormat.Format32bppArgb, planes[0]);
break;
case PixelFormat.PIXEL_FORMAT_RGB24:
bitmap = new Bitmap(width, height, pitches[index], System.Drawing.Imaging.PixelFormat.Format24bppRgb, planes[0]);
break;
case PixelFormat.PIXEL_FORMAT_DEPTH:
case PixelFormat.PIXEL_FORMAT_DEPTH_RAW:
bitmap = new Bitmap(width, height, pitches[index], System.Drawing.Imaging.PixelFormat.Format16bppGrayScale, planes[index]);
break;
default:
return null;
}
return bitmap;
}
这是将 Format32bppArgb 位图转换为 BitmapImage 并在屏幕上显示的代码(来自示例):
private void Render(Bitmap bitmap, MyTrackedPerson myTrackedPerson)
{
Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate ()
{
BitmapImage bitmapImage = ConvertBitmap(bitmap);
if (bitmapImage != null)
{
imgStream.Source = bitmapImage;
}
}));
}
private BitmapImage ConvertBitmap(Bitmap bitmap)
{
BitmapImage bitmapImage = null;
if (bitmap != null)
{
MemoryStream memoryStream = new MemoryStream();
bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Bmp);
memoryStream.Position = 0;
bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = memoryStream;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
bitmapImage.Freeze();
}
return bitmapImage;
}
我再次添加了一个单独的方法来尝试对 DepthData 流执行相同的步骤(Format16bppGrayScale Bitmap to BitmapImage):
private void RenderDepth(Bitmap bitmap, MyTrackedPerson myTrackedPerson)
{
Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate ()
{
BitmapImage bitmapImage = ConvertDepthBitmap(bitmap);
if (bitmapImage != null)
{
imgStream.Source = bitmapImage;
}
}));
}
private BitmapImage ConvertDepthBitmap(Bitmap bitmap)
{
BitmapImage bitmapImage = null;
if (bitmap != null)
{
MemoryStream memoryStream = new MemoryStream();
var rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
var bitmapCopy = (Bitmap)bitmap.Clone();
var bitmapData = bitmap.LockBits(rect, ImageLockMode.WriteOnly, bitmap.PixelFormat);
var numberOfBytes = bitmapData.Stride * bitmap.Height;
var bitmapBytes = new short[bitmap.Width * bitmap.Height];
var k = 0;
for (int i = 0; i < bitmap.Width; i++)
{
for (int j = 0; j < bitmap.Height; j++)
{
bitmapBytes[k] = (short)bitmapCopy.GetPixel(i, j).ToArgb();
k++;
}
}
var ptr = bitmapData.Scan0;
Marshal.Copy(bitmapBytes, 0, ptr, bitmapBytes.Length);
bitmap.UnlockBits(bitmapData);
bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Bmp);
memoryStream.Position = 0;
bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = memoryStream;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
bitmapImage.Freeze();
}
return bitmapImage;
}
上面的最后一个代码示例基于这个问题的答案:Generate 16-bit grayscale BitmapData and save to file
不幸的是,上面的代码给出了一个参数异常,表示此行中的参数无效:
bitmapBytes[k] = (short)bitmapCopy.GetPixel(i, j).ToArgb();
谁能指出我是否在这里遗漏了什么?我做错了吗?