基于傅里叶的图像旋转实验的Matlab代码
时间: 2023-06-26 16:06:50 浏览: 95
以下是基于傅里叶变换的图像旋转实验的Matlab代码:
```matlab
% Load the image
I = imread('input_image.jpg');
figure, imshow(I), title('Original Image');
% Define the angle of rotation
theta = 45; % in degrees
% Convert the angle to radians
theta = theta * pi / 180;
% Define the rotation matrix
R = [cos(theta) -sin(theta); sin(theta) cos(theta)];
% Define the frequency domain coordinates
[M, N] = size(I);
u = 0:(M-1);
v = 0:(N-1);
[U, V] = meshgrid(u, v);
% Define the center of the image
center_x = floor(M/2);
center_y = floor(N/2);
% Shift the frequency domain coordinates to the center
U = U - center_x;
V = V - center_y;
% Transform the frequency domain coordinates using the rotation matrix
X = R(1,1)*U + R(1,2)*V;
Y = R(2,1)*U + R(2,2)*V;
% Shift the frequency domain coordinates back to the origin
X = X + center_x;
Y = Y + center_y;
% Interpolate the image values at the new coordinates
I_rotated = interp2(double(I), X, Y);
% Display the rotated image
figure, imshow(uint8(I_rotated)), title('Rotated Image');
```
在这个代码中,我们首先加载了一个输入图像,并定义了旋转角度。接下来,我们将旋转角度转换为弧度,并定义旋转矩阵。然后,我们定义频域坐标系,并将其移动到图像中心。我们使用旋转矩阵变换频域坐标,然后将其移回原点,并在新的坐标上插值图像值。最后,我们显示旋转后的图像。
阅读全文