none
errors while trying to save depth data to text file RRS feed

  • Question

  • Hello All,

    I used the code posted in this forum as answer to the question "how to save depth data (distance) using kinect and C# wpf application." Thank you very much for sharing your codes. However, when I was trying to build the solution, I found seven similar  error messages: "The type or namespace name ................. could not be found (are you missing a using directive or an assembly reference?)." Would you please help me with solving this problem? Thank you very much for your help.

    Here are the six detailed similar error messages I found (Error 2 is exactly the same as error 1, so not included):

    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 using directive or an assembly reference?)
    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 missing a using directive or an assembly reference?)

    The code I found from this forum and tried to use is given below:

    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;

    namespace WpfApplication_SavingDepthData
    {
        /// <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)
            {



            }

        }
    }


    Zeleke

    Saturday, April 28, 2012 12:24 PM

Answers