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

来源:互联网 发布:linux gtk 编辑:程序博客网 时间:2024/05/16 18:05

作者:Splash

转自:http://blog.csdn.net/jhqin/article/details/7472212


[csharp] view plaincopy
  1. /* ---------------------------------------------------------- 
  2. 文件名称:Binarize.cs 
  3.  
  4. 作者:秦建辉 
  5.  
  6. MSN:splashcn@msn.com 
  7. QQ:36748897 
  8.  
  9. 博客:http://blog.csdn.net/jhqin 
  10.  
  11. 开发环境: 
  12.     Visual Studio V2010 
  13.     .NET Framework 4 Client Profile 
  14.  
  15. 版本历史: 
  16.     V1.1    2012年04月17日 
  17.             实现迭代法阈值计算方法 
  18.  
  19.     V1.0    2012年04月16日 
  20.             实现大津法阈值计算方法 
  21. ------------------------------------------------------------ */  
  22. using System;  
  23. using System.Windows.Media;  
  24. using System.Windows.Media.Imaging;  
  25.   
  26. namespace Splash.Imaging  
  27. {  
  28.     /// <summary>  
  29.     /// 二值化方法  
  30.     /// </summary>  
  31.     public enum BinarizationMethods  
  32.     {          
  33.         Otsu,       // 大津法  
  34.         Iterative   // 迭代法  
  35.     }  
  36.   
  37.     /// <summary>  
  38.     /// 图像处理:图像二值化  
  39.     /// </summary>  
  40.     public static partial class Binarize  
  41.     {        
  42.         /// <summary>  
  43.         /// 全局阈值图像二值化  
  44.         /// </summary>  
  45.         /// <param name="bitmap">原始图像</param>  
  46.         /// <param name="method">二值化方法</param>  
  47.         /// <param name="threshold">输出:全局阈值</param>  
  48.         /// <returns>二值化后的图像数组</returns>          
  49.         public static Byte[,] ToBinaryArray(this BitmapSource bitmap, BinarizationMethods method, out Int32 threshold)  
  50.         {   // 位图转换为灰度数组  
  51.             Byte[,] GrayArray = bitmap.ToGrayArray();  
  52.               
  53.             // 计算全局阈值  
  54.             if (method == BinarizationMethods.Otsu)  
  55.                 threshold = OtsuThreshold(GrayArray);  
  56.             else  
  57.                 threshold = IterativeThreshold(GrayArray);  
  58.   
  59.             // 根据阈值进行二值化  
  60.             Int32 PixelHeight = bitmap.PixelHeight;  
  61.             Int32 PixelWidth = bitmap.PixelWidth;  
  62.             Byte[,] BinaryArray = new Byte[PixelHeight, PixelWidth];  
  63.             for (Int32 i = 0; i < PixelHeight; i++)  
  64.             {  
  65.                 for (Int32 j = 0; j < PixelWidth; j++)  
  66.                 {  
  67.                     BinaryArray[i,j] = Convert.ToByte((GrayArray[i,j] > threshold) ? 255 : 0);  
  68.                 }  
  69.             }  
  70.   
  71.             return BinaryArray;  
  72.         }  
  73.   
  74.         /// <summary>  
  75.         /// 全局阈值图像二值化  
  76.         /// </summary>  
  77.         /// <param name="bitmap">原始图像</param>  
  78.         /// <param name="method">二值化方法</param>  
  79.         /// <param name="threshold">输出:全局阈值</param>  
  80.         /// <returns>二值化图像</returns>  
  81.         public static BitmapSource ToBinaryBitmap(this BitmapSource bitmap, BinarizationMethods method, out Int32 threshold)  
  82.         {   // 位图转换为灰度数组  
  83.             Byte[,] GrayArray = bitmap.ToGrayArray();  
  84.   
  85.             // 计算全局阈值  
  86.             if (method == BinarizationMethods.Otsu)  
  87.                 threshold = OtsuThreshold(GrayArray);  
  88.             else  
  89.                 threshold = IterativeThreshold(GrayArray);  
  90.   
  91.             // 将灰度数组转换为二值数据  
  92.             Int32 PixelHeight = bitmap.PixelHeight;  
  93.             Int32 PixelWidth = bitmap.PixelWidth;  
  94.             Int32 Stride = ((PixelWidth + 31) >> 5) << 2;  
  95.             Byte[] Pixels = new Byte[PixelHeight * Stride];  
  96.             for (Int32 i = 0; i < PixelHeight; i++)  
  97.             {  
  98.                 Int32 Base = i * Stride;  
  99.                 for (Int32 j = 0; j < PixelWidth; j++)  
  100.                 {  
  101.                     if (GrayArray[i, j] > threshold)  
  102.                     {  
  103.                         Pixels[Base + (j >> 3)] |= Convert.ToByte(0x80 >> (j & 0x7));  
  104.                     }  
  105.                 }  
  106.             }  
  107.   
  108.             // 从灰度数据中创建灰度图像  
  109.             return BitmapSource.Create(PixelWidth, PixelHeight, 96, 96, PixelFormats.Indexed1, BitmapPalettes.BlackAndWhite, Pixels, Stride);  
  110.         }  
  111.     }  
  112. }  

0 0
原创粉丝点击