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));
    }
    // returns the color map position for a depth 2d position
    public Vector2 GetColorMapPosForDepthPos(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);
    }
    // Update the User Map
    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);

        if(!DisplaySkeletonLines)
        {
            usersLblTex.Apply();
        }
    }
Esempio n. 4
0
    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();
    }
    //---------------------------------- END OF PUBLIC FUNCTIONS -----------------------------------------------------------//

    void Awake()
    {
        KinectCoordinatesAdjustment = new KinectWrapper.NuiImageViewArea
        {
            eDigitalZoom = 0,
            lCenterX     = 0,
            lCenterY     = 0
        };

        int hr = 0;

        try
        {
            hr = KinectWrapper.NuiInitialize(KinectWrapper.NuiInitializeFlags.UsesSkeleton |
                                             KinectWrapper.NuiInitializeFlags.UsesDepthAndPlayerIndex |
                                             KinectWrapper.NuiInitializeFlags.UsesColor);
            if (hr != 0)
            {
                throw new Exception("NuiInitialize Failed");
            }

            depthStreamHandle = IntPtr.Zero;
            hr = KinectWrapper.NuiImageStreamOpen(KinectWrapper.NuiImageType.DepthAndPlayerIndex,
                                                  KinectWrapper.Constants.DepthImageResolution, 0, 2, IntPtr.Zero, ref depthStreamHandle);
            if (hr != 0)
            {
                throw new Exception("Cannot open depth stream");
            }

            colorStreamHandle = IntPtr.Zero;
            hr = KinectWrapper.NuiImageStreamOpen(KinectWrapper.NuiImageType.Color,
                                                  KinectWrapper.Constants.ColorImageResolution, 0, 2, IntPtr.Zero, ref colorStreamHandle);
            if (hr != 0)
            {
                throw new Exception("Cannot open color stream");
            }

            // set kinect elevation angle
            KinectWrapper.NuiCameraElevationSetAngle(kinectMotorAngle);

            //create the transform matrix that converts from kinect-space to world-space
            Quaternion quatTiltAngle = new Quaternion();
            quatTiltAngle.eulerAngles = new Vector3(-kinectMotorAngle, 0.0f, 0.0f);

            // transform matrix - kinect to world
            kinectToWorld.SetTRS(new Vector3(0.0f, kinectPlacementHeight, 0.0f), quatTiltAngle, Vector3.one);
            flipMatrix       = Matrix4x4.identity;
            flipMatrix[2, 2] = -1;
            DontDestroyOnLoad(gameObject);
        }
        catch (DllNotFoundException e)
        {
            string message = "Please check the Kinect SDK installation.";
            Debug.LogError(message);
            Debug.LogError(e.ToString());
            if (UILog != null)
            {
                UILog.text = message;
            }

            return;
        }
        catch (Exception e)
        {
            string message = e.Message + " - " + KinectWrapper.GetNuiErrorString(hr);
            Debug.LogError(message);
            Debug.LogError(e.ToString());
            if (UILog != null)
            {
                UILog.text = message;
            }
            return;
        }

        InitializeFeeds();
        InitializeGradedDepthStreamColors();

        instance = this;

        if (UILog != null)
        {
            UILog.text = "Kinect is initialized";
        }

        Debug.Log("Kinect is initialized");
        kinectInitialized = true;
    }