完成巴特沃斯滤波器

This commit is contained in:
张梦南 2025-05-06 21:29:16 +08:00
parent 28c31c9aa2
commit f7435ab4f6
6 changed files with 113 additions and 0 deletions

75
Butterworth.py Normal file
View File

@ -0,0 +1,75 @@
import cv2 as cv
import numpy as np
def filter(img, D0, W=None, N=2, type='lp', filter='butterworth'):
'''
频域滤波器
Args:
img: 灰度图片
D0: 截止频率
W: 带宽
N: butterworth和指数滤波器的阶数
type: lp, hp, bp, bs即低通高通带通带阻
filter:butterworthidealexponential即巴特沃斯理想指数滤波器
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/(D**2-D0**2))**(2*N))
elif(type == 'bp'):
mask[i, j] = 1/(1+((D**2-D0**2)/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/(D**2 - D0**2))**(2*N))
elif(type == 'bp'):
mask[i, j] = np.exp(-((D**2 - D0**2)/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

38
Filtering_Butterworth.py Normal file
View File

@ -0,0 +1,38 @@
import cv2
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['simHei']
plt.rcParams['axes.unicode_minus'] = False
from Butterworth import filter
# 读取照片
image_characterTestPattern2 = cv2.imread("file/characterTestPattern2.jpg", cv2.IMREAD_GRAYSCALE)
# 利用巴特沃斯滤波器进行处理
image_characterTestPattern2_Butterworth1 = filter(image_characterTestPattern2, 10, type='lp')
image_characterTestPattern2_Butterworth2 = filter(image_characterTestPattern2, 100, type='lp')
image_characterTestPattern2_Butterworth3 = filter(image_characterTestPattern2, 1000, type='lp')
# 展示图像
plt.subplot(2,2,1)
plt.imshow(image_characterTestPattern2, cmap='gray')
plt.title('原图')
plt.axis('off')
plt.subplot(2,2,2)
plt.imshow(image_characterTestPattern2_Butterworth1, cmap='gray')
plt.title('D0 = 10')
plt.axis('off')
plt.subplot(2,2,3)
plt.imshow(image_characterTestPattern2_Butterworth2, cmap='gray')
plt.title('D0 = 100')
plt.axis('off')
plt.subplot(2,2,4)
plt.imshow(image_characterTestPattern2_Butterworth3, cmap='gray')
plt.title('D0 = 1000')
plt.axis('off')
plt.tight_layout()
plt.show()

Binary file not shown.

After

Width:  |  Height:  |  Size: 64 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 108 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 143 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 128 KiB