高通滤波,低通滤波,butterworth

来源:互联网 发布:金字塔决策交易软件 编辑:程序博客网 时间:2024/05/16 00:30


%%%%理想低通和高通滤波
clc;
clear;
data4=imread('caise.jpg');
subplot(3,2,1);  
imshow(data4);
title('原图');
i=fft2(data4);
subplot(3,2,2);  
i=fftshift(i);                       
z=log(abs(i));                         
x=0:1:255;
y=0:1:255;
[x,y]=meshgrid(x,y);
mesh(z); %以三维坐标显示该图像频谱图
title('原图频谱');
[n,m]=size(i);                          %对该图进行低通滤波
for k=1:1:n
    for l=1:1:m
        if (k^2+l^2)>=190^2             %选取D=190
            result(k,l)=0;
        else result(k,l)=i(k,l);
        end
    end
end
subplot(3,2,4);
z=log(abs(result));                  %三维方式显示低通滤波后的频谱图
x=0:1:255;
y=0:1:255;
[x,y]=meshgrid(x,y);
mesh(z);
title('理想低通滤波后的频谱');
subplot(3,2,3);                     %新建图像显示窗口
result=fftshift(result);            %滤波后的数据去中心化
b=ifft2(result);                    %三维方式显示低通滤波前的频谱图
imshow(uint8(abs(b)));
title('理想低通滤波后的图像');
subplot(3,2,6);                     %新建图像显示窗口  
% [n,m]=size(c);                    %对原图进行高通滤波 
for k=1:1:n
    for l=1:1:m
        if (k^2+l^2)<=190^2          %选取D=190
            result(k,l)=0;
        else result(k,l)=i(k,l);
        end
    end
end
z=log(abs(result));
x=0:1:255;                      %三维方式显示高通滤波前的频谱图                           
y=0:1:255;
[x,y]=meshgrid(x,y);
mesh(z);
title('理想高通滤波后的频谱');
subplot(3,2,5);               
result=fftshift(result);       %滤波后的数据去中心化
d=ifft2(result);               %三维方式显示高通滤波后的频谱图
imshow(uint8(abs(d)));
title('理想高通滤波后的图像');
%频域增强(巴特沃斯原型)
%利用巴特沃斯(Butterworth)低通滤波器对受噪声干扰的图像进行平滑处理
clc;
clear;
J1=imread('lena.gif');
subplot(3,2,1);
imshow(J1);
title('原图');
f=double(J1);     
g=fft2(f);        % 傅立叶变换
g=fftshift(g);     % 转换数据矩阵
subplot(3,2,2);
x=0:1:255;
y=0:1:255;
[x,y]=meshgrid(x,y);
z=log(abs(g));                          %取幅度
mesh(z); %以三维坐标显示该图像频谱图
title('原图频谱');
[M,N]=size(g);
nn=2;           % 二阶巴特沃斯(Butterworth)低通滤波器
d0=20;
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
subplot(3,2,4);
x=0:1:255;
y=0:1:255;
[x,y]=meshgrid(x,y);
z=log(abs(result));                          %取幅度
mesh(z); %以三维坐标显示该图像频谱图
title('低通滤波后的频谱');
result=ifftshift(result);
J2=ifft2(result);
J3=uint8(abs(J2));
subplot(3,2,3);
imshow(J3);
title('低通滤波后的图像');


%利用巴特沃斯(Butterworth)高通滤波器对受噪声干扰的图像进行平滑处理
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
subplot(3,2,6);
x=0:1:255;
y=0:1:255;
[x,y]=meshgrid(x,y);
z=log(abs(result));        %取幅度
mesh(z); %以三维坐标显示该图像频谱图
title('高通滤波后的频谱');
result=ifftshift(result);
J2=ifft2(result);
J3=uint8(abs(J2));
subplot(3,2,5);
imshow(J3);
title('高通滤波后的图像');
0 0