[KinectWPF程序]单帧彩色图像获取&使用WriteableBitmap对象

[KinectWPF程序]单帧彩色图像获取&使用WriteableBitmap对象

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Kinect;

//添加两个Image控件—ShotImage和ShowImage,一个Button控件
namespace WpfApplication1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {

        KinectSensor kinectSensor;

        private WriteableBitmap colorImageBitMap;
        private Int32Rect colorImageBitmapRect;
        private int colorImageStride;

        byte[] ColorPixelData;
        ColorImageFrame colorFrame;

        //初始化Kinect直接调用InitKinect()即可;注册的同步事件为AllFramesReadyEventArgs
        /// <summary>
        /// 启动Kinect设备,初始化选项,并注册AllFrameReady同步事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void InitKinect()
        {
            if (KinectSensor.KinectSensors.Count > 0)
            {
                kinectSensor = (from sensor in KinectSensor.KinectSensors
                                where sensor.Status == KinectStatus.Connected
                                select sensor).FirstOrDefault();

                kinectSensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
                kinectSensor.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);
                kinectSensor.SkeletonStream.Enable();

                //创建WriteableBitmap对象,准备接收像素数据
        ColorImageStream colorStream = kinectSensor.ColorStream;
                this.colorImageBitMap = new WriteableBitmap(colorStream.FrameWidth, colorStream.FrameHeight,
                                                                                96, 96, PixelFormats.Bgr32, null);
                this.colorImageBitmapRect = new Int32Rect(0, 0, colorStream.FrameWidth, colorStream.FrameHeight);
                this.colorImageStride = colorStream.FrameWidth * colorStream.FrameBytesPerPixel;
                ShowImage.Source = this.colorImageBitMap;

                kinectSensor.Start();

                kinectSensor.AllFramesReady += new EventHandler<AllFramesReadyEventArgs>(kinectSensor_AllFramesReady);

            }
            else
            {
                MessageBox.Show("没有检测到Kinect设备");
            }
        }

        public MainWindow()
        {
            InitializeComponent();
        }

        void kinectSensor_AllFramesReady(object sender, AllFramesReadyEventArgs e)
        {
            using (colorFrame = e.OpenColorImageFrame())
            {
                if (colorFrame != null)
                {
                    //建立存储像素的矩阵,一维矩阵
                    //colorFrame.PixelDataLength每一帧的数据长度
                    ColorPixelData = new byte[colorFrame.PixelDataLength];
                    //将Kicent获取的彩色像素拷贝存储像素的矩阵中
                    colorFrame.CopyPixelDataTo(ColorPixelData);

                    //关闭每个像素点的蓝色和绿色通道。
                    //for循环遍历每个像素,使得i的起始位置重视该像素的第一个字节。
                    //由于数据的格式是Bgr32,即RGB32位(一个像素共占4个字节,每个字节8位),
                    //所以第一个字节是蓝色通道,第二个是绿色,第三个是红色。
                    //循环体内,将第一个和第二个通道设置为0.所以输出的代码中只用红色通道的信息。这是最基本的图像处理。
                    for (int i = 0; i < ColorPixelData.Length; i += colorFrame.BytesPerPixel)
                    {
                        ColorPixelData[i] = 0x25;//蓝色
                        ColorPixelData[i + 1] = 0x00;//绿色
                    }
                    //调用WriteableBitmap对象的WritePixels方法来更新图像
                    this.colorImageBitMap.WritePixels(this.colorImageBitmapRect, ColorPixelData, this.colorImageStride, 0);
                }
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            this.ShotImage.Source = BitmapSource.Create(colorFrame.Width, colorFrame.Height, 96, 96,
                           PixelFormats.Bgr32, null, ColorPixelData, colorFrame.Width * colorFrame.BytesPerPixel);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            InitKinect();
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            kinectSensor.Stop();
        }
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值