简体   繁体   中英

How to add noise to image?

I've attempted through the code below, to add Gaussian noise to an RGB or a GRAYSCALE image, but until now I have the same problem, the result is always as follows:

当前结果

So is there a problem with the code I've written?

def gauss(mean,sigma):
     from random import uniform
     from math import sqrt,log,pi,cos
     a=uniform(0,1)
     b=uniform(0,1)
     x=sqrt(-2*log(a))*cos(2*pi*b)
     return(x)

def bruiter(image):
     from matplotlib.pyplot import imread
     if len(image.shape)==3 :
       a,b,c=image.shape
          for i in range(a):
             for j in range(b):
                 image[i][j] += [gauss(0.5,0.01),gauss(0.5,0.01),gauss(0.5,0.01)]

     elif len(image.shape)==2 :
       a,b= image.shape
          for i in range(a):
             for j in range(b):
                 image[i][j] +=  gauss(0.01)*(1/255)

return(image)

My thanks are due to Mr Martineau for pointing out that I should be answering about noise and not blur. I will answer in terms of Pillow again because I think it makes like simpler.

I open a picture of a muskrat's paw and display the image size and the value of the pixel at position (50,50). This is a RGB image and we can be fairly sure that each colour can range from 0 to 255.

I think you want additive Gaussian noise. To save myself the bother of writing and debugging a generator I'm using one that's readily available, normalvariate . You will want to vary the level and spread of the noise; I've therefore made the mean and scale parameters. Since there are limits on the ranges of the colour values I use max and min . As a convenience I package three calls to add_noise in add_noise_one_pixel so that this latter routine can be called once for each pixel in the image.

You will already have noticed that all pixels in the image can be addressed in a using knowledge of the size of the image and the getpixel method.

I have NOT checked this code. It's meant more as an illustration of principle.

>>> from PIL import Image, ImageFilter
>>> im = Image.open('muskrat.png')
>>> im.size
(100, 117)
>>> im.getpixel((50,50))
(121, 130, 116)
>>> import random
>>> def add_noise(x, mean, stddev):
...     return min(max(0, x+random.normalvariate(mean,stddev)), 255)
... 
>>> def add_noise_one_pixel(im, x, y, mean=10, stddev=5):
...     x, y, z = im.getpixel((x,y))
...     im.putpixel((x,y), add_noise(x, mean, stddev), add_noise(y, mean, stddev), add_noise(z, mean, stddev))
...     

Old answer:

If you just want to do it rather than concern yourself about the details about doing it then have a look at Pillow. It's this easy.

>>> from PIL import Image, ImageFilter
>>> im = Image.open('GENES.png')
>>> im2 = im.filter(ImageFilter.GaussianBlur(5))
>>> im2.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM