作者:曾维
图像处理,使用时注意 :
如下图图片进行处理,读者可以将该图片保存下来,再进行如下处理。笔者试过,有些图片不存在本文所说的问题。
rgbfile=imread('1.png');![matlab <wbr>每日学习 <wbr>fft2 <wbr>ifft2函数 matlab <wbr>每日学习 <wbr>fft2 <wbr>ifft2函数](http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif)
grayfile=rgb2gray(rgbfile);
subplot(2,2,1);
imshow(grayfile);%显示刚输入的图片的灰度图
Ft=fft2(grayfile);%进行ft变换
iFt=real(ifft2(Ft));%进行反变换
subplot(2,2,2);
imshow(iFt,[ ]);%显示复原的图片,可结果却不是原图片
可结果却不是原图片,这是为什么呢?理论上说经过fft2变换后再反变换(ifft2)是可以完全复原图像的。我们再来看看问题出在哪了。
不防先拿一个小的矩阵来试试
A=uint8(magic(3))%模拟图片矩阵,256灰度级
B=ifft2(fft2(A))
A =
8 1 6
3 5 7
4 9 2
B =
8.000000000000000e+000 1.000000000000000e+000 6.000000000000000e+000
3.000000000000000e+000 5.000000000000000e+000 7.000000000000000e+000
4.000000000000000e+000 9.000000000000000e+000 2.000000000000000e+000
可以看到,结果A与B唯一的不同点为,B是double型数据,而A是uint8型数据。
我们再来试试,将上面例子的代码最后加一个数据类型转换。
再加如下代码
iFt2=uint8(iFt);
subplot(2,2,3);
imshow(iFt2)
这下输出的图片和原灰度图看上去是一样的了。我们再验正一下。对两个图作一个差
D=abs(grayFile-iFt2);
sum(D(:));
ans =
0
结果等于0,即图片经过反变换复原了
附fft2 和ifft2在matlab 2010帮助
FFT2 Two-dimensional discrete FourierTransform.
FFT2(X)returns the two-dimensional Fourier transform of matrix X.
If X is avector, the result will have the same orientation.
FFT2(X,MROWS,NCOLS) pads matrix X with zeros to sizeMROWS-by-NCOLS
beforetransforming.
Classsupport for input X:
float: double, single
See alsofft, fftn, fftshift, fftw, ifft, ifft2, ifftn.
Referencepage in Help browser
doc fft2
IFFT2 Two-dimensional inverse discrete Fouriertransform.
IFFT2(F)returns the two-dimensional inverse Fourier transform ofmatrix
F. If F is a vector, the result will have thesame orientation.
IFFT2(F,MROWS,NCOLS) pads matrix F with zeros to sizeMROWS-by-NCOLS
beforetransforming.
IFFT2(...,'symmetric') causes IFFT2 to treat F as conjugate symmetric
in twodimensions so that the output is purely real. This option is
useful whenF is not exactly conjugate symmetric merely because of
round-offerror. See the reference page for the specificmathematical
definitionof this symmetry.
IFFT2(...,'nonsymmetric') causes IFFT2 to make no assumptions about the
symmetry ofF.
Classsupport for input F:
float: double, single
See alsofft, fft2, fftn, fftshift, fftw, ifft, ifftn.
Referencepage in Help browser
doc ifft2