Win8Metro(C#)数字图像处理--2.4图像颜色聚类

来源:互联网 发布:淘宝山店软妹模特 编辑:程序博客网 时间:2024/04/29 07:56


[函数名称]

图像颜色聚类函数ClusterProcess(WriteableBitmap src,int value)

[算法说明]

  图像颜色聚类的方法有很多,但是对于视频监控而言,现有方法很难满足实时性的要求,这里介绍一种位屏蔽压缩的方法实现颜色聚类,可以满足实时性的要求。

  位屏蔽法就是在3DRGB真彩空间中近似均匀采样的颜色压缩方法,即将屏蔽的颜色位置设置为0,具体可以采用移位运算来实现

  一个像素的RGBA分量各占一个字节(这里指32位格式),对于每一个字节,它的大小为[0,255],用二进制位表示如下:

                        0000 0000——1111 1111

  比如11111111移位3位为11111000,按这个方法,我们可以将其移位N(0<N<=7)位,在C#中表现为&操作,如11111111位移3位可表示为:255&248

  用上述这个移位屏蔽算法,我们可以对彩色图像中的每一个像素进行这样的移位操作,那么图像像素值就会得到相应的颜色空间的压缩,从而达到聚类的效果。


[函数代码]

       ///<summary>

       /// Cluster process (a fast method be introduced here).

       ///</summary>

       ///<param name="src">Source image.</param>

       ///<param name="value">Choose one of {254-128}</param>

       ///<returns></returns>

       publicstaticWriteableBitmap ClusterProcess(WriteableBitmap src,int value)////4聚类处理

       {

           if(src!=null )

           {

           int w = src.PixelWidth;

           int h = src.PixelHeight;

           WriteableBitmap clusterImage =newWriteableBitmap(w,h);

           byte[] temp = src.PixelBuffer.ToArray();

           for (int i = 0; i < temp.Length; i += 4)

           {

               temp[i] = (byte)(temp[i] & value);

               temp[i + 1] = (byte)(temp[i + 1] & value);

               temp[i + 2] = (byte)(temp[i + 2] & value);

           }

           Stream sTemp = clusterImage.PixelBuffer.AsStream();

           sTemp.Seek(0,SeekOrigin.Begin);

           sTemp.Write(temp, 0, w * 4 * h);

           return clusterImage;

           }

           else

           {

               returnnull;

           }  

       }


0 0
原创粉丝点击