public static ImageSource ToBitmap(this InfraredFrame frame) { int width = frame.FrameDescription.Width; int height = frame.FrameDescription.Height; PixelFormat format = PixelFormats.Bgr32; ushort[] frameData = new ushort[width * height]; byte[] pixels = new byte[width * height * (format.BitsPerPixel + 7) / 8]; frame.CopyFrameDataToArray(frameData); int colorIndex = 0; for (int infraredIndex = 0; infraredIndex < frameData.Length; infraredIndex++) { ushort ir = frameData[infraredIndex]; byte intensity = (byte)(ir >> 7); pixels[colorIndex++] = (byte)(intensity / 1); // Blue pixels[colorIndex++] = (byte)(intensity / 1); // Green pixels[colorIndex++] = (byte)(intensity / 0.4); // Red colorIndex++; } int stride = width * format.BitsPerPixel / 8; return BitmapSource.Create(width, height, 96, 96, format, null, pixels, stride); }
/// <summary> /// Converts an infrared frame to a System.Media.ImageSource. /// </summary> /// <param name="frame">The specified infrared frame.</param> /// <returns>The specified frame in a System.media.ImageSource format.</returns> public static ImageSource ToBitmap(this InfraredFrame frame) { int width = frame.FrameDescription.Width; int height = frame.FrameDescription.Height; ushort[] frameData = new ushort[width * height]; byte[] pixels = new byte[width * height * (PixelFormats.Bgr32.BitsPerPixel + 7) / 8]; frame.CopyFrameDataToArray(frameData); // Convert the infrared to RGB. int colorIndex = 0; for (int infraredIndex = 0; infraredIndex < frameData.Length; infraredIndex++) { // Get the infrared value for this pixel ushort ir = frameData[infraredIndex]; // 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." byte intensity = (byte)(ir >> 6); pixels[colorIndex++] = intensity; // Blue pixels[colorIndex++] = intensity; // Green pixels[colorIndex++] = intensity; // Red // 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. colorIndex++; } return pixels.ToBitmap(width, height); }
public static ImageSource ToBitmap(this DepthFrame frame) { int width = frame.FrameDescription.Width; int height = frame.FrameDescription.Height; PixelFormat format = PixelFormats.Bgr32; ushort minDepth = frame.DepthMinReliableDistance; ushort maxDepth = frame.DepthMaxReliableDistance; ushort[] pixelData = new ushort[width * height]; byte[] pixels = new byte[width * height * (format.BitsPerPixel + 7) / 8]; frame.CopyFrameDataToArray(pixelData); int colorIndex = 0; for (int depthIndex = 0; depthIndex < pixelData.Length; ++depthIndex) { ushort depth = pixelData[depthIndex]; byte intensity = (byte)(depth >= minDepth && depth <= maxDepth ? depth : 0); pixels[colorIndex++] = intensity; // Blue pixels[colorIndex++] = intensity; // Green pixels[colorIndex++] = intensity; // Red ++colorIndex; } int stride = width * format.BitsPerPixel / 8; return BitmapSource.Create(width, height, 96, 96, format, null, pixels, stride); }
internal static void CopyToFrameToPixelArray(this InfraredFrame infraredFrame, ref ushort[] frameData, ref byte[] pixels) { var pixelIndex = 0; infraredFrame.CopyFrameDataToArray(frameData); foreach (var intensity in frameData.Select(framePixel => (byte)(framePixel >> 8))) { pixels[pixelIndex++] = intensity; pixels[pixelIndex++] = intensity; pixels[pixelIndex++] = intensity; ++pixelIndex; } }
internal static void CopyToFrameToPixelArray(this BodyIndexFrame bodyIndexFrame, ref byte[] frameData, ref byte[] pixels) { var pixelIndex = 0; bodyIndexFrame.CopyFrameDataToArray(frameData); for (int i = 0; i < frameData.Length; ++i) { var color = BodyIndexColor.GetColorFromBodyIndex(frameData[i]); pixels[pixelIndex++] = color.B; pixels[pixelIndex++] = color.G; pixels[pixelIndex++] = color.R; pixels[pixelIndex++] = color.A; } }
/// <summary> /// Converts a depth frame to the corresponding System.Windows.Media.Imaging.BitmapSource. /// </summary> /// <param name="frame">The specified depth frame.</param> /// <returns>The corresponding System.Windows.Media.Imaging.BitmapSource representation of the depth frame.</returns> public static BitmapSource ToBitmap(this DepthFrame frame) { ushort minDepth = frame.DepthMinReliableDistance; ushort maxDepth = frame.DepthMaxReliableDistance; if (_bitmap == null) { _width = frame.FrameDescription.Width; _height = frame.FrameDescription.Height; _depthData = new ushort[_width * _height]; _pixels = new byte[_width * _height * Constants.BYTES_PER_PIXEL]; _bitmap = new WriteableBitmap(_width, _height, Constants.DPI, Constants.DPI, Constants.FORMAT, null); } frame.CopyFrameDataToArray(_depthData); // Convert the depth to RGB. int colorIndex = 0; for (int depthIndex = 0; depthIndex < _depthData.Length; ++depthIndex) { // Get the depth for this pixel ushort depth = _depthData[depthIndex]; // 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). byte intensity = (byte)(depth >= minDepth && depth <= maxDepth ? depth : 0); _pixels[colorIndex++] = intensity; // Blue _pixels[colorIndex++] = intensity; // Green _pixels[colorIndex++] = intensity; // Red // 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. ++colorIndex; } _bitmap.Lock(); Marshal.Copy(_pixels, 0, _bitmap.BackBuffer, _pixels.Length); _bitmap.AddDirtyRect(new Int32Rect(0, 0, _width, _height)); _bitmap.Unlock(); return _bitmap; }
internal static void CopyToFrameToPixelArray(this DepthFrame depthFrame, ref ushort[] frameData, ref byte[] pixels) { var pixelIndex = 0; depthFrame.CopyFrameDataToArray(frameData); var minDepth = depthFrame.DepthMinReliableDistance; var maxDepth = depthFrame.DepthMaxReliableDistance; foreach (var intensity in frameData.Select(depth => (byte)(depth >= minDepth && depth <= maxDepth ? depth : 0))) { pixels[pixelIndex++] = intensity; pixels[pixelIndex++] = intensity; pixels[pixelIndex++] = intensity; ++pixelIndex; } }
/// <summary> /// Converts an infrared frame to a System.Media.Imaging.BitmapSource. /// </summary> /// <param name="frame">The specified infrared frame.</param> /// <returns>The specified frame in a System.media.Imaging.BitmapSource representation of the infrared frame.</returns> public static ImageSource ToBitmap(this InfraredFrame frame) { if (_bitmap == null) { _width = frame.FrameDescription.Width; _height = frame.FrameDescription.Height; _infraredData = new ushort[_width * _height]; _pixels = new byte[_width * _height * Constants.BYTES_PER_PIXEL]; _bitmap = new WriteableBitmap(_width, _height, Constants.DPI, Constants.DPI, Constants.FORMAT, null); } frame.CopyFrameDataToArray(_infraredData); // Convert the infrared to RGB. int colorIndex = 0; for (int infraredIndex = 0; infraredIndex < _infraredData.Length; infraredIndex++) { // Get the infrared value for this pixel ushort ir = _infraredData[infraredIndex]; // 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." byte intensity = (byte)(ir >> 6); _pixels[colorIndex++] = intensity; // Blue _pixels[colorIndex++] = intensity; // Green _pixels[colorIndex++] = intensity; // Red // 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. colorIndex++; } _bitmap.Lock(); Marshal.Copy(_pixels, 0, _bitmap.BackBuffer, _pixels.Length); _bitmap.AddDirtyRect(new Int32Rect(0, 0, _width, _height)); _bitmap.Unlock(); return _bitmap; }
/// <summary> /// Converts an infrared frame to a System.Drawing.Bitmap. /// </summary> /// <param name="frame">The specified infrared frame.</param> /// <returns>The specified frame in a System.media.ImageSource format.</returns> public static Bitmap ToBitmap(this InfraredFrame frame) { if (_bitmap == null) { _width = frame.FrameDescription.Width; _height = frame.FrameDescription.Height; _infraredData = new ushort[_width * _height]; _pixels = new byte[_width * _height * Constants.BYTES_PER_PIXEL]; _bitmap = new Bitmap(_width, _height, Constants.FORMAT); } frame.CopyFrameDataToArray(_infraredData); // Convert the infrared to RGB. int colorIndex = 0; for (int infraredIndex = 0; infraredIndex < _infraredData.Length; ++infraredIndex) { // Get the infrared value for this pixel ushort ir = _infraredData[infraredIndex]; // 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." byte intensity = (byte)(ir >> 8); _pixels[colorIndex++] = intensity; // Blue _pixels[colorIndex++] = intensity; // Green _pixels[colorIndex++] = intensity; // Red // 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. ++colorIndex; } BitmapData bitmapData = _bitmap.LockBits(new Rectangle(0, 0, _width, _height), ImageLockMode.ReadWrite, _bitmap.PixelFormat); Marshal.Copy(_pixels, 0, bitmapData.Scan0, _pixels.Length); _bitmap.UnlockBits(bitmapData); return _bitmap; }
/// <summary> /// Converts a depth frame to the corresponding System.Drawing.Bitmap. /// </summary> /// <param name="frame">The specified depth frame.</param> /// <returns>The corresponding System.Drawing.Bitmap representation of the depth frame.</returns> public static Bitmap ToBitmap(this DepthFrame frame) { int width = frame.FrameDescription.Width; int height = frame.FrameDescription.Height; ushort minDepth = frame.DepthMinReliableDistance; ushort maxDepth = frame.DepthMaxReliableDistance; ushort[] pixelData = new ushort[width * height]; byte[] pixels = new byte[width * height * 4]; frame.CopyFrameDataToArray(pixelData); // Convert the depth to RGB. int colorIndex = 0; for (int depthIndex = 0; depthIndex < pixelData.Length; ++depthIndex) { // Get the depth for this pixel ushort depth = pixelData[depthIndex]; // 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). byte intensity = (byte)(depth >= minDepth && depth <= maxDepth ? depth : 0); pixels[colorIndex++] = intensity; // Blue pixels[colorIndex++] = intensity; // Green pixels[colorIndex++] = intensity; // Red // 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. ++colorIndex; } return pixels.ToBitmap(frame.FrameDescription.Width, frame.FrameDescription.Height); }
/// <summary> /// Serializes a depth frame. /// </summary> /// <param name="frame">The specified depth frame.</param> /// <returns>A binary representation of the frame.</returns> //public static byte[] Serialize(this DepthImageFrame frame) public static byte[] Serialize(this DepthFrame frame) { if (_depthBitmap == null) { //_depthWidth = frame.Width; //_depthHeight = frame.Height; _depthWidth = frame.FrameDescription.Width; _depthHeight = frame.FrameDescription.Height; _depthStride = _depthWidth * Constants.PIXEL_FORMAT.BitsPerPixel / 8; //_depthData = new short[frame.PixelDataLength]; _depthData = new ushort[frame.FrameDescription.LengthInPixels * frame.FrameDescription.BytesPerPixel]; _depthPixels = new byte[_depthHeight * _depthWidth * 4]; _depthBitmap = new WriteableBitmap(_depthWidth, _depthHeight, Constants.DPI, Constants.DPI, Constants.PIXEL_FORMAT, null); } //frame.CopyPixelDataTo(_depthData); frame.CopyFrameDataToArray(_depthData); for (int depthIndex = 0, colorIndex = 0; depthIndex < _depthData.Length && colorIndex < _depthPixels.Length; depthIndex++, colorIndex += 4) { // Get the depth value. //int depth = _depthData[depthIndex] >> DepthImageFrame.PlayerIndexBitmaskWidth; int depth = _depthData[depthIndex]; //todo: check // Equal coloring for monochromatic histogram. byte intensity = (byte)(255 - (255 * Math.Max(depth - Constants.MIN_DEPTH_DISTANCE, 0) / (Constants.MAX_DEPTH_DISTANCE_OFFSET))); _depthPixels[colorIndex + 0] = intensity; _depthPixels[colorIndex + 1] = intensity; _depthPixels[colorIndex + 2] = intensity; } _depthBitmap.WritePixels(new Int32Rect(0, 0, _depthWidth, _depthHeight), _depthPixels, _depthStride, 0); return FrameSerializer.CreateBlob(_depthBitmap, Constants.CAPTURE_FILE_DEPTH); }
/// <summary> /// Converts a depth frame to the corresponding System.Windows.Media.Imaging.BitmapSource with the players highlighted. /// </summary> /// <param name="depthFrame">The specified depth frame.</param> /// <param name="bodyIndexFrame">The specified body index frame.</param> /// <returns>The corresponding System.Windows.Media.Imaging.BitmapSource representation of the depth frame.</returns> public static BitmapSource ToBitmap(this DepthFrame depthFrame, BodyIndexFrame bodyIndexFrame) { ushort minDepth = depthFrame.DepthMinReliableDistance; ushort maxDepth = depthFrame.DepthMaxReliableDistance; if (_bodyData == null) { _width = depthFrame.FrameDescription.Width; _height = depthFrame.FrameDescription.Height; _depthData = new ushort[_width * _height]; _bodyData = new byte[_width * _height]; _pixels = new byte[_width * _height * Constants.BYTES_PER_PIXEL]; _bitmap = new WriteableBitmap(_width, _height, Constants.DPI, Constants.DPI, Constants.FORMAT, null); } depthFrame.CopyFrameDataToArray(_depthData); bodyIndexFrame.CopyFrameDataToArray(_bodyData); // Convert the depth to RGB for (int depthIndex = 0, colorPixelIndex = 0; depthIndex < _depthData.Length && colorPixelIndex < _pixels.Length; depthIndex++, colorPixelIndex += 4) { // Get the depth for this pixel ushort depth = _depthData[depthIndex]; byte player = _bodyData[depthIndex]; // 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). byte intensity = (byte)(depth >= minDepth && depth <= maxDepth ? depth : 0); if (player != 0xff) { // Color player gold. _pixels[colorPixelIndex + 0] = Colors.Gold.B; // B _pixels[colorPixelIndex + 1] = Colors.Gold.G; // G _pixels[colorPixelIndex + 2] = Colors.Gold.R; // R } else { // Color the rest of the image in grayscale. _pixels[colorPixelIndex + 0] = intensity; // B _pixels[colorPixelIndex + 1] = intensity; // G _pixels[colorPixelIndex + 2] = intensity; // R } } _bitmap.Lock(); Marshal.Copy(_pixels, 0, _bitmap.BackBuffer, _pixels.Length); _bitmap.AddDirtyRect(new Int32Rect(0, 0, _width, _height)); _bitmap.Unlock(); return _bitmap; }