locked
The name 'image1' does not exist in the current context_ RRS feed

  • Question

  • hi,i am doing final year project as detection of object using kinect.

    In this project i am using kinect + C# and Working With Depth Data.

    Everything working fine but i got this problem or ERROR.

    "The name 'image1' does not exist in the current context".

    please help me.

    here is my code:

    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;

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

                //using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
                //{
                //    if (colorFrame == null)
                //    {
                //        return;
                //    }

                //    byte[] pixels = new byte[colorFrame.PixelDataLength];
                //    colorFrame.CopyPixelDataTo(pixels);

                //    int stride = colorFrame.Width * 4;
                //    image1.Source =
                //        BitmapSource.Create(colorFrame.Width, colorFrame.Height,
                //        96, 96, PixelFormats.Bgr32, null, pixels, stride);
                //}

                using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
                {
                    if (depthFrame == null)
                    {
                        return;
                    }

                    byte[] pixels = GenerateColoredBytes(depthFrame);

                    //number of bytes per row width * 4 (B,G,R,Empty)
                    int stride = depthFrame.Width * 4;

                    //create image
                    
                       image1.Source = BitmapSource.Create(depthFrame.Width, depthFrame.Height,
                        96, 96, PixelFormats.Bgr32, null, pixels, stride);                                                              " ERROR here "

                }
            }


            private byte[] GenerateColoredBytes(DepthImageFrame depthFrame)
            {

                //get the raw data from kinect with the depth for every pixel
                short[] rawDepthData = new short[depthFrame.PixelDataLength];
                depthFrame.CopyPixelDataTo(rawDepthData);

                //use depthFrame to create the image to display on-screen
                //depthFrame contains color information for all pixels in image
                //Height x Width x 4 (Red, Green, Blue, empty byte)
                Byte[] pixels = new byte[depthFrame.Height * depthFrame.Width * 4];

                //Bgr32  - Blue, Green, Red, empty byte
                //Bgra32 - Blue, Green, Red, transparency
                //You must set transparency for Bgra as .NET defaults a byte to 0 = fully transparent

                //hardcoded locations to Blue, Green, Red (BGR) index positions       
                const int BlueIndex = 0;
                const int GreenIndex = 1;
                const int RedIndex = 2;


                //loop through all distances
                //pick a RGB color based on distance
                for (int depthIndex = 0, colorIndex = 0;
                    depthIndex < rawDepthData.Length && colorIndex < pixels.Length;
                    depthIndex++, colorIndex += 4)
                {
                    //get the player (requires skeleton tracking enabled for values)
                    int player = rawDepthData[depthIndex] & DepthImageFrame.PlayerIndexBitmask;

                    //gets the depth value
                    int depth = rawDepthData[depthIndex] >> DepthImageFrame.PlayerIndexBitmaskWidth;

                    //.9M or 2.95'
                    if (depth <= 900)
                    {
                        //we are very close
                        pixels[colorIndex + BlueIndex] = 255;
                        pixels[colorIndex + GreenIndex] = 0;
                        pixels[colorIndex + RedIndex] = 0;

                    }
                    // .9M - 2M or 2.95' - 6.56'
                    else if (depth > 900 && depth < 2000)
                    {
                        //we are a bit further away
                        pixels[colorIndex + BlueIndex] = 0;
                        pixels[colorIndex + GreenIndex] = 255;
                        pixels[colorIndex + RedIndex] = 0;
                    }
                    // 2M+ or 6.56'+
                    else if (depth > 2000)
                    {
                        //we are the farthest
                        pixels[colorIndex + BlueIndex] = 0;
                        pixels[colorIndex + GreenIndex] = 0;
                        pixels[colorIndex + RedIndex] = 255;
                    }


                    ////equal coloring for monochromatic histogram
                    byte intensity = CalculateIntensityFromDepth(depth);
                    pixels[colorIndex + BlueIndex] = intensity;
                    pixels[colorIndex + GreenIndex] = intensity;
                    pixels[colorIndex + RedIndex] = intensity;


                    //Color all players "gold"
                    if (player > 0)
                    {
                        pixels[colorIndex + BlueIndex] = Colors.Gold.B;
                        pixels[colorIndex + GreenIndex] = Colors.Gold.G;
                        pixels[colorIndex + RedIndex] = Colors.Gold.R;
                    }

                }


                return pixels;
            }


            public static byte CalculateIntensityFromDepth(int distance)
            {
                //formula for calculating monochrome intensity for histogram
                return (byte)(255 - (255 * Math.Max(distance - MinDepthDistance, 0)
                    / (MaxDepthDistanceOffset)));
            }

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

    Wednesday, August 5, 2015 5:36 PM