WPF:图像处理(三)二值化

来源:互联网 发布:重复数据阴影 编辑:程序博客网 时间:2024/05/17 02:55
/* ----------------------------------------------------------文件名称:Binarize.cs作者:秦建辉MSN:splashcn@msn.comQQ:36748897博客:http://blog.csdn.net/jhqin开发环境:    Visual Studio V2010    .NET Framework 4 Client Profile版本历史:    V1.1    2012年04月17日            实现迭代法阈值计算方法    V1.0    2012年04月16日            实现大津法阈值计算方法------------------------------------------------------------ */using System;using System.Windows.Media;using System.Windows.Media.Imaging;namespace Splash.Imaging{    /// <summary>    /// 二值化方法    /// </summary>    public enum BinarizationMethods    {                Otsu,       // 大津法        Iterative   // 迭代法    }    /// <summary>    /// 图像处理:图像二值化    /// </summary>    public static partial class Binarize    {              /// <summary>        /// 全局阈值图像二值化        /// </summary>        /// <param name="bitmap">原始图像</param>        /// <param name="method">二值化方法</param>        /// <param name="threshold">输出:全局阈值</param>        /// <returns>二值化后的图像数组</returns>                public static Byte[,] ToBinaryArray(this BitmapSource bitmap, BinarizationMethods method, out Int32 threshold)        {   // 位图转换为灰度数组            Byte[,] GrayArray = bitmap.ToGrayArray();                        // 计算全局阈值            if (method == BinarizationMethods.Otsu)                threshold = OtsuThreshold(GrayArray);            else                threshold = IterativeThreshold(GrayArray);            // 根据阈值进行二值化            Int32 PixelHeight = bitmap.PixelHeight;            Int32 PixelWidth = bitmap.PixelWidth;            Byte[,] BinaryArray = new Byte[PixelHeight, PixelWidth];            for (Int32 i = 0; i < PixelHeight; i++)            {                for (Int32 j = 0; j < PixelWidth; j++)                {                    BinaryArray[i,j] = Convert.ToByte((GrayArray[i,j] > threshold) ? 255 : 0);                }            }            return BinaryArray;        }        /// <summary>        /// 全局阈值图像二值化        /// </summary>        /// <param name="bitmap">原始图像</param>        /// <param name="method">二值化方法</param>        /// <param name="threshold">输出:全局阈值</param>        /// <returns>二值化图像</returns>        public static BitmapSource ToBinaryBitmap(this BitmapSource bitmap, BinarizationMethods method, out Int32 threshold)        {   // 位图转换为灰度数组            Byte[,] GrayArray = bitmap.ToGrayArray();            // 计算全局阈值            if (method == BinarizationMethods.Otsu)                threshold = OtsuThreshold(GrayArray);            else                threshold = IterativeThreshold(GrayArray);            // 将灰度数组转换为二值数据            Int32 PixelHeight = bitmap.PixelHeight;            Int32 PixelWidth = bitmap.PixelWidth;            Int32 Stride = ((PixelWidth + 31) >> 5) << 2;            Byte[] Pixels = new Byte[PixelHeight * Stride];            for (Int32 i = 0; i < PixelHeight; i++)            {                Int32 Base = i * Stride;                for (Int32 j = 0; j < PixelWidth; j++)                {                    if (GrayArray[i, j] > threshold)                    {                        Pixels[Base + (j >> 3)] |= Convert.ToByte(0x80 >> (j & 0x7));                    }                }            }            // 从灰度数据中创建灰度图像            return BitmapSource.Create(PixelWidth, PixelHeight, 96, 96, PixelFormats.Indexed1, BitmapPalettes.BlackAndWhite, Pixels, Stride);        }    }}