Advertisement
mrAnderson33

Лаба 3(6) КГ

Mar 23rd, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.68 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. from scipy.signal import convolve2d as conv2
  5. from scipy import misc
  6. from tkinter.filedialog import askopenfilename
  7. from skimage import color, data, restoration
  8.  
  9. def RL_deconv(image, PSF, iterations):
  10.     latent_est = image
  11.     PSF_HAT = PSF[::-1,::-1]
  12.     for i in range(1,iterations):
  13.         est_conv      = conv2(latent_est,PSF,'same');
  14.         relative_blur = image/est_conv;
  15.         error_est     = conv2(relative_blur,PSF_HAT,'same');
  16.         latent_est    = latent_est* error_est;
  17.     result = latent_est;
  18.     return result
  19.  
  20. #astro = color.rgb2gray( misc.imread(askopenfilename()))
  21. astro = color.rgb2gray(data.astronaut())
  22.  
  23.  
  24. psf = np.ones((5, 5)) / 25
  25. astro = conv2(astro, psf, 'same')
  26. # Add Noise to Image
  27. astro_noisy = astro.copy()
  28. astro_noisy += (np.random.poisson(lam=25, size=astro.shape) - 10) / 255.
  29.  
  30. # Restore Image using Richardson-Lucy algorithm
  31. #deconvolved_RL = restoration.richardson_lucy(astro_noisy, psf, iterations=30)
  32. deconvolved_RL = RL_deconv(astro_noisy, psf, iterations=30)
  33.  
  34. fig, ax = plt.subplots(nrows=1, ncols=3, figsize=(8, 5))
  35. plt.gray()
  36.  
  37. for a in (ax[0], ax[1], ax[2]):
  38.        a.axis('off')
  39.  
  40. ax[0].imshow(astro)
  41. ax[0].set_title('Исходное изображение')
  42.  
  43. ax[1].imshow(astro_noisy)
  44. ax[1].set_title('Зашумленное изображение')
  45.  
  46. ax[2].imshow(deconvolved_RL, vmin=astro_noisy.min(), vmax=astro_noisy.max())
  47. ax[2].set_title('Восстановление при помощи\n алгоритма Люси-Ричардсона')
  48.  
  49.  
  50. fig.subplots_adjust(wspace=0.02, hspace=0.2,
  51.                     top=0.9, bottom=0.05, left=0, right=1)
  52. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement