private void KinectAllFramesReady(object sender, AllFramesReadyEventArgs e) { KinectSensor sensor = sender as KinectSensor; foreach (var skeletonCanvas in this.skeletonCanvases) { skeletonCanvas.Skeleton = null; } // Have we already been "shut down" by the user of this viewer, // or has the SkeletonStream been disabled since this event was posted? if ((null == this.KinectSensorManager) || (null == sensor) || (null == sensor.SkeletonStream) || !sensor.SkeletonStream.IsEnabled) { return; } bool haveSkeletonData = false; long frameTimeStamp = -1; using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame()) { if (skeletonFrame != null) { if ((this.skeletonData == null) || (this.skeletonData.Length != skeletonFrame.SkeletonArrayLength)) { this.skeletonData = new Skeleton[skeletonFrame.SkeletonArrayLength]; } skeletonFrame.CopySkeletonDataTo(this.skeletonData); frameTimeStamp = skeletonFrame.Timestamp; haveSkeletonData = true; } } int trackedIndex = -1; // find the first tracked skeleton and set var trackedSkeleton accordingly for (int i = 0; i < skeletonData.Length; i++) { if (skeletonData[i].TrackingState.Equals(SkeletonTrackingState.Tracked)) { trackedIndex = i; break; } } bool isFullyTracked = false; if (isMeasuring && trackedIndex > -1) { // check to see if the skeleton @ trackedIndex is fully tracked if (fullyTrackedMapping == null && IsFullyTracked(skeletonData[trackedIndex])) { isFullyTracked = true; } SkeletonMeasurer measurer = new SkeletonMeasurer(skeletonData[trackedIndex]); measurer.determineMeasurements(); AddMeasurementsToBuffer(measurer.TestMeasurements); skeletonBuffer.Add(ObjectCopier.Clone<Skeleton>(skeletonData[trackedIndex])); frameTimeStampBuffer.Add(frameTimeStamp); } if (haveSkeletonData) { ColorImageFormat colorFormat = ColorImageFormat.Undefined; int colorWidth = 0; int colorHeight = 0; DepthImageFormat depthFormat = DepthImageFormat.Undefined; int depthWidth = 0; int depthHeight = 0; switch (this.ImageType) { case ImageType.Color: // Retrieve the current color format, from the frame if present, and from the sensor if not. using (ColorImageFrame colorImageFrame = e.OpenColorImageFrame()) { if (null != colorImageFrame) { colorFormat = colorImageFrame.Format; colorWidth = colorImageFrame.Width; colorHeight = colorImageFrame.Height; } else if (null != sensor.ColorStream) { colorFormat = sensor.ColorStream.Format; colorWidth = sensor.ColorStream.FrameWidth; colorHeight = sensor.ColorStream.FrameHeight; } } break; case ImageType.Depth: // Retrieve the current depth format, from the frame if present, and from the sensor if not. using (DepthImageFrame depthImageFrame = e.OpenDepthImageFrame()) { if (null != depthImageFrame) { depthFormat = depthImageFrame.Format; depthWidth = depthImageFrame.Width; depthHeight = depthImageFrame.Height; } else if (null != sensor.DepthStream) { depthFormat = sensor.DepthStream.Format; depthWidth = sensor.DepthStream.FrameWidth; depthHeight = sensor.DepthStream.FrameHeight; } } break; } for (int i = 0; i < this.skeletonData.Length && i < this.skeletonCanvases.Count; i++) { var skeleton = this.skeletonData[i]; var skeletonCanvas = this.skeletonCanvases[i]; var jointMapping = this.jointMappings[i]; jointMapping.Clear(); try { // Transform the data into the correct space // For each joint, we determine the exact X/Y coordinates for the target view foreach (Joint joint in skeleton.Joints) { Point mappedPoint = Get2DPosition( sensor, this.ImageType, this.RenderSize, joint.Position, colorFormat, colorWidth, colorHeight, depthFormat, depthWidth, depthHeight); jointMapping[joint.JointType] = new JointMapping { Joint = joint, MappedPoint = mappedPoint }; } } catch (UnauthorizedAccessException) { // Kinect is no longer available. return; } // Look up the center point Point centerPoint = Get2DPosition( sensor, this.ImageType, this.RenderSize, skeleton.Position, colorFormat, colorWidth, colorHeight, depthFormat, depthWidth, depthHeight); // Scale the skeleton thickness // 1.0 is the desired size at 640 width double scale = this.RenderSize.Width / 640; skeletonCanvas.Skeleton = skeleton; skeletonCanvas.JointMappings = jointMapping; skeletonCanvas.Center = centerPoint; skeletonCanvas.ScaleFactor = scale; } if (isFullyTracked) { fullyTrackedMapping = new Dictionary<JointType, JointMapping>(); foreach (JointType type in jointMappings[trackedIndex].Keys) { fullyTrackedMapping[type] = new JointMapping(); fullyTrackedMapping[type].Joint = jointMappings[trackedIndex][type].Joint; fullyTrackedMapping[type].MappedPoint = jointMappings[trackedIndex][type].MappedPoint; } } } }
private void KinectAllFramesReady(object sender, AllFramesReadyEventArgs e) { KinectSensor sensor = sender as KinectSensor; foreach (var skeletonCanvas in this.skeletonCanvases) { skeletonCanvas.Skeleton = null; } // Have we already been "shut down" by the user of this viewer, // or has the SkeletonStream been disabled since this event was posted? if ((null == this.KinectSensorManager) || (null == sensor) || (null == sensor.SkeletonStream) || !sensor.SkeletonStream.IsEnabled) { return; } bool haveSkeletonData = false; long frameTimeStamp = -1; using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame()) { if (skeletonFrame != null) { if ((this.skeletonData == null) || (this.skeletonData.Length != skeletonFrame.SkeletonArrayLength)) { this.skeletonData = new Skeleton[skeletonFrame.SkeletonArrayLength]; } skeletonFrame.CopySkeletonDataTo(this.skeletonData); frameTimeStamp = skeletonFrame.Timestamp; haveSkeletonData = true; } } int trackedIndex = -1; // find the first tracked skeleton and set var trackedSkeleton accordingly for (int i = 0; i < skeletonData.Length; i++) { if (skeletonData[i].TrackingState.Equals(SkeletonTrackingState.Tracked)) { trackedIndex = i; break; } } bool isFullyTracked = false; if (isMeasuring && trackedIndex > -1) { // check to see if the skeleton @ trackedIndex is fully tracked if (fullyTrackedMapping == null && IsFullyTracked(skeletonData[trackedIndex])) { isFullyTracked = true; } SkeletonMeasurer measurer = new SkeletonMeasurer(skeletonData[trackedIndex]); measurer.determineMeasurements(); AddMeasurementsToBuffer(measurer.TestMeasurements); skeletonBuffer.Add(ObjectCopier.Clone <Skeleton>(skeletonData[trackedIndex])); frameTimeStampBuffer.Add(frameTimeStamp); } if (haveSkeletonData) { ColorImageFormat colorFormat = ColorImageFormat.Undefined; int colorWidth = 0; int colorHeight = 0; DepthImageFormat depthFormat = DepthImageFormat.Undefined; int depthWidth = 0; int depthHeight = 0; switch (this.ImageType) { case ImageType.Color: // Retrieve the current color format, from the frame if present, and from the sensor if not. using (ColorImageFrame colorImageFrame = e.OpenColorImageFrame()) { if (null != colorImageFrame) { colorFormat = colorImageFrame.Format; colorWidth = colorImageFrame.Width; colorHeight = colorImageFrame.Height; } else if (null != sensor.ColorStream) { colorFormat = sensor.ColorStream.Format; colorWidth = sensor.ColorStream.FrameWidth; colorHeight = sensor.ColorStream.FrameHeight; } } break; case ImageType.Depth: // Retrieve the current depth format, from the frame if present, and from the sensor if not. using (DepthImageFrame depthImageFrame = e.OpenDepthImageFrame()) { if (null != depthImageFrame) { depthFormat = depthImageFrame.Format; depthWidth = depthImageFrame.Width; depthHeight = depthImageFrame.Height; } else if (null != sensor.DepthStream) { depthFormat = sensor.DepthStream.Format; depthWidth = sensor.DepthStream.FrameWidth; depthHeight = sensor.DepthStream.FrameHeight; } } break; } for (int i = 0; i < this.skeletonData.Length && i < this.skeletonCanvases.Count; i++) { var skeleton = this.skeletonData[i]; var skeletonCanvas = this.skeletonCanvases[i]; var jointMapping = this.jointMappings[i]; jointMapping.Clear(); try { // Transform the data into the correct space // For each joint, we determine the exact X/Y coordinates for the target view foreach (Joint joint in skeleton.Joints) { Point mappedPoint = Get2DPosition( sensor, this.ImageType, this.RenderSize, joint.Position, colorFormat, colorWidth, colorHeight, depthFormat, depthWidth, depthHeight); jointMapping[joint.JointType] = new JointMapping { Joint = joint, MappedPoint = mappedPoint }; } } catch (UnauthorizedAccessException) { // Kinect is no longer available. return; } // Look up the center point Point centerPoint = Get2DPosition( sensor, this.ImageType, this.RenderSize, skeleton.Position, colorFormat, colorWidth, colorHeight, depthFormat, depthWidth, depthHeight); // Scale the skeleton thickness // 1.0 is the desired size at 640 width double scale = this.RenderSize.Width / 640; skeletonCanvas.Skeleton = skeleton; skeletonCanvas.JointMappings = jointMapping; skeletonCanvas.Center = centerPoint; skeletonCanvas.ScaleFactor = scale; } if (isFullyTracked) { fullyTrackedMapping = new Dictionary <JointType, JointMapping>(); foreach (JointType type in jointMappings[trackedIndex].Keys) { fullyTrackedMapping[type] = new JointMapping(); fullyTrackedMapping[type].Joint = jointMappings[trackedIndex][type].Joint; fullyTrackedMapping[type].MappedPoint = jointMappings[trackedIndex][type].MappedPoint; } } } }