/// <summary> /// 距離データをカラー画像に変換する /// </summary> /// <param name="kinect"></param> /// <param name="depthFrame"></param> /// <returns></returns> private byte[] ConvertDepthColor( KinectSensor kinect, DepthImageFrame depthFrame ) { ColorImageStream colorStream = kinect.ColorStream; DepthImageStream depthStream = kinect.DepthStream; // 距離カメラのピクセルごとのデータを取得する short[] depthPixel = new short[depthFrame.PixelDataLength]; depthFrame.CopyPixelDataTo( depthPixel ); // 距離カメラの座標に対応するRGBカメラの座標を取得する(座標合わせ) ColorImagePoint[] colorPoint = new ColorImagePoint[depthFrame.PixelDataLength]; kinect.MapDepthFrameToColorFrame( depthStream.Format, depthPixel, colorStream.Format, colorPoint ); byte[] depthColor = new byte[depthFrame.PixelDataLength * Bgr32BytesPerPixel]; for ( int index = 0; index < depthPixel.Length; index++ ) { // 距離カメラのデータから、プレイヤーIDと距離を取得する int player = depthPixel[index] & DepthImageFrame.PlayerIndexBitmask; int distance = depthPixel[index] >> DepthImageFrame.PlayerIndexBitmaskWidth; // 変換した結果が、フレームサイズを超えることがあるため、小さいほうを使う int x = Math.Min( colorPoint[index].X, colorStream.FrameWidth - 1 ); int y = Math.Min( colorPoint[index].Y, colorStream.FrameHeight - 1 ); int colorIndex = ((y * depthFrame.Width) + x) * Bgr32BytesPerPixel; if ( player != 0 ) { depthColor[colorIndex] = 255; depthColor[colorIndex + 1] = 255; depthColor[colorIndex + 2] = 255; } else { // サポート外 0-40cm if ( distance == depthStream.UnknownDepth ) { depthColor[colorIndex] = 0; depthColor[colorIndex + 1] = 0; depthColor[colorIndex + 2] = 255; } // 近すぎ 40cm-80cm(default mode) else if ( distance == depthStream.TooNearDepth ) { depthColor[colorIndex] = 0; depthColor[colorIndex + 1] = 255; depthColor[colorIndex + 2] = 0; } // 遠すぎ 3m(Near),4m(Default)-8m else if ( distance == depthStream.TooFarDepth ) { depthColor[colorIndex] = 255; depthColor[colorIndex + 1] = 0; depthColor[colorIndex + 2] = 0; } // 有効な距離データ else { depthColor[colorIndex] = 0; depthColor[colorIndex + 1] = 255; depthColor[colorIndex + 2] = 255; } } } return depthColor; }
/// <summary> /// 获取身高 /// </summary> /// <param name="depthFrame">深度图像数据</param> /// <returns></returns> public int GetHeight(DepthImageFrame depthFrame) { int height; short[] rawDepthData = new short[depthFrame.PixelDataLength]; depthFrame.CopyPixelDataTo(rawDepthData); int Max = 0; int Min = 480; for (int depthIndex = 0; depthIndex < rawDepthData.Length; depthIndex++) { int player = rawDepthData[depthIndex] & DepthImageFrame.PlayerIndexBitmask; if (player > 0) { int TempYdata = depthIndex / 640; if (TempYdata > Max) { Max = TempYdata; } if (TempYdata < Min) { Min = TempYdata; } } } height = Max - Min; return height; }
public void Record(DepthImageFrame frame) { // Header writer.Write((int)KinectRecordOptions.Depth); // Data var time = DateTime.Now; TimeSpan timeSpan = time.Subtract(referenceTime); referenceTime = time; writer.Write((long)timeSpan.TotalMilliseconds); writer.Write(frame.BytesPerPixel); writer.Write((int)frame.Format); writer.Write(frame.Width); writer.Write(frame.Height); writer.Write(frame.FrameNumber); // Bytes short[] shorts = new short[frame.PixelDataLength]; frame.CopyPixelDataTo(shorts); writer.Write(shorts.Length); foreach (short s in shorts) { writer.Write(s); } }
private byte[] GenerateColoredBytes(DepthImageFrame depthFrame) { short[] rawDepthData = new short[depthFrame.PixelDataLength]; depthFrame.CopyPixelDataTo(rawDepthData); Byte[] pixels = new byte[depthFrame.Height * depthFrame.Width * 4]; const int BlueIndex = 0; const int GreenIndex = 1; const int RedIndex = 2; const int alpha = 3; for (int depthIndex = 0, colorIndex = 0; depthIndex < rawDepthData.Length && colorIndex < pixels.Length; depthIndex++, colorIndex += 4) { int player = rawDepthData[depthIndex] & DepthImageFrame.PlayerIndexBitmask; int depth = rawDepthData[depthIndex] >> DepthImageFrame.PlayerIndexBitmaskWidth; if (depth <= 900) { pixels[colorIndex + BlueIndex] = 255; //closest objects blue pixels[colorIndex + GreenIndex] = 0; pixels[colorIndex + RedIndex] = 0; pixels[colorIndex + alpha] = 255; } else if (depth < 2000) { pixels[colorIndex + BlueIndex] = 0; pixels[colorIndex + GreenIndex] = 255; //medium depth is green pixels[colorIndex + RedIndex] = 0; pixels[colorIndex + alpha] = 255; } else { pixels[colorIndex + BlueIndex] = 0; pixels[colorIndex + GreenIndex] = 0; pixels[colorIndex + RedIndex] = 255; //farthest is red pixels[colorIndex + alpha] = 255; } if (player > 0) { pixels[colorIndex + BlueIndex] = 0; pixels[colorIndex + GreenIndex] = 255; pixels[colorIndex + RedIndex] = 255; pixels[colorIndex + alpha] = 255; } } return pixels; }
private byte[] GenerateColoredBytes(DepthImageFrame depthFrame) { //get raw data from kinect short[] rawDepthData = new short[depthFrame.PixelDataLength]; depthFrame.CopyPixelDataTo(rawDepthData); Byte[] pixels = new byte[depthFrame.Width * depthFrame.Height * 4]; //constants const int BlueIndex = 0; const int GreenIndex = 1; const int RedIndex = 2; //loop through distances for (int depthIndex = 0, colorIndex = 0; depthIndex < rawDepthData.Length && colorIndex < pixels.Length; depthIndex++, colorIndex += 4) { //get player int player = rawDepthData[depthIndex] & DepthImageFrame.PlayerIndexBitmask; //get depth value int depth = rawDepthData[depthIndex] >> DepthImageFrame.PlayerIndexBitmaskWidth; //.9M or 2.95' if (depth <= 900) { pixels[colorIndex + BlueIndex] = 0; pixels[colorIndex + GreenIndex] = 0; pixels[colorIndex + RedIndex] = 255; } //.9M - 2M or 2.95' - 6.26' else if (depth > 900 && depth < 2000) { pixels[colorIndex + BlueIndex] = 0; pixels[colorIndex + GreenIndex] = 255; pixels[colorIndex + RedIndex] = 0; } //2M+ or 6.26'+ else if (depth > 2000) { pixels[colorIndex + BlueIndex] = 255; pixels[colorIndex + GreenIndex] = 0; pixels[colorIndex + RedIndex] = 0; } //byte intensity = CalculateIntensityFromDepth(depth); //pixels[colorIndex + BlueIndex] = intensity; //pixels[colorIndex + GreenIndex] = intensity; //pixels[colorIndex + RedIndex] = intensity; } return pixels; }
//distance given in inches public BitmapSource ApplyDistanceFilter(DepthImageFrame frame, int dist) { double depthDist = dist * 25.4; short[] depthArray = new short[frame.PixelDataLength]; frame.CopyPixelDataTo(depthArray); depthArray = applyPixelFilter(depthArray, frame.Width, frame.Height); // depthArray = applyWeightedAverageFilter(depthArray, frame.Width, frame.Height); byte[] colorFrame = new byte[frame.Width * frame.Height * 4]; Parallel.For(0, frame.Height, i => { // Process each pixel in the row for (int j = 0; j < frame.Width; j++) { var distanceIndex = j + (i * frame.Width); var index = distanceIndex * 4; double depth = depthArray[distanceIndex]>> DepthImageFrame.PlayerIndexBitmaskWidth; // Map the distance to an intesity that can be represented in RGB if (depth < depthDist && depth > 0) { var intensity = CalculateIntensityFromDistance(depthArray[distanceIndex]); colorFrame[index + BlueIndex] = intensity; colorFrame[index + GreenIndex] = intensity; colorFrame[index + RedIndex] = intensity; colorFrame[index + AlphaIndex] = 250; } else { colorFrame[index + BlueIndex] = 112; colorFrame[index + GreenIndex] = 25; colorFrame[index + RedIndex] = 25; colorFrame[index + AlphaIndex] = 200; } } }); //rgba BitmapSource bmps = BitmapSource.Create(frame.Width, frame.Height, 96, 96, PixelFormats.Bgra32, null, colorFrame, frame.Width * PixelFormats.Bgr32.BitsPerPixel / 8); //rgb //BitmapSource bmps = BitmapSource.Create(frame.Width, frame.Height, 96, 96, PixelFormats.Bgr32, null, colorFrame, frame.Width * PixelFormats.Bgr32.BitsPerPixel / 8); return bmps; }
private static Byte[] GenerateColoredBytes(DepthImageFrame depthFrame, int minRange, int maxRange) { //get the raw data from kinect with the depth for every pixel short[] rawDepthData = new short[depthFrame.PixelDataLength]; depthFrame.CopyPixelDataTo(rawDepthData); //use depthFrame to create the image to display on-screen //depthFrame contains color information for all pixels in image //Height x Width x 4 (Red, Green, Blue, empty byte) Byte[] pixels = new Byte[depthFrame.Height * depthFrame.Width * 4]; //Bgr32 - Blue, Green, Red, empty byte //Bgra32 - Blue, Green, Red, transparency //You must set transparency for Bgra as .NET defaults a byte to 0 = fully transparent //hardcoded locations to Blue, Green, Red (BGR) index positions const int BlueIndex = 0; const int GreenIndex = 1; const int RedIndex = 2; //loop through all distances //pick a RGB color based on distance for (int depthIndex = 0, colorIndex = 0; depthIndex < rawDepthData.Length && colorIndex < pixels.Length; depthIndex++, colorIndex += 4) { int depth = rawDepthData[depthIndex] >> DepthImageFrame.PlayerIndexBitmaskWidth; if (depth < 850 || depth > 4000) { pixels[colorIndex + BlueIndex] = 0; pixels[colorIndex + GreenIndex] = 0; pixels[colorIndex + RedIndex] = 0; } else if (depth <= minRange || depth > maxRange) { pixels[colorIndex + BlueIndex] = 0; pixels[colorIndex + GreenIndex] = 0; pixels[colorIndex + RedIndex] = 0; } else { pixels[colorIndex + BlueIndex] = 0; pixels[colorIndex + GreenIndex] = 255; pixels[colorIndex + RedIndex] = 0; } } return pixels; }
byte[] GetColoredBytes(DepthImageFrame frame) { var depthData = new short[frame.PixelDataLength]; frame.CopyPixelDataTo(depthData); //4 (Red, Green, Blue, empty byte) var pixels = new byte[frame.Height * frame.Width * 4]; const int blueIndex = 0; const int greenIndex = 1; const int redIndex = 2; //pick a RGB color based on distance for (int depthIndex = 0, colorIndex = 0; depthIndex < depthData.Length && colorIndex < pixels.Length; depthIndex++, colorIndex += 4) { var player = depthData[depthIndex] & DepthImageFrame.PlayerIndexBitmask; var depth = depthData[depthIndex] >> DepthImageFrame.PlayerIndexBitmaskWidth; if (depth <= 900) { pixels[colorIndex + blueIndex] = 255; pixels[colorIndex + greenIndex] = 0; pixels[colorIndex + redIndex] = 0; } else if (depth > 900 && depth < 2000) { pixels[colorIndex + blueIndex] = 0; pixels[colorIndex + greenIndex] = 255; pixels[colorIndex + redIndex] = 0; } else if (depth > 2000) { pixels[colorIndex + blueIndex] = 0; pixels[colorIndex + greenIndex] = 0; pixels[colorIndex + redIndex] = 255; } if (player > 0) { pixels[colorIndex + blueIndex] = Colors.Gold.B; pixels[colorIndex + greenIndex] = Colors.Gold.G; pixels[colorIndex + redIndex] = Colors.Gold.R; } } return pixels; }
/// <summary> /// 距離データをカラー画像に変換する /// </summary> /// <param name="kinect"></param> /// <param name="depthFrame"></param> /// <returns></returns> private byte[] ConvertDepthToColorImage(KinectSensor kinect, DepthImageFrame depthFrame) { DepthImageStream depthStream = kinect.DepthStream; // 距離カメラのピクセルごとのデータを取得する short[] depthPixel = new short[depthFrame.PixelDataLength]; depthFrame.CopyPixelDataTo(depthPixel); byte[] depthColor = new byte[depthFrame.PixelDataLength * Bgr32BytesPerPixel]; for (int index = 0; index < depthPixel.Length; index++) { // 距離カメラのデータから、距離を取得する int distance = depthPixel[index] >> DepthImageFrame.PlayerIndexBitmaskWidth; int colorIndex = index * Bgr32BytesPerPixel; // 赤:サポート外 0-40cm, 8m- if (distance == depthStream.UnknownDepth) { depthColor[colorIndex] = 0; depthColor[colorIndex + 1] = 0; depthColor[colorIndex + 2] = 255; } // 緑:近すぎ 40cm-80cm(default mode) else if (distance == depthStream.TooNearDepth) { depthColor[colorIndex] = 0; depthColor[colorIndex + 1] = 255; depthColor[colorIndex + 2] = 0; } // 青:遠すぎ 3m(Near),4m(Default)-8m else if (distance == depthStream.TooFarDepth) { depthColor[colorIndex] = 255; depthColor[colorIndex + 1] = 0; depthColor[colorIndex + 2] = 0; } // 有効な距離データ else { byte color = (byte)(255 * distance / depthStream.TooFarDepth); depthColor[colorIndex] = 0; depthColor[colorIndex + 1] = color; depthColor[colorIndex + 2] = color; } } return depthColor; }
public static ReplayDepthImageFrame FromDepthImageFrame(DepthImageFrame frame) { short[] pixelData = new short[frame.PixelDataLength]; frame.CopyPixelDataTo(pixelData); return new ReplayDepthImageFrame() { FrameNumber = frame.FrameNumber, BytesPerPixel = frame.BytesPerPixel, PixelDataLength = frame.PixelDataLength, Width = frame.Width, Height = frame.Height, Timestamp = frame.Timestamp, PixelData = pixelData }; }
void Context_AllFramesUpdated(KinectSensor sensor, ColorImageFrame cf, DepthImageFrame df, SkeletonFrame sf) { this.sensor = sensor; if (colorImage == null) { colorImage = new byte[cf.PixelDataLength]; depthImage = new short[df.PixelDataLength]; skeletonData = new Skeleton[sf.SkeletonArrayLength]; } cf.CopyPixelDataTo(colorImage); df.CopyPixelDataTo(depthImage); sf.CopySkeletonDataTo(skeletonData); TrackFace(); }
private byte[] Generatecoloredbytes(DepthImageFrame depthframe) { short[] rawdepthdata = new short[depthframe.PixelDataLength]; depthframe.CopyPixelDataTo(rawdepthdata); Byte[] pixels = new byte[depthframe.Height * depthframe.Width * 4]; const int blueindex = 0; const int greenindex = 1; const int redindex = 2; for (int depthindex = 0, colorindex = 0; depthindex < rawdepthdata.Length && colorindex < pixels.Length; depthindex++, colorindex += 4) { int player = rawdepthdata[depthindex] & DepthImageFrame.PlayerIndexBitmask; int depth = rawdepthdata[depthindex] >> DepthImageFrame.PlayerIndexBitmaskWidth; if (depth <= 500) { pixels[colorindex + blueindex] = 255; pixels[colorindex + greenindex] = 0; pixels[colorindex + redindex] = 0; } else if (depth >= 500 && depth<= 1300) { pixels[colorindex + blueindex] = 0; pixels[colorindex + greenindex] = 255; pixels[colorindex + redindex] = 0; } else if (depth >1300 && depth<2000) { pixels[colorindex + blueindex] = 0; pixels[colorindex + greenindex] = 0; pixels[colorindex + redindex] = 255; } else if (depth >= 2000) { pixels[colorindex + blueindex] = 255; pixels[colorindex + greenindex] = 255; pixels[colorindex + redindex] = 0; } } return pixels; //throw new NotImplementedException(); }
/// <summary> /// 生成深度信息图像 /// </summary> /// <param name="depthFrame">传入深入帧</param> /// <returns>返回深度图像像素集</returns> private byte[] GenerateColoredBytes(DepthImageFrame depthFrame) { short[] rawDepthData = new short[depthFrame.PixelDataLength]; depthFrame.CopyPixelDataTo(rawDepthData); byte[] pixels = new byte[depthFrame.Width * depthFrame.Height * 4]; const int BlueIndex = 0; const int GreenIndex = 1; const int RedIndex = 2; for (int depthIndex = 0, colorIndex = 0; depthIndex < rawDepthData.Length && colorIndex < pixels.Length; ++depthIndex, colorIndex += 4) { int player = rawDepthData[depthIndex] & DepthImageFrame.PlayerIndexBitmask; int depth = rawDepthData[depthIndex] >> DepthImageFrame.PlayerIndexBitmaskWidth; if (depth <= 900) { pixels[colorIndex + BlueIndex] = 255; pixels[colorIndex + GreenIndex] = 0; pixels[colorIndex + RedIndex] = 0; } else if (depth > 900 && depth < 2000) { pixels[colorIndex + BlueIndex] = 0; pixels[colorIndex + GreenIndex] = 255; pixels[colorIndex + RedIndex] = 0; } else if (depth > 2000) { pixels[colorIndex + BlueIndex] = 0; pixels[colorIndex + GreenIndex] = 0; pixels[colorIndex + RedIndex] = 255; } byte intensity = CalculateIntensityFromeDepth(depth); pixels[colorIndex + BlueIndex] = intensity; pixels[colorIndex + GreenIndex] = intensity; pixels[colorIndex + RedIndex] = intensity; //if (player > 0) //{ // pixels[colorIndex + BlueIndex] = Colors.Gold.B; // pixels[colorIndex + GreenIndex] = Colors.Gold.G; // pixels[colorIndex + RedIndex] = Colors.Gold.R; //} } return pixels; }
private short[] trimImage(DepthImageFrame frame) { int pixeldatalength = frame.PixelDataLength; short[] pixels = new short[pixeldatalength]; short[] newImagePixels = new short[153600]; frame.CopyPixelDataTo(pixels); int newIndex = 0; for (int x = 0; x < (640 * 320); x++) { if (x % 640 > 80 && x % 640 <= 560) { newImagePixels[newIndex] = pixels[x]; newIndex++; } } return newImagePixels; }
/// <summary> /// プレーヤーだけ表示する /// </summary> /// <param name="colorFrame"></param> /// <param name="depthFrame"></param> /// <returns></returns> private byte[] BackgroundMask( KinectSensor kinect, ColorImageFrame colorFrame, DepthImageFrame depthFrame ) { ColorImageStream colorStream = kinect.ColorStream; DepthImageStream depthStream = kinect.DepthStream; // RGBカメラのピクセルごとのデータを取得する byte[] colorPixel = new byte[colorFrame.PixelDataLength]; colorFrame.CopyPixelDataTo( colorPixel ); // 距離カメラのピクセルごとのデータを取得する short[] depthPixel = new short[depthFrame.PixelDataLength]; depthFrame.CopyPixelDataTo( depthPixel ); // 距離カメラの座標に対応するRGBカメラの座標を取得する(座標合わせ) ColorImagePoint[] colorPoint = new ColorImagePoint[depthFrame.PixelDataLength]; kinect.MapDepthFrameToColorFrame( depthStream.Format, depthPixel, colorStream.Format, colorPoint ); // 出力バッファ(初期値は白(255,255,255)) byte[] outputColor = new byte[colorPixel.Length]; for ( int i = 0; i < outputColor.Length; i += Bgr32BytesPerPixel ) { outputColor[i] = 255; outputColor[i + 1] = 255; outputColor[i + 2] = 255; } for ( int index = 0; index < depthPixel.Length; index++ ) { // プレイヤーを取得する int player = depthPixel[index] & DepthImageFrame.PlayerIndexBitmask; // 変換した結果が、フレームサイズを超えることがあるため、小さいほうを使う int x = Math.Min( colorPoint[index].X, colorStream.FrameWidth - 1 ); int y = Math.Min( colorPoint[index].Y, colorStream.FrameHeight - 1 ); int colorIndex = ((y * depthFrame.Width) + x) * Bgr32BytesPerPixel; // プレーヤーを検出した座標だけ、RGBカメラの画像を使う if ( player != 0 ) { outputColor[colorIndex] = colorPixel[colorIndex]; outputColor[colorIndex + 1] = colorPixel[colorIndex + 1]; outputColor[colorIndex + 2] = colorPixel[colorIndex + 2]; } } return outputColor; }
public static byte[] ConvertDepthFrameToBitmap(DepthImageFrame depthFrame) { if (depthFrame == null) { return null; } short[] depthData = new short[depthFrame.PixelDataLength]; depthFrame.CopyPixelDataTo(depthData); Byte[] depthColors = new Byte[depthData.Length * 4]; //four colors for each pixel for (int colorIndex = 0, depthIndex = 0; colorIndex < depthColors.Length; colorIndex += 4, depthIndex++) { //get the depth, then calculate the intensity (0-255 based on the depth) //depth of -1 = dark brown int depth = GetDepth(depthData[depthIndex]); if (depth == -1) { // dark brown depthColors[colorIndex + RedIndex] = 66; depthColors[colorIndex + GreenIndex] = 66; depthColors[colorIndex + BlueIndex] = 33; } else { var intensity = ImageFrameCommonExtensions.CalculateIntensityFromDepth(depth); depthColors[colorIndex + RedIndex] = intensity; depthColors[colorIndex + GreenIndex] = intensity; depthColors[colorIndex + BlueIndex] = intensity; } //if the pixel is a player, choose a color int player = GetPlayerIndex(depthData[depthIndex]); SkeletonOverlay( ref depthColors[colorIndex + RedIndex], ref depthColors[colorIndex + GreenIndex], ref depthColors[colorIndex + BlueIndex], player); } return depthColors; }
public KServerDepthStreamPaquet(DepthImageFrame _depthFrame, int _idSensor) { depthFrame = _depthFrame; idSensor = _idSensor; pxlData = new short[depthFrame.PixelDataLength]; depthFrame.CopyPixelDataTo(pxlData); setBodySize((uint)(depthFrame.PixelDataLength * sizeof(short) + 13)); byte[] size = BitConverter.GetBytes((uint)(depthFrame.PixelDataLength * sizeof(short) + 13)); Array.Reverse(size); Buffer.BlockCopy(size, 0, data, 0, size.Length); /* Builds the paquet */ build(); }
private byte[] GenerateColoredBytes(DepthImageFrame depthFrame) { short[] rawDepthData = new short[depthFrame.PixelDataLength]; depthFrame.CopyPixelDataTo(rawDepthData); Byte[] pixels = new byte[depthFrame.Height * depthFrame.Width * 4]; const int BlueIndex = 0; const int GreenIndex = 1; const int RedIndex = 2; for (int depthIndex = 0, colorIndex = 0; depthIndex < rawDepthData.Length && colorIndex < pixels.Length; depthIndex++, colorIndex += 4) { int player = rawDepthData[depthIndex] & DepthImageFrame.PlayerIndexBitmask; int depth = rawDepthData[depthIndex] >> DepthImageFrame.PlayerIndexBitmaskWidth; if (depth <= 900) { pixels[colorIndex + BlueIndex] = globalvideo2[colorIndex + BlueIndex]; pixels[colorIndex + GreenIndex] = globalvideo2[colorIndex + GreenIndex]; pixels[colorIndex + RedIndex] = globalvideo2[colorIndex + RedIndex]; } else if (depth < 2000) { pixels[colorIndex + BlueIndex] = globalvideo[colorIndex + BlueIndex]; pixels[colorIndex + GreenIndex] = globalvideo[colorIndex + GreenIndex]; pixels[colorIndex + RedIndex] = globalvideo[colorIndex + RedIndex]; } else { pixels[colorIndex + BlueIndex] = globalvideo3[colorIndex + BlueIndex]; pixels[colorIndex + GreenIndex] = globalvideo3[colorIndex + GreenIndex]; pixels[colorIndex + RedIndex] = globalvideo3[colorIndex + RedIndex]; } } return pixels; }
private byte[] GenerateColoredBytes(DepthImageFrame depthFrame) { // Get raw depth data short[] raw = new short[depthFrame.PixelDataLength]; depthFrame.CopyPixelDataTo(raw); // Create byte array with size h*w*4 (for bgr_) Byte[] pixels = new byte[depthFrame.Height * depthFrame.Width * 4]; const int BlueIndex = 0; const int GreenIndex = 1; const int RedIndex = 2; // Loop through all distances for (int depthIndex = 0, colorIndex = 0; depthIndex < raw.Length && colorIndex < pixels.Length; depthIndex++, colorIndex += 4) { // Get player int player = raw[depthIndex] & DepthImageFrame.PlayerIndexBitmask; // Get depth int depth = raw[depthIndex] >> DepthImageFrame.PlayerIndexBitmaskWidth; // Set colors based on depth if (depth <= 900) { pixels[colorIndex + BlueIndex] = 255; pixels[colorIndex + GreenIndex] = 0; pixels[colorIndex + RedIndex] = 0; } else if (depth > 900 && depth < 2000) { pixels[colorIndex + BlueIndex] = 0; pixels[colorIndex + GreenIndex] = 255; pixels[colorIndex + RedIndex] = 0; } else if (depth > 2000) { pixels[colorIndex + BlueIndex] = 0; pixels[colorIndex + GreenIndex] = 0; pixels[colorIndex + RedIndex] = 255; } } return pixels; }
//Takes a DepthImage frame and performs two noise removal techniques: //(1)pixel filtering (2)weighted moving average (reduces flicker) //It takes a DepthImage Frame and returns a BitmapSource public BitmapSource applyBothFilters(DepthImageFrame image) { // We first want to create a simple array where each index represents a single pixel of depth information. // This will make it easier to work with the data to filter and average it for smoothing. // short[] depthArray = CreateDepthArray(image); short[] depthArray = new short[image.PixelDataLength]; image.CopyPixelDataTo(depthArray); depthArray = applyPixelFilter(depthArray, image.Width, image.Height); // depthArray = applyWeightedAverageFilter(depthArray, image.Width, image.Height); int width = image.Width; int height = image.Height; // After we have processed the data, we can transform it into color channels for final rendering. byte[] colorBytes = CreateColorBytesFromDepthArray(depthArray, width, height); return BitmapSource.Create(width, height, 96, 96, PixelFormats.Bgr32, null, colorBytes, width * PixelFormats.Bgr32.BitsPerPixel / 8); }
private void FindDepth(DepthImageFrame depthFrame) { short[] rawDepthData = new short[depthFrame.PixelDataLength]; depthFrame.CopyPixelDataTo(rawDepthData); Byte[] pixels = new byte[depthFrame.Height * depthFrame.Width * 4]; for (int depthIndex = 0; depthIndex < rawDepthData.Length; depthIndex++) { int player = rawDepthData[depthIndex] & DepthImageFrame.PlayerIndexBitmask; int depth = rawDepthData[depthIndex] >> DepthImageFrame.PlayerIndexBitmaskWidth; if (player > 0) { playerDepth = rawDepthData[depthIndex]; } } }
private Bitmap CreateBitMapFromDepthFrame(DepthImageFrame frame) { if (frame != null) { var bitmapImage = new Bitmap(frame.Width, frame.Height, PixelFormat.Format16bppRgb565); var g = Graphics.FromImage(bitmapImage); g.Clear(Color.FromArgb(0, 34, 68)); //Copy the depth frame data onto the bitmap var _pixelData = new short[frame.PixelDataLength]; frame.CopyPixelDataTo(_pixelData); BitmapData bmapdata = bitmapImage.LockBits(new Rectangle(0, 0, frame.Width, frame.Height), ImageLockMode.WriteOnly, bitmapImage.PixelFormat); IntPtr ptr = bmapdata.Scan0; Marshal.Copy(_pixelData, 0, ptr, frame.Width * frame.Height); bitmapImage.UnlockBits(bmapdata); return bitmapImage; } return null; }
internal void GiveFrame(DepthImageFrame depthFrame, PointCluster floodFill) { short[] imagePixelData = new short[depthFrame.PixelDataLength]; depthFrame.CopyPixelDataTo(imagePixelData); float RenderWidth = 640.0f; float RenderHeight = 480.0f; Bitmap bmap = new System.Drawing.Bitmap(depthFrame.Width, depthFrame.Height, System.Drawing.Imaging.PixelFormat.Format16bppRgb555); System.Drawing.Imaging.BitmapData bmapdata = bmap.LockBits(new System.Drawing.Rectangle(0, 0, depthFrame.Width , depthFrame.Height), ImageLockMode.WriteOnly, bmap.PixelFormat); IntPtr ptr = bmapdata.Scan0; System.Runtime.InteropServices.Marshal.Copy(imagePixelData, 0, ptr, depthFrame.Width * depthFrame.Height); bmap.UnlockBits(bmapdata); /*System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmap); this.myImageBox.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( bmap.GetHbitmap(), IntPtr.Zero, System.Windows.Int32Rect.Empty, BitmapSizeOptions.FromWidthAndHeight((int)this.myImageBox.Width, (int)this.myImageBox.Height));*/ using (DrawingContext lfdc = drawingGroup.Open())//this.liveFeedbackGroup.Open()) { lfdc.DrawImage(System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( bmap.GetHbitmap(), IntPtr.Zero, System.Windows.Int32Rect.Empty, BitmapSizeOptions.FromWidthAndHeight((int)RenderWidth, (int)RenderHeight)), //BitmapSizeOptions.FromWidthAndHeight((int)this.myImageBox.ActualWidth, (int)this.myImageBox.ActualHeight)), new Rect(0.0, 0.0, RenderWidth, RenderHeight)); foreach (DepthPoint point in floodFill.points) { lfdc.DrawRoundedRectangle(System.Windows.Media.Brushes.Red, null, new Rect(point.x, point.y, 3, 3), null, 1, null, 1, null); } } }
public void Update(DepthImageFrame frame) { var pixelData = new short[frame.PixelDataLength]; frame.CopyPixelDataTo(pixelData); if (depthFrame32 == null) { depthFrame32 = new byte[frame.Width * frame.Height * 4]; } if (Bitmap == null) { Bitmap = new WriteableBitmap(frame.Width, frame.Height, 96, 96, PixelFormats.Bgra32, null); } ConvertDepthFrame(pixelData); int stride = Bitmap.PixelWidth * Bitmap.Format.BitsPerPixel / 8; Int32Rect dirtyRect = new Int32Rect(0, 0, Bitmap.PixelWidth, Bitmap.PixelHeight); Bitmap.WritePixels(dirtyRect, depthFrame32, stride, 0); RaisePropertyChanged(() => Bitmap); }
private static byte[] ConvertDepthFrameData(object sender, DepthImageFrame depthImageFrame) { short[] pixelsFromFrame = new short[depthImageFrame.PixelDataLength]; depthImageFrame.CopyPixelDataTo(pixelsFromFrame); return ConvertDepthFrame(pixelsFromFrame, ((KinectSensor)sender).DepthStream, 640 * 480 * 4); }
private void ParseDepthFrame(DepthImageFrame depthFrame) { short[] rawDepthData = new short[depthFrame.PixelDataLength]; depthFrame.CopyPixelDataTo(rawDepthData); int minDepth = DepthThreshold; int bestDepthIndex = -1; int minDepthIndex = (int)this.topLeft.Y * depthFrame.Width; int maxDepthIndex = (int)this.bottomRight.Y * depthFrame.Width; minDepthIndex = 0; maxDepthIndex = 479 * depthFrame.Width; Console.WriteLine(minDepthIndex + " " + depthFrame.Width); for (int depthIndex = minDepthIndex; depthIndex < maxDepthIndex; depthIndex++) {/* // Skip this depth index if it's horizontally outside of our textile int x_kinect = (int)((depthIndex) % depthFrame.Width); if (x_kinect < topLeft.X) { continue; } else if (x_kinect > bottomRight.X) { //depthIndex += (depthFrame.Width - (int)(bottomRight.X - topLeft.X - 1)); continue; }*/ int depth = rawDepthData[depthIndex] >> DepthImageFrame.PlayerIndexBitmaskWidth; // Ignore invalid depth values if (depth == -1 || depth == 0) continue; if (depth < minDepth) { minDepth = depth; bestDepthIndex = depthIndex; } } // Draw if a touch was found if (bestDepthIndex >= 0) { Console.WriteLine("here"); if (!this.hasSetDepthThreshold) { this.DepthThreshold = minDepth - TextileSpacing; this.hasSetDepthThreshold = true; } else { soundController.StartMusic(); // i add // drawController.ClearScreen(); DrawPoint(depthFrame, bestDepthIndex, minDepth); gotTouch = true; } } else { if (gotTouch == true) { soundController.StopMusic(); // i add //drawController.SaveCanvas(); //drawController.ClearScreen(); } gotTouch = false; } }
// Generates a color image from the depth frame public byte[] GenerateColoredBytes(DepthImageFrame depthFrame) { short[] rawDepthData = new short[depthFrame.PixelDataLength]; depthFrame.CopyPixelDataTo(rawDepthData); // Create the RGB pixel array Byte[] pixels = new byte[depthFrame.Height * depthFrame.Width * 4]; const int BlueIndex = 0; const int GreenIndex = 1; const int RedIndex = 2; // Loop through data and set colors for each pixel for (int depthIndex = 0, colorIndex = 0; depthIndex < rawDepthData.Length && colorIndex < pixels.Length; depthIndex++, colorIndex += 4) { int player = rawDepthData[depthIndex] & DepthImageFrame.PlayerIndexBitmask; int depth = rawDepthData[depthIndex] >> DepthImageFrame.PlayerIndexBitmaskWidth; if (depth == -1 || depth == 0) continue; byte intensity = CalculateIntensityFromDepth(depth); pixels[colorIndex + BlueIndex] = intensity; pixels[colorIndex + GreenIndex] = intensity; pixels[colorIndex + RedIndex] = intensity; } return pixels; }
/// <summary> /// Get informations between two depths values /// </summary> /// <param name="min">min depth</param> /// <param name="max">max depth</param> /// <param name="f">depthFrame</param> /// <returns>a byte array with a value of 1 if the point is inbetween, 0 if not</returns> public byte[] GetEverythingBetween(double min, double max, DepthImageFrame f) { //get the raw data from kinect with the depth for every pixel short[] rawDepthData = new short[f.PixelDataLength]; f.CopyPixelDataTo(rawDepthData); //use depthFrame to create the image to display on-screen //depthFrame contains color information for all pixels in image Byte[] pixels = new byte[f.Height * f.Width]; for (int depthIndex = 0; depthIndex < rawDepthData.Length; depthIndex++) { //gets the depth value int depth = rawDepthData[depthIndex] >> DepthImageFrame.PlayerIndexBitmaskWidth; if (depth < min * 1000 || depth > max * 1000) { pixels[depthIndex] = 0; } else { pixels[depthIndex] = 255; } } byte[] averagedDepthArray = CreateAverageDepthArray(pixels); if (depthQueue.Count == averageFrameCount) { return averagedDepthArray; } else { return pixels; } }
private byte[] GenerateColoredBytes(DepthImageFrame depthFrame) { //get the raw data from kinect with the depth for every pixel short[] rawDepthData = new short[depthFrame.PixelDataLength]; depthFrame.CopyPixelDataTo(rawDepthData); //use depthFrame to create the image to display on-screen //depthFrame contains color information for all pixels in image //Height x Width x 4 (Red, Green, Blue, empty byte) Byte[] pixels = new byte[depthFrame.Height * depthFrame.Width * 4]; //Bgr32 - Blue, Green, Red, empty byte //Bgra32 - Blue, Green, Red, transparency //You must set transparency for Bgra as .NET defaults a byte to 0 = fully transparent int depthClosest = MaxDepthDistance; List<int> lstHandTop= new List<int>(); //loop through all distances //pick a RGB color based on distance for (int depthIndex = 0, colorIndex = 0; depthIndex < rawDepthData.Length && colorIndex < pixels.Length; depthIndex++, colorIndex += 4) { //get the player (requires skeleton tracking enabled for values) int player = rawDepthData[depthIndex] & DepthImageFrame.PlayerIndexBitmask; //gets the depth value int depth = rawDepthData[depthIndex] >> DepthImageFrame.PlayerIndexBitmaskWidth; if (player > 0) { pixels[colorIndex + BlueIndex] = 0; pixels[colorIndex + GreenIndex] = 0; pixels[colorIndex + RedIndex] = 255; if (depth < depthClosest) { depthClosest = depth; } //Do Finger Tracking if (handReady) { if (depth - lastDepthClosest < 60) { pixels[colorIndex + BlueIndex] = 0; pixels[colorIndex + GreenIndex] = 255; pixels[colorIndex + RedIndex] = 255; depthX = GetX(depthIndex); //depthY = GetY(depthIndex); if (!lstHandTop.Contains(depthX)) { if (lstHandTop.Count == 0) { handDepthFirst = depth; } lstHandTop.Add(depthX); pixels[colorIndex + BlueIndex] = 0; pixels[colorIndex + GreenIndex] = 255; pixels[colorIndex + RedIndex] = 0; } } } } else { byte intensity = CalculateIntensityFromDepth(depth); pixels[colorIndex + BlueIndex] = intensity; pixels[colorIndex + GreenIndex] = intensity; pixels[colorIndex + RedIndex] = intensity; //The last point... if (IsInOnePoint(depthIndex, 319, 239)) { lastDepthClosest = depthClosest; int widthSeparator = (MaxDepthDistance - handDepthFirst) / 65-20; /* tbInfo.Text = lstHandTop.Count.ToString() + "," + widthSeparator.ToString() + "," + (lstHandTop.Count > widthSeparator ? "True" : "False") + "," + handDepthFirst.ToString(); */ isMakingAFist = lstHandTop.Count > widthSeparator; if (isMakingAFist) { if (!isMouseDown) { MouseLeftDown(); isMouseDown = true; } } else { if (isMouseDown) { MouseLeftUp(); isMouseDown = false; } } } } } return pixels; }
private void RenderGreenScreen(KinectSensor kinectDevice, ColorImageFrame colorFrame, DepthImageFrame depthFrame) { if (kinectDevice != null && depthFrame != null && colorFrame != null) { int depthPixelIndex; int playerIndex; int colorPixelIndex; ColorImagePoint colorPoint; int colorStride = colorFrame.BytesPerPixel * colorFrame.Width; int bytesPerPixel = 4; byte[] playerImage = new byte[depthFrame.Height * this._GreenScreenImageStride]; int playerImageIndex = 0; depthFrame.CopyPixelDataTo(this._DepthPixelData); colorFrame.CopyPixelDataTo(this._ColorPixelData); for (int depthY = 0; depthY < depthFrame.Height; depthY++) { for (int depthX = 0; depthX < depthFrame.Width; depthX++, playerImageIndex += bytesPerPixel) { depthPixelIndex = depthX + (depthY * depthFrame.Width); playerIndex = this._DepthPixelData[depthPixelIndex] & DepthImageFrame.PlayerIndexBitmask; colorPoint = kinectDevice.MapDepthToColorImagePoint(depthFrame.Format, depthX, depthY, this._DepthPixelData[depthPixelIndex], colorFrame.Format); colorPixelIndex = (colorPoint.X * colorFrame.BytesPerPixel) + (colorPoint.Y * colorStride); playerImage[playerImageIndex] = this._ColorPixelData[colorPixelIndex]; //Blue playerImage[playerImageIndex + 1] = this._ColorPixelData[colorPixelIndex + 1]; //Green playerImage[playerImageIndex + 2] = this._ColorPixelData[colorPixelIndex + 2]; //Red playerImage[playerImageIndex + 3] = 0xFF; } } this._GreenScreenImage.WritePixels(this._GreenScreenImageRect, playerImage, this._GreenScreenImageStride, 0); } }