68413

raw depth data to text kinect v1.0 C#

Question:

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

}

Answer1:

This is pretty simple using version 1.5.0.1, which is <strong>practically</strong> 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.

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]; }

Then you can add the depth from each frame to depthData using 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); }

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

Hope this helps!

Recommend

  • Form with a MainMenu shrinks every time the project is run
  • Inheriting code-behind class from PhoneApplicationPage's subclass
  • WPF8/C# - Binding Data To Grid
  • String encoding problem on PdoStatement->bindParam()?
  • CSS custom shape button with two colors
  • getElementsByClassName and AJAX
  • Ruby: Why does this way of using map throw an error?
  • I don't get what's the difference between format() and … (python)
  • Parse Framework with Swift
  • How to use the resource module to measure the running time of a function?
  • How to open multiple instances of a program in Linux
  • TSQL Rolling Average of Time Groupings
  • How to define an array of floats in Shader properties?
  • Scala: Function returning an unknown type
  • Find group of records that match multiple values
  • Ubuntu and bcrypt
  • Invalid Date on validation Date of js
  • Ajax Upload File: $_FILES is empty but files exists in request header
  • c# open webrowser in many tab
  • Using JRuby with Rails 3.2
  • Unable to install Git-core+svn by MacPorts
  • C: Incompatible pointer type initializing
  • Caching attributes in superclass
  • Is it possible to access block's scope in method?
  • Alert pop up with LWUIT
  • SignalR .NET Client Invoke throws an exception
  • Disabling Alt-F4 on a Win Forms NotifyIcon
  • Opengl-es onTouchEvents problem or a draw problem? [closed]
  • AES padding and writing the ciphertext to a disk file
  • VS2008 Enable C++ Exception with SEH
  • AT Commands to Send SMS not working in Windows 8.1
  • Cannot Parse HTML Data Using Android / JSOUP
  • AngularJs get employee from factory
  • WPF Applying a trigger on binding failure
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Java static initializers and reflection
  • unknown Exception android
  • Observable and ngFor in Angular 2
  • Unable to use reactive element in my shiny app