1. FFT变换与频谱/相位谱显示:
- 原理:先将图像转换为灰度图,进行二维快速傅里叶变换(FFT),再将零频率分量移到频谱中心。通过对变换结果的处理得到幅度谱和相位谱。幅度谱反映图像中不同频率成分的强度,相位谱反映各频率成分的相位信息。
- 特点:能够直观展示图像在频域的特征,为后续滤波操作提供基础频域信息。
def show_fft(image):
gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
f = np.fft.fft2(gray_image)
fshift = np.fft.fftshift(f)
magnitude_spectrum = 20 * np.log(np.abs(fshift) + 1)
phase_spectrum = np.angle(fshift)
plt.figure(figsize=(12, 4))
plt.subplot(131), plt.imshow(image)
plt.title('原始图像'), plt.axis('on')
plt.subplot(132), plt.imshow(magnitude_spectrum, cmap='gray')
plt.title('幅度谱'), plt.axis('on')
plt.subplot(133), plt.imshow(phase_spectrum, cmap='gray')
plt.title('相位谱'), plt.axis('on')
plt.show()
show_fft(image)
2. 低通滤波器:
- 原理: - 理想低通滤波器:在指定截止频率内,允许频率通过,截止频率外的频率完全阻挡。通过在频谱中心画圆来创建滤波器掩码。
- 高斯低通滤波器:基于高斯函数,频率越接近中心(低频部分),通过的比例越高,随着频率远离中心(高频部分),通过比例逐渐降低。
- 巴特沃斯低通滤波器:根据滤波器的阶数和截止频率,以一定的数学公式来控制不同频率的通过比例,阶数越高,从通带到阻带的过渡越陡峭。
- 特点: - 理想低通滤波器简单直接,但会产生振铃效应。
- 高斯低通滤波器过渡平滑,无振铃效应,能有效去除高频噪声,使图像平滑。
- 巴特沃斯低通滤波器可通过调整阶数控制过渡带的陡峭程度,性能介于理想和高斯低通滤波器之间。
def ideal_lowpass_filter(shape, cutoff):
rows, cols = shape[:2]
center = (rows // 2, cols // 2)
mask = np.zeros((rows, cols), np.uint8)
cv2.circle(mask, center, cutoff, 1, -1)
return mask
def gaussian_lowpass_filter(shape, cutoff):
rows, cols = shape[:2]
center = (rows // 2, cols // 2)
x, y = np.ogrid[:rows, :cols]
dist = np.sqrt((x - center[0]) ** 2 + (y - center[1]) ** 2)
mask = np.exp(-(dist ** 2) / (2 * (cutoff ** 2)))
return mask
def butterworth_lowpass_filter(shape, cutoff, order):
rows, cols = shape[:2]
center = (rows // 2, cols // 2)
x, y = np.ogrid[:rows, :cols]
dist = np.sqrt((x - center[0]) ** 2 + (y - center[1]) ** 2)
mask = 1 / (1 + (dist / cutoff) ** (2 * order))
return mask
def apply_filter(image, filter_func, *args):
gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
fshift = np.fft.fftshift(np.fft.fft2(gray_image))
mask = filter_func(gray_image.shape, *args)
filtered = fshift * mask
result = np.fft.ifft2(np.fft.ifftshift(filtered))
return np.abs(result)
# 应用低通滤波器
ideal_lp = apply_filter(image, ideal_lowpass_filter, 30)
gaussian_lp = apply_filter(image, gaussian_lowpass_filter, 30)
butterworth_lp = apply_filter(image, butterworth_lowpass_filter, 30, 2)
plt.figure(figsize=(12, 4))
for i, (img, title) in enumerate(zip([ideal_lp, gaussian_lp, butterworth_lp],
['理想低通滤波', '高斯低通滤波', '巴特沃斯低通滤波'])):
plt.subplot(1, 3, i + 1), plt.imshow(img, cmap='gray')
plt.title(title), plt.axis('on')
plt.show()
3. 高通滤波器:
- 原理:通过从全通滤波器(值全为1的掩码)中减去对应的低通滤波器掩码来得到高通滤波器掩码,使得低频部分被抑制,高频部分得以通过。
- 特点:突出图像中的边缘和细节信息,因为边缘和细节通常对应高频成分,常用于图像锐化处理。
def highpass_filter_from_lowpass(lowpass_filter):
return 1 - lowpass_filter
# 应用高通滤波器
ideal_hp = highpass_filter_from_lowpass(ideal_lowpass_filter(image.shape, 30))
gaussian_hp = highpass_filter_from_lowpass(gaussian_lowpass_filter(image.shape, 30))
butterworth_hp = highpass_filter_from_lowpass(butterworth_lowpass_filter(image.shape, 30, 2))
ideal_hp_result = apply_filter(image, lambda *args: ideal_hp)
gaussian_hp_result = apply_filter(image, lambda *args: gaussian_hp)
butterworth_hp_result = apply_filter(image, lambda *args: butterworth_hp)
plt.figure(figsize=(12, 4))
for i, (img, title) in enumerate(zip([ideal_hp_result, gaussian_hp_result, butterworth_hp_result],
['理想高通滤波', '高斯高通滤波', '巴特沃斯高通滤波'])):
plt.subplot(1, 3, i + 1), plt.imshow(img, cmap='gray')
plt.title(title), plt.axis('on')
plt.show()
4. 同态滤波:
- 原理:先对图像进行对数变换,将图像的光照分量和反射分量分开(在频域中),然后应用一个滤波器,该滤波器对低频和高频有不同的增益调整,最后进行指数变换恢复图像。
- 特点:可以同时增强图像的对比度和细节,改善图像的视觉效果,尤其适用于光照不均匀的图像。
def homomorphic_filter(image, gamma_l=0.5, gamma_h=2.0, cutoff=30):
gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
image_log = np.log1p(np.array(gray_image, dtype="float"))
fshift = np.fft.fftshift(np.fft.fft2(image_log))
rows, cols = gray_image.shape
center = (rows // 2, cols // 2)
x, y = np.ogrid[:rows, :cols]
dist = np.sqrt((x - center[0]) ** 2 + (y - center[1]) ** 2)
mask = (gamma_h - gamma_l) * (1 - np.exp(-(dist ** 2) / (2 * (cutoff ** 2)))) + gamma_l
filtered = fshift * mask
result = np.fft.ifft2(np.fft.ifftshift(filtered))
return np.expm1(np.abs(result))
homomorphic_result = homomorphic_filter(image)
homomorphic_result = np.clip(homomorphic_result, 0, 255).astype(np.uint8)
plt.imshow(homomorphic_result, cmap='gray')
plt.title('同态滤波'), plt.axis('on')
plt.show()
5. 陷波滤波: - 原理:在频谱中指定一些中心位置(陷波中心),以这些中心为圆心,在一定半径(截止频率)内创建滤波器掩码,将这些位置的频率成分去除或衰减。
- 特点:可以有针对性地去除或衰减图像中特定频率的噪声或干扰成分,保留其他频率信息,常用于去除周期性噪声等特定场景。
def notch_filter(shape, cutoff, notch_centers):
rows, cols = shape[:2]
mask = np.ones((rows, cols), np.uint8)
for center in notch_centers:
cv2.circle(mask, center, cutoff, 0, -1)
return mask
notch_centers = [(100, 100), (200, 200)] # 示例陷波位置
notch = notch_filter(image.shape, 10, notch_centers)
filtered_notch = apply_filter(image, lambda *args: notch)
plt.imshow(filtered_notch, cmap='gray')
plt.title('陷波滤波'), plt.axis('on')
plt.show()