四、完成编程任务:打开一幅图像,做dft变换,在频域做高通滤波,反变换,显示滤波后图像效果。
时间: 2023-11-20 22:06:50 浏览: 23
以下是Python代码实现:
```python
import cv2
import numpy as np
from matplotlib import pyplot as plt
# 读取图像
img = cv2.imread('lena.png', 0)
# 进行DFT变换
dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT)
# 将低频分量移动到中心
dft_shift = np.fft.fftshift(dft)
# 创建高通滤波器
rows, cols = img.shape
crow, ccol = rows//2, cols//2
mask = np.ones((rows, cols, 2), np.uint8)
mask[crow-30:crow+30, ccol-30:ccol+30] = 0
# 应用滤波器
fshift = dft_shift*mask
f_ishift = np.fft.ifftshift(fshift)
# 反变换
img_back = cv2.idft(f_ishift)
img_back = cv2.magnitude(img_back[:, :, 0], img_back[:, :, 1])
# 显示图像
plt.subplot(121), plt.imshow(img, cmap='gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(img_back, cmap='gray')
plt.title('High Pass Filter'), plt.xticks([]), plt.yticks([])
plt.show()
```
其中,'lena.png' 是需要使用的图像文件名。运行代码后,程序会显示原始图像和经过高通滤波处理后的图像。滤波器可以根据需要进行调整。