图像的平移并不会影响图像的频谱,同时,图像的相位会随着图像的旋转而旋转。
下面我用矩形的频谱图来说明图像中矩形的平移并不会对频谱有丝毫的影响。
Matlab代码:
clear all
close all
%% Author: J27
% Jesus love you!
Isize = 512;
Rwidth = 50;
Rlength = 3*Rwidth;
Irect = zeros(Isize);
Irect(floor((Isize - Rlength)/2) + 1:floor((Isize - Rlength)/2) + Rlength,...
(floor(Isize - Rwidth)/2) + 1:floor((Isize - Rwidth)/2) + Rwidth) = 1;
subplot(3,1,1)
imshowpair(Irect,log(abs(fftshift(fft2(Irect)))+1),'montage')
Irect = zeros(Isize);
Irect(floor((Isize - Rlength)/2) + 150 + 1:floor((Isize - Rlength)/2) + 150 + Rlength,...
(floor(Isize - Rwidth)/2) + 1 + 200:floor((Isize - Rwidth)/2) + Rwidth + 200) = 1;
subplot(3,1,2)
imshowpair(Irect,log(abs(fftshift(fft2(Irect)))+1),'montage')
Irect = zeros(Isize);
Irect(floor((Isize - Rlength)/2) + 1:floor((Isize - Rlength)/2) + Rlength,...
(floor(Isize - Rwidth)/2) + 1 - 80:floor((Isize - Rwidth)/2) + Rwidth - 80) = 1;
subplot(3,1,3)
imshowpair(Irect,log(abs(fftshift(fft2(Irect)))+1),'montage')
再比如下面这个例子:
再来看看频谱随着矩形的旋转而旋转相同的角度。
Matlab代码:
Isize = 512;
Rwidth = 50;
Rlength = 3*Rwidth;
Irect = zeros(Isize);
Irect(floor((Isize - Rlength)/2) + 1:floor((Isize - Rlength)/2) + Rlength,...
(floor(Isize - Rwidth)/2) + 1:floor((Isize - Rwidth)/2) + Rwidth) = 1;
Irot = imrotate(Irect, 15, 'crop', 'bilinear');
subplot(3,1,1)
imshowpair(Irot,log(abs(fftshift(fft2(Irot)))+1),'montage')
Irect = zeros(Isize);
Irect(floor((Isize - Rlength)/2) + 1:floor((Isize - Rlength)/2) + Rlength,...
(floor(Isize - Rwidth)/2) + 1:floor((Isize - Rwidth)/2) + Rwidth) = 1;
Irot = imrotate(Irect, 45, 'crop', 'bilinear');
subplot(3,1,2)
imshowpair(Irot,log(abs(fftshift(fft2(Irot)))+1),'montage')
Irect = zeros(Isize);
Irect(floor((Isize - Rlength)/2) + 1:floor((Isize - Rlength)/2) + Rlength,...
(floor(Isize - Rwidth)/2) + 1:floor((Isize - Rwidth)/2) + Rwidth) = 1;
Irot = imrotate(Irect, 90, 'crop', 'bilinear');
subplot(3,1,3)
imshowpair(Irot,log(abs(fftshift(fft2(Irot)))+1),'montage')
Part II 平移和旋转对相位的影响
先用一个简单的例子来说明图像相位的作用(所用图像为cameraman),在图像的频域分析和滤波中,相位是常常被忽略的。虽然相位分量的贡献很不直观,但是它恰恰很重要。相位是频谱中各正弦分量关于原点的位移的度量。
上面的小实验充分说明了,看似无用的,且常常被忽略的相位,在DFT的频域中起到了多么重要的作用(注意区分实部和虚部(直角坐标系)VS 频谱和相位(极坐标系)!)。
Matlab代码:
close all;
clear all;
I = im2double(imread('cameraman.tif'));
DFT = fft2(I);
absF = abs(DFT);
Phi = atan2(imag(DFT),real(DFT));
subplot(2,3,1);
imshow(I,[]);
title('Image(Cameraman)');
subplot(2,3,2);
imshow(log(fftshift(absF) + 1),[]);
title('Spectrum');
subplot(2,3,3);
imshow(fftshift(Phi),[]);
title('Phase');
F = absF.*exp(1j*Phi);
Rebuild = im2uint8(real(ifft2(F)));
subplot(2,3,4);
imshow(Rebuild,[]);
title('Rebuild with orginal specturm and phase');
% rebuild with spectrum only
F = absF.*exp(1j*1);
Rebuild = im2uint8(real(fftshift(ifft2(F))));
subplot(2,3,5);
imshow(Rebuild,[]);
title('Rebuild with spectrum only');
% rebuild with Phase only
g = 1.*exp(1j*Phi);
g = im2uint8(real(ifft2(g)));
subplot(2,3,6);
imshow(g,[]);
title('Rebuild with Phase only');
接下来我们再来看看图像在空间域中的移位和旋转对相位有什么影响。下图中,左边一列是图像,中间一列是频谱,右边一列是相位图。你必须意识到,通过肉眼,你很难从相位图中得到什么有用的信息。
(上图中最后一行打错了,不是“旋转改变了相位”而是“平移改变了相位”)
Matlab代码:
clear all
close all
%% Author: J27
% Jesus love you!
% shifting and rotation
Isize = 512;
Rwidth = 50;
Rlength = 3*Rwidth;
Irect = zeros(Isize);
Irect(floor((Isize - Rlength)/2) + 1:floor((Isize - Rlength)/2) + Rlength,...
(floor(Isize - Rwidth)/2) + 1:floor((Isize - Rwidth)/2) + Rwidth) = 1;
Idft = fft2(Irect);
subplot(3,3,1);
imshow(Irect);
subplot(3,3,2);
imshow(log(abs(fftshift(Idft))+1),[]);
subplot(3,3,3);
imshow((atan2(imag(Idft),real(Idft))),[]);
Irot = imrotate(Irect, 45, 'crop', 'bilinear');
Idft = fft2(Irot);
subplot(3,3,4);
imshow(Irot);
subplot(3,3,5);
imshow(log(abs(fftshift(Idft))+1),[]);
subplot(3,3,6);
imshow(atan2(imag(Idft),real(Idft)),[]);
Irect = zeros(Isize);
Irect(floor((Isize - Rlength)/2) + 150 + 1:floor((Isize - Rlength)/2) + 150 + Rlength,...
(floor(Isize - Rwidth)/2) + 1 + 200:floor((Isize - Rwidth)/2) + Rwidth + 200) = 1;
Idft = fft2(Irect);
subplot(3,3,7);
imshow(Irect);
subplot(3,3,8);
imshow(log(abs(fftshift(Idft))+1),[]);
subplot(3,3,9);
imshow(atan2(imag(Idft),real(Idft)),[]);
Matlab代码:
I = im2double(imread('cameraman.tif'));
Idft = fft2(I);
figure;
subplot(2,3,1);
imshow(I,[]);
subplot(2,3,2);
imshow(log(abs(fftshift(Idft))+1),[]);
subplot(2,3,3);
imshow(atan2(imag(Idft),real(Idft)),[]);
Iswap = fftshift(I);
Idft = fft2(Iswap);
subplot(2,3,4);
imshow(Iswap,[]);
subplot(2,3,5);
imshow(log(abs(fftshift(Idft))+1),[]);
subplot(2,3,6);
imshow(atan2(imag(Idft),real(Idft)),[]);
(全文完)
作者 --- 松下J27
谢谢收看!
格言摘抄:
我是葡萄树,你们是枝子;常在我里面的,我也常在他里面,这人就多结果子;因为离了我,你们就不能作什么。------ 《圣经》约翰福音15章5节
(*配图与本文无关*)
版权声明:所有的笔记,可能来自很多不同的网站和说明,在此没法一一列出,如有侵权,请告知,立即删除。欢迎大家转载,但是,如果有人引用或者COPY我的文章,必须在你的文章中注明你所使用的图片或者文字来自于我的文章,否则,侵权必究。 ----松下J27