博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c#图像灰度化、灰度反转、二值化
阅读量:4302 次
发布时间:2019-05-27

本文共 2847 字,大约阅读时间需要 9 分钟。

图像灰度化

将彩色图像转化成为灰度图像的过程成为图像的灰度化处理。彩色图像中的每个像素的颜色有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;

        }

转载地址:http://zzlws.baihongyu.com/

你可能感兴趣的文章
CocoaPods安装和使用教程
查看>>
Beginning Auto Layout Tutorial
查看>>
block使用小结、在arc中使用block、如何防止循环引用
查看>>
iPhone开发学习笔记002——Xib设计UITableViewCell然后动态加载
查看>>
iOS开发中遇到的问题整理 (一)
查看>>
Swift code into Object-C 出现 ***-swift have not found this file 的问题
查看>>
为什么你的App介绍写得像一坨翔?
查看>>
RTImageAssets插件--@3x可自动生成@2x图片
查看>>
iOS开发的一些奇巧淫技
查看>>
常浏览的博客和网站
查看>>
Xcode 工程文件打开不出来, cannot be opened because the project file cannot be parsed.
查看>>
iOS在Xcode6中怎么创建OC category文件
查看>>
5、JavaWeb学习之基础篇—标签(自定义&JSTL)
查看>>
8、JavaWEB学习之基础篇—文件上传&下载
查看>>
reRender属性的使用
查看>>
href="javascript:void(0)"
查看>>
h:panelGrid、h:panelGroup标签学习
查看>>
f:facet标签 的用法
查看>>
<h:panelgroup>相当于span元素
查看>>
java中append()的方法
查看>>