WPF:图像处理(二)灰度化

215 篇文章 2 订阅

作者:Splash

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


[csharp] view plain copy
  1. /* ---------------------------------------------------------- 
  2. 文件名称:Gray.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.0    2012年04月16日 
  17.             图像灰度化 
  18. ------------------------------------------------------------ */  
  19. using System;  
  20. using System.Windows.Media;  
  21. using System.Windows.Media.Imaging;  
  22.   
  23. namespace Splash.Imaging  
  24. {  
  25.     /// <summary>  
  26.     /// 图像处理:灰度化  
  27.     /// </summary>  
  28.     public static class Gray  
  29.     {  
  30.         /// <summary>  
  31.         /// 将位图转换为彩色数组  
  32.         /// </summary>  
  33.         /// <param name="bitmap">原始位图</param>  
  34.         /// <returns>彩色数组</returns>  
  35.         /// <remarks>  
  36.         ///     1.扩展方法  
  37.         ///     2.忽视Alpha通道  
  38.         /// </remarks>  
  39.         public static Color[,] ToColorArray(this BitmapSource bitmap)  
  40.         {   // 将像素格式统一到Bgr32,并提取图像数据  
  41.             Int32 PixelHeight = bitmap.PixelHeight; // 图像高度  
  42.             Int32 PixelWidth = bitmap.PixelWidth;   // 图像宽度  
  43.             Int32 Stride = PixelWidth << 2;         // 扫描行跨距  
  44.             Byte[] Pixels = new Byte[PixelHeight * Stride];  
  45.             if (bitmap.Format == PixelFormats.Bgr32 || bitmap.Format == PixelFormats.Bgra32)  
  46.             {   // 拷贝像素数据  
  47.                 bitmap.CopyPixels(Pixels, Stride, 0);  
  48.             }  
  49.             else  
  50.             {   // 先进行像素格式转换,再拷贝像素数据  
  51.                 new FormatConvertedBitmap(bitmap, PixelFormats.Bgr32, null, 0).CopyPixels(Pixels, Stride, 0);  
  52.             }  
  53.   
  54.             // 将像素数据转换为彩色数组  
  55.             Color[,] ColorArray = new Color[PixelHeight, PixelWidth];  
  56.             for (Int32 i = 0; i < PixelHeight; i++)  
  57.             {  
  58.                 for (Int32 j = 0; j < PixelWidth; j++)  
  59.                 {  
  60.                     Int32 Index = i * Stride + (j << 2);  
  61.                     ColorArray[i, j].B = Pixels[Index];  
  62.                     ColorArray[i, j].G = Pixels[Index + 1];  
  63.                     ColorArray[i, j].R = Pixels[Index + 2];  
  64.                     ColorArray[i, j].A = Pixels[Index + 3];  
  65.                 }  
  66.             }  
  67.   
  68.             return ColorArray;  
  69.         }  
  70.   
  71.         /// <summary>  
  72.         /// 将位图转换为灰度数组(256级灰度)  
  73.         /// </summary>  
  74.         /// <param name="bitmap">原始位图</param>  
  75.         /// <returns>灰度数组</returns>  
  76.         /// <remarks>扩展方法</remarks>  
  77.         public static Byte[,] ToGrayArray(this BitmapSource bitmap)  
  78.         {   // 将像素格式统一到Bgr32,并提取图像数据  
  79.             Int32 PixelHeight = bitmap.PixelHeight; // 图像高度  
  80.             Int32 PixelWidth = bitmap.PixelWidth;   // 图像宽度  
  81.             Int32 Stride = PixelWidth << 2;         // 扫描行跨距  
  82.             Byte[] Pixels = new Byte[PixelHeight * Stride];  
  83.             if (bitmap.Format == PixelFormats.Bgr32 || bitmap.Format == PixelFormats.Bgra32)  
  84.             {   // 拷贝像素数据  
  85.                 bitmap.CopyPixels(Pixels, Stride, 0);  
  86.             }  
  87.             else  
  88.             {   // 先进行像素格式转换,再拷贝像素数据  
  89.                 new FormatConvertedBitmap(bitmap, PixelFormats.Bgr32, null, 0).CopyPixels(Pixels, Stride, 0);  
  90.             }  
  91.   
  92.             // 将像素数据转换为灰度数组  
  93.             Byte[,] GrayArray = new Byte[PixelHeight, PixelWidth];  
  94.             for (Int32 i = 0; i < PixelHeight; i++)  
  95.             {  
  96.                 for (Int32 j = 0; j < PixelWidth; j++)  
  97.                 {  
  98.                     Int32 Index = i * Stride + (j << 2);                      
  99.                     GrayArray[i, j] = Convert.ToByte((Pixels[Index + 2] * 19595 + Pixels[Index + 1] * 38469 + Pixels[Index] * 7471 + 32768) >> 16);  
  100.                 }  
  101.             }  
  102.   
  103.             return GrayArray;  
  104.         }  
  105.   
  106.         /// <summary>  
  107.         /// 位图灰度化  
  108.         /// </summary>  
  109.         /// <param name="bitmap">原始位图</param>  
  110.         /// <returns>灰度位图</returns>  
  111.         /// <remarks>扩展方法</remarks>  
  112.         public static BitmapSource ToGrayBitmap(this BitmapSource bitmap)  
  113.         {   // 将像素格式统一到Bgr32,并提取图像数据  
  114.             Int32 PixelHeight = bitmap.PixelHeight; // 图像高度  
  115.             Int32 PixelWidth = bitmap.PixelWidth;   // 图像宽度  
  116.             Int32 Stride = PixelWidth << 2;         // 扫描行跨距  
  117.             Byte[] Pixels = new Byte[PixelHeight * Stride];  
  118.             if (bitmap.Format == PixelFormats.Bgr32 || bitmap.Format == PixelFormats.Bgra32)  
  119.             {   // 拷贝像素数据  
  120.                 bitmap.CopyPixels(Pixels, Stride, 0);  
  121.             }  
  122.             else  
  123.             {   // 先进行像素格式转换,再拷贝像素数据  
  124.                 new FormatConvertedBitmap(bitmap, PixelFormats.Bgr32, null, 0).CopyPixels(Pixels, Stride, 0);  
  125.             }  
  126.   
  127.             // 将像素数据转换为灰度数据  
  128.             Int32 GrayStride = ((PixelWidth + 3) >> 2) << 2;  
  129.             Byte[] GrayPixels = new Byte[PixelHeight * GrayStride];  
  130.             for (Int32 i = 0; i < PixelHeight; i++)  
  131.             {  
  132.                 for (Int32 j = 0; j < PixelWidth; j++)  
  133.                 {  
  134.                     Int32 Index = i * Stride + (j << 2);  
  135.                     GrayPixels[i * GrayStride + j] = Convert.ToByte((Pixels[Index + 2] * 19595 + Pixels[Index + 1] * 38469 + Pixels[Index] * 7471 + 32768) >> 16);  
  136.                 }  
  137.             }  
  138.   
  139.             // 从灰度数据中创建灰度图像  
  140.             return BitmapSource.Create(PixelWidth, PixelHeight, 96, 96, PixelFormats.Indexed8, BitmapPalettes.Gray256, GrayPixels, GrayStride);  
  141.         }       
  142.   
  143.         /// <summary>  
  144.         /// 将灰度数组转换为灰度图像(256级灰度)  
  145.         /// </summary>  
  146.         /// <param name="grayArray">灰度数组</param>  
  147.         /// <returns>灰度图像</returns>  
  148.         public static BitmapSource GrayArrayToGrayBitmap(Byte[,] grayArray)  
  149.         {   // 将灰度数组转换为灰度数据  
  150.             Int32 PixelHeight = grayArray.GetLength(0);     // 图像高度  
  151.             Int32 PixelWidth = grayArray.GetLength(1);      // 图像宽度  
  152.             Int32 Stride = ((PixelWidth + 3) >> 2) << 2;        // 扫描行跨距  
  153.             Byte[] Pixels = new Byte[PixelHeight * Stride];  
  154.             for (Int32 i = 0; i < PixelHeight; i++)  
  155.             {  
  156.                 for (Int32 j = 0; j < PixelWidth; j++)  
  157.                 {  
  158.                     Pixels[i * Stride + j] = grayArray[i, j];  
  159.                 }  
  160.             }  
  161.   
  162.             // 从灰度数据中创建灰度图像  
  163.             return BitmapSource.Create(PixelWidth, PixelHeight, 96, 96, PixelFormats.Indexed8, BitmapPalettes.Gray256, Pixels, Stride);  
  164.         }  
  165.   
  166.         /// <summary>  
  167.         /// 将二值化数组转换为二值化图像  
  168.         /// </summary>  
  169.         /// <param name="binaryArray">二值化数组</param>  
  170.         /// <returns>二值化图像</returns>  
  171.         public static BitmapSource BinaryArrayToBinaryBitmap(Byte[,] binaryArray)  
  172.         {   // 将二值化数组转换为二值化数据  
  173.             Int32 PixelHeight = binaryArray.GetLength(0);  
  174.             Int32 PixelWidth = binaryArray.GetLength(1);  
  175.             Int32 Stride = ((PixelWidth + 31) >> 5) << 2;  
  176.             Byte[] Pixels = new Byte[PixelHeight * Stride];  
  177.             for (Int32 i = 0; i < PixelHeight; i++)  
  178.             {  
  179.                 Int32 Base = i * Stride;  
  180.                 for (Int32 j = 0; j < PixelWidth; j++)  
  181.                 {  
  182.                     if (binaryArray[i, j] != 0)  
  183.                     {  
  184.                         Pixels[Base + (j >> 3)] |= Convert.ToByte(0x80 >> (j & 0x7));  
  185.                     }  
  186.                 }  
  187.             }  
  188.   
  189.             // 从灰度数据中创建灰度图像  
  190.             return BitmapSource.Create(PixelWidth, PixelHeight, 96, 96, PixelFormats.Indexed1, BitmapPalettes.BlackAndWhite, Pixels, Stride);  
  191.         }  
  192.     }  
  193. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值