Advertisement
StevanovicMilan

8.3

Sep 18th, 2021
722
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.66 KB | None | 0 0
  1. '''
  2. Koristeći zašumljenu sliku iz prethodnog primera, ukloniti šum upotrebnom geometrijskog usrednjivača veličina:
  3.  
  4. 3x3
  5. 5x5
  6. Koristeći PSNR odrediti koji filtar je dao objektivno najbolje otklanjanje.
  7.  
  8. Za potrebe rešavanja zadatka napisati funkciju geometricMeanFilt koja implementira filtar geometrijski usrednjivač dat formulom:
  9.  
  10. gde  (s,t)  predstavljaju prostorne koordinate regiona  Sx,y  posmatran oko centra pozicije  (x,y) ,  g  je ulazna slika a  m,n  predstavljaju dimenzije regiona koji se posmatra. Podrazumevati simetrično proširenje.
  11. '''
  12. import numpy as np
  13. import math
  14.  
  15. def geometricMeanFilt(g, S_shape):
  16.   f = np.zeros(g.shape)
  17.  
  18.   pad_rows = math.floor(S_shape[0]/2)
  19.   pad_cols = math.floor(S_shape[1]/2)
  20.  
  21.   img_p = np.pad(g, ((pad_rows,),(pad_cols,)), mode='symmetric')
  22.  
  23.   for row in range(f.shape[0]):
  24.     for col in range(f.shape[1]):
  25.       region = img_p[row:row+S_shape[0], col:col+S_shape[1]]
  26.  
  27.       res = region.prod()**(1/np.prod(S_shape))
  28.  
  29.       f[row, col] = res
  30.  
  31.   return f
  32.  
  33. # 3x3 geometrijski usrednjivac
  34. S_shape = (3,3)
  35.  
  36. img_f = geometricMeanFilt(img_noisy, S_shape)
  37.  
  38. fig = px.imshow(img_f, zmin=0, zmax=255, color_continuous_scale='gray')
  39. fig.show()
  40.  
  41. psnr = 10*np.log10(255**2/((img - img_f)**2).mean())
  42. print("PSNR za geometrijski usrednjivac dimenzije {} je {}".format(S_shape, psnr))
  43.  
  44. # 5x5 geometrijski usrednjivac
  45. S_shape = (5,5)
  46.  
  47. img_f = geometricMeanFilt(img_noisy, S_shape)
  48.  
  49. fig = px.imshow(img_f, zmin=0, zmax=255, color_continuous_scale='gray')
  50. fig.show()
  51.  
  52. psnr = 10*np.log10(255**2/((img - img_f)**2).mean())
  53. print("PSNR za geometrijski usrednjivac dimenzije {} je {}".format(S_shape, psnr))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement