【C#】二值化处理以及高斯模糊减轻毛刺效果

主要方法

		public Bitmap binaryzation(Bitmap bitmap)
        {
            //得到图形的宽度和长度  
            int width = bitmap.Width;
            int height = bitmap.Height;
            //创建二值化图像
            Bitmap binarymap = new Bitmap(bitmap);
            int avg = 0;
            int r = 0;
            int g = 0;
            int b= 0;
            for (int i = 0; i < width; i++)
            {
                for (int y = 0; y < height; y++)
                {
                    int col = binarymap.GetPixel(i, y).ToArgb();
                    r += (col & 0x00FF0000) >> 16; 
                    g += (col & 0x0000FF00) >> 8;
                    b += (col & 0x000000FF);
                }
            }

            int total = width * height;
            r = r / total;
            g = g / total;
            b = b / total;
            int avggray = (int)((float)r * 0.3 + (float)g * 0.59 +
                             (float)b * 0.11);
            var agecolor = Color.FromArgb(r, g, b);

            //依次循环,对图像的像素进行处理
            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    //得到当前像素的
                    int col = binarymap.GetPixel(i, j).ToArgb();
                    //得到alpha通道的  
                    var alpha = (int)(col & 0xFF000000);
                    //得到图像的像素RGB  blue0-8位,green9-16位,red17-24位

                    int red = (col & 0x00FF0000) >> 16;
                    int green = (col & 0x0000FF00) >> 8;
                    int blue = (col & 0x000000FF);
                    // 用公式X = 0.3×R+0.59×G+0.11×B计算出X代替原来的RGB  
                    int gray = (int)((float)red * 0.3 + (float)green * 0.59 +
                                     (float)blue * 0.11);
                    
                    //对图像进行二值化处理  
                    if (gray <= avggray)
                    {
                        binarymap.SetPixel(i, j, Color.FromArgb(red, green, blue));
                    }
                    else
                    {
                        binarymap.SetPixel(i, j, Color.White);
                    }
                }
            }

            return binarymap;
        }



        /// <summary>
        /// 模糊半径
        /// </summary>
        public int BlurRadius { get; private set; }
        private Bitmap SourceImage { get; set; }
        private List<double> BlurArray { get; set; }
        private int MaxWidth { get; set; }
        private int MaxHeight { get; set; }

        public BitmapHelper(int blurRadius)
        {
            BlurArray = new List<double>();
            this.BlurRadius = blurRadius;
            this.SetBlurArray();
        }

        /// <summary>
        /// 设置需要模糊的图片
        /// </summary>
        /// <param name="img"></param>
        public void SetSourceImage(Bitmap img)
        {
            this.SourceImage = img;
            this.MaxWidth = this.SourceImage.Width - 1;
            this.MaxHeight = this.SourceImage.Height - 1;
        }

        /// <summary>
        /// 获取模糊之后的图片
        /// </summary>
        /// <returns></returns>
        public Bitmap GetBlurImage()
        {
            if (this.SourceImage == null) return null;
            Bitmap newImage = new Bitmap(SourceImage.Width, SourceImage.Height);
            for (int y = 0; y < this.SourceImage.Height; y++)
            {
                for (int x = 0; x < this.SourceImage.Width; x++)
                {
                    var nC = GetBlurColor(x, y);
                    //return null;
                    newImage.SetPixel(x, y, nC);
                }
            }
            return newImage;
        }

        /// <summary>
        /// 获取高斯模糊的颜色值
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        private Color GetBlurColor(int x, int y)
        {
            double r = 0, g = 0, b = 0;
            int index = 0;
            for (var t = y - this.BlurRadius; t <= y + this.BlurRadius; t++)
            {
                for (var l = x - this.BlurRadius; l <= x + this.BlurRadius; l++)
                {
                    var color = GetDefautColor(l, t);
                    var weighValue = BlurArray[index];
                    r += color.R * weighValue;
                    g += color.G * weighValue;
                    b += color.B * weighValue;
                    index++;
                }
            }
            return Color.FromArgb((byte)r, (byte)g, (byte)b);
        }

        private Color GetDefautColor(int x, int y)
        {
            if (x < 0 && y < 0)
                return this.SourceImage.GetPixel(0, 0);
            else if (x < 0)
                return this.SourceImage.GetPixel(0, Math.Min(MaxHeight, y));
            else if (y < 0)
                return this.SourceImage.GetPixel(Math.Min(MaxWidth, x), 0);
            else
                return this.SourceImage.GetPixel(Math.Min(MaxWidth, x), Math.Min(MaxHeight, y));
        }

        private void SetBlurArray()
        {
            int blur = this.BlurRadius;
            double sum = 0;
            for (var y = blur; y >= blur * -1; y--)
            {
                for (var x = blur * -1; x <= blur; x++)
                {
                    var d = GetWeighing(x, y);
                    this.BlurArray.Add(d);
                    sum += d;
                }
            }
            for (var i = 0; i < this.BlurArray.Count; i++)
                this.BlurArray[i] = this.BlurArray[i] / sum;

            //sum = 0;
            //foreach (var item in this.BlurArray)
            //    sum += item;
        }

        /// <summary>
        /// 获取权重
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        private double GetWeighing(int x, int y)
        {
            double q = (this.BlurRadius * 2 + 1) / 2;
            return 1 / (2 * Math.PI * Math.Pow(q, 2)) * Math.Exp(-(x * x + y * y) / (2 * q * q));
        }

调用

var bitHelper = new BitmapHelper(1);
var newImg = bitHelper.binaryzation((Bitmap)Image.FromFile("Inked456_LI.jpg"));
bitHelper.SetSourceImage(newImg);
//获取高斯模糊后的图片
newImg = bitHelper.GetBlurImage();
newImg.MakeTransparent(Color.White);
string filePath = Path.Combine("Images");
if (!Directory.Exists(filePath))
{
    Directory.CreateDirectory(filePath);
}
string fileName = Path.Combine(filePath, DateTime.Now.ToString("HHmmss") + ".png");
newImg.Save(fileName, ImageFormat.Png);