Win8 Metro(C#)数字图像处理--2.58双峰法图像二值化



[函数名称]

  双峰法图像二值化 WriteableBitmap  PeakshistogramThSegment(WriteableBitmap src)

Win8 Metro(C#)数字图像处理--2.58双峰法图像二值化_第1张图片

        /// <summary>
        /// Peaks histogram method of image segmention.
        /// </summary>
        /// <param name="src">The source image.</param>
        /// <returns></returns>
         public static WriteableBitmap  PeakshistogramThSegment(WriteableBitmap src) ////双峰法阈值分割
        {
            if (src != null)
            {
                int w = src.PixelWidth;
                int h = src.PixelHeight;
                WriteableBitmap dstImage = new WriteableBitmap(w, h);
                byte[] temp = src.PixelBuffer.ToArray();
                byte[] tempMask = (byte[])temp.Clone();
                //定义灰度图像信息存储变量
                int[] srcData = new int[w * h];
                //定义直方图存取变量
                int[] histValues = new int[256];
                //定义双峰位置变量h1,h2,对应的灰度变量t1,t2,谷底灰度变量t
                int h1 = 0, h2 = 0, t1 = 0, t2 = 0, t = 255;
                //定义阈值变量
                int Th = 0;
                for (int j = 0; j < h; j++)
                {
                    for (int i = 0; i < w; i++)
                    {
                        srcData[i + j * w] = (int)((double)tempMask[i * 4 + j * w * 4] * 0.114 + (double)tempMask[i * 4 + 1 + j * w * 4] * 0.587 + (double)tempMask[i * 4 + 2 + j * w * 4] * 0.299);
                        histValues[srcData[i + j * w]]++;
                    }
                }
                for (int i = 0; i < 256; i++)
                {
                    if (i < 129)
                    {
                        if (histValues[i] > t1)
                        {
                            h1 = i;
                            t1 = histValues[i];
                        }
                    }
                    else
                    {
                        if (histValues[i] > t2)
                        {
                            h2 = i;
                            t2 = histValues[i];
                        }
                    }
                }
                for (int n = h1; n <= h2; n++)
                {
                    if (histValues[n] < t)
                    {
                        Th = n;
                        t = histValues[n];
                    }
                }
                for (int j = 0; j < h; j++)
                {
                    for (int i = 0; i < w; i++)
                    {
                        temp[i * 4 + j * w * 4] = temp[i * 4 + 1 + j * w * 4] = temp[i * 4 + 2 + j * w * 4] = (byte)(srcData[i + j * w] < Th ? 0 : 255);
                    }
                }
                Stream sTemp = dstImage.PixelBuffer.AsStream();
                sTemp.Seek(0, SeekOrigin.Begin);
                sTemp.Write(temp, 0, w * 4 * h);
                return dstImage;
            }
            else
            {
                return null;
            }
        }
Win8 Metro(C#)数字图像处理--2.58双峰法图像二值化_第2张图片

你可能感兴趣的:(Win8 Metro(C#)数字图像处理--2.58双峰法图像二值化)