当前位置:网站首页>Add salt and pepper noise or Gaussian noise to the picture

Add salt and pepper noise or Gaussian noise to the picture

2022-07-07 05:50:00 Yingtai night snow

Add salt and pepper noise or Gaussian noise to the picture

Salt and pepper noise added

Salt and pepper noise (salt & pepper noise) Is a common noise in digital images , Salt and pepper , Pepper is black , Salt is white , Salt and pepper noise is the random appearance of black and white pixels on the image . Salt and pepper noise is a kind of noise caused by signal pulse strength , The algorithm to generate this noise is also relatively simple .

Add salt and pepper noise to the grayscale image

# Add salt and pepper noise to grayscale images 
# src  Enter the path of the image 
# dst  Output image path 
# prob  Proportion of noise 
def addSaltPepperNoise_gray(src,dst,probility=0.05,method="salt_pepper"):
    # Convert the picture into a grayscale image 
    imarray=np.array(Image.open(src).convert('L'))
    height,width=imarray.shape

    for i in range(height):
        for j in range(width):
            # Add salt and pepper noise randomly 
            if np.random.random(1)<probility:
                if np.random.random(1)<0.05:
                    imarray[i,j]=0
                else:
                    imarray[i,j]=255
    # Add the generated salt and pepper noise image to 
    new_im=Image.fromarray(imarray)
    new_im.save(dst)
    return new_im

Add salt and pepper noise to the color map

def RGBAddNoise(src, dst, prob):  #  Add clutter at the same time (RGB Single noise )RGB Figure noise  prob: Noise ratio 

    imarray = np.array(Image.open(src))
    height, width, channels = imarray.shape

    # prob = 0.05 # Noise ratio   It's already obvious  >0.1  Seriously affect the image quality 
    NoiseImg = imarray.copy()
    NoiseNum = int(prob * height * width)
    for i in range(NoiseNum):
        rows = np.random.randint(0, height - 1)
        cols = np.random.randint(0, width - 1)
        channel = np.random.randint(0, 3)
        if np.random.randint(0, 2) == 0:
            NoiseImg[rows, cols, channel] = 0
        else:
            NoiseImg[rows, cols, channel] = 255
    # return NoiseImg
    new_im = Image.fromarray(NoiseImg)
    new_im.save(dst)
    return new_im

Gaussian noise added

Gaussian noise means that the probability density function of noise distribution obeys Gaussian distribution ( Normal distribution ) A kind of noise , The main reason is that the camera has a dark field of view and uneven brightness when shooting , At the same time, high temperature caused by long-time operation of the camera will also cause Gaussian noise , In addition, the noise and mutual influence of circuit components are also one of the important reasons for Gaussian noise .

Add Gaussian noise to the grayscale image

def addGaussNoiseGray(src,dst,mean,sigma):
    image=cv2.imread(src,cv2.IMREAD_GRAYSCALE)
    height,width=image.shape
    gauss=np.random.normal(mean,sigma,image.shape)
    gauss=gauss.reshape(height,width)
    noiseImage=image+gauss
    noiseImage=np.clip(noiseImage,0,255)
    noiseImage=noiseImage.astype('uint8')

    cv2.imwrite(dst,noiseImage)
    return noiseImage

Add Gaussian noise to the color map

# Add Gaussian noise to color images 
def addGaussNoiseRgb(src,dst,mean,sigma):
    image=cv2.imread(src)
    height,width,channels=image.shape

    # Establish Gaussian noise 
    gauss=np.random.normal(mean,sigma,(height,width,channels))
    gauss=gauss.reshape(height,width,channels)
    noiseImage=image+gauss

    noiseImage=np.clip(noiseImage,0,255)
    noiseImage=noiseImage.astype('uint8')

    cv2.imwrite(dst,noiseImage)
    return noiseImage

Result display

 Insert picture description here

原网站

版权声明
本文为[Yingtai night snow]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207062358572115.html