【问题标题】:raw depth data to text kinect v1.0 C#原始深度数据到文本 kinect v1.0 C#
【发布时间】:2012-07-28 06:05:38
【问题描述】:

我一直在搜索互联网,但我没有任何运气。我正在使用带有 Kinect SDK v1.0 的 Xbox Kinect。我想获取原始深度数据并将其转换为文本文档,以便我可以使用深度数据。我在这个网站上找到了一些东西,但它是针对 Beta2 的,我需要使用 v1.0。感谢您提供任何帮助,但我是编码新手,因此最好使用示例代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Kinect;
using System.Diagnostics;
using System.IO;

namespace DepthTextStream
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    const float MaxDepthDistance = 4095; // max value returned
    const float MinDepthDistance = 850; // min value returned
    const float MaxDepthDistanceOffset = MaxDepthDistance - MinDepthDistance;

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        kinectSensorChooser1.KinectSensorChanged += new DependencyPropertyChangedEventHandler(kinectSensorChooser1_KinectSensorChanged);

    }

    void kinectSensorChooser1_KinectSensorChanged(object sender, DependencyPropertyChangedEventArgs e)
    {

        var oldSensor = (KinectSensor)e.OldValue;

        //stop the old sensor
        if (oldSensor != null)
        {
            oldSensor.Stop();
            oldSensor.AudioSource.Stop();
        }

        //get the new sensor
        var newSensor = (KinectSensor)e.NewValue;
        if (newSensor == null)
        {
            return;
        }

        //turn on features that you need
        newSensor.DepthStream.Enable(DepthImageFormat.Resolution320x240Fps30);
        newSensor.SkeletonStream.Enable(); 

        //sign up for events if you want to get at API directly
        newSensor.AllFramesReady += new EventHandler<AllFramesReadyEventArgs>(newSensor_AllFramesReady);


        try
        {
            newSensor.Start();
        }
        catch (System.IO.IOException)
        {
            //this happens if another app is using the Kinect
            kinectSensorChooser1.AppConflictOccurred();
        }
    }

    void newSensor_AllFramesReady(object sender, AllFramesReadyEventArgs e)
    {
        short[] depthData;

        using (DepthImageFrame depthFrame = e.OpenDepthImageFrame()) //create a new frame every time one is ready
    {
    //assign a value to depthData
    depthData = new short[depthFrame.PixelDataLength];
    } 

    }


    private void SaveDepthData(short[] depthData)
    {
        //initialize a StreamWriter
        StreamWriter sw = new StreamWriter(@"C:/Example.txt");

        //search the depth data and add it to the file
        for (int i = 0; i < depthData.Length; i++)
        {
            sw.WriteLine(depthData[i] + "\n"); //\n for a new line
        }

        //dispose of sw
        sw.Close();
        SaveDepthData(depthData);
    }      

    private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        StopKinect(kinectSensorChooser1.Kinect); 
    }

    private void StopKinect(KinectSensor sensor)
    {
        if (sensor != null)
        {
            if (sensor.IsRunning)
            {
                //stop sensor 
                sensor.Stop();

                //stop audio if not null
                if (sensor.AudioSource != null)
                {
                    sensor.AudioSource.Stop();
                }
            }
        }
    } 
}

}

【问题讨论】:

  • 版本 1.0?为什么不是 1.5.0.1?
  • 为什么需要将它添加到文本文件中?

标签: c# text kinect depth


【解决方案1】:

使用 1.5.0.1 版本非常简单,实际上与 1.0 版本相同,并且可以使用。您需要完成的只是 A)a short[] 保存深度数据 B)a DepthImageFrame 将数据移动到数组中,C)A StreamWriter 保存数据。

添加一个short[] 来存储您的深度数据,并在您的DepthFrameReadyEventArgs(或AllFramesReadyEventArgs)内部“使用”DepthImageFrame,方法是:

 short[] depthData;

 ...

 using (DepthImageFrame depthFrame = e.OpenDepthImageFrame(()) //create a new frame every time one is ready
 {
       //assign a value to depthData
       depthData = new short[depthFrame.PixelDataLength];
 } 

然后你可以使用DepthImageFrame.CopyPixelDataTo将每一帧的深度添加到depthData

 using (DepthImageFrame depthFrame = e.OpenDepthImageFrame(()) //create a new frame every time one is ready
 {
       //assign a value to depthData
       depthData = new short[depthFrame.PixelDataLength];

       //add raw depth data to depthData
       depthFrame.CopyPixelDataTo(depthData);
 } 

然后我们可以编写一个方法来使用StreamWriter保存我们的数据。

 private void SaveDepthData(short[] depthData)
 {
       //initialize a StreamWriter
       StreamWriter sw = new StreamWriter(@"C:/Example.txt");

       //search the depth data and add it to the file
       for (int i = 0; i < depthData.Length; i++)
       {
            sw.WriteLine(depthData[i] + "\n"); //\n for a new line
       }

       //dispose of sw
       sw.Close();
 }      

 ...

 SaveDepthData(depthData);

希望这会有所帮助!

【讨论】:

  • 抱歉,接下来几天我将无法提供帮助,因为我要去露营:)
  • @Outlaw_Lemur 我拿了我以前的代码,其中大部分是从教程中获得的,并对其进行了编辑并添加了您给我的内容。添加流写入器时出现错误。我需要添加参考吗?还有我要保存的东西在哪里发送。到 (@"C:/Example.txt");?
  • 添加“使用 System.IO;”到 StreamWriter 的顶部。是的,在他的示例中,所有数据都将保存到 C:/Example.txt
  • 当我添加所有内容时,我没有收到任何错误,但是当我运行它时,有一个未处理的 NullReferenceException。我将在下面发布我的代码。谁能告诉我我的错误在哪里?
猜你喜欢
  • 2012-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-03
  • 2015-06-21
相关资源
最近更新 更多