StevanovicMilan

9.2

Sep 18th, 2021
707
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. '''
  2. Napisati funkciju gaussBandStopFilter koja implementira Gausov filtar nepropusnik opsega dat formulom:
  3.  
  4. gde su  (u,v)  prostorne koordinate DFT slike,  D  je euklidsko rastojanje posmatrano od sredine slike,  D0  je poluprečnik a  W  širina prstena.
  5.  
  6. Definisanim filtrom filtrirati periodičnu smetnju prisutnu u slici moon_periodic.png. Dodatno prikazati amplitudsku karakteristiku filtra, 3D prikaz i poprečni presek.
  7. ------------------------------------------------------------------------------------------------------------------------------------------------------------------
  8. Napomena: Za eventualne probleme deljenja sa 0 koristiti np.finfo('float').eps.
  9. '''
  10. def gaussBandStopFilter(H_size,D0,W):
  11.   u = np.arange(H_size[0]).reshape(-1, 1) - np.floor(H_size[0]/2)
  12.   v = np.arange(H_size[1]) - np.floor(H_size[1]/2)
  13.  
  14.   D = np.sqrt(u**2 + v**2)
  15.   H = 1-np.exp(-0.5*((D**2-D0**2)/(D*W + np.finfo('float').eps))**2)
  16.   return H
  17.  
  18. import numpy as np
  19.  
  20. print(np.finfo('float').eps)
  21.  
  22. import plotly.express as px
  23. from skimage import io
  24.  
  25. img = io.imread('moon_periodic.png')
  26. fig = px.imshow(img, color_continuous_scale='gray')
  27. fig.show()
  28.  
  29. IMG = np.fft.fftshift(np.fft.fft2(img, s=(2*img.shape[0],2*img.shape[1])))
  30. fig = px.imshow(np.log(np.abs(IMG)+1), color_continuous_scale='gray')
  31. fig.show(config={'modeBarButtonsToAdd':['drawline',
  32.                                         'drawopenpath',
  33.                                         'drawcircle',
  34.                                         'eraseshape']})
  35.                                        
  36. print(np.sqrt((256-128)**2+(256-216)**2))
  37.  
  38. H = gaussBandStopFilter(IMG.shape, 134, 5)
  39. fig = px.imshow(np.abs(H), color_continuous_scale='gray')
  40. fig.show()
  41.  
  42. import plotly.graph_objects as go
  43. from plotly.subplots import make_subplots
  44.  
  45. fig = make_subplots(rows=1, cols=2, specs=[[{'type': 'surface'}, {'type': 'scatter'}]])
  46.  
  47. fig.add_trace(go.Surface(z=np.abs(H)), row=1, col=1)
  48. fig.add_trace(go.Scatter(y=np.abs(H)[256], mode='lines'), row=1, col=2)
  49. fig.show()
  50.  
  51. IMG1 = IMG * H
  52. fig = px.imshow(np.log(np.abs(IMG1)+1), color_continuous_scale='gray')
  53. fig.show()
  54.  
  55. img1 = np.real(np.fft.ifft2(np.fft.ifftshift(IMG1)))
  56. img1 = img1[:img.shape[0],:img.shape[1]]
  57.  
  58. fig = px.imshow(img1, color_continuous_scale='gray')
  59. fig.show()
RAW Paste Data