kinect初识--color

来源:互联网 发布:保时捷车载香水知乎 编辑:程序博客网 时间:2024/05/02 01:23

        今天(2015-03-06 00:20:47)晚上开始学kinect,其实以前看过一次视频,不过那个时候没学c#,看不懂(原谅我仗着c学的好,c++根本没有听过课看过书吧)。昨天写了一个选号器,感觉对C#还是有一点认识了,今天看了一下视频,在结合ColorBasics-WPF示例,也算是基本看懂了。视频将的很短,一共就2分钟,基本框架大概提了一下,不过还是很不错了,有人讲我就知足了。当中的具体实现我还是看了接近半个小时,虽然代码不多就百多行吧,但是以前真没接触kinect.senser类,也不了解colorFrameRead这些,所以看着总是瞻前顾后,前面的定义刚刚瞄了一眼,后面实现又想不起了,还得回去翻定义的具体。

        kinect的color实现大概框架是这样:

     *1.获取一个kinect设备

foreach (var potentialSensor in KinectSensor.KinectSensors)            {                if (potentialSensor.Status == KinectStatus.Connected)                {                    this.sensor = potentialSensor;                    break;                }            }
用foreach对kinect对象进行遍历,如果有连接状态的则给前面定义的sensor对象赋上已经连接的对象

     * 2.初始化,启用彩色、骨骼数据流

if (null != this.sensor)            {                // Turn on the color stream to receive color frames                this.sensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);                // 建立本地缓冲区                this.colorPixels = new byte[this.sensor.ColorStream.FramePixelDataLength];                // 这是RGB位图,将显示在屏幕上                this.colorBitmap = new WriteableBitmap(this.sensor.ColorStream.FrameWidth, this.sensor.ColorStream.FrameHeight, 96.0, 96.0, PixelFormats.Bgr32, null);                // Set the image we display to point to the bitmap where we'll put the image data                this.Image.Source = this.colorBitmap;                // 添加一个事件处理程序被称为帧数据,每当有新颜色                this.sensor.ColorFrameReady += this.SensorColorFrameReady;                // Start the sensor!                try                {                    this.sensor.Start();                }                catch (IOException)                {                    this.sensor = null;                }            }
这里是初始化给sensor对象的ColorStream属性加上参数数据帧(也就是位图)的格式,然后本地建一个缓存区来存储位图(微软的命名方式还别说,一翻译就懂偷笑),然后使用olorBitmap来存储对象获取到的图像。
this.Image.Source = this.colorBitmap;

这一句说实在的我没完全明白,大概意思是显示到屏幕上,但是Image这个(诶,翻了下定义,原来是窗体控件名,算我笨了尴尬)。现在明白了,很简单    窗体控件接收位图,就这样子。 
ColorFrameReady是kinect.sensor的一个事件函数,具体下面:
private void SensorColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)        {            using (ColorImageFrame colorFrame = e.OpenColorImageFrame())            {                if (colorFrame != null)                {                    //将当前对象获取的像素拷贝到colorPixels数据缓存区中                    colorFrame.CopyPixelDataTo(this.colorPixels);                    // Write the pixel data into our bitmap                    this.colorBitmap.WritePixels(                        new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight),                        this.colorPixels,                        this.colorBitmap.PixelWidth * sizeof(int),0);<span style="font-family: Arial, Helvetica, sans-serif;">//为什么*int</span>                }            }        }

该事件触发时,会传入采集到的一帧图像,然后通过调用colorFrame的copy方法将传入的一帧图像拷贝到缓冲区colorPixels中去。后面两句就是传入显示的区域中去了WritePixels应该是colorBitmap的写入函数吧,不过这里我是没看懂,为什么写入函数传参会传一个new Int32Rect(),可能是我知识不足还不了解这些吧,以前没见到过参数还有new的,难得是临时定义的一个?亦或是new还有其他用法,我知道的new一般就定义用的(其实我也没学c#几天)。还有colorBitmap.PixelWidth * sizeof(int),为什么位图宽度还得*一个sizeof(int),这个也没想通。

* 3.注册事件处理函数,当有数据产生回调事件处理函数

注册事件处理函数好像上面也一起讲了害羞。然后启动

 try                {                    this.sensor.Start();                }                catch (IOException)                {                    this.sensor = null;                }


     * 4.通知kinect设备开始捕获数据

           好啦,今天就记到这吧,这程序真的很简单,不过叫我从一篇空白我不知道能不能建立起来呢?明天自己实践一下就知道了
0 0