代码注释之Kinnect简单图像变化

 

代码来源介绍

作者:   yangecnu(yangecnu's Blog on 博客园)
出处:http://www.cnblogs.com/yangecnu/
作品由yangecnu创作,采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。 欢迎转载,但任何转载必须保留完整文章,在显要地方显示署名以及原文链接。如您有任何疑问或者授权方面的协商,请给我留言。

//处理数据流函数
        void kinectSensor_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
        {
            using (ColorImageFrame frame = e.OpenColorImageFrame())
            {
                if (frame != null)
                {
                    //数据流对象的PixelDataLength属性返回数据和序列的具体大小
                    byte[] pixelData = new byte[frame.PixelDataLength];
                    //填充像素数据到byte数组pixelData,现在已经将Kinnect获得的数据存入 了内存中的一个数组
                    frame.CopyPixelDataTo(pixelData);
                    /*
                    ColorImageElement.Source = BitmapImage.Create(frame.Width, frame.Height, 96, 96,
                                                                 PixelFormats.Bgr32, null, pixelData,
                                                                 frame.Width * frame.BytesPerPixel);
                     * */
                    //对获得的图像数据进行简单的处理

                    //循环每个像素点,并进行相应处理
                    for (int i = 0; i < pixelData.Length; i += frame.BytesPerPixel)
                    {
                       

/*
                        pixelData[i] = 0x00;//蓝色
                        pixelData[i + 1] = 0x00;//绿色
                       // pixelData[i + 2] = 0x00;//红色
                         * */
                        /*将图像的每个像素翻转,及黑变白。。0-255像素级倒转
                        pixelData[i] = (byte)~pixelData[i];

                        pixelData[i + 1] = (byte)~pixelData[i + 1];

                        pixelData[i + 2] = (byte)~pixelData[i + 2];
                        */
                        /*
                         * Apocalyptic Zombie启示录的僵尸。。呵呵
                        pixelData[i] = pixelData[i + 1];

                        pixelData[i + 1] = pixelData[i];

                        pixelData[i + 2] = (byte)~pixelData[i + 2];
                        */
                        /*灰度处理
                         byte gray = Math.Max(pixelData[i], pixelData[i + 1]);
 
                         gray=Math.Max(gray,pixelData[i+2]);
 
                         pixelData[i]=gray;
 
                         pixelData[i+1]=gray;
 
                         pixelData[i+2]=gray;
                        */
                        /*Grainy black and white movie模糊的黑白电影
                        byte gray = Math.Min(pixelData[i], pixelData[i + 1]);

                        gray = Math.Min(gray, pixelData[i + 2]);

                        pixelData[i] = gray;

                        pixelData[i + 1] = gray;

                        pixelData[i + 2] = gray;
                        */
                        /*Washed out color冲毁了的颜色
                        double gray = (pixelData[i] * 0.11) + (pixelData[i + 1] * 0.59) + (pixelData[i + 2] * 0.3);

                        double desaturation = 0.75;

                        pixelData[i] = (byte)(pixelData[i] + desaturation * (gray - pixelData[i]));

                        pixelData[i + 1] = (byte)(pixelData[i + 1] + desaturation * (gray - pixelData[i + 1]));

                        pixelData[i + 2] = (byte)(pixelData[i + 2] + desaturation * (gray - pixelData[i + 2]));
                        */
                        //高饱和度
                        if (pixelData[i]<0x33||pixelData[i]>0xE5)
                        {
 
                           pixelData[i]=0x00;
 
                        } else
 
                        {
 
                           pixelData[i]=0Xff;
 
                         }
 
                         if (pixelData[i+1]<0x33||pixelData[i+1]>0xE5)
 
                         {
 
                           pixelData[i+1]=0x00;
 
                         } else
 
                         {
 
                           pixelData[i+1]=0Xff;
 
                         }
 
                         if (pixelData[i+2]<0x33||pixelData[i+2]>0xE5)
 
                         {
 
                          pixelData[i+2]=0x00;
 
                         } else
 
                         {
 
                            pixelData[i+1]=0Xff;
 
                         }

 

                    }
                    //更新位图避免多次更新创建位图产生的内存浪费
                    this.colorImageBitMap.WritePixels(this.colorImageBitmapRect, pixelData, this.colorImageStride, 0);
                }
            }
        }

你可能感兴趣的:(代码注释之Kinnect简单图像变化)