Advertisement
StevanovicMilan

8.7

Sep 18th, 2021
755
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.77 KB | None | 0 0
  1. '''
  2. Učitati sliku lena.png i dodati joj AWGN varijanse 100 i srednje vrednosti 0.
  3. Nad ovom zašumljenom slikom dodati impulsni so i biber šum gustine 5%.
  4.  
  5. Za potrebe uklanjanja ovog kombinovanog šuma potrebno je napisati funkciju
  6. alphaTrimmedMeanFilt koja implementira alfa-trimovani usrednjivač dat formulom:
  7. '''
  8.  
  9. from skimage import io
  10. import plotly.express as px
  11. import numpy as np
  12.  
  13. img = io.imread('lena.png')
  14. fig = px.imshow(img, color_continuous_scale='gray')
  15. fig.show()
  16.  
  17. rng = np.random.default_rng()
  18.  
  19. img_noisy = img + rng.standard_normal(size=img.shape)*np.sqrt(100) + 0
  20. img_noisy[img_noisy>255] = 255
  21. img_noisy[img_noisy<0] = 0
  22.  
  23. p = rng.uniform(size=img.shape)
  24. p0 = 0.05
  25. img_noisy[p<p0/2] = 0
  26. img_noisy[(p0/2 <= p) & (p<p0)] = 255
  27.  
  28. fig = px.imshow(img_noisy, color_continuous_scale='gray')
  29. fig.show()
  30.  
  31. import math
  32.  
  33. def alphaTrimmedMeanFilt(g, S_shape, alpha):
  34.     f = np.zeros(g.shape)
  35.  
  36.     pad_rows = math.floor(S_shape[0]/2)
  37.     pad_cols = math.floor(S_shape[1]/2)
  38.  
  39.     img_p = np.pad(g, ((pad_rows,),(pad_cols,)), mode='symmetric')
  40.  
  41.     for row in range(f.shape[0]):
  42.         for col in range(f.shape[1]):
  43.             region = img_p[row:row+S_shape[0], col:col+S_shape[1]]
  44.  
  45.             region = np.sort(region, axis = None)
  46.  
  47.             res = region[alpha//2:-alpha//2].mean()
  48.  
  49.             f[row,col] = res
  50.     return f
  51.  
  52. # 3x3 region
  53. S_shape = (3,3)
  54.  
  55. img_f = alphaTrimmedMeanFilt(img_noisy, S_shape, alpha=4)
  56.  
  57. fig = px.imshow(img_f, color_continuous_scale='gray')
  58. fig.show()
  59.  
  60. psnr1 = 10*np.log10(255**2/((img - img_noisy)**2).mean())
  61. psnr2 = 10*np.log10(255**2/((img - img_f)**2).mean())
  62.  
  63. print("PSNR za zasumljenu sliku je {}".format(psnr1))
  64. print("PSNR za alfa-trimovani usrednjivac dimenzije {} je {}".format(S_shape, psnr2))
  65.  
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement