且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Win8 Metro(C#)数字图像处理--2.41彩色图像密度分割算法

更新时间:2022-09-27 14:31:31

原文:Win8 Metro(C#)数字图像处理--2.41彩色图像密度分割算法



[函数名称]

  彩色图像密度分割函数      DensitySegmentProcess(WriteableBitmap src)

[算法说明]

  图像密度分割又叫做彩色等密度分割处理,一般图像(或影像)上色调的明暗是以附着在片基上的银粒子密度来计量的。因此,为了突出某一密度等级的色调(或相应地物),即将图像(或影像)的色调密度分划成若干个等级,并用不同的颜色分别表示这不同的密度等级,得到一幅彩色的等密度分割图像。这一技术过程就叫作密度分割处理,或简称密度分割。密度分割可使影像轮廓更清晰,突出某些具有一定色调特征的地物及分布状态,在显示环境污染范围,隐伏构造,以及寻找地下水等方面有广泛的应用,并取得较好的效果。密度分割后得到的彩色图像的色彩是人为加于的,一般并不代表地物的实际颜色,所以一般也称密度分割为假彩色密度分割。

  这里我们列举的是基于颜色灰度的密度分割。

[函数代码]

       /// <summary>
        /// Density segmentation.
        /// </summary>
        /// <param name="src">The source image.</param>
        /// <returns></returns>
        public static WriteableBitmap DensitySegmentProcess(WriteableBitmap src)////彩色图像密度分割 
        {
            if (src != null)
            {
                int w = src.PixelWidth;
                int h = src.PixelHeight;
                WriteableBitmap srcImage = new WriteableBitmap(w, h);
                byte[] temp = src.PixelBuffer.ToArray();
                int color = 0;
                for (int i = 0; i < temp.Length; i += 4)
                {
                    byte tempByte = (byte)((int)(temp[i] * 0.114 + temp[i + 1] * 0.587 + temp[i + 2] * 0.299));
                    color = GetColor(tempByte);
                    switch (color)
                    {
                        case 1:
                            temp[i] = (byte)255; temp[i + 1] = (byte)255; temp[i + 2] = (byte)0;
                            break;
                        case 2:
                            temp[i] = (byte)255; temp[i + 1] = (byte)0; temp[i + 2] = (byte)255;
                            break;
                        case 3:
                            temp[i] = (byte)0; temp[i + 1] = (byte)255; temp[i + 2] = (byte)255;
                            break;
                        case 4:
                            temp[i] = (byte)255; temp[i + 1] = (byte)0; temp[i + 2] = (byte)0;
                            break;
                        case 5:
                            temp[i] = (byte)0; temp[i + 1] = (byte)0; temp[i + 2] = (byte)255;
                            break;
                        case 0:
                            temp[i] = (byte)0; temp[i + 1] = (byte)0; temp[i + 2] = (byte)0;
                            break;
                        default:
                            break;
                    }
                }
                Stream sTemp = srcImage.PixelBuffer.AsStream();
                sTemp.Seek(0, SeekOrigin.Begin);
                sTemp.Write(temp, 0, w * 4 * h);
                return srcImage;
            }
            else
            {
                return null;
            }
        }
        //定义密度等级获得函数
        private static int GetColor(int v)
        {
            int t = 0;
            if (v == 0)
            {
                t = 0;
            }
            else if (v > 0 && v < 50)
            {
                t = 1;
            }
            else if (v >= 50 && v < 100)
            {
                t = 2;
            }
            else if (v >= 100 && v < 150)
            {
                t = 3;
            }
            else if (v >= 150 && v < 200)
            {
                t = 4;
            }
            else
            {
                t = 5;
            }
            return t;
        }
Win8 Metro(C#)数字图像处理--2.41彩色图像密度分割算法