【发布时间】:2013-06-05 19:03:46
【问题描述】:
我正在尝试流式传输 Kinect 视频数据(只是图像,而不是深度/红外),但我发现图像上的默认缓冲区大小非常大 (1228800) 并且无法通过网络发送。我想知道是否有任何方法可以访问较小的阵列而不必走编解码器压缩的路线。下面是我声明从 Microsoft 样本中获取的 Kinect 的方式;
// Turn on the color stream to receive color frames
this.sensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
// Allocate space to put the pixels we'll receive
this.colorPixels = new byte[this.sensor.ColorStream.FramePixelDataLength];
// This is the bitmap we'll display on-screen
this.colorBitmap = new WriteableBitmap(this.sensor.ColorStream.FrameWidth,
this.sensor.ColorStream.FrameHeight, 96.0, 96.0, PixelFormats.Bgr32, null);
// Set the image we display to point to the bitmap where we'll put the image data
this.kinectVideo.Source = this.colorBitmap;
// Add an event handler to be called whenever there is new color frame data
this.sensor.ColorFrameReady += this.SensorColorFrameReady;
// Start the sensor!
this.sensor.Start();
这里是 New Frame 事件,然后我尝试发送每一帧;
private void SensorColorFrameReady(object sender,
ColorImageFrameReadyEventArgs e)
{
using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
{
if (colorFrame != null)
{
// Copy the pixel data from the image to a temporary array
colorFrame.CopyPixelDataTo(this.colorPixels);
// Write the pixel data into our bitmap
this.colorBitmap.WritePixels(
new Int32Rect(0, 0, this.colorBitmap.PixelWidth,
this.colorBitmap.PixelHeight),
this.colorPixels,
this.colorBitmap.PixelWidth * sizeof(int),
0);
if (NetworkStreamEnabled)
{
networkStream.Write(this.colorPixels, 0,
this.colorPixels.GetLength(0));
}
}
}
}
更新
我使用以下两种方法将ImageFrame 转换为Bitmap,然后将Bitmap 转换为Byte[]。这使缓冲区大小降低到 ~730600。还不够,但进步了。 (来源:Convert Kinect ColorImageFrame to Bitmap)
public static byte[] ImageToByte(Image img)
{
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(img, typeof(byte[]));
}
Bitmap ImageToBitmap(ColorImageFrame Image)
{
byte[] pixeldata = new byte[Image.PixelDataLength];
Image.CopyPixelDataTo(pixeldata);
Bitmap bmap = new Bitmap(Image.Width, Image.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
BitmapData bmapdata = bmap.LockBits(
new Rectangle(0, 0, Image.Width, Image.Height),
ImageLockMode.WriteOnly,
bmap.PixelFormat);
IntPtr ptr = bmapdata.Scan0;
Marshal.Copy(pixeldata, 0, ptr, Image.PixelDataLength);
bmap.UnlockBits(bmapdata);
return bmap;
}
【问题讨论】:
-
“无能”到底是什么意思?流式传输大量数据是完全可能的。怎么了?您希望在没有压缩的情况下拥有什么“更小的数组”?
-
当我尝试使用该大小的缓冲区发送时,我的应用程序挂起而没有崩溃,并且这两个客户端都在同一台机器上运行。通过较小的数组,我的意思是 Kinect 是否试图让我推送无用的数据(例如深度数据)?因为相机上的质量看起来不够好,需要这样大小的缓冲区。
-
640 列 * 480 行 * 每像素 4 字节 (BGR32) = 每帧 1,228,800 字节
-
@Inmx BGR24 会是更好的流媒体格式吗?
-
你见过Kinect Service吗?
标签: c# wpf networking kinect