0

我已经开始在 c# 中为带有 WPF 的 kinect 进行开发。

当我从 Kinect for Windows Developer Toolkit 启动示例程序“colorBasics”时,相机工作正常,但几秒钟后就冻结了。

我复制了相关代码(所以只有查看相机的代码),它也发生在我自己的程序中。

有人知道我在做什么错吗?

我没有收到任何错误。

这是代码

namespace Testapp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{

    private KinectSensor sensor;

    private WriteableBitmap colorBitmap;

    private byte[] colorPixels;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void WindowLoaded(object sender, RoutedEventArgs e)
    {
        foreach (var potentialSensor in KinectSensor.KinectSensors)
        {
            if (potentialSensor.Status == KinectStatus.Connected)
            {
                this.sensor = potentialSensor;
                break;
            }
        }

        if (null != this.sensor)
        {
            // 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.Image.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!
            try
            {
                this.sensor.Start();
            }
            catch (IOException)
            {
                this.sensor = null;
            }
        }
    }

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

2 回答 2

0

您是使用 USB 电缆的任何类型的扩展,还是直接插入机器?当传感器和机器之间的延迟太长时,我已经看到您所描述的情况。在那种情况下,它是由 Kinect USB 电缆插入延长线引起的。

于 2013-01-21T03:10:42.980 回答
0

在这里有同样的问题,相机一直工作到最终只是冻结并关闭电源,几秒钟后回来只是重复冻结循环。

经过多次测试,我的结论是这个问题是由三件事引起的:

  • PC 并不像运行您的代码那样快速/强大。

  • Kinect 太热了。即使您触摸它并且它“不太热”,传感器也对过热非常敏感。

  • Kinect 不知何故受到了“干扰”。这指的是物理上的振动或运动和/或图像中有太多类似于人类的东西,所以软件试图以 30fps 的每一帧计算它,这是很多微积分,这个事实可能导致上面列出的另外两个问题。

这也可能导致 Michal 描述的延迟问题

于 2015-01-29T11:18:25.150 回答