none
how to save depth data (distance) using kinect and C# wpf application RRS feed

  • Question


  • I am using the same sample working in depth data  in your links

    http://channel9.msdn.com/Series/KinectSDKQuickstarts/

     

    I need to save the distances from depth image to txt file (just one column for depth ), I can't do that by myself, after that I need to define objects appear in this depth image, my idea is to search for same object appear in same distance from the camera then by approximity I want to define the object and dispaly new image contain only this object.

    This is the code I already used it:

     

     

    // color depth image, RGB image, Tilt control , color code bar

    // August, 30

    // tested

    // try make output

    // display Distance on console and WPF

    // capture video image RGB

    //capture Depth image

    // write distance .txt file

    //test Sep.04

     

     

    using System;

    using OpenCvSharp;

    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.Research.Kinect.Audio;

    using Microsoft.Research.Kinect.Nui;

    using Coding4Fun.Kinect.Wpf;

    using System.IO.MemoryMappedFiles;

     

    using System.Diagnostics;

    using System.IO;

    //using System.Windows.Drawing.Image;

     

     

    namespace WpfApplication2

    {

        /// <summary>

        /// Interaction logic for MainWindow.xaml

        /// </summary>

        public partial class MainWindow : Window

        {

            public MainWindow()

     

            {

                InitializeComponent();

            }

     

            //

     

     

            //

     

            /// <summary>

            //

            /// </summary>

     

            Runtime nui = new Runtime();

     

            private void button1_cliked(object sender, RoutedEventArgs e)

            {

                //Set angle to slider1 value

                nui.NuiCamera.ElevationAngle = (int)slider4.Value;

     

            }

     

     

     

     

            //private void button2_Click(object sender, RoutedEventArgs e)

            //{

            //    nui.Initialize(RuntimeOptions.UseColor | RuntimeOptions.UseDepthAndPlayerIndex |

            //       RuntimeOptions.UseDepth);

            //    nui.VideoFrameReady += new EventHandler<ImageFrameReadyEventArgs>(nui_VideoFrameReady);

            //    nui.DepthFrameReady += new EventHandler<ImageFrameReadyEventArgs>(nui_DepthFrameReady);

            //    //nui.VideoStream.Open(ImageStreamType.Video, 2, 

            //     //   ImageResolution.Resolution640x480, ImageType.Color);

            //  //2//  nui.DepthStream.Open(ImageStreamType.Depth, 2, 

            //    //2//   ImageResolution.Resolution320x240, ImageType.DepthAndPlayerIndex);

            //    //

            //    nui.VideoStream.Open(ImageStreamType.Video, 2,

            //        ImageResolution.Resolution640x480, ImageType.Color);

            //    nui.DepthStream.Open(ImageStreamType.Depth, 2, 

            //      ImageResolution.Resolution320x240, ImageType.DepthAndPlayerIndex);

            //}

     

            private void Window_Loaded(object sender, RoutedEventArgs e)

            {

                nui.Initialize(RuntimeOptions.UseColor | RuntimeOptions.UseDepthAndPlayerIndex |

                   RuntimeOptions.UseDepth);

     

               //1// nui.Initialize(RuntimeOptions.UseColor | RuntimeOptions.UseDepthAndPlayerIndex );

                //

                // new code

                //nui.Initialize(RuntimeOptions.UseColor | RuntimeOptions.UseDepthAndPlayerIndex |

                //   RuntimeOptions.UseDepth);

               // nui.DepthFrameReady+=new EventHandler<ImageFrameReadyEventArgs>(nui_DepthFrameReady);

     

                //

                nui.VideoFrameReady += new EventHandler<ImageFrameReadyEventArgs>(nui_VideoFrameReady);

                nui.DepthFrameReady += new EventHandler<ImageFrameReadyEventArgs>(nui_DepthFrameReady);

                //nui.VideoStream.Open(ImageStreamType.Video, 2, 

                 //   ImageResolution.Resolution640x480, ImageType.Color);

              //2//  nui.DepthStream.Open(ImageStreamType.Depth, 2, 

                //2//   ImageResolution.Resolution320x240, ImageType.DepthAndPlayerIndex);

                //

                nui.VideoStream.Open(ImageStreamType.Video, 2,

                    ImageResolution.Resolution640x480, ImageType.Color);

                nui.DepthStream.Open(ImageStreamType.Depth, 2, 

                  ImageResolution.Resolution320x240, ImageType.DepthAndPlayerIndex);

     

     

                //

               // Console.

                //ConsoleManager.Show()

                //

     

            }

     

            void nui_DepthFrameReady(object sender, ImageFrameReadyEventArgs e)

            {

     

                //BitmapSource source = e.ImageFrame.ToBitmapSource();

                //source.Save("C:/Users/cuicar/Documents/Visual Studio 2010/Projects/tested programmes/color depth image_distance wpf/WpfApplication2/RGB image.jpg", ImageFormat.Jpeg);

     

                //Convert depth information for a pixel into color information 

                //(Generate own histograme)

                byte[] ColoredBytes = GenerateColoredBytes(e.ImageFrame);

     

                //create an image based on returned colors

     

                PlanarImage image = e.ImageFrame.Image;

                //image1.Source = e.ImageFrame.ToBitmapSource();

                //e.ImageFrame.ToDepthArray2D()[160][120];

                //e.ImageFrame.GetDistance(160, 120);

                //e.ImageFrame.ToDepthArray();

                //e.ImageFrame.ToBitmapSource().Save("C:/Users/cuicar/Documents/Visual Studio 2010/Projects/tested programmes/color depth image_distance wpf/WpfApplication2/Depth image.jpg", ImageFormat.Jpeg);

                //image1.Source = e.ImageFrame.ToBitmapSource();

                image1.Source = BitmapSource.Create(image.Width, image.Height, 96, 96,

                    PixelFormats.Bgr32, null, ColoredBytes,

                    image.Width * PixelFormats.Bgr32.BitsPerPixel / 8);

                //e.ImageFrame.ToBitmapSource().Save("C:/Users/cuicar/Documents/Visual Studio 2010/Projects/tested programmes/color depth image_distance wpf/WpfApplication2/Depth image.jpg", ImageFormat.Jpeg);

                //image1.Source = e.ImageFrame.ToBitmapSource();

                //e.ImageFrame.ToDepthArray2D()[160][120];

                //e.ImageFrame.GetDistance(160, 120);

                //e.ImageFrame.ToDepthArray();

                //e.ImageFrame.ToBitmapSource().Save("C:/Users/cuicar/Documents/Visual Studio 2010/Projects/tested programmes/color depth image_distance wpf/WpfApplication2/Depth image.jpg", ImageFormat.Jpeg);

                //image1.Source = e.ImageFrame.ToBitmapSource();

                ////e..ImageFrame.ToDepthArray2D()[160][120];

                //e.ImageFrame.GetDistance(160, 120);

                //e.ImageFrame.ToDepthArray();

                //e.ImageFrame.ToBitmapSource().Save("catpure.jpg", ImageFormat.Jpeg);

                //BitmapSource source2 = e.ImageFrame.ToBitmapSource();

                //source2.Save("C:/Users/cuicar/Documents/Visual Studio 2010/Projects/tested programmes/color depth image_distance wpf/WpfApplication2/Depth_image.jpg", ImageFormat.Jpeg);

     

     

                //BitmapSource source = e.ImageFrame.ToBitmapSource();

                //source.Save("C:/Users/cuicar/Documents/Visual Studio 2010/Projects/tested programmes/color depth image_distance wpf/WpfApplication2/Depth image.jpg", ImageFormat.Jpeg);

     

              //save video;

                //{

                    //PlanarImage Image = e.ImageFrame.Image;

                    //imgVideo.Source = BitmapSource.Create(

                    //    Image.Width, Image.Height, 96, 96, PixelFormats.Bgr32, null,

                    //    Image.Bits, Image.Width * Image.BytesPerPixel);

     

                //e.ImageFrame.ToBitmapSource().Save("catpure.jpg", ImageFormat.Jpeg);

     

                //}

     

            }

     

            private byte[] GenerateColoredBytes(ImageFrame imageFrame)

            {

     

                int height = imageFrame.Image.Height;

                int width = imageFrame.Image.Width;

     

                //Depth data for each pixel

                Byte[] depthData = imageFrame.Image.Bits;

     

     

                //

     

     

     

     

     

                //for (int m = 0; m <= 10;m++ )

                //{

     

                //  Console.WriteLine(depthData.Length.ToString());

     

                //Console.WriteLine(depthData);

               // depthData.Max();

                //Console.WriteLine("depthData.max = ",depthData.Max());

                //Console.WriteLine( depthData.Max());

              //  Trace.WriteLine(depthData.Max());

                //Console.WriteLine(depthData.Max());

                ////}

     

                //Console.WriteLine('depthData.Length'=depthData.Length);

                //Console.ReadLine();

     

                //colorFrame contains color information for all pixels in image

                //Height x Width x 4 (Red, Green, Blue, empty byte)

                Byte[] colorFrame = new byte[imageFrame.Image.Height * imageFrame.Image.Width * 4];

     

     

     

                //hardcoded locations to Blue, Green, Red (BGR) index positions       

                const int BlueIndex = 0;

                const int GreenIndex = 1;

                const int RedIndex = 2;

     

     

                var depthIndex = 0;

                for (var y = 0; y < height; y++)

                {

     

                    var heightOffset = y * width;

     

                    for (var x = 0; x < width; x++)

                    {

     

                        var index = ((width - x - 1) + heightOffset) * 4;

     

     

                        var distance = GetDistanceWithPlayerIndex(depthData[depthIndex], depthData[depthIndex + 1]);

                        //foreach (int i in  distance)

     

                        //using (var writer = new StreamWriter("C:/Users/cuicar/Documents/Visual Studio 2010/Projects/tested programmes/color depth image_distance wpf/WpfApplication2/depth.txt"))

                        ////{

                        ////    // Loop through ten numbers

                        ////    for (int k = 0; k < depthIndex; k++)

                        ////    {

                        ////        // Write format string to file

     

                        //    writer.WriteLine(distance + " ");

     

                        //MessageBox.Show(distance.ToString());

                        //    }

     

                        //using (var writer = new StreamWriter("C:/Users/cuicar/Documents/Visual Studio 2010/Projects/tested programmes/color depth image_distance wpf/WpfApplication2/depth.txt"))

                        //{

                        //}

                        //if (distance >= 850 && distance < 950)

                        //{

                        //    writer.WriteLine(distance + " ");

                        //}

                        //}

                        // Loop through ten numbers

                        //for (int k = 0; k < depthData.Length; k++)

                        //{

     

                        // Write format string to file

                        //writer.WriteLine(distance + " "); ;

                        //}

                        //}

                        // Use var type which is shorter

                        ////using (var writer = new StreamWriter("loop.txt"))

                        //using (var writer = new StreamWriter("C:/Users/cuicar/Documents/Visual Studio 2010/Projects/tested programmes/color depth image_distance wpf/WpfApplication2/depth.txt"))

                        //{

                        //    // Loop through ten numbers

                        //    for (int k = 0; k < depthIndex; k++)

                        //    {

                        //        // Write format string to file

                        //        writer.WriteLine(depthIndex + " "); ;

                        //    }

                        //}

     

     

     

     

     

                        //

                        //MessageBox.Show(distance.ToString());

                        //Console.WriteLine(distance);

                        //Console.ReadLine();

                        ////Trace.WriteLine(distance);

                        //Console.WriteLine(distance);

                        //// depthData.Max();

                        ////Console.WriteLine("depthData.max = ",depthData.Max());

                        ////Console.WriteLine( depthData.Max());

                        ////  Trace.WriteLine(depthData.Max());

                        ////Console.WriteLine(depthData.Max());

                        //////}

     

                        ////Console.WriteLine('depthData.Length'=depthData.Length);

                        //Console.ReadLine();

     

     

     

     

     

                        //for (int m = 0; m <= 10;m++ )

                        //{

     

                        //  Console.WriteLine(depthData.Length.ToString());

     

                        //Console.WriteLine(distance);

                        // depthData.Max();

                        //Console.WriteLine("depthData.max = ",depthData.Max());

                        //Console.WriteLine( depthData.Max());

                        //  Trace.WriteLine(depthData.Max());

                        //Console.WriteLine(depthData.Max());

                        ////}

     

                        //Console.WriteLine('depthData.Length'=depthData.Length);

                        //  Console.ReadLine()

                        //}

     

                        //for (d1= distance; d1<=MaxHeight())

                        //foreach (int i in distance)

                        //int u=int.MaxValue(distance);

     

     

                        //using (var writer = new StreamWriter("C:/Users/cuicar/Documents/Visual Studio 2010/Projects/tested programmes/color depth image_distance wpf/WpfApplication2/depth.txt"))

                        //{

                        //    // Loop through ten numbers

                        //    for (int k = 0; k < depthIndex; k++)

                        //    {

                        //        // Write format string to file

     

                        //writer.WriteLine(distance + " ");

     

     

                        if (distance < 850)

                        {

                            ////we are very close white

                            colorFrame[index + BlueIndex] = 255;

                            colorFrame[index + GreenIndex] = 255;

                            colorFrame[index + RedIndex] = 255;

     

                            //we are very close white

                            //colorFrame[index + BlueIndex] = 237;

                            //colorFrame[index + GreenIndex] = 149;

                            //colorFrame[index + RedIndex] = 100;

                            //MessageBox.Show(distance.ToString());

     

                        }

                        else if (distance >= 850 && distance < 950)

                        {

                            //we are a bit further away Green

                            colorFrame[index + BlueIndex] = 0;

                            colorFrame[index + GreenIndex] = 255;

                            colorFrame[index + RedIndex] = 0;

                            //MessageBox.Show(distance.ToString());

                            //MessageBox.Show(distance.ToString());

                        }

                        else if (distance >= 950 && distance < 1050)

                        {

                            //we are a bit further away Blue

                            colorFrame[index + BlueIndex] = 255;

                            colorFrame[index + GreenIndex] = 0;

                            colorFrame[index + RedIndex] = 0;

     

                            // Use var type which is shorter

                            ////using (var writer = new StreamWriter("loop.txt"))

                            //distance.Equals()

     

                            //MessageBox.Show(distance.ToString());

     

                            //MessageBox.Show(distance.ToString());

     

                            //////////////////////  // output distance using wpf massage ////////////////////////////////////////////////////////

                            //for (int m = 0; m <= 10; m++)

                            ////{

                            //MessageBox.Show(distance.ToString());

     

                            //}

     

                            /////////////////////// output distance using console////////////////////////

                            //Console.WriteLine(distance);

                            //// depthData.Max();

                            ////Console.WriteLine("depthData.max = ",depthData.Max());

                            ////Console.WriteLine( depthData.Max());

                            ////  Trace.WriteLine(depthData.Max());

                            ////Console.WriteLine(depthData.Max());

                            //////}

     

                            ////Console.WriteLine('depthData.Length'=depthData.Length);

                            //Console.ReadLine();

     

     

                        }

                        else if (distance >= 1050 && distance < 1150)

                        {

                            //we are a bit further away Red

                            colorFrame[index + BlueIndex] = 0;

                            colorFrame[index + GreenIndex] = 0;

                            colorFrame[index + RedIndex] = 255;

     

                        }

                        else if (distance >= 1150 && distance < 1250)

                        {

                            //we are a bit further away yellow

                            colorFrame[index + BlueIndex] = 0;

                            colorFrame[index + GreenIndex] = 255;

                            colorFrame[index + RedIndex] = 255;

                        }

     

                        else if (distance >= 1250)

                        {

                            //we are the farthest black

                            colorFrame[index + BlueIndex] = 0;

                            colorFrame[index + GreenIndex] = 0;

                            colorFrame[index + RedIndex] = 0;

                        }

     

     

                        ////equal coloring for monochromatic histogram

                        // var intensity = CalculateIntensityFromDepth(distance);

                        //colorFrame[index + BlueIndex] = intensity;

                        //colorFrame[index + GreenIndex] = intensity;

                        //colorFrame[index + RedIndex] = intensity;

     

                        ////Color a player

                        //if (GetPlayerIndex(depthData[depthIndex]) > 0)

                        //{

                        //    //we are the farthest

                        //    colorFrame[index + BlueIndex] = 0;

                        //    colorFrame[index + GreenIndex] = 255;

                        //    colorFrame[index + RedIndex] = 255;

                        //}

     

                        //jump two bytes at a time

                        depthIndex += 2;

                        // for (distance = 900; distance <= 1000; distance++) ;

                        //{

                        //    MessageBox.Show(distance.ToString());

                        //    //using (var writer = new StreamWriter("C:/Users/cuicar/Documents/Visual Studio 2010/Projects/tested programmes/color depth image_distance wpf/WpfApplication2/depth.txt"))

                        //    //{

                        //    //var writer = new StreamWriter("C:/Users/cuicar/Documents/Visual Studio 2010/Projects/tested programmes/color depth image_distance wpf/WpfApplication2/depth.txt");

                        //    //{

                        //}

     

                    }

     

                }

     

                return colorFrame;

     

     

            }

     

            //private static int GetPlayerIndex(byte firstFrame)

            //{

            //    //returns 0 = no player, 1 = 1st player, 2 = 2nd player...

            //    //bitwise & on firstFrame

            //    return (int)firstFrame & 7;

            //}

     

     

            private int GetDistanceWithPlayerIndex(byte firstFrame, byte secondFrame)

            {

                //offset by 3 in first byte to get value after player index 

                int distance = (int)(firstFrame >> 3 | secondFrame << 5);

                return distance;

                //MessageBox.Show(distance.ToString());

            //using (var writer = new StreamWriter("C:/Users/cuicar/Documents/Visual Studio 2010/Projects/tested programmes/color depth image_distance wpf/WpfApplication2/depth.txt"))

            }

     

     

            //const float MaxDepthDistance = 4000; // max value returned

            //const float MinDepthDistance = 850; // min value returned

            //const float MaxDepthDistanceOffset = MaxDepthDistance - MinDepthDistance;

     

            //public static byte CalculateIntensityFromDepth(int distance)

            //{

            //    //formula for calculating monochrome intensity for histogram

            //    return (byte)(255 - (255 * Math.Max(distance - MinDepthDistance, 0)

            //        / (MaxDepthDistanceOffset)));

            //}

     

            // new code

     

            //const float MaxDistance = 4000; // max value returned

            //const float MinDistance = 850; // min value returned

            //const float DistanceOffset = MaxDepthDistance - MinDepthDistance;

     

            //public static byte printdistance(int distance)

            //{

     

            //    if (distance >= MinDistance && distance <= MaxDistance)

            //    { 

     

            //    }

            //}

     

     

            //

     

     

     

            void nui_VideoFrameReady(object sender, ImageFrameReadyEventArgs e)

            {

     

                PlanarImage image = e.ImageFrame.Image;

                //image2.Source = BitmapSource.Create(image.Width, image.Height,

                //    96, 96, PixelFormats.Bgr32, null, image.Bits, image.Width * image.BytesPerPixel);

                //image2.Source=e.ImageFrame.ToBitmapSource();

                //nui.NuiCamera.ElevationAngle = Camera.ElevationMaximum;

                 //BitmapSource.Save(string fileName, ImageFormat format)

    //returns nothing

                //save video;

                //{

                //PlanarImage Image = e.ImageFrame.Image;

                //imgVideo.Source = BitmapSource.Create(

                //    Image.Width, Image.Height, 96, 96, PixelFormats.Bgr32, null,

                //    Image.Bits, Image.Width * Image.BytesPerPixel);

     

     

                //////////////////////////////////////////////////////////

                //BitmapSource source1 = e.ImageFrame.ToBitmapSource();

                //source1.Save("C:/Users/cuicar/Documents/Visual Studio 2010/Projects/tested programmes/color depth image_distance wpf/WpfApplication2/RGB image.jpg", ImageFormat.Jpeg);

                image2.Source = e.ImageFrame.ToBitmapSource();

                //e.ImageFrame.ToBitmapSource().Save("C:/Users/cuicar/Documents/Visual Studio 2010/Projects/tested programmes/color depth image_distance wpf/WpfApplication2/RGB image.jpg", ImageFormat.Jpeg);

                //source.Save(

            //     private void button2_Click(object sender, RoutedEventArgs e)

            //{

     

            //    source.Save("foo.jpg", ImageFormat.Jpeg);

            //}

     

                //e.ImageFrame.ToBitmapSource().Save("catpure.jpg", ImageFormat.Jpeg);

     

                //}

     

     

            }

     

            private void Window_Closed(object sender, EventArgs e)

            {

                nui.Uninitialize();

            }

     

     

     

            private void slider4_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)

            {

     

            }

     

     

     

     

     

    //        BitmapSource.Save(string fileName, ImageFormat format)

    //returns nothing

     

     

     

     

     

     

        }

    }

    Any help please?

    Rami 

    Wednesday, September 7, 2011 4:11 AM

Answers

  • Yes, sure , I can now save the depth value to text file , the text file will contain x and y (pixel corrdinate) and the third coloum depth value. This is the full code (I tested it).

     

    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.Research.Kinect.Audio;

    using Microsoft.Research.Kinect.Nui;

    using Coding4Fun.Kinect.Wpf;

    using System.IO.MemoryMappedFiles;

     

     

    using System.Diagnostics;

    using System.IO;

     

     

    namespace WpfApplication2

    {

        /// <summary>

        /// Interaction logic for MainWindow.xaml

        /// </summary>

        public partial class MainWindow : Window

        {

            public MainWindow()

     

            {

                InitializeComponent();

            }

     

            //

     

     

            //

     

            /// <summary>

            //

            /// </summary>

     

            Runtime nui = new Runtime();

     

            private void button1_cliked(object sender, RoutedEventArgs e)

            {

                //Set angle to slider1 value

                nui.NuiCamera.ElevationAngle = (int)slider4.Value;

     

            }

     

     

     

            private void Window_Loaded(object sender, RoutedEventArgs e)

            {

     

     

             nui.Initialize(RuntimeOptions.UseColor | RuntimeOptions.UseDepthAndPlayerIndex );

     

                nui.VideoFrameReady += new EventHandler<ImageFrameReadyEventArgs>(nui_VideoFrameReady);

                nui.DepthFrameReady += new EventHandler<ImageFrameReadyEventArgs>(nui_DepthFrameReady);

     

                nui.VideoStream.Open(ImageStreamType.Video, 2,

                    ImageResolution.Resolution640x480, ImageType.Color);

                nui.DepthStream.Open(ImageStreamType.Depth, 2,

                  ImageResolution.Resolution320x240, ImageType.DepthAndPlayerIndex);

     

     

            }

     

            void nui_DepthFrameReady(object sender, ImageFrameReadyEventArgs e)

            {

     

     

                byte[] ColoredBytes = GenerateColoredBytes(e.ImageFrame);

     

                //create an image based on returned colors

     

                PlanarImage image = e.ImageFrame.Image;

     

                image1.Source = BitmapSource.Create(image.Width, image.Height, 96, 96,

                    PixelFormats.Bgr32, null, ColoredBytes,

                    image.Width * PixelFormats.Bgr32.BitsPerPixel / 8);

     

            }

     

            private byte[] GenerateColoredBytes(ImageFrame imageFrame)

            {

     

                int height = imageFrame.Image.Height;

                int width = imageFrame.Image.Width;

     

                //Depth data for each pixel

                Byte[] depthData = imageFrame.Image.Bits;

     

     

                Byte[] colorFrame = new byte[imageFrame.Image.Height * imageFrame.Image.Width * 4];

     

     

                //hardcoded locations to Blue, Green, Red (BGR) index positions       

                const int BlueIndex = 0;

                const int GreenIndex = 1;

                const int RedIndex = 2;

                var depthIndex = 0;

                TextWriter tw = new StreamWriter("sameobjects_test" + ".txt");

     

                for (var y = 0; y < height; y++)

                {

     

                    var heightOffset = y * width;

     

                    for (var x = 0; x < width; x++)

                    {

     

                        var index = ((width - x - 1) + heightOffset) * 4;

     

     

     

                        // save distance to text file

                        var distance = GetDistanceWithPlayerIndex(depthData[depthIndex], depthData[depthIndex + 1]);

                        tw.WriteLine("x: " + x + ", y: " + y + ", distance: " + distance + " ");                    

     

                        if (distance < 850)

                        {

                            ////we are very close white

                            colorFrame[index + BlueIndex] = 255;

                            colorFrame[index + GreenIndex] = 255;

                            colorFrame[index + RedIndex] = 255;

     

     

     

                        }

     

     

                        else if (distance >= 850 && distance < 950)

                        {

                            //we are a bit further away Green

                            colorFrame[index + BlueIndex] = 0;

                            colorFrame[index + GreenIndex] = 255;

                            colorFrame[index + RedIndex] = 0;

     

                        }

                        else if (distance >= 950 && distance < 1050)

                        {

                            //we are a bit further away Blue

                            colorFrame[index + BlueIndex] = 255;

                            colorFrame[index + GreenIndex] = 0;

                            colorFrame[index + RedIndex] = 0;

     

     

                        }

                        else if (distance >= 1050 && distance < 1150)

                        {

                            //we are a bit further away Red

                            colorFrame[index + BlueIndex] = 0;

                            colorFrame[index + GreenIndex] = 0;

                            colorFrame[index + RedIndex] = 255;

     

                        }

                        else if (distance >= 1150 && distance < 1250)

                        {

                            //we are a bit further away yellow

                            colorFrame[index + BlueIndex] = 0;

                            colorFrame[index + GreenIndex] = 255;

                            colorFrame[index + RedIndex] = 255;

                        }

     

                        else if (distance >= 1250)

                        {

                            //we are the farthest black

                            colorFrame[index + BlueIndex] = 0;

                            colorFrame[index + GreenIndex] = 0;

                            colorFrame[index + RedIndex] = 0;

                        }

     

     

     

                        //jump two bytes at a time

                        depthIndex += 2;

     

     

                    }

     

                }

                //tw.Close();

                return colorFrame;

     

     

            }

     

     

     

     

            private int GetDistanceWithPlayerIndex(byte firstFrame, byte secondFrame)

            {

                //    //offset by 3 in first byte to get value after player index 

                int distance = (int)(firstFrame >> 3 | secondFrame << 5);

                return distance;

     

            }

     

            void nui_VideoFrameReady(object sender, ImageFrameReadyEventArgs e)

            {

     

                PlanarImage image = e.ImageFrame.Image;

     

     

                image2.Source = e.ImageFrame.ToBitmapSource();

     

     

     

     

     

                    }

     

            private void Window_Closed(object sender, EventArgs e)

            {

                nui.Uninitialize();

            }

     

     

            private void slider4_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)

            {

     

            }

     

     

     

        }

    }


    Rami
    Sunday, October 9, 2011 4:51 AM

All replies

  • So, the first problem I see with your code is that the line that reads:
    nui.Initialize(RuntimeOptions.UseColor | RuntimeOptions.UseDepthAndPlayerIndex | RuntimeOptions.UseDepth);
    

    Will throw an exception, and then the Runtime object will not be properly initialized. You can't use "UseDepthAndPlayerIndex" and "UseDepth" options simultaneously. Looks like you want to use depth-and-player index stream in other parts of code, so change this line to:
    nui.Initialize(RuntimeOptions.UseColor | RuntimeOptions.UseDepthAndPlayerIndex);
    

    Also, all places where you try to create a file to write to are commented out, so I'm not sure which one to pay attention to. The GenerateColoredBytes method seems like a good place to do this, since that's where you iterate through image, and the inner loop initializes a "distance" variable which contains what sounds like the data you want to output to file.
    Also, your example is really long and mostly consists of commented out lines. Could you edit post and remove lines that contain code that has been commented out? right now the extra commented-out code is pretty distracting.
    Eddy

    I'm here to help
    Wednesday, September 7, 2011 6:24 PM
  • Sorry Eddy to confuse you,
    the code where I used is below, when I compile this program the txt file appear to have 45642 numbers with same value.
    how can I have the distance for all the pixels appear in the image (I need just to have for one frame, static image). 
    Also Can I have text file contain two coloum : one coloum show pixel corrdinate i.e. (0,0) and another coloumn contain distance in mm ?
    Many Thanks in Advance,
    Rami
    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.Research.Kinect.Audio;
    using Microsoft.Research.Kinect.Nui;
    using Coding4Fun.Kinect.Wpf;
    using System.IO.MemoryMappedFiles;
    using System.Diagnostics;
    using System.IO;
    namespace WpfApplication2
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
            //
            
            
            //
           
            /// <summary>
            //
            /// </summary>
            Runtime nui = new Runtime();
            private void button1_cliked(object sender, RoutedEventArgs e)
            {
                //Set angle to slider1 value
                nui.NuiCamera.ElevationAngle = (int)slider4.Value;
                
            }
          
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                nui.Initialize(RuntimeOptions.UseColor | RuntimeOptions.UseDepthAndPlayerIndex);
               
                nui.VideoFrameReady += new EventHandler<ImageFrameReadyEventArgs>(nui_VideoFrameReady);
                nui.DepthFrameReady += new EventHandler<ImageFrameReadyEventArgs>(nui_DepthFrameReady);
                
                nui.VideoStream.Open(ImageStreamType.Video, 2,
                    ImageResolution.Resolution640x480, ImageType.Color);
                nui.DepthStream.Open(ImageStreamType.Depth, 2, 
                  ImageResolution.Resolution320x240, ImageType.DepthAndPlayerIndex);
                
            }
            void nui_DepthFrameReady(object sender, ImageFrameReadyEventArgs e)
            {
             
              
                byte[] ColoredBytes = GenerateColoredBytes(e.ImageFrame);
                //create an image based on returned colors
                PlanarImage image = e.ImageFrame.Image;
                
                image1.Source = BitmapSource.Create(image.Width, image.Height, 96, 96,
                    PixelFormats.Bgr32, null, ColoredBytes,
                    image.Width * PixelFormats.Bgr32.BitsPerPixel / 8);
                
                
            }
            private byte[] GenerateColoredBytes(ImageFrame imageFrame)
            {
                int height = imageFrame.Image.Height;
                int width = imageFrame.Image.Width;
                //Depth data for each pixel
                Byte[] depthData = imageFrame.Image.Bits;
               
                Byte[] colorFrame = new byte[imageFrame.Image.Height * imageFrame.Image.Width * 4];
               //hardcoded locations to Blue, Green, Red (BGR) index positions       
                const int BlueIndex = 0;
                const int GreenIndex = 1;
                const int RedIndex = 2;
                  var depthIndex = 0;
                for (var y = 0; y < height; y++)
                {
                    var heightOffset = y * width;
                    for (var x = 0; x < width; x++)
                    {
                        var index = ((width - x - 1) + heightOffset) * 4;
                        
                        var distance = GetDistanceWithPlayerIndex(depthData[depthIndex], depthData[depthIndex + 1]);
    ////////////////////////////////////////////////////////////////////////////////////////////////////
                     
     // Here I am trying to write to send all the distance value to file//
                        TextWriter tw = new StreamWriter("getdistance" + ".txt");
                        for (int i = 0; i < depthData.Length; i+=2)
                           
                            {
                                tw.WriteLine(distance + " ");
                            }
                        tw.Close();
                        ///////////////////////////////////////////////////////////////
                        if (distance < 850)
                        
                        {
                            ////we are very close white
                            colorFrame[index + BlueIndex] = 255;
                            colorFrame[index + GreenIndex] = 255;
                            colorFrame[index + RedIndex] = 255;  
                        }
                        else if (distance >= 850 && distance < 950)
                        {
                            //we are a bit further away Green
                            colorFrame[index + BlueIndex] = 0;
                            colorFrame[index + GreenIndex] = 255;
                            colorFrame[index + RedIndex] = 0;
                           
                        }
                        else if (distance >= 950 && distance < 1050)
                        {
                            //we are a bit further away Blue
                            colorFrame[index + BlueIndex] = 255;
                            colorFrame[index + GreenIndex] = 0;
                            colorFrame[index + RedIndex] = 0;                     
                        }
                        else if (distance >= 1050 && distance < 1150)
                        {
                            //we are a bit further away Red
                            colorFrame[index + BlueIndex] = 0;
                            colorFrame[index + GreenIndex] = 0;
                            colorFrame[index + RedIndex] = 255;
                        }
                        else if (distance >= 1150 && distance < 1250)
                        {
                            //we are a bit further away yellow
                            colorFrame[index + BlueIndex] = 0;
                            colorFrame[index + GreenIndex] = 255;
                            colorFrame[index + RedIndex] = 255;
                        }
                        else if (distance >= 1250)
                        {
                            //we are the farthest black
                            colorFrame[index + BlueIndex] = 0;
                            colorFrame[index + GreenIndex] = 0;
                            colorFrame[index + RedIndex] = 0;
                        }
                      
                        depthIndex += 2;
                    }
                }
                return colorFrame;
            }
            private int GetDistanceWithPlayerIndex(byte firstFrame, byte secondFrame)
            {
                //offset by 3 in first byte to get value after player index 
                int distance = (int)(firstFrame >> 3 | secondFrame << 5);
                return distance;
                
            }
                     
            void nui_VideoFrameReady(object sender, ImageFrameReadyEventArgs e)
            {
                PlanarImage image = e.ImageFrame.Image;
                image2.Source = e.ImageFrame.ToBitmapSource();
                e.ImageFrame.ToBitmapSource().Save("C:/Users/cuicar/Documents/Visual Studio 2010/Projects/tested programmes/color depth image_distance wpf/WpfApplication2/RGB image.jpg", ImageFormat.Jpeg);
            
            }
            private void Window_Closed(object sender, EventArgs e)
            {
                nui.Uninitialize();
            }
                    private void slider4_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
            {
            }
            
           
           
            
        }
    }

    Rami
    Wednesday, September 7, 2011 8:54 PM
  • Rami,
    it is certainly possible to do what you want, including printing out pixel coordinate, but your code is not correct for what you want it to do. This is just a plain programming bug that almost all programmers run into at some point or another and not related to Kinect, so I'm just going to give you a couple of hints and let you figure out the rest.
    ...
    
    for (var y = 0; y < height; y++)
    {
        var heightOffset = y * width;
     
        for (var x = 0; x < width; x++)
        {
            var index = ((width - x - 1) + heightOffset) * 4;
     
            var distance = GetDistanceWithPlayerIndex(depthData[depthIndex], depthData[depthIndex + 1]);

    // <<<HINT>>> Here you already have all the information you need for one pixel: (x,y) distance
    // <<<HINT>>> Do you really need one new file for each pixel? TextWriter tw = new StreamWriter("getdistance" + ".txt"); for (int i = 0; i < depthData.Length; i+=2) { tw.WriteLine(distance + " "); } tw.Close(); ... } }
    Eddy

    I'm here to help
    Wednesday, September 7, 2011 10:09 PM
  • Thanks Eddy, I do not need one new file for each pixel, I need to send the depthimage to txt file.

    this code not send all the depth data to txt file

    TextWriter tw = new StreamWriter("getdistance" + ".txt");
     
            for (int i = 0; i < depthData.Length; i+=2)
            {
                tw.WriteLine(distance + " ");
     
            }
     
            tw.Close();
    


    Rami
    Wednesday, September 7, 2011 10:43 PM
  • I know exactly what you need to do to fix your problem, but I think it's better for your learning to try to solve this yourself. That's why I'm asking these questions as hints. Another hint: that innermost loop over depthData.Length is completely unnecessary. Refer to my initial hint about "// <<<HINT>>> Here you already have all the information you need for one pixel: (x,y) distance".

    When I asked "// <<<HINT>>> Do you really need one new file for each pixel?" I was hinting at the fact that currently you are creating one file for each pixel in image. Since all files being created have the same name, the file created for the last pixel value overwrites all previous files written.

    Eddy


    I'm here to help
    Thursday, September 8, 2011 6:06 PM
  • Sorry Eddy to ask you same question again. I tried by my self to remove the loop and save distance but I still can't solve it.
    Rami
    Wednesday, September 14, 2011 4:48 PM
  • OK. Did you try creating the file one single time per frame, outside of all the loops? E.g.:

    TextWriter tw = new StreamWriter("getdistance" + ".txt");
    
    for (var y = 0; y < height; y++)
    {
        var heightOffset = y * width;
     
        for (var x = 0; x < width; x++)
        {
            var index = ((width - x - 1) + heightOffset) * 4;
     
            var distance = GetDistanceWithPlayerIndex(depthData[depthIndex], depthData[depthIndex + 1]);        
            tw.WriteLine("x: " + x + ", y: " + y + ", distance: " + distance + " ");
     
            ... 
    
        }
     
    }
    
    tw.Close();
    
    

    That should accomplish your goal as I understand it.

    Eddy


    I'm here to help
    Thursday, September 15, 2011 8:50 PM
  • Rami82

     

    Any luck with exporting to txt file ?

    Just got my self on Kinect, trying to use it with LabView with limited luck.

    Can you please share the code ?

     

    Thanks

    Saturday, October 1, 2011 5:11 PM
  • Yes, sure , I can now save the depth value to text file , the text file will contain x and y (pixel corrdinate) and the third coloum depth value. This is the full code (I tested it).

     

    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.Research.Kinect.Audio;

    using Microsoft.Research.Kinect.Nui;

    using Coding4Fun.Kinect.Wpf;

    using System.IO.MemoryMappedFiles;

     

     

    using System.Diagnostics;

    using System.IO;

     

     

    namespace WpfApplication2

    {

        /// <summary>

        /// Interaction logic for MainWindow.xaml

        /// </summary>

        public partial class MainWindow : Window

        {

            public MainWindow()

     

            {

                InitializeComponent();

            }

     

            //

     

     

            //

     

            /// <summary>

            //

            /// </summary>

     

            Runtime nui = new Runtime();

     

            private void button1_cliked(object sender, RoutedEventArgs e)

            {

                //Set angle to slider1 value

                nui.NuiCamera.ElevationAngle = (int)slider4.Value;

     

            }

     

     

     

            private void Window_Loaded(object sender, RoutedEventArgs e)

            {

     

     

             nui.Initialize(RuntimeOptions.UseColor | RuntimeOptions.UseDepthAndPlayerIndex );

     

                nui.VideoFrameReady += new EventHandler<ImageFrameReadyEventArgs>(nui_VideoFrameReady);

                nui.DepthFrameReady += new EventHandler<ImageFrameReadyEventArgs>(nui_DepthFrameReady);

     

                nui.VideoStream.Open(ImageStreamType.Video, 2,

                    ImageResolution.Resolution640x480, ImageType.Color);

                nui.DepthStream.Open(ImageStreamType.Depth, 2,

                  ImageResolution.Resolution320x240, ImageType.DepthAndPlayerIndex);

     

     

            }

     

            void nui_DepthFrameReady(object sender, ImageFrameReadyEventArgs e)

            {

     

     

                byte[] ColoredBytes = GenerateColoredBytes(e.ImageFrame);

     

                //create an image based on returned colors

     

                PlanarImage image = e.ImageFrame.Image;

     

                image1.Source = BitmapSource.Create(image.Width, image.Height, 96, 96,

                    PixelFormats.Bgr32, null, ColoredBytes,

                    image.Width * PixelFormats.Bgr32.BitsPerPixel / 8);

     

            }

     

            private byte[] GenerateColoredBytes(ImageFrame imageFrame)

            {

     

                int height = imageFrame.Image.Height;

                int width = imageFrame.Image.Width;

     

                //Depth data for each pixel

                Byte[] depthData = imageFrame.Image.Bits;

     

     

                Byte[] colorFrame = new byte[imageFrame.Image.Height * imageFrame.Image.Width * 4];

     

     

                //hardcoded locations to Blue, Green, Red (BGR) index positions       

                const int BlueIndex = 0;

                const int GreenIndex = 1;

                const int RedIndex = 2;

                var depthIndex = 0;

                TextWriter tw = new StreamWriter("sameobjects_test" + ".txt");

     

                for (var y = 0; y < height; y++)

                {

     

                    var heightOffset = y * width;

     

                    for (var x = 0; x < width; x++)

                    {

     

                        var index = ((width - x - 1) + heightOffset) * 4;

     

     

     

                        // save distance to text file

                        var distance = GetDistanceWithPlayerIndex(depthData[depthIndex], depthData[depthIndex + 1]);

                        tw.WriteLine("x: " + x + ", y: " + y + ", distance: " + distance + " ");                    

     

                        if (distance < 850)

                        {

                            ////we are very close white

                            colorFrame[index + BlueIndex] = 255;

                            colorFrame[index + GreenIndex] = 255;

                            colorFrame[index + RedIndex] = 255;

     

     

     

                        }

     

     

                        else if (distance >= 850 && distance < 950)

                        {

                            //we are a bit further away Green

                            colorFrame[index + BlueIndex] = 0;

                            colorFrame[index + GreenIndex] = 255;

                            colorFrame[index + RedIndex] = 0;

     

                        }

                        else if (distance >= 950 && distance < 1050)

                        {

                            //we are a bit further away Blue

                            colorFrame[index + BlueIndex] = 255;

                            colorFrame[index + GreenIndex] = 0;

                            colorFrame[index + RedIndex] = 0;

     

     

                        }

                        else if (distance >= 1050 && distance < 1150)

                        {

                            //we are a bit further away Red

                            colorFrame[index + BlueIndex] = 0;

                            colorFrame[index + GreenIndex] = 0;

                            colorFrame[index + RedIndex] = 255;

     

                        }

                        else if (distance >= 1150 && distance < 1250)

                        {

                            //we are a bit further away yellow

                            colorFrame[index + BlueIndex] = 0;

                            colorFrame[index + GreenIndex] = 255;

                            colorFrame[index + RedIndex] = 255;

                        }

     

                        else if (distance >= 1250)

                        {

                            //we are the farthest black

                            colorFrame[index + BlueIndex] = 0;

                            colorFrame[index + GreenIndex] = 0;

                            colorFrame[index + RedIndex] = 0;

                        }

     

     

     

                        //jump two bytes at a time

                        depthIndex += 2;

     

     

                    }

     

                }

                //tw.Close();

                return colorFrame;

     

     

            }

     

     

     

     

            private int GetDistanceWithPlayerIndex(byte firstFrame, byte secondFrame)

            {

                //    //offset by 3 in first byte to get value after player index 

                int distance = (int)(firstFrame >> 3 | secondFrame << 5);

                return distance;

     

            }

     

            void nui_VideoFrameReady(object sender, ImageFrameReadyEventArgs e)

            {

     

                PlanarImage image = e.ImageFrame.Image;

     

     

                image2.Source = e.ImageFrame.ToBitmapSource();

     

     

     

     

     

                    }

     

            private void Window_Closed(object sender, EventArgs e)

            {

                nui.Uninitialize();

            }

     

     

            private void slider4_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)

            {

     

            }

     

     

     

        }

    }


    Rami
    Sunday, October 9, 2011 4:51 AM
  • Hi!

    Has anyone an updated version of this which works with version 1.0 of the sdk?

    Wednesday, March 14, 2012 11:31 PM
  • Please create a new forum post with your question. There are other threads that discuss ways of doing this and the relevant code for saving a bitmap is the same.
    Friday, March 16, 2012 4:53 AM
  • Perfect! Well, by clicking on the depth video using my mouse, how can I get the real-world depth value(distance) of the pixel I clicked? 
    Tuesday, March 20, 2012 1:04 PM
  • Using your code provided above, I encountered an error when debugging. While the txt file can be created and the data(x,y,distance) seems alright. Need your help!

    Tuesday, March 20, 2012 1:49 PM
  • Dear All,

    Have you ever met the following problem while trying to implement the above code Rami is sharing with us? I am not sure if the SDK has been updated after the code is posted. Here are the error messages - which look similar:

    Error 1  The type or namespace name 'Research' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)
    Error 3  The type or namespace name 'Coding4Fun' could not be found (are you missing a using directive or an assembly reference?)
    Error 4  The type or namespace name 'Runtime' could not be found (are you missing a using directive or an assembly reference?)
    Error 5  The type or namespace name 'ImageFrameReadyEventArgs' could not be found (are you missing a ...
    Error 6  The type or namespace name 'ImageFrame' could not be found (are you missing a using directive or an assembly reference?)
    Error 7  The type or namespace name 'ImageFrameReadyEventArgs' could not be found (are you ...

    Thank you for your help,

    Zeleke


    Zeleke

    Sunday, April 29, 2012 7:17 AM
  • Hi Zeleke,

          It seems you are having new version of Kinect for Windows SDK , But this code is for Beta2.

    Gowri!

    Monday, April 30, 2012 10:04 AM
  • Thanks Gowrri,

    Yes my kinect is v1.0 version. Is there anyone with an updated version of the file saving depth data (distance) using kinect and C# wpf application, for v1.0?

    Thank you,

    Mulugeta


    Zeleke

    Monday, April 30, 2012 4:49 PM
  • I will gladly update as long as its in beta 2 code minimum. post the solution and code in a zip file somewhere and I will gladly upadte for people here op

    Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth. - "Sherlock holmes" "speak softly and carry a big stick" - theodore roosevelt. Fear leads to anger, anger leads to hate, hate leads to suffering - Yoda. Blog - http://jefferycarlsonblog.blogspot.com/

    Tuesday, May 1, 2012 5:51 PM