Advertisement
StevanovicMilan

8.1

Sep 18th, 2021
5,044
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.60 KB | None | 0 0
  1. '''
  2. Napraviti demo sliku dimenzije 256x256 sa dva kvadrata u sredini (jedan u drugom) koji su sa nijansama 127 i 191. Postaviti pozadinu na 63. Prikazati njen histogram.
  3.  
  4. Na ovu sliku dodati AWGN šum varijanse 100 i srednje vrednosti 0. Prikazati rezultat i histogram nove slike. Odrediti vršni odnost signal-šum.
  5. '''
  6.  
  7. import plotly.graph_objects as go
  8. from plotly.subplots import make_subplots
  9. import numpy as np
  10. import plotly.express as px
  11.  
  12.  
  13. img = np.full((256, 256), fill_value=63)
  14. img[32:32+192,32:32+192] = 127
  15. img[64:64+128,64:64+128] = 191
  16. fig = px.imshow(img, color_continuous_scale='gray')
  17. fig.show()
  18.  
  19.  
  20. rng = np.random.default_rng()
  21. noise_im = rng.standard_normal(size=img.shape)
  22.  
  23. fig = px.imshow(noise_im, color_continuous_scale='gray')
  24. fig.show()
  25.  
  26. img_noisy = img + noise_im*np.sqrt(100) + 0 # varijansa i srednja vrednost
  27. img_noisy[img_noisy > 255] = 255
  28. img_noisy[img_noisy < 0] = 0
  29. fig = px.imshow(img_noisy, color_continuous_scale='gray')
  30. fig.show()
  31.  
  32. import plotly.express as px
  33.  
  34. rng = np.random.default_rng()
  35. noise_im = rng.standard_normal(size=img.shape)
  36.  
  37. fig = px.imshow(noise_im, color_continuous_scale='gray')
  38. fig.show()
  39.  
  40.  
  41. img_noisy = img + noise_im*np.sqrt(100) + 0
  42. img_noisy[img_noisy > 255] = 255
  43. img_noisy[img_noisy < 0] = 0
  44.  
  45. hist_n, bin_edges = np.histogram(img_noisy, bins = np.arange(257))
  46.  
  47. fig = make_subplots(rows=1, cols=2)
  48. fig.add_trace(go.Image(z=np.stack((img_noisy, img_noisy, img_noisy), axis=2)), row=1, col=1)
  49. fig.add_trace(go.Scatter(x=np.arange(256), y=hist_n, mode='lines',name='hist_noisy', line=dict(color='blue')), row=1, col=2)
  50. fig.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement