title: UI Freezes while processing frames from Kinect using EmguCV in WPF C# Application
body: <p>I am working on a project which involves extracting features from color and depth frames from Kinect Camera. The problem I am facing is that whenever I try displaying 2 Images, the UI hangs. When I tried debugging, the depthFrame and colorFrame were coming as null. If enable only the color steam then both colorImage and featureImage1 are displayed properly and if I enable only the depth stream, it works as it should. But when I enable them both, the UI hangs. I have no idea what is causing the problem. I have the following the following code for my Kinect Application. What is the cause of this problem and how can I fix it?
  Config: Windows 8 Pro 64bit, 2Ghz Core2Duo, VisualStudio 2012 Ultimate, EmguCV 2.4.0.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Drawing;
  using System.Linq;
  using System.Runtime.InteropServices;
  using System.Text;
  using System.Threading.Tasks;
  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 Emgu.CV;
  using Emgu.CV.WPF;
  using Emgu.CV.Structure;
  using Emgu.Util;
  namespace features
  {
  public partial class MainWindow : Window
  {  
      public MainWindow()
      {
          InitializeComponent();
      }
      private Image&lt;Bgra, Byte&gt; cvColorImage;
      private Image&lt;Gray, Int16&gt; cvDepthImage;
      private int colorWidth = 640;
      private int colorHeight = 480;
      private int depthWidth = 640;
      private int depthHeight = 480;
      private static readonly int Bgr32BytesPerPixel = (PixelFormats.Bgr32.BitsPerPixel + 7) / 8;
      private byte[] colorPixels;
      private byte[] depthPixels;
      private short[] rawDepthData;
      private bool first = true;
      private bool firstDepth = true;
      Image&lt;Bgra, byte&gt; image2;
      private void Window_Loaded(object sender, RoutedEventArgs e)
      {
          kinectSensorChooser.KinectSensorChanged += new DependencyPropertyChangedEventHandler(kinectSensorChooser_KinectSensorChanged);
      }
      void kinectSensorChooser_KinectSensorChanged(object sender, DependencyPropertyChangedEventArgs e)
      {
          KinectSensor oldSensor = (KinectSensor)e.OldValue;
          KinectStop(oldSensor);
          KinectSensor _sensor = (KinectSensor)e.NewValue;
          _sensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
          _sensor.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);
          _sensor.DepthFrameReady += new EventHandler&lt;DepthImageFrameReadyEventArgs&gt;(_sensor_DepthFrameReady);
          _sensor.ColorFrameReady += new EventHandler&lt;ColorImageFrameReadyEventArgs&gt;(_sensor_ColorFrameReady);
          _sensor.DepthStream.FrameHeight);
          try
          {
              _sensor.Start();
          }
          catch
          {
              kinectSensorChooser.AppConflictOccurred();
          }
      }
      void KinectStop(KinectSensor sensor)
      {
          if (sensor != null)
          {
              sensor.Stop();
          }
      }
  
      private void Window_Closed(object sender, EventArgs e)
      {
          KinectStop(kinectSensorChooser.Kinect);
      }
  
      void _sensor_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
      {
          using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
          {
              if (colorFrame == null) return;
              if (first)
              {
                  this.colorPixels = new Byte[colorFrame.PixelDataLength];
                  first = false;
              }
              colorFrame.CopyPixelDataTo(this.colorPixels); //raw data in bgrx format
              processColor();
          }
      }
      void _sensor_DepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
      {
          using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
          {
              if (depthFrame == null) return;
              if (firstDepth)
              {
                  this.rawDepthData = new short[depthFrame.PixelDataLength];
                  firstDepth = false;
              }
              depthFrame.CopyPixelDataTo(rawDepthData);
              processDepth();
          }
      }
      private void processColor(){...}
      private void processDepth(){...}
  }
  }
  </code></pre>
  
  <p>The processDepth function is as follows. I am just making an Image from the RAW depth data.</p>
  
  <pre><code>private void processDepth() {
      GCHandle pinnedArray = GCHandle.Alloc(this.rawDepthData, GCHandleType.Pinned);
      IntPtr pointer = pinnedArray.AddrOfPinnedObject();
      cvDepthImage = new Image&lt;Gray, Int16&gt;(depthWidth, depthHeight, depthWidth &lt;&lt; 1, pointer);
      pinnedArray.Free();
      depthImage.Source = BitmapSourceConvert.ToBitmapSource(cvDepthImage.Not().Bitmap);
  }
  </code></pre>
  
  <p>The processColor function is as follows. Here just for the sake of it, I am trying to display the cloned image instead of extracting features, just to check the lag. When both streams are enabled (color and depth) the following function displays the colorImage properly, but as soon as I uncomment the commented lines, the UI hangs.</p>
  
  <pre><code>private void processColor() {
      GCHandle handle = GCHandle.Alloc(this.colorPixels, GCHandleType.Pinned);
      Bitmap image = new Bitmap(colorWidth, colorHeight, colorWidth&lt;&lt;2, System.Drawing.Imaging.PixelFormat.Format32bppRgb, handle.AddrOfPinnedObject());
      handle.Free();
      cvColorImage = new Image&lt;Bgra, byte&gt;(image);
      image.Dispose();
      BitmapSource src = BitmapSourceConvert.ToBitmapSource(cvColorImage.Bitmap);
      colorImage.Source = src;
      //image2 = new Image&lt;Bgra, byte&gt;(cvColorImage.ToBitmap()); //uncomment and it hangs
      //featureImage1.Source = BitmapSourceConvert.ToBitmapSource(image2.Bitmap); //uncomment and it hangs
  }
  </code></pre>