Python实现图像去噪(中值去噪和均值去噪)

实现对图像进行简单的高斯去噪和椒盐去噪。代码如下:

import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import random
import scipy.misc
import scipy.signal
import scipy.ndimage
from matplotlib.font_manager import FontProperties
font_set = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=10)

def medium_filter(im, x, y, step):
    sum_s = []
    for k in range(-int(step / 2), int(step / 2) + 1):
        for m in range(-int(step / 2), int(step / 2) + 1):
            sum_s.append(im[x + k][y + m])
    sum_s.sort()
    return sum_s[(int(step * step / 2) + 1)]


def mean_filter(im, x, y, step):
    sum_s = 0
    for k in range(-int(step / 2), int(step / 2) + 1):
        for m in range(-int(step / 2), int(step / 2) + 1):
            sum_s += im[x + k][y + m] / (step * step)
    return sum_s


def convert_2d(r):
    n = 3
    # 3*3 滤波器, 每个系数都是 1/9
    window = np.ones((n, n)) / n ** 2
    # 使用滤波器卷积图像
    # mode = same 表示输出尺寸等于输入尺寸
    # boundary 表示采用对称边界条件处理图像边缘
    s = scipy.signal.convolve2d(r, window, mode='same', boundary='symm')
    return s.astype(np.uint8)


def convert_3d(r):
    s_dsplit = []
    for d in range(r.shape[2]):
        rr = r[:, :, d]
        ss = convert_2d(rr)
        s_dsplit.append(ss)
    s = np.dstack(s_dsplit)
    return s


def add_salt_noise(img):
   # rows, cols, dims = img.shape
    #R = np.mat(img[:, :, 0])
   # G = np.mat(img[:, :, 1])
    #B = np.mat(img[:, :, 2])
    rows, cols= img.shape
    R = np.mat(img[:, :])
    G = np.mat(img[:, :])
    B = np.mat(img[:, :])

    Grey_sp = R * 0.299 + G * 0.587 + B * 0.114
    Grey_gs = R * 0.299 + G * 0.587 + B * 0.114

    snr = 0.9

    noise_num = int((1 - snr) * rows * cols)

    for i in range(noise_num):
        rand_x = random.randint(0, rows - 1)
        rand_y = random.randint(0, cols - 1)
        if random.randint(0, 1) == 0:
            Grey_sp[rand_x, rand_y] = 0
        else:
            Grey_sp[rand_x, rand_y] = 255
    #给图像加入高斯噪声
    Grey_gs = Grey_gs + np.random.normal(0, 48, Grey_gs.shape)
    Grey_gs = Grey_gs - np.full(Grey_gs.shape, np.min(Grey_gs))
    Grey_gs = Grey_gs * 255 / np.max(Grey_gs)
    Grey_gs = Grey_gs.astype(np.uint8)

    # 中值滤波
    Grey_sp_mf = scipy.ndimage.median_filter(Grey_sp, (7, 7))
    Grey_gs_mf = scipy.ndimage.median_filter(Grey_gs, (8, 8))

    # 均值滤波
    Grey_sp_me = convert_2d(Grey_sp)
    Grey_gs_me = convert_2d(Grey_gs)

    plt.subplot(321)
    plt.title('加入椒盐噪声',fontproperties=font_set)
    plt.imshow(Grey_sp, cmap='gray')
    plt.subplot(322)
    plt.title('加入高斯噪声',fontproperties=font_set)
    plt.imshow(Grey_gs, cmap='gray')

    plt.subplot(323)
    plt.title('中值滤波去椒盐噪声(8*8)',fontproperties=font_set)
    plt.imshow(Grey_sp_mf, cmap='gray')
    plt.subplot(324)
    plt.title('中值滤波去高斯噪声(8*8)',fontproperties=font_set)
    plt.imshow(Grey_gs_mf, cmap='gray')

    plt.subplot(325)
    plt.title('均值滤波去椒盐噪声',fontproperties=font_set)
    plt.imshow(Grey_sp_me, cmap='gray')
    plt.subplot(326)
    plt.title('均值滤波去高斯噪声',fontproperties=font_set)
    plt.imshow(Grey_gs_me, cmap='gray')
    plt.show()


def main():
    img = np.array(Image.open('E:/pycharm/GraduationDesign/Test/testthree.png'))
    add_salt_noise(img)


if __name__ == '__main__':
    main()

  效果如下

                                  

  • 26
    点赞
  • 224
    收藏
    觉得还不错? 一键收藏
  • 21
    评论
传统图像去噪和卷积神经网络图像去噪是两种不同的方法,下面我将为您介绍它们的对比。 传统图像去噪方法主要基于信号处理和图像处理的技术,常见的方法包括均值滤波、中值滤波、高斯滤波等。这些方法通过对图像进行滤波操作,去除图像中的噪声。传统图像去噪方法的优点是计算速度快,适用于一些简单的噪声类型。然,传统方法往往需要手动选择合适的滤波参数,并且对于复杂的噪声类型效果不佳。 卷积神经网络(Convolutional Neural Network,CNN)是一种深度学习模型,已经在图像处理领域取得了很大的成功。CNN可以通过学习大量的图像样本,自动提取图像中的特征,并进行噪声去除。相比传统方法,CNN具有以下优点: 1. 自动学习特征:CNN可以通过训练自动学习图像中的特征,无需手动选择滤波参数。 2. 对复杂噪声有较好的适应性:CNN可以通过大量的训练数据学习到复杂噪声的特征,对复杂噪声类型有较好的去除效果。 3. 可扩展性强:CNN可以通过增加网络层数和训练样本来提高去噪效果。 然而,卷积神经网络图像去噪也存在一些限制: 1. 需要大量的训练数据:CNN需要大量的带有噪声和无噪声对应的图像样本进行训练,以获得较好的去噪效果。 2. 计算资源要求高:CNN模型通常需要较大的计算资源和时间来进行训练和推理。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值