Esempio n. 1
0
    // clips color camera image to the dimensions of the user
    private Texture2D ClipUserImage(long userId, Texture2D texImage)
    {
        BodySlicer slicer = BodySlicer.Instance;

        if (slicer && texImage)
        {
            if (slicer.getCalibratedUserId() != userId)
            {
                slicer.OnCalibrationSuccess(userId);
            }

            BodySliceData sliceH = slicer.getBodySliceData(BodySlice.HEIGHT);
            BodySliceData sliceW = slicer.getBodySliceData(BodySlice.WIDTH);

            if (sliceH.isSliceValid && sliceW.isSliceValid)
            {
                int rectX = (int)sliceW.startColorPoint.x;
                int rectW = sliceW.colorPointsLength;

                int rectY = (int)sliceH.startColorPoint.y;
                int rectH = sliceH.colorPointsLength;

                Texture2D texClipped = new Texture2D(rectW, rectH, TextureFormat.ARGB32, false);
                texClipped.SetPixels(texImage.GetPixels(rectX, rectY, rectW, rectH));

                return(texClipped);
            }
        }

        return(texImage);
    }
Esempio n. 2
0
    // Update is called once per frame
    void Update()
    {
        if (manager && manager.IsInitialized())
        {
            Texture2D depthImage = manager ? manager.GetUsersLblTex() : null;

            if (bodySlicer && bodySlicer.getLastFrameTime() != lastFrameTime)
            {
                lastFrameTime = bodySlicer.getLastFrameTime();
                int sliceCount = bodySlicer.getBodySliceCount();

                if (depthImage)
                {
                    //depthImage = GameObject.Instantiate(depthImage) as Texture2D;

                    for (int i = 0; i < sliceCount; i++)
                    {
                        BodySliceData bodySlice = bodySlicer.getBodySliceData((BodySlice)i);

                        if (depthImage && bodySlice.isSliceValid &&
                            bodySlice.startDepthPoint != Vector2.zero && bodySlice.endDepthPoint != Vector2.zero)
                        {
                            KinectInterop.DrawLine(depthImage, (int)bodySlice.startDepthPoint.x, (int)bodySlice.startDepthPoint.y,
                                                   (int)bodySlice.endDepthPoint.x, (int)bodySlice.endDepthPoint.y, Color.red);
                        }
                    }

                    depthImage.Apply();
                }

                if (statusText)
                {
                    if (bodySlicer.getCalibratedUserId() != 0)
                    {
                        userHeight = !float.IsNaN(userHeight) ? Mathf.Lerp(userHeight, bodySlicer.getUserHeight(), smoothFactor * Time.deltaTime) : bodySlicer.getUserHeight();
                        string sUserInfo = string.Format("User {0} Height: {1:F2} m", bodySlicer.playerIndex, userHeight);

                        userW1 = !float.IsNaN(userW1) ? Mathf.Lerp(userW1, bodySlicer.getSliceWidth(BodySlice.TORSO_1), smoothFactor * Time.deltaTime) : bodySlicer.getSliceWidth(BodySlice.TORSO_1);
                        userW2 = !float.IsNaN(userW2) ? Mathf.Lerp(userW2, bodySlicer.getSliceWidth(BodySlice.TORSO_2), smoothFactor * Time.deltaTime) : bodySlicer.getSliceWidth(BodySlice.TORSO_2);
                        userW3 = !float.IsNaN(userW3) ? Mathf.Lerp(userW3, bodySlicer.getSliceWidth(BodySlice.TORSO_3), smoothFactor * Time.deltaTime) : bodySlicer.getSliceWidth(BodySlice.TORSO_3);
                        userW4 = !float.IsNaN(userW4) ? Mathf.Lerp(userW4, bodySlicer.getSliceWidth(BodySlice.TORSO_4), smoothFactor * Time.deltaTime) : bodySlicer.getSliceWidth(BodySlice.TORSO_4);

                        sUserInfo += string.Format("\n\nTorso-4: {3:F2} m\nTorso-3: {2:F2} m\nTorso-2: {1:F2} m\nTorso-1: {0:F2} m", userW1, userW2, userW3, userW4);

                        statusText.text = sUserInfo;
                    }
                    else
                    {
                        statusText.text = string.Format("User {0} not found", bodySlicer.playerIndex);;
                    }
                }
            }

            if (backgroundImage)
            {
                backgroundImage.texture = depthImage;
            }
        }
    }
Esempio n. 3
0
 private void DrawBodySlice(BodySliceData bodySlice)
 {
     if (depthImage && bodySlice.isSliceValid &&
         bodySlice.startDepthPoint != Vector2.zero && bodySlice.endDepthPoint != Vector2.zero)
     {
         KinectInterop.DrawLine(depthImage, (int)bodySlice.startDepthPoint.x, (int)bodySlice.startDepthPoint.y,
                                (int)bodySlice.endDepthPoint.x, (int)bodySlice.endDepthPoint.y, Color.red);
     }
 }
Esempio n. 4
0
 private void DrawBodySlice(Texture2D imageTex, BodySliceData bodySlice)
 {
     if (imageTex && bodySlice.isSliceValid &&
         bodySlice.startDepthPoint != Vector2.zero && bodySlice.endDepthPoint != Vector2.zero)
     {
         KinectInterop.DrawLine(imageTex, (int)bodySlice.startDepthPoint.x, (int)bodySlice.startDepthPoint.y,
                                (int)bodySlice.endDepthPoint.x, (int)bodySlice.endDepthPoint.y, Color.red);
     }
 }
Esempio n. 5
0
    private BodySliceData GetBodySliceParams(Vector2 middlePoint, bool bSliceOnX, bool bSliceOnY, int maxDepthLength)
    {
        BodySliceData sliceData = new BodySliceData();

        sliceData.isSliceValid = false;
        sliceData.depthsLength = 0;

        if (!manager || middlePoint == Vector2.zero)
        {
            return(sliceData);
        }
        if (!bSliceOnX && !bSliceOnY)
        {
            return(sliceData);
        }

        middlePoint.x = Mathf.FloorToInt(middlePoint.x + 0.5f);
        middlePoint.y = Mathf.FloorToInt(middlePoint.y + 0.5f);

        int depthWidth  = sensorData.depthImageWidth;
        int depthHeight = sensorData.depthImageHeight;

        int  indexMid  = (int)middlePoint.y * depthWidth + (int)middlePoint.x;
        byte userIndex = sensorData.bodyIndexImage[indexMid];

        if (userIndex != userBodyIndex)
        {
            return(sliceData);
        }

        sliceData.startDepthPoint = middlePoint;
        sliceData.endDepthPoint   = middlePoint;

        int indexDiff1 = 0;
        int indexDiff2 = 0;

        if (bSliceOnX)
        {
            // min-max
            int minIndex = (int)middlePoint.y * depthWidth;
            int maxIndex = (int)(middlePoint.y + 1) * depthWidth;

            // horizontal left
            int stepIndex = -1;
            indexDiff1 = TrackSliceInDirection(indexMid, stepIndex, minIndex, maxIndex, userIndex);

            // horizontal right
            stepIndex  = 1;
            indexDiff2 = TrackSliceInDirection(indexMid, stepIndex, minIndex, maxIndex, userIndex);
        }
        else if (bSliceOnY)
        {
            // min-max
            int minIndex = 0;
            int maxIndex = depthHeight * depthWidth;

            // vertical up
            int stepIndex = -depthWidth;
            indexDiff1 = TrackSliceInDirection(indexMid, stepIndex, minIndex, maxIndex, userIndex);

            // vertical down
            stepIndex  = depthWidth;
            indexDiff2 = TrackSliceInDirection(indexMid, stepIndex, minIndex, maxIndex, userIndex);
        }

        // calculate depth length
        sliceData.depthsLength = indexDiff1 + indexDiff2 + 1;

        // check for max length (used by upper legs)
        if (maxDepthLength > 0 && sliceData.depthsLength > maxDepthLength)
        {
//			indexDiff1 = (int)((float)indexDiff1 * maxDepthLength / sliceData.depthsLength);
//			indexDiff2 = (int)((float)indexDiff2 * maxDepthLength / sliceData.depthsLength);

            if (indexDiff1 > indexDiff2)
            {
                indexDiff1 = indexDiff2;
            }
            else
            {
                indexDiff2 = indexDiff1;
            }

            sliceData.depthsLength = indexDiff1 + indexDiff2 + 1;
        }

        // set start and end depth points
        if (bSliceOnX)
        {
            sliceData.startDepthPoint.x -= indexDiff1;
            sliceData.endDepthPoint.x   += indexDiff2;
        }
        else if (bSliceOnY)
        {
            sliceData.startDepthPoint.y -= indexDiff1;
            sliceData.endDepthPoint.y   += indexDiff2;
        }

        // start point
        int    index1 = (int)sliceData.startDepthPoint.y * depthWidth + (int)sliceData.startDepthPoint.x;
        ushort depth1 = sensorData.depthImage[index1];

        sliceData.startKinectPoint = manager.MapDepthPointToSpaceCoords(sliceData.startDepthPoint, depth1, true);

        // end point
        int    index2 = (int)sliceData.endDepthPoint.y * depthWidth + (int)sliceData.endDepthPoint.x;
        ushort depth2 = sensorData.depthImage[index2];

        sliceData.endKinectPoint = manager.MapDepthPointToSpaceCoords(sliceData.endDepthPoint, depth2, true);

        // diameter
        sliceData.diameter     = (sliceData.endKinectPoint - sliceData.startKinectPoint).magnitude;
        sliceData.isSliceValid = true;

//		// get depths
//		sliceData.depths = new ushort[sliceData.depthsLength];
//		int stepDepthIndex = 1;
//
//		if(bSliceOnX)
//		{
//			stepDepthIndex = 1;
//		}
//		else if(bSliceOnY)
//		{
//			stepDepthIndex = depthWidth;
//		}
//
//		for(int i = index1, d = 0; i <= index2; i+= stepDepthIndex, d++)
//		{
//			sliceData.depths[d] = sensorData.depthImage[i];
//		}

        return(sliceData);
    }
Esempio n. 6
0
    private BodySliceData GetUserHeightParams(Vector2 pointSpineBase)
    {
        int depthLength = sensorData.depthImage.Length;
        int depthWidth  = sensorData.depthImageWidth;
        int depthHeight = sensorData.depthImageHeight;

        Vector2 posTop = new Vector2(0, depthHeight);

        for (int i = 0, x = 0, y = 0; i < depthLength; i++)
        {
            if (sensorData.bodyIndexImage [i] == userBodyIndex)
            {
                //if (posTop.y > y)
                posTop = new Vector2(x, y);
                break;
            }

            x++;
            if (x >= depthWidth)
            {
                x = 0;
                y++;
            }
        }

        Vector2 posBottom = new Vector2(0, -1);

        for (int i = depthLength - 1, x = depthWidth - 1, y = depthHeight - 1; i >= 0; i--)
        {
            if (sensorData.bodyIndexImage [i] == userBodyIndex)
            {
                //if (posBottom.y < y)
                posBottom = new Vector2(x, y);
                break;
            }

            x--;
            if (x < 0)
            {
                x = depthWidth - 1;
                y--;
            }
        }

        BodySliceData sliceData = new BodySliceData();

        sliceData.isSliceValid = false;

        if (posBottom.y >= 0)
        {
            sliceData.startDepthPoint = posTop;
            sliceData.endDepthPoint   = posBottom;
            sliceData.depthsLength    = (int)posBottom.y - (int)posTop.y + 1;

            int    index1 = (int)posTop.y * depthWidth + (int)posTop.x;
            ushort depth1 = sensorData.depthImage[index1];
            sliceData.startKinectPoint = manager.MapDepthPointToSpaceCoords(sliceData.startDepthPoint, depth1, true);

            int    index2 = (int)posBottom.y * depthWidth + (int)posBottom.x;
            ushort depth2 = sensorData.depthImage[index2];
            sliceData.endKinectPoint = manager.MapDepthPointToSpaceCoords(sliceData.endDepthPoint, depth2, true);

            // correct x-positions of depth points
            sliceData.startDepthPoint.x = pointSpineBase.x;
            sliceData.endDepthPoint.x   = pointSpineBase.x;

            sliceData.diameter     = (sliceData.endKinectPoint - sliceData.startKinectPoint).magnitude;
            sliceData.isSliceValid = true;
        }

        return(sliceData);
    }
Esempio n. 7
0
    private BodySliceData GetUserWidthParams(Vector2 pointSpineBase)
    {
        int depthLength = sensorData.depthImage.Length;
        int depthWidth  = sensorData.depthImageWidth;
        //int depthHeight = sensorData.depthImageHeight;

        Vector2 posLeft  = new Vector2(depthWidth, 0);
        Vector2 posRight = new Vector2(-1, 0);

        for (int i = 0, x = 0, y = 0; i < depthLength; i++)
        {
            if (sensorData.bodyIndexImage [i] == userBodyIndex)
            {
                if (posLeft.x > x)
                {
                    posLeft = new Vector2(x, y);
                }
                if (posRight.x < x)
                {
                    posRight = new Vector2(x, y);
                }
            }

            x++;
            if (x >= depthWidth)
            {
                x = 0;
                y++;
            }
        }

        BodySliceData sliceData = new BodySliceData();

        sliceData.isSliceValid = false;

        if (posRight.x >= 0)
        {
            sliceData.startDepthPoint   = posLeft;
            sliceData.endDepthPoint     = posRight;
            sliceData.depthPointsLength = (int)posRight.x - (int)posLeft.x + 1;

            int    index1 = (int)posLeft.y * depthWidth + (int)posLeft.x;
            ushort depth1 = sensorData.depthImage[index1];
            sliceData.startKinectPoint = manager.MapDepthPointToSpaceCoords(sliceData.startDepthPoint, depth1, true);

            int    index2 = (int)posRight.y * depthWidth + (int)posRight.x;
            ushort depth2 = sensorData.depthImage[index2];
            sliceData.endKinectPoint = manager.MapDepthPointToSpaceCoords(sliceData.endDepthPoint, depth2, true);

            sliceData.startColorPoint = manager.MapDepthPointToColorCoords(sliceData.startDepthPoint, depth1);
            sliceData.endColorPoint   = manager.MapDepthPointToColorCoords(sliceData.endDepthPoint, depth2);

            if (sliceData.startColorPoint.x < 0)
            {
                sliceData.startColorPoint.x = 0;
            }
            if (sliceData.endColorPoint.x >= manager.GetColorImageWidth())
            {
                sliceData.endColorPoint.x = manager.GetColorImageWidth() - 1;
            }
            sliceData.colorPointsLength = (int)sliceData.endColorPoint.x - (int)sliceData.startColorPoint.x + 1;

            // correct y-positions of depth points
            sliceData.startDepthPoint.y = pointSpineBase.y;
            sliceData.endDepthPoint.y   = pointSpineBase.y;

            sliceData.diameter     = (sliceData.endKinectPoint - sliceData.startKinectPoint).magnitude;
            sliceData.isSliceValid = true;
        }

        return(sliceData);
    }