0

我一直在搜索互联网,但我没有任何运气。我正在使用带有 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();
                }
            }
        }
    } 
}

}

4

1 回答 1

4

使用 1.5.0.1 版本非常简单,它与 1.0 版本几乎相同,并且可以使用。您只需要 A)ashort[]保存深度数据 B)aDepthImageFrame将数据移动到数组中,C)AStreamWriter保存数据。

添加 ashort[]来存储您的深度数据,并在您的DepthFrameReadyEventArgs(或AllFramesReadyEventArgs)内部通过执行以下操作“使用” a 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];
 } 

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

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

希望这可以帮助!

于 2012-07-31T02:22:43.323 回答