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

python实现图像的理想滤波器、butterworth滤波器、指数滤波器1 傅里叶变换 图像的频率滤波是基于傅里叶变换的 通过二维傅里叶变换把图像从空域转换到频域 对频域的图像的频率进行操作 比如限制某个频率范围的像素通过 1 傅里叶变换 其中离散傅里叶变换为 2 傅里叶逆变换

大家好,我是讯享网,很高兴认识大家。

1.傅里叶变换

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

(1)傅里叶变换


讯享网

其中离散傅里叶变换为:

 

(2)傅里叶逆变换

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

 

        (3)傅里叶变换性质

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

 2.频域滤波

频域滤波的基本流程如下

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

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

3.常用滤波器

  (1)理想滤波器

理想低通滤波器

 


理想高通滤波器

 

 

 (2)butterwoth滤波器

butterwoth低通滤波器:

 


butterwoth高通滤波器:

(3)指数滤波器

指数低通滤波 :

 


指数高通滤波: 

 

4.python实现

import cv2 as cv import numpy as np import matplotlib.pyplot as plt def filter(img, D0, W=None, N=2, type='lp', filter='butterworth'): ''' 频域滤波器 Args: img: 灰度图片 D0: 截止频率 W: 带宽 N: butterworth和指数滤波器的阶数 type: lp, hp, bp, bs即低通、高通、带通、带阻 filter:butterworth、ideal、exponential即巴特沃斯、理想、指数滤波器 Returns: imgback:滤波后的图像 ''' #离散傅里叶变换 dft=cv.dft(np.float32(img),flags=cv.DFT_COMPLEX_OUTPUT) #中心化 dtf_shift=np.fft.fftshift(dft) rows,cols=img.shape crow,ccol=int(rows/2),int(cols/2) #计算频谱中心 mask=np.ones((rows,cols,2)) #生成rows行cols列的2纬矩阵 for i in range(rows): for j in range(cols): D = np.sqrt((i-crow)2+(j-ccol)2) if(filter.lower() == 'butterworth'): if(type == 'lp'): mask[i, j] = 1/(1+(D/D0)(2*N)) elif(type == 'hp'): mask[i, j] = 1/(1+(D0/D)(2*N)) elif(type == 'bs'): mask[i, j] = 1/(1+(D*W/(D2-D02))(2*N)) elif(type == 'bp'): mask[i, j] = 1/(1+((D2-D02)/D*W)(2*N)) else: assert('type error') elif(filter.lower() == 'ideal'): #理想滤波器 if(type == 'lp'): if(D > D0): mask[i, j] = 0 elif(type == 'hp'): if(D < D0): mask[i, j] = 0 elif(type == 'bs'): if(D > D0 and D < D0+W): mask[i, j] = 0 elif(type == 'bp'): if(D < D0 and D > D0+W): mask[i, j] = 0 else: assert('type error') elif(filter.lower() == 'exponential'): #指数滤波器 if(type == 'lp'): mask[i, j] = np.exp(-(D/D0)(2*N)) elif(type == 'hp'): mask[i, j] = np.exp(-(D0/D)(2*N)) elif(type == 'bs'): mask[i, j] = np.exp(-(D*W/(D2 - D02))(2*N)) elif(type == 'bp'): mask[i, j] = np.exp(-((D2 - D02)/D*W)(2*N)) else: assert('type error') fshift = dtf_shift*mask f_ishift=np.fft.ifftshift(fshift) img_back=cv.idft(f_ishift) img_back=cv.magnitude(img_back[:,:,0],img_back[:,:,1]) #计算像素梯度的绝对值 img_back=np.abs(img_back) img_back=(img_back-np.amin(img_back))/(np.amax(img_back)-np.amin(img_back)) return img_back img=cv.imread('lena.jpg',0) plt.subplot(121),plt.imshow(img,cmap='gray'),plt.title('origin image') img_back = filter(img, 30, type='hp') plt.subplot(122),plt.imshow(img_back,cmap='gray'),plt.title('after butterworth highpass filter image') plt.show()

讯享网

 

小讯
上一篇 2025-01-26 12:05
下一篇 2025-02-18 17:43

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/128786.html