I am using python open cv for image restoration and in the first step, I have to add gaussian noise to my "binary" image. My image pixel values are integers that can take values 0 or 1. How can I add gaussian noise to my image knowing SNR or noise variance?
I came up with this piece of code which adds 15 percent noise to my image but I don't know if this noise is normal Gaussian and how I can find its variance and SNR.
def add_noise(im):
im_noisy = im.copy()
for i in range(len(im_noisy)):
for j in range(len(im_noisy[0])):
r = np.random.rand()
if r < 0.15:
im_noisy[i][j] = -im_noisy[i][j]+1 #(converts 0 to 1 and vice versa)
return im_noisy
Since the image is binary, the solution is not well defined - we have to select a threshold that above the threshold the noise is
1.The Gaussian noise supposes to be symmetric, so pixels below the minus threshold are going to be
-1.We may define the threshold by number of sigma's (for example: above 2 sigma, the noise is
1, and below -2 sigma the noise is-1).When selecting threshold of 2 sigma, statistically we expect that about 2.5% of total pixels are going to be
1and 2.5% are going to be-1.Select lower threshold for higher percentage.
Create random normal (Gaussian) distribution image with mean=0 and sigma=1:
Convert to values to
-1,0,1- assume pixels value above 2 sigma are "1", below -2 sigma are-1and other are "0" (2 sigma is an example, we may select other value):After adding
binary_gausstoim, clip the result to range[0, 1]:Code sample (first part reads a sample image, and convert to binary):
im(input image after converting to binary):binary_gauss(noise image after threshold - values are-1,0,1):noisey_im(im + binary_gauss after threshold):