/// <summary> /// Converts an array of DepthImagePixels into a byte array in Bgr32 format. /// Pixel intensity represents depth; colors indicate players. /// </summary> /// <param name="depthFrame">The depth buffer to convert.</param> /// <param name="minDepth">The minimum reliable depth for this frame.</param> /// <param name="maxDepth">The maximum reliable depth for this frame.</param> /// <param name="depthTreatment">The depth treatment to apply.</param> /// <param name="colorFrame">The buffer to fill with color pixels.</param> public void ConvertDepthFrame( DepthImagePixel[] depthFrame, int minDepth, int maxDepth, KinectDepthTreatment depthTreatment, byte[] colorFrame) { // Test that the buffer lengths are appropriately correlated, which allows us to use only one // value as the loop condition. if ((depthFrame.Length * Bgr32BytesPerPixel) != colorFrame.Length) { throw new InvalidOperationException(); } ColorMapping[] mappingTable = GetColorMappingTable(minDepth, maxDepth, depthTreatment); for (int depthIndex = 0, colorIndex = 0; colorIndex < colorFrame.Length; depthIndex++, colorIndex += Bgr32BytesPerPixel) { short depth = depthFrame[depthIndex].Depth; ColorMapping color = mappingTable[(ushort)depth]; int player = depthFrame[depthIndex].PlayerIndex; // Write color pixel to buffer colorFrame[colorIndex + RedIndex] = (byte)(color.R >> IntensityShiftByPlayerR[player]); colorFrame[colorIndex + GreenIndex] = (byte)(color.G >> IntensityShiftByPlayerG[player]); colorFrame[colorIndex + BlueIndex] = (byte)(color.B >> IntensityShiftByPlayerB[player]); } }
public void ConvertDepthFrame( DepthImagePixel[] depthFrame, int minDepth, int maxDepth, KinectDepthTreatment depthTreatment, byte[] colorFrame) { if ((depthFrame.Length * Bgr32BytesPerPixel) != colorFrame.Length) { throw new InvalidOperationException(); } Color[] mappingTable = GetColorMappingTable(minDepth, maxDepth, depthTreatment); for (int depthIndex = 0, colorIndex = 0; colorIndex < colorFrame.Length; depthIndex++, colorIndex += Bgr32BytesPerPixel) { short depth = depthFrame[depthIndex].Depth; Color color = mappingTable[(ushort)depth]; int player = depthFrame[depthIndex].PlayerIndex; colorFrame[colorIndex + RedIndex] = (byte)(color.R >> IntensityShiftByPlayerR[player]); colorFrame[colorIndex + GreenIndex] = (byte)(color.G >> IntensityShiftByPlayerG[player]); colorFrame[colorIndex + BlueIndex] = (byte)(color.B >> IntensityShiftByPlayerB[player]); } }
/// <summary> /// Returns the depth-to-color mapping table. /// </summary> /// <param name="minDepth">The minimum reliable depth value.</param> /// <param name="maxDepth">The maximum reliable depth value.</param> /// <param name="depthTreatment">The depth treatment to apply.</param> /// <returns>The color mapping table.</returns> private ColorMapping[] GetColorMappingTable(int minDepth, int maxDepth, KinectDepthTreatment depthTreatment) { if (initializeColorMappingTable || minDepth != currentMinDepth || maxDepth != currentMaxDepth || depthTreatment != currentDepthTreatment) { initializeColorMappingTable = false; currentMinDepth = minDepth; currentMaxDepth = maxDepth; currentDepthTreatment = depthTreatment; // Initialize table to all zero (black) Array.Clear(colorMappingTable, 0, colorMappingTable.Length); // Fill in the color for an unknown depth value colorMappingTable[UnknownDepth] = UnknownDepthColor; switch (depthTreatment) { case KinectDepthTreatment.ClampUnreliableDepths: // Fill in the "near" portion of the table with solid color for (int i = 1; i < minDepth; i++) { colorMappingTable[i] = TooNearColor; } // Fill in the "far" portion of the table with solid color for (int i = maxDepth + 1; i < MaxMaxDepth; i++) { colorMappingTable[i] = TooFarColor; } break; case KinectDepthTreatment.TintUnreliableDepths: // Fill in the "near" portion of the table with a tinted gradient for (int i = 1; i < minDepth; i++) { byte intensity = intensityTable[i]; // Apply cobalt tint colorMappingTable[i] = new ColorMapping { R = (byte)(intensity >> 3), G = (byte)(intensity >> 1), B = intensity }; } // Fill in the "far" portion of the table with a tinted gradient for (int i = maxDepth + 1; i < MaxMaxDepth; i++) { byte intensity = intensityTable[i]; // Apply cranberry tint colorMappingTable[i] = new ColorMapping { R = intensity, G = (byte)(intensity >> 3), B = (byte)(intensity >> 1) }; } break; case KinectDepthTreatment.DisplayAllDepths: // Fill in the "nearer than near" values with solid white for (int i = 1; i < MinMinDepth; i++) { colorMappingTable[i] = NearestDepthColor; } // Adjust minDepth and maxDepth to allow unreliable values to be rendered minDepth = MinMinDepth; maxDepth = MaxMaxDepth; // anything more distant will be black break; } // Fill in values that will be rendered normally with a gray gradient for (int i = minDepth; i < maxDepth; i++) { byte intensity = intensityTable[i]; colorMappingTable[i] = new ColorMapping { R = intensity, G = intensity, B = intensity }; } } return colorMappingTable; }
/// <summary> /// Returns the depth-to-color mapping table. /// </summary> /// <param name="minDepth">The minimum reliable depth value.</param> /// <param name="maxDepth">The maximum reliable depth value.</param> /// <param name="depthTreatment">The depth treatment to apply.</param> /// <returns>The color mapping table.</returns> private ColorMapping[] GetColorMappingTable(int minDepth, int maxDepth, KinectDepthTreatment depthTreatment) { if (this.initializeColorMappingTable || minDepth != this.currentMinDepth || maxDepth != this.currentMaxDepth || depthTreatment != this.currentDepthTreatment) { this.initializeColorMappingTable = false; this.currentMinDepth = minDepth; this.currentMaxDepth = maxDepth; this.currentDepthTreatment = depthTreatment; // Initialize table to all zero (black) Array.Clear(this.colorMappingTable, 0, this.colorMappingTable.Length); // Fill in the color for an unknown depth value this.colorMappingTable[UnknownDepth] = UnknownDepthColor; switch (depthTreatment) { case KinectDepthTreatment.ClampUnreliableDepths: // Fill in the "near" portion of the table with solid color for (int i = 1; i < minDepth; i++) { this.colorMappingTable[i] = TooNearColor; } // Fill in the "far" portion of the table with solid color for (int i = maxDepth + 1; i < MaxMaxDepth; i++) { this.colorMappingTable[i] = TooFarColor; } break; case KinectDepthTreatment.TintUnreliableDepths: // Fill in the "near" portion of the table with a tinted gradient for (int i = 1; i < minDepth; i++) { byte intensity = intensityTable[i]; // Apply cobalt tint this.colorMappingTable[i] = new ColorMapping { R = (byte)(intensity >> 3), G = (byte)(intensity >> 1), B = intensity }; } // Fill in the "far" portion of the table with a tinted gradient for (int i = maxDepth + 1; i < MaxMaxDepth; i++) { byte intensity = intensityTable[i]; // Apply cranberry tint this.colorMappingTable[i] = new ColorMapping { R = intensity, G = (byte)(intensity >> 3), B = (byte)(intensity >> 1) }; } break; case KinectDepthTreatment.DisplayAllDepths: // Fill in the "nearer than near" values with solid white for (int i = 1; i < MinMinDepth; i++) { this.colorMappingTable[i] = NearestDepthColor; } // Adjust minDepth and maxDepth to allow unreliable values to be rendered minDepth = MinMinDepth; maxDepth = MaxMaxDepth; // anything more distant will be black break; } // Fill in values that will be rendered normally with a gray gradient for (int i = minDepth; i < maxDepth; i++) { byte intensity = intensityTable[i]; this.colorMappingTable[i] = new ColorMapping { R = intensity, G = intensity, B = intensity }; } } return(this.colorMappingTable); }
private Color[] GetColorMappingTable(int minDepth, int maxDepth, KinectDepthTreatment depthTreatment) { if (this.initializeColorMappingTable || minDepth != this.currentMinDepth || maxDepth != this.currentMaxDepth || depthTreatment != this.currentDepthTreatment) { this.initializeColorMappingTable = false; this.currentMinDepth = minDepth; this.currentMaxDepth = maxDepth; this.currentDepthTreatment = depthTreatment; Array.Clear(this.colorMappingTable, 0, this.colorMappingTable.Length); this.colorMappingTable[UnknownDepth] = UnknownDepthColor; switch (depthTreatment) { case KinectDepthTreatment.ClampUnreliableDepths: for (int i = 1; i < minDepth; i++) { this.colorMappingTable[i] = TooNearColor; } for (int i = maxDepth + 1; i < MaxMaxDepth; i++) { this.colorMappingTable[i] = TooFarColor; } break; case KinectDepthTreatment.TintUnreliableDepths: for (int i = 1; i < minDepth; i++) { byte intensity = intensityTable[i]; this.colorMappingTable[i] = Color.FromRgb((byte)(intensity >> 3), (byte)(intensity >> 1), intensity); } for (int i = maxDepth + 1; i < MaxMaxDepth; i++) { byte intensity = intensityTable[i]; this.colorMappingTable[i] = Color.FromRgb(intensity, (byte)(intensity >> 3), (byte)(intensity >> 1)); } break; case KinectDepthTreatment.DisplayAllDepths: for (int i = 1; i < MinMinDepth; i++) { this.colorMappingTable[i] = NearestDepthColor; } minDepth = MinMinDepth; maxDepth = MaxMaxDepth; break; } for (int i = minDepth; i < maxDepth; i++) { byte intensity = intensityTable[i]; this.colorMappingTable[i] = Color.FromRgb(intensity, intensity, intensity); } } return(this.colorMappingTable); }