Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. from numpy import *
  2. from scipy import *
  3. from scipy.signal import *
  4. from scipy.stats import *
  5. from pylab import*
  6.  
  7. ''' Funkcja obliczajaca dwt '''
  8. def dwt(img):
  9. maskL = [0.02674875741080976, -0.01686411844287795, -0.07822326652898785, 0.2668641184428723, 0.6029490182363579, 0.2668641184428723,
  10. -0.07822326652898785, -0.01686411844287795, 0.02674875741080976]
  11. maskH = [0.09127176311424948, -0.05754352622849957, -0.5912717631142470, 1.115087052456994, -0.5912717631142470, -0.05754352622849957,
  12. 0.09127176311424948]
  13. height = img.shape[0]
  14. width = img.shape[1]
  15. bandL = zeros([height,int(width/2)])
  16. bandH = zeros(bandL.shape)
  17. for r in range(0, height): #1D-DWT dla wierszy
  18. bandL[r,:] = convolve(img[r,:], maskL, mode='same')[::2]
  19. bandH[r,:] = convolve(img[r,:], maskH, mode='same')[::2]
  20. bandLL = zeros([int(height/2),int(width/2)])
  21. bandLH = zeros(bandLL.shape)
  22. bandHL = zeros(bandLL.shape)
  23. bandHH = zeros(bandLL.shape)
  24. for c in range(0, int(width/2)): #1D-DWT dla kolumn
  25. bandLL[:,c] = convolve(bandL[:,c], maskL, mode='same')[::2]
  26. bandLH[:,c] = convolve(bandL[:,c], maskH, mode='same')[::2]
  27. bandHL[:,c] = convolve(bandH[:,c], maskL, mode='same')[::2]
  28. bandHH[:,c] = convolve(bandH[:,c], maskH, mode='same')[::2]
  29. return clip(bandLL, 0.0, 1.0), bandLH, bandHL, bandHH
  30.  
  31. ''' funkcja wyswietlajaca i zapisujaca obraz '''
  32. def showAndSaveImg(img, imgTitle):
  33. figure()
  34. axis('off')
  35. title(imgTitle)
  36. imshow(img, cmap = cm.gray, vmin = -1.0, vmax = 1.0)
  37. imsave("C:/Users/Wojtek/Documents/" + imgTitle + '.png', img, cmap = cm.gray)
  38.  
  39. def histogram1(img, ilosc, minimum, histTitle):
  40. figure()
  41. hist(img.flatten(), ilosc, range = [minimum, 1.0])
  42. title('Histogram pasma ' + histTitle)
  43. xlabel('Wartosc piksela')
  44. ylabel('Ilosc wystapien')
  45.  
  46. ''' funkcja obliczajaca entropie '''
  47. def entropia(img, ilosc, minimum):
  48. hist = histogram(img.flatten(), ilosc, range = [minimum, 1.0])[0] + sys.float_info.epsilon #utworzenie histogramu
  49. pstwo = hist/sum(hist)
  50. ent = -1.0*sum(pstwo*log2(pstwo)) #obliczenie entropii
  51. return ent
  52.  
  53. obraz = imread(r"C:\Users\Wojtek\Downloads\pto_obrazytestowepng (2)\obrazyTestowe\monochrome\galera_mono.png") #wczytanie obrazu
  54.  
  55. obrazLL, obrazLH, obrazHL, obrazHH = dwt(obraz) #wywolanie funkcji dwt
  56.  
  57. #otworzenie i zapisanie obrazow dla wszystkich pasm
  58. showAndSaveImg(obrazLL, 'Pasmo LL')
  59. showAndSaveImg(obrazLH, 'Pasmo LH')
  60. showAndSaveImg(obrazHL, 'Pasmo HL')
  61. showAndSaveImg(obrazHH, 'Pasmo HH')
  62.  
  63. #stworzenie histogramow dla poszczegolnych pasm
  64. histogram1(obrazLL, 256, 0.0, 'LL')
  65. histogram1(obrazLH, 511, -1.0, 'LH')
  66. histogram1(obrazHL, 511, -1.0, 'HL')
  67. histogram1(obrazHH, 511, -1.0, 'HH')
  68.  
  69. #obliczenie entropii dla poszczegolnch pasm
  70. print ('Entropia dla pasma LL: ', entropia(obrazLL, 256, 0.0))
  71. print ('Entropia dla pasma LH: ', entropia(obrazLH, 511, -1.0))
  72. print ('Entropia dla pasma HL: ', entropia(obrazHL, 511, -1.0))
  73. print ('Entropia dla pasma HH: ', entropia(obrazHH, 511, -1.0))
  74.  
  75. show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement