图像增强:椒盐噪声

最近做字符检测,ZZ有点痴迷于椒盐噪声,不仅要求只要白点,而且噪声点还不能太小了。

行吧,开始造。

1.椒盐噪声原理

椒盐噪声就是给图片添加黑白噪点,椒指的是黑色的噪点(0,0,0)盐指的是白色的噪点(255,255,255),通过设置amount来控制添加噪声的比例,值越大添加的噪声越多,图像损坏的更加严重

2.怎样生成椒盐噪声

def SaltAndPepper(src,percetage):
    SP_NoiseImg=src.copy()
    SP_NoiseNum=int(percetage*src.shape[0]*src.shape[1])
    for i in range(SP_NoiseNum):
        randR=np.random.randint(0, src.shape[0]-1)
        randG=np.random.randint(0, src.shape[1]-1)
        randB=np.random.randint(0, 3)
        # if np.random.randint(0,1)==0:
        #     SP_NoiseImg[randR,randG,randB]=0
        # else:
        #     SP_NoiseImg[randR,randG,randB]=255
        SP_NoiseImg[randR, randG, randB] = 255
    return SP_NoiseImg

3.怎样快速生成像素点大一点的盐噪声

def  add_salt_pepper(image, s_vs_p = 0.1, amount = 0.01, salt_flag = True, pepper_flag = False, num = 2):
    '''
    生成椒盐噪声
    :param image: 待处理的图片
    :param s_vs_p:设置添加椒盐噪声的数目比例
    :param amount:设置添加噪声图像像素的数目
    :param salt_flag:是否生成盐噪声的标志
    :param pepper_flag:是否生成椒噪声的标志
    :param num:控制生成的噪声点的大小
    :return:
    '''
    noisy_img = np.copy(image)
    if salt_flag:
        # 添加salt噪声
        num_salt = np.ceil(amount * image.size * s_vs_p)
        # 设置添加噪声的坐标位置
        # coords = [np.random.randint(0, i - 1, int(num_salt)) for i in image.shape]
        coords = [np.random.randint(0, i - num, int(num_salt)) for i in image.shape]
        # coords = [np.random.randint(0, i - 1, int(num_salt)) for i in (image.shape[0]-2,image.shape[0]-2,image.shape[0])]
        noisy_img[coords[0], coords[1], :] = [255, 255, 255]
        noisy_img[coords[0]+1, coords[1], :] = [255, 255, 255]
        noisy_img[coords[0], coords[1]+1, :] = [255, 255, 255]
        noisy_img[coords[0] + 1, coords[1] + 1, :] = [255, 255, 255]
        if num == 2:
            noisy_img[coords[0] + 2, coords[1], :] = [255, 255, 255]
            noisy_img[coords[0] + 2, coords[1]+1, :] = [255, 255, 255]
            noisy_img[coords[0], coords[1] + 2, :] = [255, 255, 255]
            noisy_img[coords[0]+1, coords[1] + 2, :] = [255, 255, 255]
            noisy_img[coords[0] + 2, coords[1] + 2, :] = [255, 255, 255]

    if pepper_flag:
        # 添加pepper噪声
        num_pepper = np.ceil(amount * image.size * (1. - s_vs_p))
        # 设置添加噪声的坐标位置
        coords = [np.random.randint(0, i - 1, int(num_pepper)) for i in image.shape]
        noisy_img[coords[0], coords[1], :] = [0, 0, 0]

    return noisy_img

生成的效果图(左图的噪声点大,右图的噪声点小)

参考:python使用opencv对图像添加(高斯/椒盐/泊松/斑点)噪声_修炼之路的博客-CSDN博客_opencv添加噪声

有趣的链接:雨点生成

数据增强:模拟雨天算法Python - 灰信网(软件开发博客聚合) 

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猫猫与橙子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值