用python实现图像频谱图旋转代码
时间: 2023-12-20 08:05:39 浏览: 34
以下是使用Python实现图像频谱图旋转的代码:
```python
import cv2
import numpy as np
# 读取图像
img = cv2.imread('image.png', 0)
# 进行傅里叶变换
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
# 获取频谱图像
magnitude_spectrum = 20 * np.log(np.abs(fshift))
# 进行旋转
rows, cols = img.shape
theta = 45 # 旋转角度
M = cv2.getRotationMatrix2D((cols / 2, rows / 2), theta, 1)
magnitude_spectrum_rotated = cv2.warpAffine(magnitude_spectrum, M, (cols, rows))
# 显示原始图像和旋转后的频谱图像
cv2.imshow('Image', img)
cv2.imshow('Magnitude Spectrum', magnitude_spectrum)
cv2.imshow('Rotated Magnitude Spectrum', magnitude_spectrum_rotated)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
其中,代码的主要步骤包括:
1. 读取图像。
2. 进行傅里叶变换,并通过fftshift函数将低频分量移动到频谱图像的中心。
3. 获取频谱图像,并通过20 * np.log函数将其转换为可视化的灰度图像。
4. 进行旋转,并通过cv2.getRotationMatrix2D函数获取旋转矩阵。
5. 将旋转矩阵应用于频谱图像,并通过cv2.warpAffine函数进行旋转操作。
6. 显示原始图像和旋转后的频谱图像。
需要注意的是,由于频谱图像是复数形式的,因此在进行旋转操作时需要对其实部和虚部同时进行旋转,而不仅仅是实部或虚部。