|
| 1 | +using Microsoft.Kinect; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using System.Windows.Controls; |
| 7 | +using System.Windows.Media; |
| 8 | +using System.Windows.Media.Imaging; |
| 9 | +using System.Windows.Shapes; |
| 10 | + |
| 11 | +namespace KinectV2_to_MATLAB |
| 12 | +{ |
| 13 | + public static class Extension |
| 14 | + // to use extension, the class must be static. |
| 15 | + { |
| 16 | + #region Camera |
| 17 | + |
| 18 | + public static ImageSource ToBitmap(this ColorFrame frame) |
| 19 | + { |
| 20 | + int width = frame.FrameDescription.Width; |
| 21 | + int height = frame.FrameDescription.Height; |
| 22 | + PixelFormat format = PixelFormats.Bgr32; |
| 23 | + |
| 24 | + byte[] pixels = new byte[width * height * ((format.BitsPerPixel + 7) / 8)]; |
| 25 | + |
| 26 | + if (frame.RawColorImageFormat == ColorImageFormat.Bgra) |
| 27 | + { |
| 28 | + frame.CopyRawFrameDataToArray(pixels); |
| 29 | + } |
| 30 | + else |
| 31 | + { |
| 32 | + frame.CopyConvertedFrameDataToArray(pixels, ColorImageFormat.Bgra); |
| 33 | + } |
| 34 | + |
| 35 | + int stride = width * format.BitsPerPixel / 8; |
| 36 | + |
| 37 | + return BitmapSource.Create(width, height, 96, 96, format, null, pixels, stride); |
| 38 | + } |
| 39 | + |
| 40 | + #endregion |
| 41 | + #region Coordinates |
| 42 | + |
| 43 | + #region scaling |
| 44 | + private static float Scale(double maxPixel, double maxSkeleton, float position) |
| 45 | + { |
| 46 | + float value = (float)((((maxPixel / maxSkeleton) / 2) * position) + (maxPixel / 2)); |
| 47 | + |
| 48 | + if (value > maxPixel) |
| 49 | + { |
| 50 | + return (float)maxPixel; |
| 51 | + } |
| 52 | + |
| 53 | + if (value < 0) |
| 54 | + { |
| 55 | + return 0; |
| 56 | + } |
| 57 | + |
| 58 | + return value; |
| 59 | + } |
| 60 | + |
| 61 | + public static Joint ScaleTo(this Joint joint, double width, double height, float skeletonMaxX, float skeletonMaxY) |
| 62 | + { |
| 63 | + joint.Position = new CameraSpacePoint |
| 64 | + { |
| 65 | + X = Scale(width, skeletonMaxX, joint.Position.X), |
| 66 | + Y = Scale(height, skeletonMaxY, -joint.Position.Y), |
| 67 | + Z = joint.Position.Z |
| 68 | + }; |
| 69 | + |
| 70 | + return joint; |
| 71 | + } |
| 72 | + |
| 73 | + public static Joint ScaleTo(this Joint joint, double width, double height) |
| 74 | + { |
| 75 | + return ScaleTo(joint, width, height, 1.0f, 1.0f); |
| 76 | + } |
| 77 | + |
| 78 | + #endregion |
| 79 | + |
| 80 | + #region drawing |
| 81 | + // drawing joints |
| 82 | + public static void DrawPoint(this Canvas canvas, Joint joint) |
| 83 | + { |
| 84 | + if (joint.TrackingState == TrackingState.NotTracked) return; |
| 85 | + |
| 86 | + joint = joint.ScaleTo(canvas.ActualWidth, canvas.ActualHeight); |
| 87 | + |
| 88 | + Ellipse ellipse = new Ellipse |
| 89 | + { |
| 90 | + Width = 20, |
| 91 | + Height = 20, |
| 92 | + Fill = new SolidColorBrush(Colors.LightBlue) |
| 93 | + }; |
| 94 | + |
| 95 | + Canvas.SetLeft(ellipse, joint.Position.X - ellipse.Width / 2); |
| 96 | + Canvas.SetTop(ellipse, joint.Position.Y - ellipse.Height / 2); |
| 97 | + |
| 98 | + canvas.Children.Add(ellipse); |
| 99 | + } |
| 100 | + // drawing lines by two points coordinates |
| 101 | + public static void DrawLine(this Canvas canvas, Joint first, Joint second) |
| 102 | + { |
| 103 | + if (first.TrackingState == TrackingState.NotTracked || second.TrackingState == TrackingState.NotTracked) return; |
| 104 | + |
| 105 | + first = first.ScaleTo(canvas.ActualWidth, canvas.ActualHeight); |
| 106 | + second = second.ScaleTo(canvas.ActualWidth, canvas.ActualHeight); |
| 107 | + |
| 108 | + Line line = new Line |
| 109 | + { |
| 110 | + X1 = first.Position.X, |
| 111 | + Y1 = first.Position.Y, |
| 112 | + X2 = second.Position.X, |
| 113 | + Y2 = second.Position.Y, |
| 114 | + StrokeThickness = 8, |
| 115 | + Stroke = new SolidColorBrush(Colors.LightBlue) |
| 116 | + }; |
| 117 | + |
| 118 | + canvas.Children.Add(line); |
| 119 | + } |
| 120 | + |
| 121 | + // drawing skeleton by using DrawLine method |
| 122 | + public static void DrawSkeleton(this Canvas canvas, Body body) |
| 123 | + { |
| 124 | + if (body == null) return; |
| 125 | + |
| 126 | + foreach (Joint joint in body.Joints.Values) |
| 127 | + { |
| 128 | + canvas.DrawPoint(joint); |
| 129 | + } |
| 130 | + |
| 131 | + canvas.DrawLine(body.Joints[JointType.Head], body.Joints[JointType.Neck]); |
| 132 | + canvas.DrawLine(body.Joints[JointType.Neck], body.Joints[JointType.SpineShoulder]); |
| 133 | + canvas.DrawLine(body.Joints[JointType.SpineShoulder], body.Joints[JointType.ShoulderLeft]); |
| 134 | + canvas.DrawLine(body.Joints[JointType.SpineShoulder], body.Joints[JointType.ShoulderRight]); |
| 135 | + canvas.DrawLine(body.Joints[JointType.SpineShoulder], body.Joints[JointType.SpineMid]); |
| 136 | + canvas.DrawLine(body.Joints[JointType.ShoulderLeft], body.Joints[JointType.ElbowLeft]); |
| 137 | + canvas.DrawLine(body.Joints[JointType.ShoulderRight], body.Joints[JointType.ElbowRight]); |
| 138 | + canvas.DrawLine(body.Joints[JointType.ElbowLeft], body.Joints[JointType.WristLeft]); |
| 139 | + canvas.DrawLine(body.Joints[JointType.ElbowRight], body.Joints[JointType.WristRight]); |
| 140 | + canvas.DrawLine(body.Joints[JointType.WristLeft], body.Joints[JointType.HandLeft]); |
| 141 | + canvas.DrawLine(body.Joints[JointType.WristRight], body.Joints[JointType.HandRight]); |
| 142 | + canvas.DrawLine(body.Joints[JointType.HandLeft], body.Joints[JointType.HandTipLeft]); |
| 143 | + canvas.DrawLine(body.Joints[JointType.HandRight], body.Joints[JointType.HandTipRight]); |
| 144 | + canvas.DrawLine(body.Joints[JointType.HandTipLeft], body.Joints[JointType.ThumbLeft]); |
| 145 | + canvas.DrawLine(body.Joints[JointType.HandTipRight], body.Joints[JointType.ThumbRight]); |
| 146 | + canvas.DrawLine(body.Joints[JointType.SpineMid], body.Joints[JointType.SpineBase]); |
| 147 | + canvas.DrawLine(body.Joints[JointType.SpineBase], body.Joints[JointType.HipLeft]); |
| 148 | + canvas.DrawLine(body.Joints[JointType.SpineBase], body.Joints[JointType.HipRight]); |
| 149 | + canvas.DrawLine(body.Joints[JointType.HipLeft], body.Joints[JointType.KneeLeft]); |
| 150 | + canvas.DrawLine(body.Joints[JointType.HipRight], body.Joints[JointType.KneeRight]); |
| 151 | + canvas.DrawLine(body.Joints[JointType.KneeLeft], body.Joints[JointType.AnkleLeft]); |
| 152 | + canvas.DrawLine(body.Joints[JointType.KneeRight], body.Joints[JointType.AnkleRight]); |
| 153 | + canvas.DrawLine(body.Joints[JointType.AnkleLeft], body.Joints[JointType.FootLeft]); |
| 154 | + canvas.DrawLine(body.Joints[JointType.AnkleRight], body.Joints[JointType.FootRight]); |
| 155 | + } |
| 156 | + #endregion |
| 157 | + |
| 158 | + #endregion |
| 159 | + } |
| 160 | +} |
0 commit comments