用Python编写代码显示图像的DFT图像和log transformed DFT图像和centralized DFT图像的代码
时间: 2023-06-01 19:07:26 浏览: 83
以下是代码:
```python
import cv2
import numpy as np
from matplotlib import pyplot as plt
# 读取图像
img = cv2.imread("image.jpg", 0)
# 计算DFT
dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT)
# 平移DFT
dft_shift = np.fft.fftshift(dft)
# 计算幅值谱
magnitude_spectrum = 20 * np.log(cv2.magnitude(dft_shift[:, :, 0], dft_shift[:, :, 1]))
# 显示图像
plt.subplot(131), plt.imshow(img, cmap='gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(132), plt.imshow(magnitude_spectrum, cmap='gray')
plt.title('Log Transformed DFT'), plt.xticks([]), plt.yticks([])
# 中心化DFT
rows, cols = img.shape
crow, ccol = int(rows / 2), int(cols / 2)
dft_shift[crow - 30:crow + 30, ccol - 30:ccol + 30] = 0
# 逆变换DFT
idft_shift = np.fft.ifftshift(dft_shift)
idft = cv2.idft(idft_shift)
centralized_spectrum = cv2.magnitude(idft[:, :, 0], idft[:, :, 1])
plt.subplot(133), plt.imshow(centralized_spectrum, cmap='gray')
plt.title('Centralized DFT'), plt.xticks([]), plt.yticks([])
plt.show()
```
阅读全文