0

I have written a simple code to add noise to image:

import cv2
from skimage.util import *
img = cv2.imread("./A/0030050944.jpg")

img = random_noise(img, mode='poisson', seed=42, clip=False)

cv2.imwrite("test.jpg", img)

But this only gives a blank black image.

meu
  • 125
  • 2
  • 10
  • Anybody else for whom the above mentioned answer didn't work: https://stackoverflow.com/questions/19289470/adding-poisson-noise-to-an-image – meu Jan 23 '20 at 09:13

1 Answers1

1

Check this code. It 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

From How to add noise (Gaussian/salt and pepper etc) to image in Python with OpenCV

possion noise from PIL import Image import numpy as np from skimage.util import random_noise

im = Image.open("test.jpg")
# convert PIL Image to ndarray
im_arr = np.asarray(im)

# random_noise() method will convert image in [0, 255] to [0, 1.0],
# inherently it use np.random.normal() to create normal distribution
# and adds the generated noised back to image
noise_img = random_noise(im_arr, mode='possion', var=0.05**2)
noise_img = (255*noise_img).astype(np.uint8)

img = Image.fromarray(noise_img)
img.show()
user1850484
  • 388
  • 1
  • 3
  • 23
  • And can you please explain intuition behind speckle? – meu Jan 23 '20 at 06:30
  • Speckle is a granular interference that inherently exists in and degrades the quality of the active radar, synthetic aperture radar (SAR), medical ultrasound and optical coherence tomography images. – user1850484 Jan 23 '20 at 06:35
  • how do I change the poisson function. I tried it and it returns pretty much the same image – meu Jan 23 '20 at 06:43
  • 21 # inherently it use np.random.normal() to create normal distribution 22 # and adds the generated noised back to image ---> 23 noise_img = random_noise(im_arr, mode='poisson', var=0.05**2) 24 noise_img = (255*noise_img).astype(np.uint8) 25 123 if key not in allowedkwargs[allowedtypes[mode]]: 124 raise ValueError('%s keyword not in allowed keywords %s' % --> 125 (key, allowedkwargs[allowedtypes[mode]])) 126 127 # Set kwarg defaults ValueError: var keyword not in allowed keywords [] – meu Jan 23 '20 at 07:05
  • You made an error. noise_img = random_noise(im_arr, mode='poisson', var=0.05 ** 2): : "poisson", NOT "possion" – CSDD Apr 08 '22 at 10:31