当前位置:   article > 正文

python 实现小波维纳滤波_python wienerfilter

python wienerfilter
通过在小波域高频处,进行维纳滤波,来降低噪声对图像的影响。(假设噪声分布呈独立同分布)
  1. import os
  2. import cv2
  3. from matplotlib import pyplot as plt
  4. import numpy as np
  5. import pywt
  6. # print(pywt.families())
  7. # ['haar', 'db', 'sym', 'coif', 'bior', 'rbio', 'dmey', 'gaus', 'mexh', 'morl', 'cgau', 'shan', 'fbsp', 'cmor']
  8. def guass_noise(pic, SNR = 1):
  9. #SNR为信噪比
  10. pic = np.array(pic, dtype = float)
  11. SNR = 10 ** (SNR / 10)
  12. row, col = np.shape(pic)
  13. pic_power = np.sum(pic * pic) / (row * col)
  14. noise_power = pic_power / SNR
  15. noise = np.random.randn(row, col) * np.sqrt(noise_power)
  16. pic = (noise + pic)
  17. pic = np.where(pic <= 0, 0, pic)
  18. pic = np.where(pic >255, 255, pic)
  19. return np.uint8(pic)
  20. def SNF_calcu(pic, pic_):
  21. #pic 原图 pic_被污染的图片
  22. pic = np.array(pic, dtype = float)
  23. pic_ = np.array(pic_, dtype=float)
  24. noise = pic - pic_
  25. return 10 * np.log10(np.sum(pic * pic) / np.sum(noise * noise))
  26. def wiener_filter(pic, HH):
  27. #r padding半径
  28. row, col = np.shape(pic)
  29. noise_std = (np.median(np.abs(HH)) / 0.6745)
  30. noise_var = noise_std ** 2
  31. var = 1 / (row * col) * np.sum(pic * pic) - noise_var
  32. ans = pic * var / (var + noise_var)
  33. return ans
  34. def wiener_dwt(pic, index = 1):
  35. #index 为进行几层分解与重构
  36. pic = np.array(pic, dtype = float)
  37. coeffs = pywt.dwt2(pic, 'bior4.4')
  38. LL, (LH, HL, HH) = coeffs
  39. #LL为低频信号 LH为水平高频 HL为垂直高频 HH为对角线高频信号
  40. #维纳滤波
  41. LH = wiener_filter(LH, HH)
  42. HL = wiener_filter(HL, HH)
  43. HH = wiener_filter(HH, HH)
  44. #重构
  45. if index > 1:
  46. LL = wiener_dwt(LL, index - 1)
  47. #bior4.4小波重构可能会改变矩阵维数,现统一矩阵维数
  48. row, col = np.shape(LL)
  49. d1 = row - np.shape(HH)[0]
  50. d2 = col - np.shape(HH)[1]
  51. if d1 > 0 or d2 > 0:
  52. d1 = row - np.arange(d1) - 1
  53. d2 = col - np.arange(d2) - 1
  54. LL = np.delete(LL, d1, axis = 0)
  55. LL = np.delete(LL, d2, axis = 1)
  56. plt.figure()
  57. plt.subplot(2, 2, 1)
  58. plt.imshow(LL, cmap='gray')
  59. plt.title('The ' + str(time - index + 1) + 'th dwt----' + 'LL')
  60. plt.subplot(2, 2, 2)
  61. plt.imshow(LH, cmap='gray')
  62. plt.title('The ' + str(time - index + 1) + 'th dwt----' + 'LH')
  63. plt.subplot(2, 2, 3)
  64. plt.imshow(HL, cmap='gray')
  65. plt.title('The ' + str(time - index + 1) + 'th dwt----' + 'HL')
  66. plt.subplot(2, 2, 4)
  67. plt.imshow(HH, cmap='gray')
  68. plt.title('The ' + str(time - index + 1) + 'th dwt----' + 'HH')
  69. plt.show()
  70. pic_ans = pywt.idwt2((LL, (LH, HL, HH)), 'bior4.4')
  71. # pic_ans = np.where(pic_ans <= 0, 0, pic_ans)
  72. # pic_ans = np.where(pic_ans > 255, 255, pic_ans)
  73. return pic_ans
  74. def run(filename):
  75. SNR = 22
  76. global time
  77. time = 5 #分解次数
  78. f = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
  79. f_noise = guass_noise(f, SNR)
  80. f_process = wiener_dwt(f_noise, time)
  81. f_process = np.where(f_process <= 0, 0, f_process)
  82. f_process = np.where(f_process > 255, 255, f_process)
  83. f_process = np.uint8(f_process)
  84. print(SNF_calcu(f, f_noise))
  85. print(SNF_calcu(f, f_process))
  86. plt.figure()
  87. plt.subplot(1, 3, 1)
  88. plt.imshow(f, cmap = 'gray')
  89. plt.title('original image')
  90. plt.subplot(1, 3, 2)
  91. plt.imshow(f_noise,cmap = 'gray')
  92. plt.title('polluted image ----SNR = ' + str(SNR))
  93. plt.subplot(1, 3, 3)
  94. plt.imshow(f_process, cmap='gray')
  95. plt.title('polluted image after wiener_dwt')
  96. plt.show()
  97. if __name__ == "__main__":
  98. run('lena.tiff')

 

最终效果图:

原图信噪比与处理后图片信噪比:

SNF of polluted image is 21.985682296401862
SNF of processed image is 25.442700349930245

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/117295?site
推荐阅读
相关标签
  

闽ICP备14008679号