private void Button_Click(object sender, RoutedEventArgs e) { kinect.Connect(); kinect.StartSkeletonStream(); SkeletonDrawer skeletonDrawer = new SkeletonDrawer(kinect.CoordinateMapper); kinect.SkeletonReady += skeletonDrawer.SensorSkeletonFrameReady; Image.Source = skeletonDrawer.getImageSource(); }
private void Window_Initialized(object sender, EventArgs e) { string configPath = Properties.Settings.Default.ONIConfig; Title += " - " + configPath; try { if (!File.Exists(configPath)) throw new ApplicationException("Config file '" + configPath + "' does not exist."); _labelDataBrush = Resources["labelData"] as ImageBrush; _skeletonDrawer = new SkeletonDrawer(depthGrid); _skeleton = new Dictionary<int, Dictionary<SkeletonJoint, SkeletonJointPosition>>(); _sensor = new Sensor(configPath); Console.WriteLine("Sensor initialized successfully."); _skeletonCapability = _sensor.UserGenerator.SkeletonCapability; _skeletonCapability.SetSkeletonProfile(SkeletonProfile.All); _skeletonCapability.SetSmoothing(Properties.Settings.Default.SkeletonSmoothing); _sensor.GeneratorUpdate += delegate(object s, EventArgs args) { Action action = () => { _labelDataBrush.ImageSource = _sensor.LabelBitmap; depthImage.Source = _sensor.DepthBitmap; rgbImage.Source = _sensor.RGBBitmap; if (Properties.Settings.Default.DisplaySkeleton && _skeleton.Count > 0) { var users = _skeleton.Keys; Func<int, SkeletonJoint, SkeletonJointPosition> jointPosition = (int user, SkeletonJoint joint) => { SkeletonJointPosition position = _skeletonCapability.GetSkeletonJointPosition(user, joint); if (position.Position.Z == 0) position.Confidence = 0; else position.Position = _sensor.DepthGenerator.ConvertRealWorldToProjective(position.Position); return position; }; foreach (int user in users) { _skeleton[user][SkeletonJoint.Head] = jointPosition(user, SkeletonJoint.Head); _skeleton[user][SkeletonJoint.Neck] = jointPosition(user, SkeletonJoint.Neck); _skeleton[user][SkeletonJoint.Torso] = jointPosition(user, SkeletonJoint.Torso); _skeleton[user][SkeletonJoint.LeftShoulder] = jointPosition(user, SkeletonJoint.LeftShoulder); _skeleton[user][SkeletonJoint.LeftElbow] = jointPosition(user, SkeletonJoint.LeftElbow); _skeleton[user][SkeletonJoint.LeftHand] = jointPosition(user, SkeletonJoint.LeftHand); _skeleton[user][SkeletonJoint.RightShoulder] = jointPosition(user, SkeletonJoint.RightShoulder); _skeleton[user][SkeletonJoint.RightElbow] = jointPosition(user, SkeletonJoint.RightElbow); _skeleton[user][SkeletonJoint.RightHand] = jointPosition(user, SkeletonJoint.RightHand); _skeleton[user][SkeletonJoint.LeftHip] = jointPosition(user, SkeletonJoint.LeftHip); _skeleton[user][SkeletonJoint.LeftKnee] = jointPosition(user, SkeletonJoint.LeftKnee); _skeleton[user][SkeletonJoint.LeftFoot] = jointPosition(user, SkeletonJoint.LeftFoot); _skeleton[user][SkeletonJoint.RightHip] = jointPosition(user, SkeletonJoint.RightHip); _skeleton[user][SkeletonJoint.RightKnee] = jointPosition(user, SkeletonJoint.RightKnee); _skeleton[user][SkeletonJoint.RightFoot] = jointPosition(user, SkeletonJoint.RightFoot); } _skeletonDrawer.Draw(_skeleton); } }; Dispatcher.Invoke(action); }; _sensor.NewUser += delegate(object s, NewUserEventArgs args) { Console.WriteLine("[" + args.ID + "] Found"); if(Properties.Settings.Default.DisplaySkeleton) _skeletonCapability.RequestCalibration(args.ID, true); }; _sensor.LostUser += delegate(object s, UserLostEventArgs args) { Console.WriteLine("[" + args.ID + "] Lost"); _skeleton.Remove(args.ID); Action action = () => { _skeletonDrawer.UserLost(args.ID); }; Dispatcher.Invoke(action); }; _skeletonCapability.CalibrationComplete += delegate(object s, CalibrationProgressEventArgs args) { Console.WriteLine("[" + args.ID + "] Calibration status: " + args.Status); if (args.Status == CalibrationStatus.OK) { _skeletonCapability.StartTracking(args.ID); Dictionary<SkeletonJoint, SkeletonJointPosition> jointPositions = new Dictionary<SkeletonJoint, SkeletonJointPosition>(); jointPositions.Add(SkeletonJoint.Head, new SkeletonJointPosition()); jointPositions.Add(SkeletonJoint.Neck, new SkeletonJointPosition()); jointPositions.Add(SkeletonJoint.Torso, new SkeletonJointPosition()); jointPositions.Add(SkeletonJoint.LeftShoulder, new SkeletonJointPosition()); jointPositions.Add(SkeletonJoint.LeftElbow, new SkeletonJointPosition()); jointPositions.Add(SkeletonJoint.LeftHand, new SkeletonJointPosition()); jointPositions.Add(SkeletonJoint.RightShoulder, new SkeletonJointPosition()); jointPositions.Add(SkeletonJoint.RightElbow, new SkeletonJointPosition()); jointPositions.Add(SkeletonJoint.RightHand, new SkeletonJointPosition()); jointPositions.Add(SkeletonJoint.LeftHip, new SkeletonJointPosition()); jointPositions.Add(SkeletonJoint.LeftKnee, new SkeletonJointPosition()); jointPositions.Add(SkeletonJoint.LeftFoot, new SkeletonJointPosition()); jointPositions.Add(SkeletonJoint.RightHip, new SkeletonJointPosition()); jointPositions.Add(SkeletonJoint.RightKnee, new SkeletonJointPosition()); jointPositions.Add(SkeletonJoint.RightFoot, new SkeletonJointPosition()); _skeleton.Add(args.ID, jointPositions); } else if (args.Status != CalibrationStatus.ManualAbort) _skeletonCapability.RequestCalibration(args.ID, true); }; } catch (Exception ex) { Console.Error.WriteLine(ex); MessageBox.Show(ex.Message, "Error while initializing OpenNI", MessageBoxButton.OK, MessageBoxImage.Error); Application.Current.Shutdown(); } }
//drawing color image and skeleton private DrawingImage DrawImage(ColorImageFrame colorFrame, Skeleton skeleton, System.Windows.Media.Media3D.Vector3D? armCoM = null, System.Windows.Media.Media3D.Vector3D? forearmCoM = null, System.Windows.Media.Media3D.Vector3D? upperArmCoM = null) { DrawingGroup dgColorImageAndSkeleton = new DrawingGroup(); DrawingImage drawingImage = new DrawingImage(dgColorImageAndSkeleton); skeletonDrawer = new SkeletonDrawer(kinectSensor); using (DrawingContext drawingContext = dgColorImageAndSkeleton.Open()) { InitializeDrawingImage(colorFrame, drawingContext); skeletonDrawer.DrawSkeleton(skeleton, drawingContext); skeletonDrawer.DrawPoint(skeleton, armCoM, drawingContext); skeletonDrawer.DrawPoint(skeleton, forearmCoM, drawingContext); skeletonDrawer.DrawPoint(skeleton, upperArmCoM, drawingContext); } //Make sure the image remains within the defined width and height dgColorImageAndSkeleton.ClipGeometry = new RectangleGeometry(new Rect(0.0, 0.0, RenderWidth, RenderHeight)); return drawingImage; }