完成高斯高通滤波

This commit is contained in:
张梦南 2025-05-06 22:04:15 +08:00
parent f7435ab4f6
commit b8a95e3da7
6 changed files with 74 additions and 0 deletions

View File

@ -0,0 +1,63 @@
import cv2
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['simHei']
plt.rcParams['axes.unicode_minus'] = False
from Gaussian_Highpass import gaussian_high_pass_filter
# 读取照片
image_characterTestPattern2 = cv2.imread('file/characterTestPattern2.jpg', 0) # 改成你自己的图片路径
# 快速傅里叶变换
dft_characterTestPattern2 = np.fft.fft2(image_characterTestPattern2)
shift_characterTestPattern2 = np.fft.fftshift(dft_characterTestPattern2)
# 高斯高通滤波器参数
D0 = 10
D1 = 100
D2 = 1000
ghpf0 = gaussian_high_pass_filter(image_characterTestPattern2.shape, D0)
ghpf1 = gaussian_high_pass_filter(image_characterTestPattern2.shape, D1)
ghpf2 = gaussian_high_pass_filter(image_characterTestPattern2.shape, D2)
# 应用滤波器
shift_filtered0 = shift_characterTestPattern2 * ghpf0
shift_filtered1 = shift_characterTestPattern2 * ghpf1
shift_filtered2 = shift_characterTestPattern2 * ghpf2
# 逆傅里叶变换
dft_ishift0 = np.fft.ifftshift(shift_filtered0)
image0 = np.fft.ifft2(dft_ishift0)
image0 = np.abs(image0)
dft_ishift1 = np.fft.ifftshift(shift_filtered1)
image1 = np.fft.ifft2(dft_ishift1)
image1 = np.abs(image1)
dft_ishift2 = np.fft.ifftshift(shift_filtered2)
image2 = np.fft.ifft2(dft_ishift2)
image2 = np.abs(image2)
#展示图像
plt.subplot(2,2,1)
plt.imshow(image_characterTestPattern2, cmap='gray')
plt.title('原图')
plt.axis('off')
plt.subplot(2,2,2)
plt.imshow(image0, cmap='gray')
plt.title('高斯高通滤波器D0=10')
plt.axis('off')
plt.subplot(2,2,3)
plt.imshow(image1, cmap='gray')
plt.title('高斯高通滤波器D0=100')
plt.axis('off')
plt.subplot(2,2,4)
plt.imshow(image2, cmap='gray')
plt.title('高斯高通滤波器D0=1000')
plt.axis('off')
plt.tight_layout()
plt.show()

11
Gaussian_Highpass.py Normal file
View File

@ -0,0 +1,11 @@
import numpy as np
def gaussian_high_pass_filter(shape, cutoff):
rows, cols = shape
crow, ccol = rows // 2 , cols // 2
x = np.arange(0, cols)
y = np.arange(0, rows)
x, y = np.meshgrid(x, y)
distance = np.sqrt((x - ccol)**2 + (y - crow)**2)
high_pass = 1 - np.exp(-(distance**2) / (2 * (cutoff**2)))
return high_pass

Binary file not shown.

After

Width:  |  Height:  |  Size: 149 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 119 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 110 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 132 KiB