基于FFT的图像平移与旋转实验的代码设计
时间: 2023-12-18 22:02:43 浏览: 31
以下是基于FFT的图像平移与旋转实验的代码设计,以Python语言为例:
```python
import cv2
import numpy as np
# 读取图像
img = cv2.imread('image.jpg', 0)
# 进行傅里叶变换
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
# 平移操作
tx, ty = 100, 50
rows, cols = img.shape
crow, ccol = rows // 2, cols // 2
shift_matrix = np.float32([[1, 0, tx], [0, 1, ty]])
shifted = cv2.warpAffine(fshift, shift_matrix, (cols, rows))
# 旋转操作
theta = 30
M = cv2.getRotationMatrix2D((ccol, crow), theta, 1)
rotated = cv2.warpAffine(fshift, M, (cols, rows))
# 进行傅里叶反变换
ishift = np.fft.ifftshift(shifted)
ifft = np.fft.ifft2(ishift)
ishift = np.fft.ifftshift(rotated)
ifft2 = np.fft.ifft2(ishift)
# 归一化处理
shifted_img = np.abs(ifft)
shifted_img = cv2.normalize(shifted_img, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
rotated_img = np.abs(ifft2)
rotated_img = cv2.normalize(rotated_img, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
# 显示结果图像
cv2.imshow('original', img)
cv2.imshow('shifted', shifted_img)
cv2.imshow('rotated', rotated_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
其中,假设原始图像为'image.jpg',平移距离为(100, 50),旋转角度为30度。代码使用OpenCV库进行图像处理,包括傅里叶变换、平移操作、旋转操作、傅里叶反变换、归一化处理等。最终显示原始图像、平移后的图像、旋转后的图像。