// draws a body slice line private void DrawBodySlice(BodySliceData bodySlice) { if (bodySlice.isSliceValid && bodySlice.startDepthPoint != Vector2.zero && bodySlice.endDepthPoint != Vector2.zero) { float rectX = foregroundImgRect.xMin; float rectY = foregroundImgRect.yMin; float scaleX = foregroundImgRect.width / depthImageWidth; float scaleY = foregroundImgRect.height / depthImageHeight; float x1 = rectX + (depthScale.x >= 0f ? bodySlice.startDepthPoint.x : depthImageWidth - bodySlice.startDepthPoint.x) * scaleX; float y1 = rectY + (depthScale.y >= 0f ? bodySlice.startDepthPoint.y : depthImageHeight - bodySlice.startDepthPoint.y) * scaleY; float x2 = rectX + (depthScale.x >= 0f ? bodySlice.endDepthPoint.x : depthImageWidth - bodySlice.endDepthPoint.x) * scaleX; float y2 = rectY + (depthScale.y >= 0f ? bodySlice.endDepthPoint.y : depthImageHeight - bodySlice.endDepthPoint.y) * scaleY; KinectInterop.DrawLine((int)x1, (int)y1, (int)x2, (int)y2, 2f, Color.red); } }
// creates body slice data for the given body slice private BodySliceData GetBodySliceParams(BodySlice sliceType, Vector2 middlePoint, bool bSliceOnX, bool bSliceOnY, int maxDepthLength) { BodySliceData sliceData = new BodySliceData(); sliceData.sliceType = sliceType; sliceData.isSliceValid = false; sliceData.depthPointsLength = 0; if (!kinectManager || 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.depthPointsLength = indexDiff1 + indexDiff2 + 1; // check for max length (used by upper legs) if (maxDepthLength > 0 && sliceData.depthPointsLength > maxDepthLength) { if (indexDiff1 > indexDiff2) { indexDiff1 = indexDiff2; } else { indexDiff2 = indexDiff1; } sliceData.depthPointsLength = 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 = kinectManager.MapDepthPointToSpaceCoords(sensorIndex, sliceData.startDepthPoint, depth1, true); // end point int index2 = (int)sliceData.endDepthPoint.y * depthWidth + (int)sliceData.endDepthPoint.x; ushort depth2 = sensorData.depthImage[index2]; sliceData.endKinectPoint = kinectManager.MapDepthPointToSpaceCoords(sensorIndex, sliceData.endDepthPoint, depth2, true); sliceData.startColorPoint = kinectManager.MapDepthPointToColorCoords(sensorIndex, sliceData.startDepthPoint, depth1); sliceData.endColorPoint = kinectManager.MapDepthPointToColorCoords(sensorIndex, sliceData.endDepthPoint, depth2); if (sliceData.startColorPoint.x < 0) { sliceData.startColorPoint.x = 0; } if (sliceData.endColorPoint.x >= sensorData.colorImageWidth) { sliceData.endColorPoint.x = sensorData.colorImageWidth - 1; } sliceData.colorPointsLength = (int)sliceData.endColorPoint.x - (int)sliceData.startColorPoint.x + 1; // diameter sliceData.diameter = (sliceData.endKinectPoint - sliceData.startKinectPoint).magnitude; sliceData.isSliceValid = true; return(sliceData); }
// creates body slice data for user width 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.sliceType = BodySlice.WIDTH; 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 = kinectManager.MapDepthPointToSpaceCoords(sensorIndex, sliceData.startDepthPoint, depth1, true); int index2 = (int)posRight.y * depthWidth + (int)posRight.x; ushort depth2 = sensorData.depthImage[index2]; sliceData.endKinectPoint = kinectManager.MapDepthPointToSpaceCoords(sensorIndex, sliceData.endDepthPoint, depth2, true); sliceData.startColorPoint = kinectManager.MapDepthPointToColorCoords(sensorIndex, sliceData.startDepthPoint, depth1); sliceData.endColorPoint = kinectManager.MapDepthPointToColorCoords(sensorIndex, sliceData.endDepthPoint, depth2); if (sliceData.startColorPoint.x < 0) { sliceData.startColorPoint.x = 0; } if (sliceData.endColorPoint.x >= sensorData.colorImageWidth) { sliceData.endColorPoint.x = sensorData.colorImageWidth - 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); }
// creates body slice data for user height 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.sliceType = BodySlice.HEIGHT; sliceData.isSliceValid = false; if (posBottom.y >= 0) { sliceData.startDepthPoint = posTop; sliceData.endDepthPoint = posBottom; sliceData.depthPointsLength = (int)posBottom.y - (int)posTop.y + 1; int index1 = (int)posTop.y * depthWidth + (int)posTop.x; ushort depth1 = sensorData.depthImage[index1]; sliceData.startKinectPoint = kinectManager.MapDepthPointToSpaceCoords(sensorIndex, sliceData.startDepthPoint, depth1, true); int index2 = (int)posBottom.y * depthWidth + (int)posBottom.x; ushort depth2 = sensorData.depthImage[index2]; sliceData.endKinectPoint = kinectManager.MapDepthPointToSpaceCoords(sensorIndex, sliceData.endDepthPoint, depth2, true); sliceData.startColorPoint = kinectManager.MapDepthPointToColorCoords(sensorIndex, sliceData.startDepthPoint, depth1); sliceData.endColorPoint = kinectManager.MapDepthPointToColorCoords(sensorIndex, sliceData.endDepthPoint, depth2); if (sliceData.startColorPoint.y < 0) { sliceData.startColorPoint.y = 0; } if (sliceData.endColorPoint.y >= sensorData.colorImageHeight) { sliceData.endColorPoint.y = sensorData.colorImageHeight - 1; } sliceData.colorPointsLength = (int)sliceData.endColorPoint.y - (int)sliceData.startColorPoint.y + 1; // 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); }