美文网首页
给图像加椒盐噪声

给图像加椒盐噪声

作者: jxc1 | 来源:发表于2019-11-29 16:42 被阅读0次
    """
    #对某图片添加椒盐噪声(此处为3通道图片)
    #如果图像时单通道的,那就直接赋一个值即可,255或0
    以下pNum为像素点个数;nRate为信噪比
    """
    def addNoise(filename):
        import cv2
        import random
        import numpy as np
    
        im = cv2.imread(filename)
        cv2.imshow('original img',im)
        h,w = im.shape[0:2]
        pNum = h*w
        nRate = 0.3 
        for r in range(int(pNum*nRate)):
            randh = random.randint(0,h-1)
            randw = random.randint(0,w-1)
            #print(h,randh,w,randw)
            if random.random()<0.5:
                im[randh,randw,:] = np.array([0,0,0])
            else:
                im[randh,randw,:] = np.array([255,255,255])
        cv2.imshow('add noise',im)
        cv2.waitKey(0)
    
    addNoise('test.png')
    
    原始图片(图片来自网络) 加噪声后

    相关文章

      网友评论

          本文标题:给图像加椒盐噪声

          本文链接:https://www.haomeiwen.com/subject/xxugwctx.html