void GetColorForRegsiteredColorStreamPixel(ref int index, ref KinectData sourceKinectData, ref Color32 result) { result = new Color32(0, 0, 0, 255); if (fullResolutionKinectData.RawColorStreamColors != null) { int x = index % sourceKinectData.Width; int y = index / sourceKinectData.Width; int cx, cy; int hr = KinectWrapper.NuiImageGetColorPixelCoordinatesFromDepthPixelAtResolution( KinectWrapper.Constants.ColorImageResolution, KinectWrapper.Constants.DepthImageResolution, ref KinectCoordinatesAdjustment, x, y, sourceKinectData.RawDepths[index], out cx, out cy); if (hr == 0) { int colorIndex = cy * sourceKinectData.Width + cx; if (colorIndex >= 0 && colorIndex < sourceKinectData.Size) { result = sourceKinectData.RawColorStreamColors [colorIndex]; } } } return; }
public Vector2 GetColorCorrespondingToDepthPixelPosition(Vector2 posDepth) { int cx, cy; KinectWrapper.NuiImageViewArea pcViewArea = new KinectWrapper.NuiImageViewArea { eDigitalZoom = 0, lCenterX = 0, lCenterY = 0 }; KinectWrapper.NuiImageGetColorPixelCoordinatesFromDepthPixelAtResolution( KinectWrapper.Constants.ColorImageResolution, KinectWrapper.Constants.DepthImageResolution, ref pcViewArea, (int)posDepth.x, (int)posDepth.y, GetDepthForPixel((int)posDepth.x, (int)posDepth.y), out cx, out cy); return(new Vector2(cx, cy)); }
void UpdateUserMap() { int numOfPoints = 0; Array.Clear(usersHistogramMap, 0, usersHistogramMap.Length); // Calculate cumulative histogram for depth for (int i = 0; i < usersMapSize; i++) { // Only calculate for depth that contains users if ((usersDepthMap[i] & 7) != 0) { ushort userDepth = (ushort)(usersDepthMap[i] >> 3); usersHistogramMap[userDepth]++; numOfPoints++; } } if (numOfPoints > 0) { for (int i = 1; i < usersHistogramMap.Length; i++) { usersHistogramMap[i] += usersHistogramMap[i - 1]; } for (int i = 0; i < usersHistogramMap.Length; i++) { usersHistogramMap[i] = 1.0f - (usersHistogramMap[i] / numOfPoints); } } // dummy structure needed by the coordinate mapper KinectWrapper.NuiImageViewArea pcViewArea = new KinectWrapper.NuiImageViewArea { eDigitalZoom = 0, lCenterX = 0, lCenterY = 0 }; // Create the actual users texture based on label map and depth histogram Color32 clrClear = Color.clear; for (int i = 0; i < usersMapSize; i++) { // Flip the texture as we convert label map to color array int flipIndex = i; // usersMapSize - i - 1; ushort userMap = (ushort)(usersDepthMap[i] & 7); ushort userDepth = (ushort)(usersDepthMap[i] >> 3); ushort nowUserPixel = userMap != 0 ? (ushort)((userMap << 13) | userDepth) : userDepth; ushort wasUserPixel = usersPrevState[flipIndex]; // draw only the changed pixels if (nowUserPixel != wasUserPixel) { usersPrevState[flipIndex] = nowUserPixel; if (userMap == 0) { usersMapColors[flipIndex] = clrClear; } else { if (colorImage != null) { int x = i % KinectWrapper.Constants.DepthImageWidth; int y = i / KinectWrapper.Constants.DepthImageWidth; int cx, cy; int hr = KinectWrapper.NuiImageGetColorPixelCoordinatesFromDepthPixelAtResolution( KinectWrapper.Constants.ColorImageResolution, KinectWrapper.Constants.DepthImageResolution, ref pcViewArea, x, y, usersDepthMap[i], out cx, out cy); if (hr == 0) { int colorIndex = cx + cy * KinectWrapper.Constants.ColorImageWidth; //colorIndex = usersMapSize - colorIndex - 1; if (colorIndex >= 0 && colorIndex < usersMapSize) { Color32 colorPixel = colorImage[colorIndex]; usersMapColors[flipIndex] = colorPixel; // new Color(colorPixel.r / 256f, colorPixel.g / 256f, colorPixel.b / 256f, 0.9f); usersMapColors[flipIndex].a = 230; // 0.9f } } } else { // Create a blending color based on the depth histogram float histDepth = usersHistogramMap[userDepth]; Color c = new Color(histDepth, histDepth, histDepth, 0.9f); switch (userMap % 4) { case 0: usersMapColors[flipIndex] = Color.red * c; break; case 1: usersMapColors[flipIndex] = Color.green * c; break; case 2: usersMapColors[flipIndex] = Color.blue * c; break; case 3: usersMapColors[flipIndex] = Color.magenta * c; break; } } } } } // Draw it! usersLblTex.SetPixels32(usersMapColors); usersLblTex.Apply(); }