当前位置:   article > 正文

python实现图像的理想滤波器、butterworth滤波器、指数滤波器_butterworth ideal

butterworth ideal

1.傅里叶变换

图像的频率滤波是基于傅里叶变换的,通过二维傅里叶变换把图像从空域转换到频域,对频域的图像的频率进行操作,比如限制某个频率范围的像素通过。

(1)傅里叶变换

其中离散傅里叶变换为:

 

(2)傅里叶逆变换

其中离散傅里叶逆变换为:

 

        (3)傅里叶变换性质

 上述傅里叶变换均为一维傅里叶变换,然而图像中为二维的傅里叶变换,因此我们可以分别在行方向和列方向分别做傅里叶变换,

 2.频域滤波

频域滤波的基本流程如下

 傅里叶变换一般要中心化,根据二维傅里叶的平移性质:

 我们只需要对变换后的图像像素乘以(1)x+y就可以进行中心化和去中心化:

3.常用滤波器

  (1)理想滤波器

理想低通滤波器

 


理想高通滤波器

 

 

 (2)butterwoth滤波器

butterwoth低通滤波器:

 


butterwoth高通滤波器:

(3)指数滤波器

指数低通滤波 :

 


指数高通滤波: 

 

4.python实现

  1. import cv2 as cv
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. def filter(img, D0, W=None, N=2, type='lp', filter='butterworth'):
  5. '''
  6. 频域滤波器
  7. Args:
  8. img: 灰度图片
  9. D0: 截止频率
  10. W: 带宽
  11. N: butterworth和指数滤波器的阶数
  12. type: lp, hp, bp, bs即低通、高通、带通、带阻
  13. filter:butterworth、ideal、exponential即巴特沃斯、理想、指数滤波器
  14. Returns:
  15. imgback:滤波后的图像
  16. '''
  17. #离散傅里叶变换
  18. dft=cv.dft(np.float32(img),flags=cv.DFT_COMPLEX_OUTPUT)
  19. #中心化
  20. dtf_shift=np.fft.fftshift(dft)
  21. rows,cols=img.shape
  22. crow,ccol=int(rows/2),int(cols/2) #计算频谱中心
  23. mask=np.ones((rows,cols,2)) #生成rows行cols列的2纬矩阵
  24. for i in range(rows):
  25. for j in range(cols):
  26. D = np.sqrt((i-crow)**2+(j-ccol)**2)
  27. if(filter.lower() == 'butterworth'):
  28. if(type == 'lp'):
  29. mask[i, j] = 1/(1+(D/D0)**(2*N))
  30. elif(type == 'hp'):
  31. mask[i, j] = 1/(1+(D0/D)**(2*N))
  32. elif(type == 'bs'):
  33. mask[i, j] = 1/(1+(D*W/(D**2-D0**2))**(2*N))
  34. elif(type == 'bp'):
  35. mask[i, j] = 1/(1+((D**2-D0**2)/D*W)**(2*N))
  36. else:
  37. assert('type error')
  38. elif(filter.lower() == 'ideal'): #理想滤波器
  39. if(type == 'lp'):
  40. if(D > D0):
  41. mask[i, j] = 0
  42. elif(type == 'hp'):
  43. if(D < D0):
  44. mask[i, j] = 0
  45. elif(type == 'bs'):
  46. if(D > D0 and D < D0+W):
  47. mask[i, j] = 0
  48. elif(type == 'bp'):
  49. if(D < D0 and D > D0+W):
  50. mask[i, j] = 0
  51. else:
  52. assert('type error')
  53. elif(filter.lower() == 'exponential'): #指数滤波器
  54. if(type == 'lp'):
  55. mask[i, j] = np.exp(-(D/D0)**(2*N))
  56. elif(type == 'hp'):
  57. mask[i, j] = np.exp(-(D0/D)**(2*N))
  58. elif(type == 'bs'):
  59. mask[i, j] = np.exp(-(D*W/(D**2 - D0**2))**(2*N))
  60. elif(type == 'bp'):
  61. mask[i, j] = np.exp(-((D**2 - D0**2)/D*W)**(2*N))
  62. else:
  63. assert('type error')
  64. fshift = dtf_shift*mask
  65. f_ishift=np.fft.ifftshift(fshift)
  66. img_back=cv.idft(f_ishift)
  67. img_back=cv.magnitude(img_back[:,:,0],img_back[:,:,1]) #计算像素梯度的绝对值
  68. img_back=np.abs(img_back)
  69. img_back=(img_back-np.amin(img_back))/(np.amax(img_back)-np.amin(img_back))
  70. return img_back
  71. img=cv.imread('lena.jpg',0)
  72. plt.subplot(121),plt.imshow(img,cmap='gray'),plt.title('origin image')
  73. img_back = filter(img, 30, type='hp')
  74. plt.subplot(122),plt.imshow(img_back,cmap='gray'),plt.title('after butterworth highpass filter image')
  75. plt.show()

 

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

闽ICP备14008679号