图像的傅里叶变换的频谱特征 三(平移,旋转,相位的重要性)

本文探讨了图像傅里叶变换的频谱特征,重点分析了图像平移和旋转对频谱及相位的影响,通过Matlab代码演示了平移不会改变频谱,但会改变相位,而旋转则会使频谱随之旋转。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

图像傅里叶变换的频谱特征 三

 

6,平移和旋转

图像的平移并不会影响图像的频谱,同时,图像的相位会随着图像的旋转而旋转。

 

Part I 平移和旋转对频谱的影响

下面我用矩形的频谱图来说明图像中矩形的平移并不会对频谱有丝毫的影响。

 

Matlab代码:

 


 
 
  1. clear all
  2. close all
  3. %% Author: J27
  4. % Jesus love you!
  5. Isize = 512;
  6. Rwidth = 50;
  7. Rlength = 3*Rwidth;
  8. Irect = zeros(Isize);
  9. Irect(floor((Isize - Rlength)/ 2) + 1:floor((Isize - Rlength)/ 2) + Rlength,...
  10. (floor(Isize - Rwidth)/ 2) + 1:floor((Isize - Rwidth)/ 2) + Rwidth) = 1;
  11. subplot( 3, 1, 1)
  12. imshowpair(Irect,log(abs(fftshift(fft2(Irect)))+ 1), 'montage')
  13. Irect = zeros(Isize);
  14. Irect(floor((Isize - Rlength)/ 2) + 150 + 1:floor((Isize - Rlength)/ 2) + 150 + Rlength,...
  15. (floor(Isize - Rwidth)/ 2) + 1 + 200:floor((Isize - Rwidth)/ 2) + Rwidth + 200) = 1;
  16. subplot( 3, 1, 2)
  17. imshowpair(Irect,log(abs(fftshift(fft2(Irect)))+ 1), 'montage')
  18. Irect = zeros(Isize);
  19. Irect(floor((Isize - Rlength)/ 2) + 1:floor((Isize - Rlength)/ 2) + Rlength,...
  20. (floor(Isize - Rwidth)/ 2) + 1 - 80:floor((Isize - Rwidth)/ 2) + Rwidth - 80) = 1;
  21. subplot( 3, 1, 3)
  22. imshowpair(Irect,log(abs(fftshift(fft2(Irect)))+ 1), 'montage')

 

再比如下面这个例子:

 

再来看看频谱随着矩形的旋转而旋转相同的角度。

Matlab代码:


 
 
  1. Isize = 512;
  2. Rwidth = 50;
  3. Rlength = 3*Rwidth;
  4. Irect = zeros(Isize);
  5. Irect(floor((Isize - Rlength)/ 2) + 1:floor((Isize - Rlength)/ 2) + Rlength,...
  6. (floor(Isize - Rwidth)/ 2) + 1:floor((Isize - Rwidth)/ 2) + Rwidth) = 1;
  7. Irot = imrotate(Irect, 15, 'crop', 'bilinear');
  8. subplot( 3, 1, 1)
  9. imshowpair(Irot,log(abs(fftshift(fft2(Irot)))+ 1), 'montage')
  10. Irect = zeros(Isize);
  11. Irect(floor((Isize - Rlength)/ 2) + 1:floor((Isize - Rlength)/ 2) + Rlength,...
  12. (floor(Isize - Rwidth)/ 2) + 1:floor((Isize - Rwidth)/ 2) + Rwidth) = 1;
  13. Irot = imrotate(Irect, 45, 'crop', 'bilinear');
  14. subplot( 3, 1, 2)
  15. imshowpair(Irot,log(abs(fftshift(fft2(Irot)))+ 1), 'montage')
  16. Irect = zeros(Isize);
  17. Irect(floor((Isize - Rlength)/ 2) + 1:floor((Isize - Rlength)/ 2) + Rlength,...
  18. (floor(Isize - Rwidth)/ 2) + 1:floor((Isize - Rwidth)/ 2) + Rwidth) = 1;
  19. Irot = imrotate(Irect, 90, 'crop', 'bilinear');
  20. subplot( 3, 1, 3)
  21. imshowpair(Irot,log(abs(fftshift(fft2(Irot)))+ 1), 'montage')

 

 

Part II 平移和旋转对相位的影响

     先用一个简单的例子来说明图像相位的作用(所用图像为cameraman),在图像的频域分析和滤波中,相位是常常被忽略的。虽然相位分量的贡献很不直观,但是它恰恰很重要。相位是频谱中各正弦分量关于原点的位移的度量。

上面的小实验充分说明了,看似无用的,且常常被忽略的相位,在DFT的频域中起到了多么重要的作用(注意区分实部和虚部(直角坐标系)VS 频谱和相位(极坐标系)!)。

Matlab代码:


 
 
  1. close all;
  2. clear all;
  3. I = im2double(imread( 'cameraman.tif'));
  4. DFT = fft2(I);
  5. absF = abs(DFT);
  6. Phi = atan2(imag(DFT),real(DFT));
  7. subplot( 2, 3, 1);
  8. imshow(I,[]);
  9. title( 'Image(Cameraman)');
  10. subplot( 2, 3, 2);
  11. imshow(log(fftshift(absF) + 1),[]);
  12. title( 'Spectrum');
  13. subplot( 2, 3, 3);
  14. imshow(fftshift(Phi),[]);
  15. title( 'Phase');
  16. F = absF.*exp( 1j*Phi);
  17. Rebuild = im2uint8(real(ifft2(F)));
  18. subplot( 2, 3, 4);
  19. imshow(Rebuild,[]);
  20. title( 'Rebuild with orginal specturm and phase');
  21. % rebuild with spectrum only
  22. F = absF.*exp( 1j* 1);
  23. Rebuild = im2uint8(real(fftshift(ifft2(F))));
  24. subplot( 2, 3, 5);
  25. imshow(Rebuild,[]);
  26. title( 'Rebuild with spectrum only');
  27. % rebuild with Phase only
  28. g = 1.*exp( 1j*Phi);
  29. g = im2uint8(real(ifft2(g)));
  30. subplot( 2, 3, 6);
  31. imshow(g,[]);
  32. title( 'Rebuild with Phase only');

     接下来我们再来看看图像在空间域中的移位和旋转对相位有什么影响。下图中,左边一列是图像,中间一列是频谱,右边一列是相位图。你必须意识到,通过肉眼,你很难从相位图中得到什么有用的信息。

(上图中最后一行打错了,不是“旋转改变了相位”而是“平移改变了相位”)

Matlab代码:


 
 
  1. clear all
  2. close all
  3. %% Author: J27
  4. % Jesus love you!
  5. % shifting and rotation
  6. Isize = 512;
  7. Rwidth = 50;
  8. Rlength = 3*Rwidth;
  9. Irect = zeros(Isize);
  10. Irect(floor((Isize - Rlength)/ 2) + 1:floor((Isize - Rlength)/ 2) + Rlength,...
  11. (floor(Isize - Rwidth)/ 2) + 1:floor((Isize - Rwidth)/ 2) + Rwidth) = 1;
  12. Idft = fft2(Irect);
  13. subplot( 3, 3, 1);
  14. imshow(Irect);
  15. subplot( 3, 3, 2);
  16. imshow(log(abs(fftshift(Idft))+ 1),[]);
  17. subplot( 3, 3, 3);
  18. imshow((atan2(imag(Idft),real(Idft))),[]);
  19. Irot = imrotate(Irect, 45, 'crop', 'bilinear');
  20. Idft = fft2(Irot);
  21. subplot( 3, 3, 4);
  22. imshow(Irot);
  23. subplot( 3, 3, 5);
  24. imshow(log(abs(fftshift(Idft))+ 1),[]);
  25. subplot( 3, 3, 6);
  26. imshow(atan2(imag(Idft),real(Idft)),[]);
  27. Irect = zeros(Isize);
  28. Irect(floor((Isize - Rlength)/ 2) + 150 + 1:floor((Isize - Rlength)/ 2) + 150 + Rlength,...
  29. (floor(Isize - Rwidth)/ 2) + 1 + 200:floor((Isize - Rwidth)/ 2) + Rwidth + 200) = 1;
  30. Idft = fft2(Irect);
  31. subplot( 3, 3, 7);
  32. imshow(Irect);
  33. subplot( 3, 3, 8);
  34. imshow(log(abs(fftshift(Idft))+ 1),[]);
  35. subplot( 3, 3, 9);
  36. imshow(atan2(imag(Idft),real(Idft)),[]);

 

Matlab代码:


 
 
  1. I = im2double(imread( 'cameraman.tif'));
  2. Idft = fft2(I);
  3. figure;
  4. subplot( 2, 3, 1);
  5. imshow(I,[]);
  6. subplot( 2, 3, 2);
  7. imshow(log(abs(fftshift(Idft))+ 1),[]);
  8. subplot( 2, 3, 3);
  9. imshow(atan2(imag(Idft),real(Idft)),[]);
  10. Iswap = fftshift(I);
  11. Idft = fft2(Iswap);
  12. subplot( 2, 3, 4);
  13. imshow(Iswap,[]);
  14. subplot( 2, 3, 5);
  15. imshow(log(abs(fftshift(Idft))+ 1),[]);
  16. subplot( 2, 3, 6);
  17. imshow(atan2(imag(Idft),real(Idft)),[]);

                                                                                (全文完)

                                                                                 谢谢收看!

### 实现二维傅里叶变换旋转的Matlab仿真 为了在 MATLAB 中实现二维傅里叶变换并对其进行旋转仿真的操作,可以通过以下方法完成。首先,加载一张灰度图像,并对其应用 `fft2` 函数来进行二维傅立叶变换[^1]。 接着,通过调整频率域中的相位角来模拟旋转效果。这涉及到创建一个与原始图像大小相同的网格,并在此基础上构建相应的相位因子矩阵。最后再利用逆傅里叶变换 (`ifft2`) 将修改后的频谱转换回空间域以观察到旋转的效果。 以下是具体的实现过程: #### 加载图片并显示原图 ```matlab % 读取测试用的灰度图像 gray_image = imread('cameraman.tif'); figure; imshow(uint8(gray_image)); title('Original Image'); ``` #### 执行二维傅里叶变换 ```matlab % 对输入图像做双精度浮点数类型的强制转换后再求解其FFT fft_image = fftshift(fft2(double(gray_image))); figure; imagesc(log(abs(fft_image)+eps)); colormap gray; colorbar; title('Fourier Transform Magnitude Spectrum (log scale)'); axis equal tight; ``` #### 构建用于旋转的角度参数以及对应的相位因子 假设要顺时针方向旋转 θ 度,则有如下关系: \[ e^{-j \cdot 2\pi / N} \] 其中 \(N\)图像尺寸的一边长度(假定为正方形),\( j=\sqrt{-1}\) 表示虚单位。对于任意给定点 `(u,v)` 的位置,在新坐标系下的对应点可通过下述公式得到新的索引值 `(u',v')`: \[ u' = cos(\theta)\times u + sin(\theta)\times v \\ v' = -sin(\theta)\times u + cos(\theta)\times v \] 因此可以根据上述定义构造出整个图像范围内的相位偏移量数组。 ```matlab angle_degrees = 30; % 设置想要旋转的角度 [M, N] = size(gray_image); [U,V] = meshgrid(-1)/2), ... -floor(M/2):ceil((M-1)/2)); rotation_matrix = [cosd(angle_degrees) sind(angle_degrees);... -sind(angle_degrees) cosd(angle_degrees)]; UV_prime = rotation_matrix * [U(:)', V(:)']'; U_rotated = reshape(UV_prime(1,:), M, N); V_rotated = reshape(UV_prime(2,:), M, N); phase_factor = exp(-1i*2*pi*(U_rotated./N + V_rotated./M)); rotated_fft = double(fft_image .* phase_factor); ``` #### 进行反向傅里叶变换查看结果 ```matlab inverse_transformed = real(ifft2(ifftshift(rotated_fft))); figure; subplot(1,2,1); imshow(uint8(inverse_transformed)), title('Rotated Image via FFT'); subplot(1,2,2); fft_after_rotation = fftshift(fft2(double(inverse_transformed))); imagesc(log(abs(fft_after_rotation)+eps)); colormap gray; colorbar; title('Magnitude Spectrum After Rotation'); axis equal tight; ``` 这段程序展示了如何通过对频谱施加特定角度的相位变化来达到图像旋转的目的。值得注意的是这里仅考虑理想情况下的纯几何变换而不涉及任何物理意义层面的影响因素。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值