title: Aligning depth image to RGB image
body: <p>I've been trying to align the depth stream to my RGB stream from my Kinect for quite some time. I've read several articles about it but I must be missing some key point because I cannot get it to work...</p>
  
  <p>Here is an image of what I have managed to do, I've circled some easy to spot misalignments
  <img src="https://i.stack.imgur.com/ilQf1.png" alt="Kinect Depth misalignment"></p>
  
  <p>I've been trying to get this down into as small code as possible, but its still a good chunk of code so please bear with me, the code snippet below is what the Kinect SDK calls everytime there is a depth and RGB frame ready.</p>
  
  <p>As you can see I've been experimenting with </p>
  
  <pre><code>ColorImagePoint colorpoint = _Sensor.CoordinateMapper.MapDepthPointToColorPoint(DepthImageFormat.Resolution640x480Fps30, depthpoint, ColorImageFormat.RgbResolution640x480Fps30);
  </code></pre>
  
  <p>I would prefer to use CoordinateMapper.MapDepthFrameToColorFrame (as this should be able to solve the problem) but I can't get it to work.. I am probably not doing it correctly though..</p>
  
  <p>I am using Microsofts Kinect SDK 1.6</p>
  
  <pre><code>private void EventAllFramesReady(Object Sender, AllFramesReadyEventArgs e)
  {
      System.Drawing.Color color;
  
      Bitmap image = null;
      Bitmap depth = null;
  
      using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
      {
          using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
          {
              // color 
              image = new Bitmap(colorFrame.Width, colorFrame.Height);
              byte[] colorPixels = new byte[colorFrame.PixelDataLength];
              colorFrame.CopyPixelDataTo(colorPixels);
  
              //lock bitmap, and work with BitmapData (way faster than SetPixel())
              BitmapData imageBitmapData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height),
                                                  ImageLockMode.WriteOnly,
                                                  image.PixelFormat);
              IntPtr IptrImage = imageBitmapData.Scan0;
              byte[] PixelsImage = new byte[image.Width * image.Height * 4];
  
  
              // depth
              depth = new Bitmap(depthFrame.Width, depthFrame.Height);
              DepthImagePixel[] depthData = new DepthImagePixel[depthFrame.PixelDataLength];
              depthFrame.CopyDepthImagePixelDataTo(depthData);
  
              //lock bitmap, and work with BitmapData (way faster than SetPixel())
              BitmapData depthBitmapData = depth.LockBits(new Rectangle(0, 0, depth.Width, depth.Height),
                                                  ImageLockMode.WriteOnly,
                                                  depth.PixelFormat);
              IntPtr IptrDepth = depthBitmapData.Scan0;
              byte[] PixelsDepth = new byte[depth.Width * depth.Height * 4];
  
  
              DepthImagePoint depthpoint = new DepthImagePoint();
  
              for (int x = 1; x &lt; colorFrame.Width; x++)
              {
                  for (int y = 1; y &lt; colorFrame.Height; y++)
                  {
                      int i = ((y * image.Width) + x) * 4;
  
                      short depthdistanceRAW = (depthData[x + y * depth.Width]).Depth;
  
                      // convert distance value into a color
                      color = System.Drawing.Color.Pink;
                      if (depthdistanceRAW &gt; 0 &amp;&amp; depthdistanceRAW &lt;= 4000)
                      {
                          int depthdistance = (int)((depthdistanceRAW / 4090f) * 255f);
                          color = System.Drawing.Color.FromArgb((int)(depthdistance / 2f), depthdistance, (int)(depthdistance * 0.7f));
                      }
  
                      depthpoint.X = x;
                      depthpoint.Y = y;
                      depthpoint.Depth = depthdistanceRAW;
  
                      ColorImagePoint colorpoint = _Sensor.CoordinateMapper.MapDepthPointToColorPoint(DepthImageFormat.Resolution640x480Fps30, depthpoint, ColorImageFormat.RgbResolution640x480Fps30);
  
                      //if (colorpoint.X &gt; 0 &amp;&amp; colorpoint.X &lt;= 640 &amp;&amp; colorpoint.Y &gt; 0 &amp;&amp; colorpoint.Y &lt;= 480)
                      //{
                      int adjustedposition = ((colorpoint.Y * image.Width) + colorpoint.X) * 4;
                      //if (adjustedposition &lt; depthData.Length)
                      //{
                      PixelsDepth[i] = color.B;
                      PixelsDepth[i + 1] = color.G;
                      PixelsDepth[i + 2] = color.R;
                      PixelsDepth[i + 3] = DepthTransparency;
                      //}
                      //}
  
                      PixelsImage[i] = colorPixels[i];
                      PixelsImage[i + 1] = colorPixels[i + 1];
                      PixelsImage[i + 2] = colorPixels[i + 2];
                      PixelsImage[i + 3] = 255;
                  }
              }
  
              Marshal.Copy(PixelsImage, 0, IptrImage, PixelsImage.Length);
              image.UnlockBits(imageBitmapData);
  
              Marshal.Copy(PixelsDepth, 0, IptrDepth, PixelsDepth.Length);
              depth.UnlockBits(depthBitmapData);
          }
      }
  
      _kf.UpdateImage(image); // update the RGB picture in the form
      _kf.UpdateDepth(depth); // update the Depth picture in the form
  }
  </code></pre>