基于c#图像灰度化、灰度反转、二值化的实现方法详解

 更新时间:2020年6月25日 11:41  点击:1831
图像灰度化:
将彩色图像转化成为灰度图像的过程成为图像的灰度化处理。彩色图像中的每个像素的颜色有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,以这个亮度值表达图像的灰度值。
复制代码 代码如下:

/// <summary>
      /// 图像灰度化
      /// </summary>
      /// <param name="bmp"></param>
      /// <returns></returns>
      public static Bitmap ToGray(Bitmap bmp)
      {
          for (int i = 0; i < bmp.Width; i++)
          {
              for (int j = 0; j < bmp.Height; j++)
              {
                  //获取该点的像素的RGB的颜色
                  Color color = bmp.GetPixel(i, j);
                  //利用公式计算灰度值
                  int gray = (int)(color.R * 0.3 + color.G * 0.59 + color.B * 0.11);
                  Color newColor = Color.FromArgb(gray, gray, gray);
                  bmp.SetPixel(i, j, newColor);
              }
          }
          return bmp;
      }

灰度反转:
把每个像素点的R、G、B三个分量的值0的设为255,255的设为0。
复制代码 代码如下:

/// <summary>
      /// 图像灰度反转
      /// </summary>
      /// <param name="bmp"></param>
      /// <returns></returns>
      public static Bitmap GrayReverse(Bitmap bmp)
      {
          for (int i = 0; i < bmp.Width; i++)
          {
              for (int j = 0; j < bmp.Height; j++)
              {
                  //获取该点的像素的RGB的颜色
                  Color color = bmp.GetPixel(i, j);
                  Color newColor = Color.FromArgb(255 - color.R, 255 - color.G, 255 - color.B);
                  bmp.SetPixel(i, j, newColor);
              }
          }
          return bmp;
      }

灰度图像二值化:
在进行了灰度化处理之后,图像中的每个象素只有一个值,那就是象素的灰度值。它的大小决定了象素的亮暗程度。为了更加便利的开展下面的图像处理操作,还需要对已经得到的灰度图像做一个二值化处理。图像的二值化就是把图像中的象素根据一定的标准分化成两种颜色。在系统中是根据象素的灰度值处理成黑白两种颜色。和灰度化相似的,图像的二值化也有很多成熟的算法。它可以采用自适应阀值法,也可以采用给定阀值法。
复制代码 代码如下:

  /// <summary>
        /// 图像二值化1:取图片的平均灰度作为阈值,低于该值的全都为0,高于该值的全都为255
        /// </summary>
        /// <param name="bmp"></param>
        /// <returns></returns>
        public static Bitmap ConvertTo1Bpp1(Bitmap bmp)
        {
            int average = 0;
            for (int i = 0; i < bmp.Width; i++)
            {
                for (int j = 0; j < bmp.Height; j++)
                {
                    Color color = bmp.GetPixel(i, j);
                    average += color.B;                    
                }
            }
            average = (int)average / (bmp.Width * bmp.Height);

            for (int i = 0; i < bmp.Width; i++)
            {
                for (int j = 0; j < bmp.Height; j++)
                {
                    //获取该点的像素的RGB的颜色
                    Color color = bmp.GetPixel(i, j);
                    int value = 255 - color.B;
                    Color newColor = value > average ? Color.FromArgb(0, 0, 0): Color.FromArgb(255,

255, 255);                   
                    bmp.SetPixel(i, j, newColor);
                }
            }
            return bmp;
        }

        /// <summary>
        /// 图像二值化2
        /// </summary>
        /// <param name="img"></param>
        /// <returns></returns>
        public static Bitmap ConvertTo1Bpp2(Bitmap img)
        {
            int w = img.Width;
            int h = img.Height;
            Bitmap bmp = new Bitmap(w, h, PixelFormat.Format1bppIndexed);
            BitmapData data = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite,

PixelFormat.Format1bppIndexed);
            for (int y = 0; y < h; y++)
            {
                byte[] scan = new byte[(w + 7) / 8];
                for (int x = 0; x < w; x++)
                {
                    Color c = img.GetPixel(x, y);
                    if (c.GetBrightness() >= 0.5) scan[x / 8] |= (byte)(0x80 >> (x % 8));
                }
                Marshal.Copy(scan, 0, (IntPtr)((int)data.Scan0 + data.Stride * y), scan.Length);
            }
            return bmp;
        }

[!--infotagslink--]

相关文章