图像处理学习笔记(1)——图像滤波

参考文章
https://blog.csdn.net/zaishuiyifangxym/article/details/89788020
https://www.cnblogs.com/zbjuke/p/10304863.html
https://www.cnblogs.com/henuliulei/p/10559954.html

图像滤波

图像的频率代表了图像颜色变化的剧烈程度。
低频分量:一幅图中,颜色变化缓慢的部分就叫做低频部分。通常低频是描述图像的主要部分。
高频分量:一幅图中,颜色变化剧烈的部分就叫做高频部分。通常高频是描述图像的边缘、细节或者是噪声。

均值滤波

均值滤波是指任意一点的像素值,都是周围N×M个像素值的均值。例如下图中,红色点的像素值是其周围蓝色背景区域像素值之和除25,25=5×5 是蓝色区域的大小。
并且,5×5的矩阵称为核,针对原始图像内的像素点,采用核进行处理,得到结果图像。
随着核大小逐渐变大,会让图像变得更加模糊。
如果设置为核大小为(1,1),则结果就是原始图像。
在这里插入图片描述

中值滤波

中值滤波是非线性的图像处理方法,在去噪的同时可以兼顾到边界信息的保留。选一个含有奇数点的窗口W,将这个窗口在图像上扫描,把窗口中所含的像素点按灰度级的升或降序排列,取位于中间的灰度值来代替该点的灰度值。
随着核大小逐渐变大,会让图像变得更加模糊。
核必须是大于1的奇数,如3、5、7等。
在这里插入图片描述

高斯滤波

图像高斯平滑也是邻域平均的思想对图像进行平滑的一种方法,在图像高斯平滑中,对图像进行平均时,不同位置的像素被赋予了不同的权重。高斯平滑与简单平滑不同,它在对邻域内像素进行平均时,给予不同位置的像素不同的权值。
随着核大小逐渐变大,会让图像变得更加模糊。
核大小(N, N)必须是大于1的奇数,如3、5、7等。
在这里插入图片描述

效果图

均值滤波效果图,核为(5,5):
在这里插入图片描述
中值滤波效果图,核为(5,5):
在这里插入图片描述
高斯滤波效果图,核为(5,5):
在这里插入图片描述

滤波算法

高斯滤波(高斯模糊,高斯平滑)

作用 ——通过高斯平滑使整个图片过渡均匀平滑,去除细节,过滤掉噪声。
二维高斯分布公式
在这里插入图片描述
高斯平滑滤波器用来模糊图像,和均值滤波器差不多,但是和均值滤波器不一样的地方就是核不同。均值滤波器的核每一个值都是相等,而高斯平滑滤波器的核内的数却是呈现高斯分布的。

在网上找到了一个高斯模糊的算法,经试验可以用,记录一下方便之后使用。
算法基于二维高斯分布公式:

//主函数
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespace NetShadow
{
    class Program
    {
        static void Main(string[] args)
        {
            //导入图片
            Image img = Image.FromFile(@"E:/Desktop/66n.jpg");
            //设置模糊半径
            GaussianBlur gBlur = new GaussianBlur(3);
            //设置需要模糊的图片
            gBlur.SetSourceImage(img);
            //获得模糊后的图片
            Bitmap newImage = gBlur.GetBlurImage();
            //输出图片
            if (newImage != null)
                newImage.Save(@"E:\Desktop\66ns.jpg");
            else
                Console.WriteLine("高斯模糊失败!");

        }
    }
}
//新建一个类
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespace NetShadow
{
    /// <summary>
    /// 高斯模糊
    /// </summary>
    public class GaussianBlur
    {
        /// <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 GaussianBlur(int blurRadius)
        {
            //初始化泛型List<T>,创建指定类型的集合
            BlurArray = new List<double>();
            this.BlurRadius = blurRadius;
            //计算模糊半径范围内所有值的权重
            this.SetBlurArray();
        }
        /// <summary>
        /// 设置需要模糊的图片
        /// </summary>
        /// <param name="img"></param>
        public void SetSourceImage(Image img)
        {
            //得到图片
            this.SourceImage = (Bitmap)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新实例
            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];
                    //RGB原值分别乘权重值得到模糊后的值
                    r += color.R * weighValue;
                    g += color.G * weighValue;
                    b += color.B * weighValue;
                    index++;
                }
            }
            //从指定的颜色值中创建Color结构
            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);
                    //将权重值加到List<T>的结尾处
                    this.BlurArray.Add(d);
                    //计算所有值的总和,这里和应该小于1
                    sum += d;
                }
            }
            //将模糊半径范围内的所有值分别除以所有值总和sum,使得他们的和为1
            for (var i = 0; i < this.BlurArray.Count; i++)
                this.BlurArray[i] = this.BlurArray[i] / sum;
        }
        /// <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));
        }
    }
}

效果图(模糊半径分别为1和2)
在这里插入图片描述
在这里插入图片描述
可以看到,模糊半径越大,去噪效果越好。但是图片也会变得越模糊,所以应该根据需求选择合适的模糊半径。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值