Python-图像-添加噪声

目录

1.椒盐噪声

2.高斯噪声


1.椒盐噪声

椒盐噪声:噪声幅度基本相同(0或255),出现位置随机

def add_noise_salt_pepper(img, salt, pepper=None):
    """添加椒盐噪声
    :param img:输入灰度图像
    :param salt:salt的概率
    :param pepper:pepper的概率
    :return:img:加入椒盐噪声后的图像
    """
    if pepper==None:pepper = 1 - salt
    row, col = img.shape

    for x in range(row):
        for y in range(col ):
            r = random.random()
            if r > salt:
                img[x][y] = 255
            elif r < pepper:
                img[x][y] = 0
            else:
                img[x][y] = img[x][y]

    return img

2.高斯噪声

高斯噪声:概率密度函数(幅度值)服从高斯分布(正态分布),噪声出现在图像的各个点上

def add_noise_Gauss(img,loc=0,scale=0.005):
    """
    添加高斯噪声
    :param img: 输入灰度图像
    :param loc: 高斯均值
    :param scale: 高斯方差
    :param scale**2:高斯标准差
    :return: img:加入高斯噪声后的图像
    """

    img = np.float32(img)/255  # (0-255)->(0-1)
    Gauss_noise =np.random.normal(loc,scale**0.5,img.shape)
    img = img + Gauss_noise  # 加性噪声
    if img.min()<0:
        low_clip = -1
    else:
        low_clip = 0
    img = np.clip(img,low_clip,1.0)  # 约束结果在(0-1)
    img = np.uint8(img*255)  # (0-1)->(0-255)
   
    return img

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值