/// <summary> /// Event handler for Kinect sensor's DepthFrameReady event /// </summary> /// <param name="sender">object sending the event</param> /// <param name="e">event arguments</param> private void SensorDepthFrameReady(object sender, DepthImageFrameReadyEventArgs e) { using (DepthImageFrame depthFrame = e.OpenDepthImageFrame()) { if (depthFrame != null) { // Copy the pixel data from the image to a temporary array depthFrame.CopyDepthImagePixelDataTo(this.depthPixels); // calculate the mapping CoordinateMapper mapper = new CoordinateMapper(this.sensor); mapper.MapDepthFrameToColorFrame( DepthImageFormat.Resolution640x480Fps30, this.depthPixels, ColorImageFormat.RgbResolution640x480Fps30, colorCoordinates); // Get the min and max reliable depth for the current frame int minDepth = depthFrame.MinDepth; int maxDepth = depthFrame.MaxDepth; // Convert the depth to RGB int colorPixelIndex = 0; for (int i = 0; i < this.depthPixels.Length; ++i) { // Get the depth for this pixel short depth = depthPixels[i].Depth; this.depthPixelsCopy[i] = (depth >= minDepth && depth <= maxDepth ? depth : (short)0); // To convert to a byte, we're discarding the most-significant // rather than least-significant bits. // We're preserving detail, although the intensity will "wrap." // Values outside the reliable depth range are mapped to 0 (black). // Note: Using conditionals in this loop could degrade performance. // Consider using a lookup table instead when writing production code. // See the KinectDepthViewer class used by the KinectExplorer sample // for a lookup table example. long intensity = (depth >= minDepth && depth <= maxDepth ? depth : 0); double value = (double)(intensity - minDepth) / (maxDepth - minDepth); // Write out blue byte this.depthPseudoColorPixels[colorPixelIndex++] = intensity == 0 ? (byte)100 : (byte)(255 * Blue(value)); // Write out green byte this.depthPseudoColorPixels[colorPixelIndex++] = intensity == 0 ? (byte)100 : (byte)(255 * Green(value)); // Write out red byte this.depthPseudoColorPixels[colorPixelIndex++] = intensity == 0 ? (byte)100 : (byte)(255 * Red(value)); // We're outputting BGR, the last byte in the 32 bits is unused so skip it // If we were outputting BGRA, we would write alpha here. ++colorPixelIndex; } // Write the pixel data into our bitmap this.depthPseudoColorBitmap.WritePixels( new Int32Rect(0, 0, this.depthPseudoColorBitmap.PixelWidth, this.depthPseudoColorBitmap.PixelHeight), this.depthPseudoColorPixels, this.depthPseudoColorBitmap.PixelWidth * sizeof(int), 0); for (int i = 0; i < this.sensor.DepthStream.FrameHeight; i++) { for (int j = 0; j < this.sensor.DepthStream.FrameWidth; j++) { int pos1 = i * this.sensor.DepthStream.FrameWidth + j; int pos2 = i * this.sensor.DepthStream.FrameWidth + (this.sensor.DepthStream.FrameWidth - 1 - j); short depth = depthPixels[pos2].Depth; depth = (depth >= minDepth && depth <= maxDepth ? depth : (short) 0); this.depthPixelsCopy[pos1] = depth; } } } } }