2011年6月19日 星期日

Microsoft Kinect(第二課 VideoStream)

Ref : SketetalViewer_Walkthrough.docx
PART 3—C# SkeletalViewer Sample
The C# SkeletalViewer application is installed in the following folder:
Users\Public\Documents\Microsoft Research KinectSDK Samples\NUI\SkeletalViewer\CS
The sample is implemented in the following files:
• App.xaml declares application-level resources.
• App.xaml.cs contains the code behind app.xaml.
• MainWindow.xaml declares the layout of the main application window.
• MainWindow.xaml.cs contains the code behind the main window, which implements NUI API initialization, processing and display.
• SkeletalViewer.ico defines the application icon that is used in title bar and task bar.
1. Open WPF Application
2. Add ref Microsoft.Research.Kinect
3. Add using Microsoft.Research.Kinect.Nui;
4. Copy parts of sample code as follows to test videostream function
4-1  Global variables
        Runtime nui;
        int totalFrames = 0;
        int lastFrames = 0;
        DateTime lastTime = DateTime.MaxValue;
        const int RED_IDX = 2;
        const int GREEN_IDX = 1;
        const int BLUE_IDX = 0;

4-2 Window_Loaded event method (Parts of them from example)
Key steps:
4-2-1 Create Runtime object
  nui = new Runtime();
4-2-2 Initialize Runtime object
  nui.Initialize(RuntimeOptions.UseDepthAndPlayerIndex | RuntimeOptions.UseSkeletalTracking | RuntimeOptions.UseColor);
4-2-3 Open VideoStream
 nui.VideoStream.Open(ImageStreamType.Video, 2, ImageResolution.Resolution640x480, ImageType.Color);
4-2-4 Set VideoFrameReady event
 nui.VideoFrameReady += new EventHandler<ImageFrameReadyEventArgs>(nui_VideoFrameReady); 

       
4-3 VideoFrameReady event method (Copy from sample)
  // 32-bit per pixel, RGBA image
            PlanarImage Image = e.ImageFrame.Image;
            im1.Source = BitmapSource.Create(
                Image.Width, Image.Height, 96, 96, PixelFormats.Bgr32, null, Image.Bits, Image.Width * Image.BytesPerPixel);
            ++totalFrames;
            DateTime cur = DateTime.Now;
            if (cur.Subtract(lastTime) > TimeSpan.FromSeconds(1))
            {
                int frameDiff = totalFrames - lastFrames;
                lastFrames = totalFrames;
                lastTime = cur;
                this.Title = frameDiff.ToString() + " fps";
            }
Notes: im1 is a image object set in xaml file
5 Download: https://sites.google.com/site/csjoublogfiles/home/files/KinectSDKEx1.zip

沒有留言:

張貼留言