【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);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Halcon是一种广泛应用于机器视觉领域的软件库,它的二功能可以将图像转为黑白两个颜色的二图像。其二函数提供了几种不同的算法来实现二过程。以下是使用Halcon进行二的C语言代码示例: ``` #include <stdio.h> #include <stdlib.h> #include "Halcon.h" int main() { Hobject Image, BinaryImage; // 读取原始图像 ReadImage(&Image, "image.png"); // 进行二 Threshold(Image, &BinaryImage, 128, 255); // 保存二图像 WriteImage(BinaryImage, "binary_image.png"); // 释放图像对象 CloseImage(Image); CloseImage(BinaryImage); return 0; } ``` 以上代码中,首先包含了Halcon的头文件,并创建了用于存储原始图像和二图像的对象。然后使用“ReadImage”函数读取原始图像,并通过“Threshold”函数进行二处理。其中,阈参数设置为128和255,表示将原始图像中灰度大于128的像素设置为255(白色),灰度小于等于128的像素设置为0(黑色)。最后使用“WriteImage”函数将二图像保存到磁盘中,并释放图像对象。 此外,Halcon还提供了其他一些二函数,如自适应阈、Otsu阈等,可以根据实际需要选择合适的函数来实现特定的二效果。 ### 回答2: Halcon是一种用于机器视觉应用的软件库,提供了丰富的图像处理和分析功能。在Halcon中进行二操作可以将图像转换为黑白两种颜色的像素,便于后续的图像处理和分析。 在Halcon中进行二操作可以使用threshold函数,该函数需要两个参数:输入图像和阈。输入图像可以是灰度图像或彩色图像,阈可以是确定的数或是根据图像自适应计算得到的。 如果阈为确定的数,例如128,那么所有大于128的像素将被设置为白色,所有小于或等于128的像素将被设置为黑色。 如果阈是根据图像自适应计算得到的,可以使用函数gen_threshold,该函数需要三个参数:输入图像、参数1和参数2。调整参数1和参数2的可以控制二效果越大,则图像中的细节越多,但也可能引入噪声。 使用Halcon进行二操作可以帮助我们在图像中提取感兴趣的目标,例如文字、边缘等。二后的图像可以进行形态学处理、特征提取、目标识别等操作,进一步用于机器视觉领域的应用,例如文字识别、物体检测等。 总之,Halcon提供了丰富的二函数和方法,能够帮助我们快速、准确地进行图像二操作,为后续的图像处理和分析提供基础。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值