Matlab傅里叶变换和各种滤波处理
- 格式:docx
- 大小:1.19 MB
- 文档页数:8
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; %截止频率为50
m=fix(M/2); n=fix(N/2);
for i=1:M
for j=1:N
d=sqrt((i-m)^2+(j-n)^2);
h=1/(1+0.414*(d/d0)^(2*nn)); % 计算低通滤波器传递函数
result(i,j)=h*g(i,j);
end
end
result=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,[5 5]); %维纳滤波
Y2=wiener2(J2,[5 5]);
Y3=wiener2(J3,[5 5]);
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; for i=1:a for j=1:b distance=sqrt((i-a0)^2+(j-b0)^2); if distance<=d h=1;
else h=0;
end;
s(i,j)=h*s(i,j);
end; end;
s=uint8(real(ifft2(ifftshift(s))));
figure(3); imshow(s);
title('低通滤波所得图像');