none
Question about depth image automatically capture RRS feed

  • Question

  • Hello,

    I want to capture depth image automatically, can continuous take image about 1ms.

    Here is my code, but i'm using mouse down to do capture it, it's so slow.

    Can anyone can teach me how to do it and help me to correct my code.

    Thanks a lot.

    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.Shapes;
    using Microsoft.Kinect;
    using System.IO;

    namespace WpfApplication1
    {

        public partial class ColorWindow : Window
        {
            KinectSensor kinect;

            public ColorWindow(KinectSensor sensor) : this()
            {
                kinect = sensor;
            }

            public ColorWindow()
            {
                InitializeComponent();
                Loaded += ColorWindow_Loaded;
                Unloaded += ColorWindow_Unloaded;
            }
            void ColorWindow_Unloaded(object sender, RoutedEventArgs e)
            {
                if (kinect != null)
                {
                    kinect.DepthStream.Disable();
                    kinect.Stop();
                    kinect.DepthFrameReady -= myKinect_DepthFrameReady;
                }
            }
            private WriteableBitmap _DepthImageBitmap;
            private Int32Rect _DepthImageBitmapRect;
            private int _DepthImageStride;
            void ColorWindow_Loaded(object sender, RoutedEventArgs e)
            {
                if (kinect != null)
                {
                    DepthImageStream DepthStream = kinect.DepthStream;

                    _DepthImageBitmap = new WriteableBitmap(DepthStream.FrameWidth, DepthStream.FrameHeight, 96, 96, PixelFormats.Gray16, null);

                    _DepthImageBitmapRect = new Int32Rect(0, 0, DepthStream.FrameWidth, DepthStream.FrameHeight);

                    _DepthImageStride = DepthStream.FrameWidth * DepthStream.FrameBytesPerPixel;
                    ColorData.Source = _DepthImageBitmap;

                    kinect.DepthStream.Enable();
                    kinect.DepthFrameReady += myKinect_DepthFrameReady;
                    kinect.Start();
                }
            }
            void myKinect_DepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
            {
                using (DepthImageFrame frame = e.OpenDepthImageFrame())
                {
                    if (frame != null)
                    {
                        short[] pixelData = new short[frame.PixelDataLength];
                        frame.CopyPixelDataTo(pixelData);
                        _DepthImageBitmap.WritePixels(_DepthImageBitmapRect, pixelData, _DepthImageStride, 0);
                    }
                }
            }
            private void ColorData_MouseDown(object sender, MouseButtonEventArgs e)
            {       
                string filename = NewFileName();
                SaveToFile(filename);
            }
            private int i = 000;
            public string NewFileName()
            {
                i++;
                string mypicsfolder = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
                string fileName = mypicsfolder + "\\Frame_" + i + ".png";


                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                }
                return fileName;

            }
            public void SaveToFile(string fileName)
            {
                using (FileStream fs = new FileStream(fileName, FileMode.CreateNew))
                {
                    BitmapSource image = (BitmapSource)ColorData.Source;
                    PngBitmapEncoder encoder = new PngBitmapEncoder();
                    encoder.Frames.Add(BitmapFrame.Create(image));
                    encoder.Save(fs);
                }
            }

        }
    }



    • Edited by Sharon_Y Monday, October 29, 2012 6:17 AM
    Monday, October 29, 2012 6:16 AM