Matlab傅里叶变换模和相位和各种滤波处理傅里叶变换模和相位img=imread('h:\img.png');f=fft2(img);%傅里叶变换f=fftshift(f);%使图像对称r=real(f);%图像频域实部i=imag(f);%图像频域虚部margin=log(abs(f));%图像幅度谱,加log便于显示phase=log(angle(f)*180/pi);%图像相位谱l=log(f);subplot(2,2,1),imshow(img),title('源图像');subplot(2,2,2),imshow(l,[]),title('图像频谱');subplot(2,2,3),imshow(margin,[]),title('图像幅度谱');subplot(2,2,4),imshow(phase,[]),title('图像相位谱');添加高斯噪声、四阶巴特沃斯低通滤波I=imread('h:\img.png');x=rgb2gray(I);y1=imnoise(x,'gaussian',0,0.02);f=double(y1);%数据类型转换,MATLAB不支持图像的无符号整型的计算g=fft2(f);%傅立叶变换g=fftshift(g);%转换数据矩阵[M,N]=size(g);nn=4;%四阶巴特沃斯(Butterworth)低通滤波器d0=50;%截止频率为50m=fix(M/2);n=fix(N/2);fori=1:Mforj=1:Nd=sqrt((i-m)^2+(j-n)^2);h=1/(1+0.414*(d/d0)^(2*nn));%计算低通滤波器传递函数result(i,j)=h*g(i,j);endendresult=ifftshift(result);y2=ifft2(result);y3=uint8(real(y2));subplot(1,2,1),imshow(y1),title('添加高斯噪声后的图像');subplot(1,2,2),imshow(y3),title('四阶巴特沃斯低通滤波图像');维纳滤波I=imread('h:\img.png');x=rgb2gray(I);J1=imnoise(x,'gaussian',0,0.02);%给图像添加高斯噪声J2=imnoise(x,'salt&pepper',0.02);%给图像添加椒盐噪声J3=imnoise(x,'speckle',0.02);%给图像添加乘性噪声Y1=wiener2(J1,[55]);%维纳滤波Y2=wiener2(J2,[55]);Y3=wiener2(J3,[55]);subplot(2,2,1),imshow(x),title('灰度图');subplot(2,2,2),imshow(Y1),title('高斯噪声维纳滤波');subplot(2,2,3),imshow(Y2),title('椒盐噪声维纳滤波');subplot(2,2,4),imshow(Y3),title('乘性噪声维纳滤波');中值滤波I=imread('h:\img.png');x=rgb2gray(I);J1=imnoise(x,'gaussian',0,0.02);%给图像添加高斯噪声J2=imnoise(x,'salt&pepper',0.02);%给图像添加椒盐噪声J3=imnoise(x,'speckle',0.02);%给图像添加乘性噪声Y1=medfilt2(J1);%在默认3×3的邻域窗中进行中值滤波Y2=medfilt2(J2);%在默认3×3的邻域窗中进行中值滤波Y3=medfilt2(J3);%在默认3×3的邻域窗中进行中值滤波subplot(2,2,1),imshow(x),title('灰度图');subplot(2,2,2),imshow(Y1),title('高斯噪声中值滤波');subplot(2,2,3),imshow(Y2),title('椒盐噪声中值滤波');subplot(2,2,4),imshow(Y3),title('乘性噪声中值滤波');低通滤波器在频率域实现低通滤波I=imread('h:\img.png');I=rgb2gray(I);figure(1),imshow(I);title('原图像');s=fftshift(fft2(I));figure(2);imshow(abs(s),[]);title('图像傅里叶变换所得频谱');[a,b]=size(s);a0=round(a/2);b0=round(b/2);d=10;fori=1:aforj=1:bdistance=sqrt((i-a0)^2+(j-b0)^2);ifdistance=dh=1;elseh=0;end;s(i,j)=h*s(i,j);end;end;s=uint8(real(ifft2(ifftshift(s))));figure(3);imshow(s);title('低通滤波所得图像');高通滤波器在频率域实现高频增强I=imread('h:\img.png');I=rgb2gray(I);figure(1),imshow(I);title('原图像');s=fftshift(fft2(I));figure(2);imshow(abs(s),[]);title('图像傅里叶变换所得频谱');figure(3);imshow(log(abs(s)),[]);title('图像傅里叶变换取对数所得频谱');[a,b]=size(s);a0=round(a/2);b0=round(b/2);d=10;p=0.2;q=0.5;fori=1:aforj=1:bdistance=sqrt((i-a0)^2+(j-b0)^2);ifdistance=dh=0;elseh=1;end;s(i,j)=(p+q*h)*s(i,j);end;end;s=uint8(real(ifft2(ifftshift(s))));figure(4);imshow(s);title('高通滤波所得图像');figure(5);imshow(s+I);title('高通滤波所得高频增强图像');其他函数滤波I=imread('h:\img.png');J=imnoise(I,'gaussian',0.001,0.01);%高斯噪声定义A=fspecial('average',[33]);%定义average滤波器B=fspecial('gaussian',[33],0.5);%定义gaussian滤波器C=fspecial('motion',9,0);%定义motion滤波器D=fspecial('unsharp',0.2);%定义unsharp滤波器subplot(2,3,1);imshow(I);title('(a)原始图像');%为图像叠加高斯噪声subplot(2,3,2);imshow(J);title('(b)叠加高斯噪声图');h1=imfilter(J,A);%对图像进行average滤波subplot(2,3,3);imshow(h1);title('(c)average滤波图');h2=imfilter(J,B);%对图像进行gaussian滤波subplot(2,3,4);imshow(h2);title('(d)gaussian滤波图');h3=imfilter(J,C);%对图像进行motion滤波subplot(2,3,5);imshow(h3);title('(e)motion滤波图');h4=imfilter(J,D);%对图像进行unsharp滤波subplot(2,3,6);imshow(h4);title('(f)unsharp滤波图');figure;%新建图像窗口K=imnoise(I,'salt&pepper',0.1);%定义椒盐噪声subplot(2,3,1);imshow(I);title('(a)原始图像');%为图像叠加椒盐噪声subplot(2,3,2);imshow(J);title('(b)叠加椒盐噪声图');l1=imfilter(K,A);%对图像进行average滤波subplot(2,3,3);imshow(l1);title('(c)average滤波图');l2=imfilter(K,B);%对图像进行gaussian滤波subplot(2,3,4);imshow(l2);title('(d)gaussion滤波图');l3=imfilter(K,B);%对图像进行motion滤波subplot(2,3,5);imshow(l3);title('(e)motion滤波图');l4=imfilter(K,B);%对图像进行unsharp滤波subplot(2,3,6);imshow(l4);title('(f)unsharp滤波图');