视频数据:彩色数据采集(Color Data)

在Kinect for windows上,有三个摄像头,一个红外发射摄像头,一个红外摄像头 ,一个普通摄像头,这篇无博文就是说明关于普通摄像头采集视频数据的。另外还有深度数据采集(Depth Data)和骨骼数据(Skeleton Data)采集在后面几篇博文中进行说明。windows

项目的准备见上一篇博文。
新建一个 WPF项目,在窗体上放一个Image控件,Name为img。
C#代码以下:
 
  
  
  
  
  1. //Kinect对象  
  2. KinectSensor kinectsensor = null;  
  3. private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)  
  4. {  
  5.     if (kinectsensor.Status == KinectStatus.Connected)  
  6.     {  
  7.         kinectsensor.Stop();//中止Kinect  
  8.     }  
  9. }  
  10.  
  11. private void Window_Loaded(object sender, RoutedEventArgs e)  
  12. {  
  13.     foreach (KinectSensor ks in KinectSensor.KinectSensors)  
  14.     {  
  15.         if (ks.Status == KinectStatus.Connected)  
  16.         {  
  17.             kinectsensor = ks;  
  18.             //开启彩色流,参数为采集方式,分辨率640x480和采集频率每秒30帧  
  19.             kinectsensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);  
  20.             //订阅采集事件  
  21.             kinectsensor.ColorFrameReady += kinectsensor_ColorFrameReady;  
  22.             kinectsensor.Start();//启动Kinect  
  23.             this.Title = "Kinect开始工做……";  
  24.             return;  
  25.         }  
  26.     }  
  27. }  
  28.  
  29. byte[] colorPixels;//采集数据的字节数组  
  30. WriteableBitmap colorBitmap;//位图对象  
  31. void kinectsensor_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)  
  32. {  
  33.     using (ColorImageFrame colorFrame = e.OpenColorImageFrame())  
  34.     {  
  35.         if (colorFrame != null)//判断彩色数据框架是否有数据  
  36.         {  
  37.             //初始化字节数据长度  
  38.             this.colorPixels = new byte[kinectsensor.ColorStream.FramePixelDataLength];  
  39.             //把数据复制到字节数组中  
  40.             colorFrame.CopyPixelDataTo(colorPixels);  
  41.             //实例化彩色位图  
  42.             colorBitmap = new WriteableBitmap(kinectsensor.ColorStream.FrameWidth, kinectsensor.ColorStream.FrameHeight, 96.0, 96.0, PixelFormats.Bgr32, null);  
  43.             //加载字节数据数据到位图中  
  44.             this.colorBitmap.WritePixels(new Int32Rect(0, 0, colorBitmap.PixelWidth, colorBitmap.PixelHeight), colorPixels,colorBitmap.PixelWidth * sizeof(int),0);  
  45.             //把位图赋给图片控件,并显示出来  
  46.             img.Source = colorBitmap;  
  47.         }  
  48.     }  
  49. }  

结果以下:数组

相关文章
相关标签/搜索