C#将RGB图像转换为8位灰度图像

 项目需要将RGB图像转换为8位灰度图像,之前不了解图像格式,以为只要对像素进行灰度化就能获得灰度图像,以下代码使用System.Drawing.Imaging.ColorMatrix类配合System.Drawing.Imaging.ImageAttributes类对组成一个5 x 5的线性转换,转换 ARGB 单色值,再使用GDI+获得新图像。

[csharp]  view plain  copy
  1. /// <summary>  
  2. /// 将源图像灰度化,但是没有转化为8位灰度图像。  
  3. /// http://www.bobpowell.net/grayscale.htm  
  4. /// </summary>  
  5. /// <param name="original"> 源图像。 </param>  
  6. /// <returns> 灰度RGB图像。 </returns>  
  7. public static Bitmap MakeGrayScale(Bitmap original)  
  8. {  
  9.     //create a blank bitmap the same size as original  
  10.     Bitmap newBitmap = new Bitmap(original.Width, original.Height, PixelFormat.Format24bppRgb);  
  11.   
  12.     //get a graphics object from the new image  
  13.     Graphics g = Graphics.FromImage(newBitmap);  
  14.   
  15.     //create the grayscale ColorMatrix  
  16.     ColorMatrix colorMatrix = new ColorMatrix(  
  17.         new float[][]   
  18.         {  
  19.             new float[] { .3f, .3f, .3f, 0, 0 },  
  20.             new float[] { .59f, .59f, .59f, 0, 0 },  
  21.             new float[] { .11f, .11f, .11f, 0, 0 },  
  22.             new float[] { 0, 0, 0, 1, 0 },  
  23.             new float[] { 0, 0, 0, 0, 1 }  
  24.         });  
  25.     /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
  26.     ┌                          ┐ 
  27.     │  0.3   0.3   0.3   0   0 │ 
  28.     │ 0.59  0.59  0.59   0   0 │ 
  29.     │ 0.11  0.11  0.11   0   0 │ 
  30.     │    0     0     0   1   0 │ 
  31.     │    0     0     0   0   1 │ 
  32.     └                          ┘ 
  33.      * * * * * * * * * * * * * * * * * * * * * * * * * * * * */  
  34.     //create some image attributes  
  35.     ImageAttributes attributes = new ImageAttributes();  
  36.   
  37.     //set the color matrix attribute  
  38.     attributes.SetColorMatrix(colorMatrix);  
  39.   
  40.     //draw the original image on the new image  
  41.     //using the grayscale color matrix  
  42.     g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),  
  43.        0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);  
  44.   
  45.     //dispose the Graphics object  
  46.     g.Dispose();  
  47.     return newBitmap;  
  48. }  

        这种方法无需手动操作图像数据,也不用考虑图像扫描宽度等因素,能非常高效、鲁棒的进行灰度化,但是,灰度化后依旧是RGB图像,PixelFormat值依旧为Format24bppRgb。只不过三个通道的值都变成了T = 0.3R + 0.59G + 0.11B。


图1. 灰度化后依旧是RGB图像,PixelFormat值

        需要的8位灰度图像的PixelFormat值为Format8bppIndexed,该格式指定每像素8位,因此不方便将RGB图像直接修改成8位灰度图像,需要创建一个新的8位灰度图像。

       

图2. DotNet中灰度图像参数值

        Format8bppIndexed为索引格式,已经创建索引。因此颜色表中有 256 种颜色。实际是伪彩颜色。可以看到灰度图像的调色板是灰度的,即Palette.Entries中每个项的RGB值都相等。因此,创建一个新的8位灰度图像是不够的,还需要修改灰度位图的索引表。

不修改索引表的话,有些操作后图像可能出现类似红外图像那样色彩斑斓的伪彩图像。如下面的实例所示:

    (1)随便在桌面截取一幅任意尺寸的图像,先进行灰度化,然后用画图程序将灰度化后的RGB图像(Format24bppRgb,256灰度)直接转化为256色灰度图像。


图3.用画图程序将一幅灰度化后的RGB图像转化为256色灰度


        转化后参数如下图所示,可以发现图像格式已变为Format8bppIndexed索引格式,但是图像的索引表中每项的RGB值不相同。

图4. 用画图程序转化为256色灰度后图像的参数值

    (2)用转化后的图像进行直方图均衡化,可以看到出现了伪彩色。因为转化后“灰度”图像的Format8bppIndexed的索引表并非灰度,而是伪彩的。

图5. 均衡化后出现伪彩色 

        以下算法先将RGB(以Format24bppRgb为例)图像灰度化,然后得到灰度图像的灰度数组,最后构建一个8位灰度图像。

[csharp]  view plain  copy
  1. /// <summary>  
  2. /// 将源图像灰度化,并转化为8位灰度图像。  
  3. /// </summary>  
  4. /// <param name="original"> 源图像。 </param>  
  5. /// <returns> 8位灰度图像。 </returns>  
  6. public static Bitmap RgbToGrayScale(Bitmap original)  
  7. {  
  8.      if (original != null)  
  9.      {  
  10.           // 将源图像内存区域锁定  
  11.           Rectangle rect = new Rectangle(0, 0, original.Width, original.Height);  
  12.           BitmapData bmpData = original.LockBits(rect, ImageLockMode.ReadOnly,  
  13.                original.PixelFormat);  
  14.   
  15.           // 获取图像参数  
  16.           int width = bmpData.Width;  
  17.           int height = bmpData.Height;  
  18.           int stride = bmpData.Stride;  // 扫描线的宽度  
  19.           int offset = stride - width * 3;  // 显示宽度与扫描线宽度的间隙  
  20.           IntPtr ptr = bmpData.Scan0;   // 获取bmpData的内存起始位置  
  21.           int scanBytes = stride * height;  // 用stride宽度,表示这是内存区域的大小  
  22.   
  23.           // 分别设置两个位置指针,指向源数组和目标数组  
  24.           int posScan = 0, posDst = 0;  
  25.           byte[] rgbValues = new byte[scanBytes];  // 为目标数组分配内存  
  26.           Marshal.Copy(ptr, rgbValues, 0, scanBytes);  // 将图像数据拷贝到rgbValues中  
  27.           // 分配灰度数组  
  28.           byte[] grayValues = new byte[width * height]; // 不含未用空间。  
  29.           // 计算灰度数组  
  30.           for (int i = 0; i < height; i++)  
  31.           {  
  32.                for (int j = 0; j < width; j++)  
  33.                {  
  34.                     double temp = rgbValues[posScan++] * 0.11 +  
  35.                         rgbValues[posScan++] * 0.59 +   
  36.                         rgbValues[posScan++] * 0.3;  
  37.                     grayValues[posDst++] = (byte)temp;  
  38.                }  
  39.                // 跳过图像数据每行未用空间的字节,length = stride - width * bytePerPixel  
  40.                posScan += offset;  
  41.           }  
  42.   
  43.           // 内存解锁  
  44.           Marshal.Copy(rgbValues, 0, ptr, scanBytes);  
  45.           original.UnlockBits(bmpData);  // 解锁内存区域  
  46.   
  47.           // 构建8位灰度位图  
  48.           Bitmap retBitmap = BuiltGrayBitmap(grayValues, width, height);  
  49.           return retBitmap;  
  50.      }  
  51.      else  
  52.      {  
  53.           return null;  
  54.      }  
  55. }  
  56.   
  57. /// <summary>  
  58. /// 用灰度数组新建一个8位灰度图像。  
  59. /// http://www.cnblogs.com/spadeq/archive/2009/03/17/1414428.html  
  60. /// </summary>  
  61. /// <param name="rawValues"> 灰度数组(length = width * height)。 </param>  
  62. /// <param name="width"> 图像宽度。 </param>  
  63. /// <param name="height"> 图像高度。 </param>  
  64. /// <returns> 新建的8位灰度位图。 </returns>  
  65. private static Bitmap BuiltGrayBitmap(byte[] rawValues, int width, int height)  
  66. {  
  67.      // 新建一个8位灰度位图,并锁定内存区域操作  
  68.      Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format8bppIndexed);  
  69.      BitmapData bmpData = bitmap.LockBits(new Rectangle(0, 0, width, height),  
  70.           ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);  
  71.               
  72.      // 计算图像参数  
  73.      int offset = bmpData.Stride - bmpData.Width;        // 计算每行未用空间字节数  
  74.      IntPtr ptr = bmpData.Scan0;                         // 获取首地址  
  75.      int scanBytes = bmpData.Stride * bmpData.Height;    // 图像字节数 = 扫描字节数 * 高度  
  76.      byte[] grayValues = new byte[scanBytes];            // 为图像数据分配内存  
  77.               
  78.      // 为图像数据赋值  
  79.      int posSrc = 0, posScan = 0;                        // rawValues和grayValues的索引  
  80.      for (int i = 0; i < height; i++)  
  81.      {  
  82.            for (int j = 0; j < width; j++)  
  83.            {  
  84.                 grayValues[posScan++] = rawValues[posSrc++];  
  85.            }  
  86.            // 跳过图像数据每行未用空间的字节,length = stride - width * bytePerPixel  
  87.            posScan += offset;  
  88.      }  
  89.   
  90.      // 内存解锁  
  91.      Marshal.Copy(grayValues, 0, ptr, scanBytes);  
  92.      bitmap.UnlockBits(bmpData);  // 解锁内存区域  
  93.   
  94.      // 修改生成位图的索引表,从伪彩修改为灰度  
  95.      ColorPalette palette;  
  96.      // 获取一个Format8bppIndexed格式图像的Palette对象  
  97.      using (Bitmap bmp = new Bitmap(1, 1, PixelFormat.Format8bppIndexed))  
  98.      {  
  99.            palette = bmp.Palette;  
  100.      }  
  101.      for (int i = 0; i < 256; i++)  
  102.      {  
  103.            palette.Entries[i] = Color.FromArgb(i, i, i);  
  104.      }  
  105.      // 修改生成位图的索引表  
  106.      bitmap.Palette = palette;  
  107.   
  108.      return bitmap;  
  109. }  

         用上面算法将数字图像处理常用测试图像之一的PeppersRGB.bmp图像转换为8位灰度图像,并与PeppersRGB.bmp对应的灰度图像Peppers.bmp进行比对。

图6. 重构的8位灰度图像(上)和Peppers.bmp(下)对比


图7. Matlab中使用rgb2gray函数转换的8位灰度图像(上)和Peppers.bmp(下)对比

             Matlab中使用rgb2gray函数进行格式转换,源码见rgb2gray.m。rgb2gray的算法原理是将RGB色彩模型转为YIQ模型(北美NTSC彩色制式,灰度信息与彩色信息分离)。YIQ模型中,Y代表亮度、I代表色调、Q代表饱和度。转换后的Y分量即为灰度。

        转换公式为:



图8. Matlab.rgb2gray的转换矩阵T

        可以看到转换矩阵T的第一行就是灰度转换公式的系数。

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值