原始深度数据到文本kinect v1.0 C# [英] raw depth data to text kinect v1.0 C#

查看:94
本文介绍了原始深度数据到文本kinect v1.0 C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been searching the internet and I haven't had any luck. I'm using an Xbox Kinect with the Kinect SDK v1.0. I want to take the raw depth data and convert it into a text document so I can use the depth data. I found something on this site but it was for the Beta2 and I need to use v1.0. Any help is appreciated but I am new to coding so sample code would be best.

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.5.0.1非常简单,实际上与版本1.0相同,并且可以在版本1.0上使用.完成此操作所需要做的就是A)a short[]来保存深度数据B)a DepthImageFrame来将数据移动到数组中,以及C)A StreamWriter来保存数据.

This is pretty simple using version 1.5.0.1, which is practically the same as version 1.0 and will work on it. All you need to complete this is A)a short[] to hold the depth data B)a DepthImageFrame to move the data to the array, and C)A StreamWriter to save the data.

添加short[]来存储深度数据,然后在DepthFrameReadyEventArgs(或AllFramesReadyEventArgs)内部通过以下方式使用" DepthImageFrame:

Add a short[] to store your depth data, and inside of your DepthFrameReadyEventArgs (or AllFramesReadyEventArgs) you "use" a DepthImageFrame by doing:

 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

 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保存数据的方法.

Then we can write a method to save our data using a 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);

希望这会有所帮助!

这篇关于原始深度数据到文本kinect v1.0 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆