给出python代码实现,图像频率域数据相位和幅度分离,对幅度不做改变,然后将相位旋转到正确的角度。一旦相位被旋转,我们可以将幅度和已修正的相位合并,并通过逆变换将它们转换回时域
时间: 2024-04-29 14:26:12 浏览: 8
Sure! 以下是实现图像频率域相位和幅度分离的 Python 代码:
```python
import numpy as np
import cv2
# 读入图像
img = cv2.imread('example.jpg', 0)
# 将图像做傅里叶变换
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
# 获取频率域的幅度和相位
magnitude = 20 * np.log(np.abs(fshift))
phase = np.angle(fshift)
# 旋转相位到正确的角度
rotated_phase = np.zeros_like(phase)
rows, cols = img.shape
theta = 45 # 旋转的角度
rotation_matrix = cv2.getRotationMatrix2D((cols/2, rows/2), theta, 1)
for i in range(rows):
for j in range(cols):
rotated_coords = np.dot(rotation_matrix, [j, i, 1]) # 旋转坐标
rotated_col, rotated_row, _ = rotated_coords.astype(int)
if rotated_col >= cols or rotated_row >= rows:
continue
rotated_phase[rotated_row, rotated_col] = phase[i, j]
# 合并幅度和已修正的相位
new_fshift = magnitude * np.exp(1j * rotated_phase)
# 将修正后的频率域数据做逆变换,得到时域图像
ifft = np.fft.ifftshift(new_fshift)
reconstructed_image = np.fft.ifft2(ifft).real
# 显示结果
cv2.imshow('Original Image', img)
cv2.imshow('Reconstructed Image', reconstructed_image.astype(np.uint8))
cv2.waitKey(0)
cv2.destroyAllWindows()
```
代码中使用的 `getRotationMatrix2D` 函数可以帮助我们计算出一个旋转矩阵,用于将坐标旋转到正确的位置。为了保证旋转后的坐标不超出图像范围,我们使用了 `if rotated_col >= cols or rotated_row >= rows` 这个判断语句。
希望这个代码能够帮到你!