当前位置:   article > 正文

python求巴特沃斯和高斯的低通滤波和高通滤波_高斯高通滤波python

高斯高通滤波python

低通滤波 

  1. import numpy as np
  2. import cv2 as cv
  3. # 加载图像
  4. image = cv.imread('www.jpg', 0)
  5. def BLPF(image, D0):
  6. L = np.empty_like(image, float)
  7. h, w = image.shape
  8. for i in range(0, h):
  9. for j in range(0, w):
  10. d = np.sqrt((j - int(h / 2)) ** 2 + (i - int(w / 2)) ** 2)
  11. L[j, i] = 1 / (1 + (d / D0) ** 2)
  12. return L
  13. def fft(image, L):
  14. dft = np.fft.fft2(image)
  15. dftshift = np.fft.fftshift(dft)
  16. rs = dftshift * L
  17. idft_shift = np.fft.ifftshift(rs)
  18. idft = np.fft.ifft2(idft_shift)
  19. result = np.real(idft)
  20. return np.uint8(result)
  21. def GLPF(image, D0):
  22. L = np.empty_like(image, float)
  23. h, w = image.shape
  24. for i in range(0, h):
  25. for j in range(0, w):
  26. d = np.sqrt((j - int(h / 2)) ** 2 + (i - int(w / 2)) ** 2)
  27. L[j, i] = np.exp((-1) * (d ** 2) / (2 * (D0 ** 2)))
  28. return L
  29. cv.imshow('image', image)
  30. # cv.imshow('BLPF_D0=10', fft(image, BLPF(image, 10)))
  31. # cv.imshow('BLPF_D0=20', fft(image, BLPF(image, 20)))
  32. # cv.imshow('BLPF_D0=40', fft(image, BLPF(image, 40)))
  33. # cv.imshow('BLPF_D0=80', fft(image, BLPF(image, 80)))
  34. cv.imshow('GLPF_D0=10', fft(image, GLPF(image, 10)))
  35. cv.imshow('GLPF_D0=20', fft(image, GLPF(image, 20)))
  36. cv.imshow('GLPF_D0=40', fft(image, GLPF(image, 40)))
  37. cv.imshow('GLPF_D0=80', fft(image, GLPF(image, 80)))
  38. cv.waitKey()

高通滤波

  1. import numpy as np
  2. import cv2 as cv
  3. # 加载图像
  4. image = cv.imread('www.jpg', 0)
  5. def BHPF(image, D0):
  6. L = np.empty_like(image, float)
  7. h, w = image.shape
  8. for i in range(0, h):
  9. for j in range(0, w):
  10. d = np.sqrt((j - int(h / 2)) ** 2 + (i - int(w / 2)) ** 2)
  11. L[j, i] = 1 / (1 + (D0 / d) ** 2)
  12. return L
  13. def fft(image, L):
  14. dft = np.fft.fft2(image)
  15. dftshift = np.fft.fftshift(dft)
  16. rs = dftshift * L
  17. idft_shift = np.fft.ifftshift(rs)
  18. idft = np.fft.ifft2(idft_shift)
  19. result = np.real(idft)
  20. return np.uint8(result)
  21. def GHPF(image, D0):
  22. L = np.empty_like(image, float)
  23. h, w = image.shape
  24. for i in range(0, h):
  25. for j in range(0, w):
  26. d = np.sqrt((j - int(h / 2)) ** 2 + (i - int(w / 2)) ** 2)
  27. L[j, i] = 1-np.exp((-1) * (d ** 2) / (2 * (D0 ** 2)))
  28. return L
  29. cv.imshow('image', image)
  30. cv.imshow('BHPF_D0=10', fft(image, BHPF(image, 10)))
  31. cv.imshow('BHPF_D0=20', fft(image, BHPF(image, 20)))
  32. cv.imshow('BHPF_D0=40', fft(image, BHPF(image, 40)))
  33. cv.imshow('BHPF_D0=80', fft(image, BHPF(image, 80)))
  34. # cv.imshow('GHPF_D0=10', fft(image, GHPF(image, 10)))
  35. # cv.imshow('GHPF_D0=20', fft(image, GHPF(image, 20)))
  36. # cv.imshow('GHPF_D0=40', fft(image, GHPF(image, 40)))
  37. # cv.imshow('GHPF_D0=80', fft(image, GHPF(image, 80)))
  38. cv.waitKey()

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

闽ICP备14008679号