图像的频域滤波
import numpy as np
import matplotlib.pyplot as plt
def gauss_filter(img, D0, N=2, type='lp'):
# 离散傅里叶变换
fft = np.fft.fft2(img)
# 中心化
fft_shift = np.fft.fftshift(fft)
plt.imshow(np.log(1 + np.abs(fft_shift)), 'gray')
plt.show()
rows, cols = img.shape
cx, cy = int(rows / 2), int(cols / 2) # 计算频谱中心
mask = np.zeros((rows, cols))
for i in range(rows):
for j in range(cols):
D = np.sqrt((i - cx) ** 2 + (j - cy) ** 2) # 计算D(u,v)
if (type == 'lp'): # 低通
mask[i, j] = np.exp(-(D * D) / (2 * D0 * D0))
elif (type == 'hp'): # 高通
mask[i, j] = (1 - np.exp(-(D * D) / (2 * D0 * D0)))
elif (type == 'notch'): # 陷波
# crow1, ccol1 = int(rows / 2), int(1.5*cols / 4)
# crow2, ccol2 = int(rows / 2), int(2.5 * cols / 4)
crow1, ccol1 = int(rows / 2), int(1.22*cols / 3)
crow2, ccol2 = int(rows / 2), int(1.78 * cols / 3)
crow3, ccol3 = int(rows / 2), int(1.3*cols / 4)
crow4, ccol4 = int(rows / 2), int(2.7 *cols / 4)
crow5, ccol5 = int(rows / 2), int(1.4*cols / 6)
crow6, ccol6 = int(rows / 2), int(4.6 *cols / 6)
D1 = np.sqrt((i - crow1) ** 2 + (j - ccol1) ** 2) # 计算D(crow1,ccol1)
D2 = np.sqrt((i - crow2) ** 2 + (j - ccol2) ** 2) # 计算D(crow2,ccol2)
D3 = np.sqrt((i - crow3) ** 2 + (j - ccol3) ** 2) # 计算D(crow3,ccol3)
D4 = np.sqrt((i - crow4) ** 2 + (j - ccol4) ** 2) # 计算D(crow4,ccol4)
D5 = np.sqrt((i - crow5) ** 2 + (j - ccol5) ** 2) # 计算D(crow5,ccol5)
D6 = np.sqrt((i - crow6) ** 2 + (j - ccol6) ** 2) # 计算D(crow6,ccol6)
# 陷波
mask[i, j] = (1 - np.exp(- (D1 ** 2) / (2 * D0 ** 2)))*(1 - np.exp(- (D2 ** 2) / (2 * D0 ** 2)))\
*(1 - np.exp(- (D3 ** 2) / (2 * D0 ** 2)))*(1-np.exp(- (D4 ** 2) / (2 * D0 ** 2))) \
* (1 - np.exp(- (D5 ** 2) / (2 * D0 ** 2))) * (1 - np.exp(- (D6 ** 2) / (2 * D0 ** 2)))
elif (type == 'boost'): # 提升
mask[i, j] = 1 - np.exp(- (D ** 2) / (2 * D0 ** 2)) + 1
assert ('type error')
fshift = fft_shift * mask
plt.imshow(np.log(1 + np.abs(fshift)), 'gray')
plt.show()
f_ishift = np.fft.ifftshift(fshift)
img_back = np.fft.ifft2(f_ishift)
img_back = np.abs(img_back)
return img_back
高斯低通
高斯低通表达式为:
Hl(u,v)=e−D2(u,v)/2D02H_{l}(u, v)=e^{-D^{2}(u, v) / 2 D_{0}^{2}}Hl(u,v)=e−D2(u,v)/2D02
其中D(u,v)D(u,v)D(u,v)是频率矩阵中心到矩阵中包含的任意一点(u,v)(u,v)(u,v)的距离。D0D_0D0是截止频率。
高斯高通
高斯高通表达式为
Hh(u,v)=1−e−D2(u,v)/2D02H_{h}(u, v)=1-e^{-D^{2}(u, v) / 2 D_{0}^{2}}Hh(u,v)=1−e−D2(u,v)/2D02
高频提升滤波器
高频提升滤波器是高通加全通HaH_aHa:
H(u,v)=Hh(u,v)+Ha(u,v)H(u,v)=H_h(u,v)+H_a(u,v)H(u,v)=Hh(u,v)+Ha(u,v)
高斯陷波滤波器
高斯陷波滤波器:陷波滤波器可以阻止或允许以某个频率为中心的邻域里的频率通过,例如,设D1(u,v),D2(u,v)D_1(u,v), D_2(u,v)D1(u,v),D2(u,v)分别为两个频率中心到矩阵中包含的任意一点(u,v)(u , v )(u,v) 的距离,要阻止这两个频域中心的邻域,相应的陷波滤波器为:
Hh(u,v)=(1−e−D12(u,v)/2D02)⋅(1−e−D22(u,v)/2D02)H_{h}(u, v)=\left(1-e^{-D_{1}^{2}(u, v) / 2 D_{0}^{2}}\right) \cdot\left(1-e^{-D_{2}^{2}(u, v) / 2 D_{0}^{2}}\right)Hh(u,v)=(1−e−D12(u,v)/2D02)⋅(1−e−D22(u,v)/2D02)
竖直条纹噪声反应在频谱图中的 XXX轴上,因此,使用六个高斯高通滤波器合成的陷波滤波器对图像进行处理,可以对其进行去除: