I am wondering if there exists some functions in Python with OpenCV or any other python image processing library that adds Gaussian or salt and pepper noise to an image? For example, in MATLAB there exists straight-forward functions that do the same job.
Or, how to add noise to an image using Python with OpenCV?
Python – noise() function in Wand We can add noise to the image using noise() function. noise function can be useful when applied before a blur operation to defuse an image. Following are the noise we can add using noise() function: gaussian.
As stated in the previous answers, to model AWGN you need to add a zero-mean gaussian random variable to your original signal. The variance of that random variable will affect the average noise power.
J = imnoise( I ,'speckle') adds multiplicative noise using the equation J = I+n*I , where n is uniformly distributed random noise with mean 0 and variance 0.05. J = imnoise( I ,'speckle', var_speckle ) adds multiplicative noise with variance var_speckle .
The Function adds gaussian , salt-pepper , poisson and speckle noise in an image
Parameters ---------- image : ndarray Input image data. Will be converted to float. mode : str One of the following strings, selecting the type of noise to add: 'gauss' Gaussian-distributed additive noise. 'poisson' Poisson-distributed noise generated from the data. 's&p' Replaces random pixels with 0 or 1. 'speckle' Multiplicative noise using out = image + n*image,where n is uniform noise with specified mean & variance. import numpy as np import os import cv2 def noisy(noise_typ,image): if noise_typ == "gauss": row,col,ch= image.shape mean = 0 var = 0.1 sigma = var**0.5 gauss = np.random.normal(mean,sigma,(row,col,ch)) gauss = gauss.reshape(row,col,ch) noisy = image + gauss return noisy elif noise_typ == "s&p": row,col,ch = image.shape s_vs_p = 0.5 amount = 0.004 out = np.copy(image) # Salt mode 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] out[coords] = 1 # Pepper mode 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] out[coords] = 0 return out elif noise_typ == "poisson": vals = len(np.unique(image)) vals = 2 ** np.ceil(np.log2(vals)) noisy = np.random.poisson(image * vals) / float(vals) return noisy elif noise_typ =="speckle": row,col,ch = image.shape gauss = np.random.randn(row,col,ch) gauss = gauss.reshape(row,col,ch) noisy = image + image * gauss return noisy
I don't know is there any method in Python API.But you can use this simple code to add Salt-and-Pepper noise to an image.
import numpy as np import random import cv2 def sp_noise(image,prob): ''' Add salt and pepper noise to image prob: Probability of the noise ''' output = np.zeros(image.shape,np.uint8) thres = 1 - prob for i in range(image.shape[0]): for j in range(image.shape[1]): rdn = random.random() if rdn < prob: output[i][j] = 0 elif rdn > thres: output[i][j] = 255 else: output[i][j] = image[i][j] return output image = cv2.imread('image.jpg',0) # Only for grayscale image noise_img = sp_noise(image,0.05) cv2.imwrite('sp_noise.jpg', noise_img)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With