欢迎光临
我们一直在努力

【无标题】

OpenCV 图像滤波:椒盐噪声添加与均值、高斯、中值、方框滤波

一、前言

在数字图像处理中,由于摄像设备、环境光线等因素影响,图像中经常会出现噪声。

常见噪声类型:

  • 椒盐噪声
  • 高斯噪声
  • 随机噪声

为了降低噪声对图像质量的影响,需要使用滤波算法进行处理。

OpenCV 中常见的图像滤波方法:

  • 均值滤波(Blur)
  • 方框滤波(Box Filter)
  • 高斯滤波(Gaussian Blur)
  • 中值滤波(Median Blur)
  • 本文通过添加椒盐噪声,对比不同滤波方法的效果。


    二、添加椒盐噪声

    1. 椒盐噪声原理

    椒盐噪声会随机改变图像中的部分像素:

    • 黑色点:像素值为 0
    • 白色点:像素值为 255

    例如:

    原像素:

    120 130 140
    150 160 170

    加入噪声:

    120 255 140
    0 160 170


    2. 创建椒盐噪声函数

    import cv2
    import numpy as np

    def add_peppersalt_noise(image, n=10000):

    result = image.copy()

    # 获取图片高度和宽度
    h, w = image.shape[:2]

    # 生成n个噪声点
    for i in range(n):

    # 随机生成坐标
    x = np.random.randint(0, h)
    y = np.random.randint(0, w)

    # 随机产生黑点或者白点
    if np.random.randint(0, 2) == 0:

    # 椒噪声
    result[x, y] = 0

    else:

    # 盐噪声
    result[x, y] = 255

    return result


    三、读取图片并添加噪声

    image = cv2.imread('3.png')

    #显示原图
    cv2.imshow(
    'yuan tu',
    image
    )

    #添加椒盐噪声
    noise = add_peppersalt_noise(image)

    cv2.imshow(
    'noise',
    noise
    )

    运行效果:

    原图:

    清晰图片

    加入噪声:

    随机出现黑白点


    四、均值滤波(cv2.blur)

    1. 原理

    均值滤波又叫平均滤波。

    使用邻域像素平均值替代当前像素。

    例如:

    3×3区域:

    100 120 130
    110 140 150
    90 100 120

    计算平均:

    (100+120+130+110+140+150+90+100+120)/9

    作为中心像素的新值。


    2. OpenCV实现

    函数:

    cv2.blur(src, ksize)

    参数:

    参数含义
    src 输入图像
    ksize 卷积核大小

    3. 3×3均值滤波

    blur_1 = cv2.blur(
    noise,
    (3,3)
    )

    cv2.imshow(
    'blur_1',
    blur_1
    )

    特点:

    优点:

    • 图像较清晰

    缺点:

    • 去噪能力一般

    4. 63×63均值滤波

    blur_2 = cv2.blur(
    noise,
    (63,63)
    )

    cv2.imshow(
    'blur_2',
    blur_2
    )

    特点:

    优点:

    • 去噪效果明显

    缺点:

    • 图像模糊严重

    五、方框滤波(Box Filter)

    1. 函数

    cv2.boxFilter()

    格式:

    cv2.boxFilter(
    src,
    ddepth,
    ksize,
    normalize
    )

    参数:

    参数说明
    src 输入图片
    ddepth 输出图像深度
    ksize 卷积核大小
    normalize 是否归一化

    2. 归一化方框滤波

    代码:

    boxFilter_1 = cv2.boxFilter(
    noise,
    1,
    (3,3),
    normalize=True
    )

    效果:

    等价于均值滤波。

    计算:

    [
    dst=\\frac{sum}{n}
    ]


    3. 非归一化方框滤波

    代码:

    boxFilter_2 = cv2.boxFilter(
    noise,
    1,
    (3,3),
    normalize=False
    )

    区别:

    不会除以像素数量。

    结果:

    像素值可能快速增大。


    六、高斯滤波(Gaussian Blur)

    1. 原理

    高斯滤波使用高斯函数计算权重。

    中心像素权重大:

    中心区域影响大

    边缘像素权重小:

    影响较小

    相比均值滤波:

    • 平滑效果更自然
    • 边缘保持更好

    2. 函数

    cv2.GaussianBlur()

    格式:

    cv2.GaussianBlur(
    src,
    ksize,
    sigmaX
    )

    参数:

    参数说明
    src 输入图片
    ksize 卷积核大小
    sigmaX X方向标准差

    3. 代码

    GaussianB = cv2.GaussianBlur(
    noise,
    (3,3),
    1
    )

    cv2.imshow(
    'GaussianBlur',
    GaussianB
    )


    七、中值滤波(Median Blur)

    1. 原理

    中值滤波:

    将邻域像素排序:

    例如:

    10 20 200
    30 40 50
    60 70 80

    排序:

    10 20 30 40 50 60 70 80 200

    取中间值:

    50

    替换中心像素。


    2. 函数

    cv2.medianBlur()

    格式:

    cv2.medianBlur(
    src,
    ksize
    )


    3. 代码

    medianB = cv2.medianBlur(
    noise,
    3
    )

    cv2.imshow(
    'medianBlur',
    medianB
    )


    八、完整代码

    import cv2
    import numpy as np

    def add_peppersalt_noise(image,n=10000):

    result=image.copy()

    h,w=image.shape[:2]

    for i in range(n):

    x=np.random.randint(0,h)
    y=np.random.randint(0,w)

    if np.random.randint(0,2)==0:

    result[x,y]=0

    else:

    result[x,y]=255

    return result

    image=cv2.imread("3.png")

    noise=add_peppersalt_noise(image)

    #均值滤波
    blur=cv2.blur(
    noise,
    (3,3)
    )

    #方框滤波
    box=cv2.boxFilter(
    noise,
    1,
    (3,3),
    normalize=True
    )

    #高斯滤波
    gaussian=cv2.GaussianBlur(
    noise,
    (3,3),
    1
    )

    #中值滤波
    median=cv2.medianBlur(
    noise,
    3
    )

    cv2.imshow("noise",noise)
    cv2.imshow("blur",blur)
    cv2.imshow("box",box)
    cv2.imshow("gaussian",gaussian)
    cv2.imshow("median",median)

    cv2.waitKey(0)
    cv2.destroyAllWindows()


    九、不同滤波方法对比

    滤波方法函数特点适合噪声
    均值滤波 cv2.blur() 简单平滑,容易模糊 随机噪声
    方框滤波 cv2.boxFilter() 可控制是否归一化 图像平滑
    高斯滤波 cv2.GaussianBlur() 平滑自然,保边效果较好 高斯噪声
    中值滤波 cv2.medianBlur() 去除椒盐噪声效果最好 椒盐噪声

    十、总结

    本文学习了 OpenCV 中常用的图像滤波方法:

    • 使用随机方式添加椒盐噪声
    • 使用均值滤波进行平滑处理
    • 使用方框滤波控制像素平均
    • 使用高斯滤波进行自然模糊
    • 使用中值滤波去除椒盐噪声

    实际应用中:

    • 椒盐噪声 → 首选中值滤波
    • 高斯噪声 → 首选高斯滤波
    • 普通平滑 → 均值滤波即可

    这些滤波方法是后续学习边缘检测、图像分割、目标识别的重要基础。

    赞(0)
    未经允许不得转载:171主机测评 » 【无标题】
    分享到: 更多 (0)

    评论 抢沙发

    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址