c#图像灰度化、灰度反转、二值化

215 篇文章 2 订阅

作者:gdjlc

转自:http://blog.csdn.net/gdjlc/article/details/8636606


图像灰度化:
将彩色图像转化成为灰度图像的过程成为图像的灰度化处理。彩色图像中的每个像素的颜色有R、G、B三个分量决定,而每个分量有255中值可取,这样一个像素点可以有1600多万(255*255*255)的颜色的变化范围。而灰度图像是R、G、B三个分量相同的一种特殊的彩色图像,其一个像素点的变化范围为255种,所以在数字图像处理种一般先将各种格式的图像转变成灰度图像以使后续的图像的计算量变得少一些。灰度图像的描述与彩色图像一样仍然反映了整幅图像的整体和局部的色度和亮度等级的分布和特征。图像的灰度化处理可用两种方法来实现。
第一种方法使求出每个像素点的R、G、B三个分量的平均值,然后将这个平均值赋予给这个像素的三个分量。
第二种方法是根据YUV的颜色空间中,Y的分量的物理意义是点的亮度,由该值反映亮度等级,根据RGB和YUV颜色空间的变化关系可建立亮度Y与R、G、B三个颜色分量的对应:Y=0.3R+0.59G+0.11B,以这个亮度值表达图像的灰度值。

  1.  /// <summary>  
  2. /// 图像灰度化  
  3. /// </summary>  
  4. /// <param name="bmp"></param>  
  5. /// <returns></returns>  
  6. public static Bitmap ToGray(Bitmap bmp)  
  7. {  
  8.     for (int i = 0; i < bmp.Width; i++)  
  9.     {  
  10.         for (int j = 0; j < bmp.Height; j++)  
  11.         {  
  12.             //获取该点的像素的RGB的颜色  
  13.             Color color = bmp.GetPixel(i, j);  
  14.             //利用公式计算灰度值  
  15.             int gray = (int)(color.R * 0.3 + color.G * 0.59 + color.B * 0.11);  
  16.             Color newColor = Color.FromArgb(gray, gray, gray);  
  17.             bmp.SetPixel(i, j, newColor);  
  18.         }  
  19.     }  
  20.     return bmp;  
  21. }  
灰度反转
把每个像素点的R、G、B三个分量的值0的设为255,255的设为0。

  1. /// <summary>  
  2. /// 图像灰度反转  
  3. /// </summary>  
  4. /// <param name="bmp"></param>  
  5. /// <returns></returns>  
  6. public static Bitmap GrayReverse(Bitmap bmp)  
  7. {  
  8.     for (int i = 0; i < bmp.Width; i++)  
  9.     {  
  10.         for (int j = 0; j < bmp.Height; j++)  
  11.         {  
  12.             //获取该点的像素的RGB的颜色  
  13.             Color color = bmp.GetPixel(i, j);  
  14.             Color newColor = Color.FromArgb(255 - color.R, 255 - color.G, 255 - color.B);  
  15.             bmp.SetPixel(i, j, newColor);  
  16.         }  
  17.     }  
  18.     return bmp;  
  19. }  
灰度图像二值化
在进行了灰度化处理之后,图像中的每个象素只有一个值,那就是象素的灰度值。它的大小决定了象素的亮暗程度。为了更加便利的开展下面的图像处理操作,还需要对已经得到的灰度图像做一个二值化处理。图像的二值化就是把图像中的象素根据一定的标准分化成两种颜色。在系统中是根据象素的灰度值处理成黑白两种颜色。和灰度化相似的,图像的二值化也有很多成熟的算法。它可以采用自适应阀值法,也可以采用给定阀值法。

  1.  /// <summary>  
  2.         /// 图像二值化1:取图片的平均灰度作为阈值,低于该值的全都为0,高于该值的全都为255  
  3.         /// </summary>  
  4.         /// <param name="bmp"></param>  
  5.         /// <returns></returns>  
  6.         public static Bitmap ConvertTo1Bpp1(Bitmap bmp)  
  7.         {  
  8.             int average = 0;  
  9.             for (int i = 0; i < bmp.Width; i++)  
  10.             {  
  11.                 for (int j = 0; j < bmp.Height; j++)  
  12.                 {  
  13.                     Color color = bmp.GetPixel(i, j);  
  14.                     average += color.B;                      
  15.                 }  
  16.             }  
  17.             average = (int)average / (bmp.Width * bmp.Height);  
  18.   
  19.             for (int i = 0; i < bmp.Width; i++)  
  20.             {  
  21.                 for (int j = 0; j < bmp.Height; j++)  
  22.                 {  
  23.                     //获取该点的像素的RGB的颜色  
  24.                     Color color = bmp.GetPixel(i, j);  
  25.                     int value = 255 - color.B;  
  26.                     Color newColor = value > average ? Color.FromArgb(0, 0, 0): Color.FromArgb(255,  
  27.   
  28. 255, 255);                     
  29.                     bmp.SetPixel(i, j, newColor);  
  30.                 }  
  31.             }  
  32.             return bmp;  
  33.         }  
  34.           
  35.         /// <summary>  
  36.         /// 图像二值化2  
  37.         /// </summary>  
  38.         /// <param name="img"></param>  
  39.         /// <returns></returns>  
  40.         public static Bitmap ConvertTo1Bpp2(Bitmap img)  
  41.         {  
  42.             int w = img.Width;  
  43.             int h = img.Height;  
  44.             Bitmap bmp = new Bitmap(w, h, PixelFormat.Format1bppIndexed);  
  45.             BitmapData data = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite,  
  46.   
  47. PixelFormat.Format1bppIndexed);  
  48.             for (int y = 0; y < h; y++)  
  49.             {  
  50.                 byte[] scan = new byte[(w + 7) / 8];  
  51.                 for (int x = 0; x < w; x++)  
  52.                 {  
  53.                     Color c = img.GetPixel(x, y);  
  54.                     if (c.GetBrightness() >= 0.5) scan[x / 8] |= (byte)(0x80 >> (x % 8));  
  55.                 }  
  56.                 Marshal.Copy(scan, 0, (IntPtr)((int)data.Scan0 + data.Stride * y), scan.Length);  
  57.             }  
  58.             return bmp;  
  59.         }  

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值