Kinect开发随笔①——红外扫描仪(Kinect 数据源)

来源于 MVA 的 快速入门:Kinect for Windows v2 开发 的学习随笔

具体内容为上图所示章节内容

章节内全部代码:GitHub地址点我(链接失效,待补档)

1 <Page
2     <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
3         <Image Name="image" Width="512" Height="512"/>
4         <Canvas Name="bodyCanves" Width="512" Height="512"/>
5     </Grid>
6 </Page>
Xaml Code
  1 using WindowsPreview.Kinect;
  2 using Windows.UI.Xaml.Media.Imaging;
  3 using Windows.UI.Xaml.Shapes;
  4 using Windows.UI;
  5     
  6 public sealed partial class MainPage : Page
  7     {
  8         public MainPage()
  9         {
 10             this.InitializeComponent();
 11             this.Loaded += MainPage_Loaded;
 12         }
 13 
 14         KinectSensor sensor;
 15         InfraredFrameReader irReader;
 16         ushort[] irData;
 17         byte[] irDataConverted;
 18         WriteableBitmap irBitmap;
 19 
 20         Body[] bodies;
 21         MultiSourceFrameReader msfr;
 22 
 23         private void MainPage_Loaded(object sender, RoutedEventArgs e)
 24         {
 25             sensor = KinectSensor.GetDefault();
 26             irReader = sensor.InfraredFrameSource.OpenReader();
 27             FrameDescription fd = sensor.InfraredFrameSource.FrameDescription;
 28             irData = new ushort[fd.LengthInPixels];
 29             irDataConverted = new byte[fd.LengthInPixels * 4];
 30             irBitmap = new WriteableBitmap(fd.Width, fd.Height);
 31             image.Source = irBitmap;
 32 
 33             bodies = new Body[6];
 34             msfr = sensor.OpenMultiSourceFrameReader(FrameSourceTypes.Body | FrameSourceTypes.Infrared);
 35             msfr.MultiSourceFrameArrived += msfr_MultiSourceFrameArrived;
 36 
 37 
 38             sensor.Open();
 39             irReader.FrameArrived += irReader_frameArrived;
 40         }
 41 
 42         private void msfr_MultiSourceFrameArrived(MultiSourceFrameReader sender, MultiSourceFrameArrivedEventArgs args)
 43         {
 44             using (MultiSourceFrame msf = args.FrameReference.AcquireFrame())
 45             {
 46                 if (msf != null)
 47                 {
 48                     using (BodyFrame bodyFrame = msf.BodyFrameReference.AcquireFrame())
 49                     {
 50                         using (InfraredFrame irFrame = msf.InfraredFrameReference.AcquireFrame())
 51                         {
 52                             if (bodyFrame != null && irFrame != null)
 53                             {
 54                                 irFrame.CopyFrameDataToArray(irData);
 55                                 for (int i = 0; i < irData.Length; i++)
 56                                 {
 57                                     byte intensity = (byte)(irData[i] >> 8);
 58                                     irDataConverted[i * 4] = intensity;
 59                                     irDataConverted[i * 4 + 1] = intensity;
 60                                     irDataConverted[i * 4 + 2] = intensity;
 61                                     irDataConverted[i * 4 + 3] = 255;
 62                                 }
 63                                 irDataConverted.CopyTo(irBitmap.PixelBuffer);
 64                                 irBitmap.Invalidate();
 65 
 66                                 bodyFrame.GetAndRefreshBodyData(bodies);
 67                                 bodyCanves.Children.Clear();
 68                                 foreach(Body body in bodies)
 69                                 {
 70                                     if(body.IsTracked)
 71                                     {
 72                                         Joint headJoint = body.Joints[JointType.Head];
 73                                         if (headJoint.TrackingState == TrackingState.Tracked)
 74                                         {
 75                                             DepthSpacePoint dsp = sensor.CoordinateMapper.MapCameraPointToDepthSpace(headJoint.Position);
 76                                             Ellipse headcircle = new Ellipse() { Width = 50, Height = 50, Fill = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0)) };
 77                                             bodyCanves.Children.Add(headcircle);
 78                                             Canvas.SetLeft(headcircle ,dsp.X - 25);
 79                                             Canvas.SetTop(headcircle ,dsp.Y - 25);
 80 
 81                                         }
 82                                     }
 83                                 }
 84 
 85                             }
 86                         }
 87                     }
 88                 }
 89             }
 90         }
 91 
 92         private void irReader_frameArrived(InfraredFrameReader sender, InfraredFrameArrivedEventArgs args)
 93         {
 94             using (InfraredFrame irFrame = args.FrameReference.AcquireFrame())
 95             {
 96                 if (irFrame != null)
 97                 {
 98                     irFrame.CopyFrameDataToArray(irData);
 99                     for (int i = 0; i < irData.Length; i++)
100                     {
101                         byte intensity = (byte)(irData[i] >> 8);
102                         irDataConverted[i * 4] = intensity;
103                         irDataConverted[i * 4 + 1] = intensity;
104                         irDataConverted[i * 4 + 2] = intensity;
105                         irDataConverted[i * 4 + 3] = 255;
106                     }
107                     irDataConverted.CopyTo(irBitmap.PixelBuffer);
108                     irBitmap.Invalidate();
109                 }
110             }
111         }
112     }

 

posted @ 2016-11-29 19:47  4770K  阅读(1075)  评论(0编辑  收藏  举报