Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.84 KB | None | 0 0
  1. # The noise in this piece of code is taken from the accepted answer in https://stackoverflow.com/questions/22937589/how-to-add-noise-gaussian-salt-and-pepper-etc-to-image-in-python-with-opencv
  2. from skimage.measure import compare_ssim
  3. import numpy as np
  4. import cv2
  5. import math
  6. from PIL import Image
  7. def distance(a,b):
  8.     return math.sqrt((a[1]-b[1])**2 + (a[0]-b[0])**2)
  9. def ti_radgrad(d):
  10.     RG = np.empty((d,d,3), np.uint8)
  11.     mp = d//2 #midpoint / maximum path
  12.     for y in range(d):
  13.         for x in range(d):
  14.             di = abs(distance((y,x),(mp,mp))/mp)
  15.             dis = di**8
  16.             g = int(max(0,min(255,255*dis)))
  17.             RG[y,x,:] = g
  18.     return RG
  19. def noisy(noise_typ,image):
  20.    if noise_typ == "gauss":
  21.       row,col,ch= image.shape
  22.       mean = 0
  23.       var = 0.1
  24.       sigma = var**0.5
  25.       gauss = np.random.normal(mean,sigma,(row,col,ch))
  26.       gauss = gauss.reshape(row,col,ch)
  27.       noisy = image + gauss
  28.       return noisy
  29.    elif noise_typ == "s&p":
  30.       row,col,ch = image.shape
  31.       s_vs_p = 0.5
  32.       amount = 0.004
  33.       out = np.copy(image)
  34.       # Salt mode
  35.       num_salt = np.ceil(amount * image.size * s_vs_p)
  36.       coords = [np.random.randint(0, i - 1, int(num_salt))
  37.               for i in image.shape]
  38.       out[coords] = 1
  39.  
  40.       # Pepper mode
  41.       num_pepper = np.ceil(amount* image.size * (1. - s_vs_p))
  42.       coords = [np.random.randint(0, i - 1, int(num_pepper))
  43.               for i in image.shape]
  44.       out[coords] = 0
  45.       return out
  46.    elif noise_typ == "poisson":
  47.       vals = len(np.unique(image))
  48.       vals = 2 ** np.ceil(np.log2(vals))
  49.       noisy = np.random.poisson(image * vals) / float(vals)
  50.       return noisy
  51.    elif noise_typ =="speckle":
  52.       row,col,ch = image.shape
  53.       gauss = np.random.randn(row,col,ch)
  54.       gauss = gauss.reshape(row,col,ch)        
  55.       noisy = image + image * gauss
  56.       return noisy
  57. def mark(im,c):
  58.     im[20:40,20:40,c] = 255
  59.     return im
  60.  
  61. image = ti_radgrad(256) # 256x256 RGB (numpy array uint8)
  62. image = noisy("speckle", image)
  63. image = cv2.blur(image, (5,5))
  64. Image.fromarray(mark(image,0).astype(np.uint8)).show("original") # red square in top-left
  65. noise = noisy("speckle", image.copy())
  66. n_obs = 5
  67. observations = np.zeros((5,256,256,3),dtype=np.uint8) # error: OpenCV(3.4.2) /tmp/build/80754af9/opencv-suite_1535558553474/work/modules/core/src/arithm.cpp:656: error: (-209:Sizes of input arguments do not match) The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array' in function 'arithm_op'
  68. for n in range(n_obs):
  69.     ni = cv2.addWeighted(image,0.1+n*0.1,noised,0.9-n*0.1,0).astype(np.uint8)
  70.     print(ni.dtype, ni.shape, type(ni))
  71.     observations[n,:,:,:] = ni
  72. #observations = np.dstack(observations)
  73. print(observations.dtype, observations.shape, type(observations))
  74. Image.fromarray(mark(observations[3,:,:,:],1)).show("noise") # green square
  75. result = observations[4,:,:,:].copy().astype(np.uint8)
  76. result_diff = result.copy()
  77. print(result.dtype, result.shape, type(result))
  78. #result = [noised.copy()]*n_obs # TypeError: result is not a numpy array, neither a scalar
  79. for chan in range(3):
  80.     cv2.denoise_TVL1(observations[2,:,:,chan], result[:,:,chan], 0.1, niters=200)
  81. #Image.fromarray(mark(result,2).astype(np.uint8)).show("denoise") # cyan square
  82. Image.fromarray(result.astype(np.uint8)).show("denoise")
  83. diff = compare_ssim(result, result_diff, win_size=9, multichannel=True)
  84. print(diff)
  85.  
  86. ======== OUTPUT ============
  87. uint8 (256, 256, 3) <class 'numpy.ndarray'>
  88. uint8 (256, 256, 3) <class 'numpy.ndarray'>
  89. uint8 (256, 256, 3) <class 'numpy.ndarray'>
  90. uint8 (256, 256, 3) <class 'numpy.ndarray'>
  91. uint8 (256, 256, 3) <class 'numpy.ndarray'>
  92. uint8 (5, 256, 256, 3) <class 'numpy.ndarray'>
  93. uint8 (256, 256, 3) <class 'numpy.ndarray'>
  94. 1.0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement