MATLAB滤波代码

来源:互联网 发布:java 主要开源项目 编辑:程序博客网 时间:2024/04/30 14:27

clear all;clc;
%FFT变换
I=imread('cameraman.tif');
A1=fft2(I);
A2=ifft2(A1);
A3=uint8(A2);
figure,subplot(221),imshow(I);
subplot(222);imshow(A3);

%DCT变换
I=imread('cameraman.tif');
figure,subplot(223);imshow(I);
I=im2double(I);
T=dctmtx(8);
B=blkproc(I,[8 8], 'P1*x*P2',T,T');
Mask=[1 1 1 1 0 0 0 0
       1 1 1 0 0 0 0 0
       1 1 0 0 0 0 0 0
       1 0 0 0 0 0 0 0
       0 0 0 0 0 0 0 0
       0 0 0 0 0 0 0 0
       0 0 0 0 0 0 0 0
       0 0 0 0 0 0 0 0];
B2=blkproc(B,[8 8],'P1.*x',Mask);    % 此处为点乘(.*)
I2=blkproc(B2,[8 8], 'P1*x*P2',T',T);
subplot(224);imshow(I2);

%直方图均衡
I=imread('pout.tif');
figure,subplot(321);imshow(I);
subplot(322);imhist(I);    
[J,T]=histeq(I,64);      % 图像灰度扩展到0~255,但是只有64个灰度级
subplot(323);imshow(J);
subplot(324);imhist(J);
J=histeq(I,32);
subplot(325);imshow(J);   % 图像灰度扩展到0~255,但是只有32个灰度级
subplot(326);imhist(J);
figure,plot((0:255)/255,T); % 转移函数的变换曲线

%灰度拉伸
I=imread('pout.tif');
figure,subplot(221);imshow(I);
subplot(222);imhist(I);
[X,map]=imread('pout.tif');
Y=imadjust(X,[0.1 0.99],[0 1]);
subplot(223);imshow(Y,map);
subplot(224);imhist(Y);

% 图像空间域处理
[I,map] = imread('pout.tif');
J1=imnoise(I,'gaussian',0,0.02);
% 叠加均值为0,方差为0.02的高斯噪声,可以用localvar代替
%J2=imnoise(I,'salt & pepper',0.04); % 叠加密度为0.04的椒盐噪声
M4=[0 1 0; 1 0 1; 0 1 0];
M4=M4/4;                 % 4邻域平均滤波
I_filter1=filter2(M4,J1);
figure,subplot(321);imshow(I);
subplot(322);imshow(J1);
subplot(323);imshow(I_filter1,map);
M8=[1 1 1; 1 0 1; 1 1 1];      % 8邻域平均滤波
M8=M8/8;
I_filter2=filter2(M8,J1);
subplot(324);imshow(I_filter2,map);
K1 = medfilt2(J1,[3,3]);
subplot(325);imshow(K1);
K2=medfilt2(J2,[7 7]);
subplot(326);imshow(K2);


%频域处理
I=imread('pout.tif');
figure,subplot(221);imshow(I);
J1=imnoise(I,'salt & pepper');   % 叠加椒盐噪声
subplot(222);imshow(J1);
f=double(J1);     % 数据类型转换,MATLAB不支持图像的无符号整型的计算
g=fft2(f);        % 傅立叶变换
g=fftshift(g);     % 转换数据矩阵
[M,N]=size(g);
nn=2;           % 二阶巴特沃斯(Butterworth)低通滤波器
d0=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);
J2=ifft2(result);
J3=uint8(real(J2));
subplot(223);imshow(J3);                      % 显示滤波处理后的图像

[I,map]=imread('pout.tif');
figure,subplot(221);imshow(I,map);
H2=[-1 -1 -1;-1 -9 -1;-1 -1 -1];
J1=filter2(H2,I);             % 高通滤波
subplot(222);imshow(J1,map);

I=imread('pout.tif');
figure,subplot(221);imshow(I);
f=double(I);     % 数据类型转换,MATLAB不支持图像的无符号整型的计算
g=fft2(f);        % 傅立叶变换
g=fftshift(g);     % 转换数据矩阵
[M,N]=size(g);
nn=2;           % 二阶巴特沃斯(Butterworth)高通滤波器
d0=5;
m=fix(M/2);
n=fix(N/2);
for i=1:M
       for j=1:N
           d=sqrt((i-m)^2+(j-n)^2);
           if (d==0)
              h=0;
           else
              h=1/(1+0.414*(d0/d)^(2*nn));% 计算传递函数
           end
result(i,j)=h*g(i,j);
end
end
result=ifftshift(result);
J2=ifft2(result);
J3=uint8(real(J2));
subplot(222);imshow(J3);  % 滤波后图像显示
end

原创粉丝点击