# 数字图像处理matlab上机实验 （五）

2020/7/6 22:07:27 人评论 次浏览 分类：学习教程

----2020-7-6

## 实验 5 图像频域增强

B =
1 2 0 0 0
3 4 0 0 0
0 0 0 0 0
0 0 0 0 0

2 、低通滤波器生成函数

function [U,V]=dftuv(M,N)
%DFTUV Computes meshgrid frequency matrices.
% [U,V]=DFTUV(M,N] computes meshgrid frequency matrices U and V. U
and V are useful for computing frequency-domain filter functions that
can be used with DFTFILT. U and V are both M-by-N.

% Set up range of variables.
u=0:(M-1);
v=0:(N-1);
% Compute the indices for use in meshgrid.
idx=find(u>M/2);
u(idx)=u(idx)-M;
idy=find(v>N/2);
v(idy)=v(idy)-N;
%Compute the meshgrid arrays.
[V,U]=meshgrid(v,u);

function [H,D]=lpfilter(type,M,N,D0,n)
% LPFILTER computers frequency domain lowpass filters.
% H=lpfilter(TYPE,M,N,D0,n) creates the transfer function of a lowpass
filter, H, of the specified TYPE and size(M-by-N). To view the filter as
an image or mesh plot, it should be centered using H=fftshift(H).
% valid values for TYPE, D0, and n are:
% ‘ideal’ Ideal lowpass filter with cutoff frequency D0. n need not
be supplied. D0 must be positive.
% ‘btw’ Butterworth lowpass filter of ordern, and cutoff D0. The
default value for n is 1. D0 must be positive.
% ‘gaussian’ Gaussian lowpass filter with cutoff (standard deviation)D0.
n need not be supplied. D0 must be positive.
%Use function dftuv to set up the meshgrid arrays needed for computing
the required distances.
[U,V]=dftuv(M,N); %
D=sqrt(U.2+V.2); % Compute the distances D(U,V)
% Begin filter computations.
switch type
case ‘ideal’
H=double(D<=D0);
case ‘btw’
if nargin==4
n=1;
end
H=1./(1+(D./D0).^(2*n));
case ‘gaussian’
H=exp(-(D.2)./(2*(D02)));
otherwise
error(‘Unknown filter type’)
end

3、 频域滤波
F=fft2(f,size(H,1),size(H,2)); % 对延拓的 f 计算 FFT。注意，这里隐含着对 f 的延拓。
G=real(ifft2(H.*F)); % 滤波
Gf=G(1:size(f,1),1:size(f,2)); %裁剪后的图像

（一）图像频域增强的步骤

（二）频域低通滤波

``````I=zeros(64,64);
I(32-20:32+20,32-8:32+8)=255;
subplot(1,2,1)
imshow(I);
s=fftshift(fft2(I));
[M,N]=size(s);
n1=floor(M/2);
n2=floor(N/2);
d0=5;
for i=1:M
for j=1:N
d=sqrt((i-n1)^2+(j-n2)^2);
h=1*exp(-1/2*(d^2/d0^2));
s(i,j)=h*s(i,j);
end
end
s=ifftshift(s);
s=uint8(real(ifft2(s)));
subplot(1,2,2);
imshow(s)

``````

``````I=zeros(64,64);
I(32-20:32+20,32-8:32+8)=255;
subplot(1,2,1)
imshow(I);
s=fftshift(fft2(I));
[M,N]=size(s);
n1=floor(M/2);
n2=floor(N/2);
d0=50;
for i=1:M
for j=1:N
d=sqrt((i-n1)^2+(j-n2)^2);
h=1*exp(-1/2*(d^2/d0^2));
s(i,j)=h*s(i,j);
end
end
s=ifftshift(s);
s=uint8(real(ifft2(s)));
subplot(1,2,2);
imshow(s);
``````

``````I=zeros(64,64);
I(32-20:32+20,32-8:32+8)=255;
subplot(1,2,1)
imshow(I);
s=fftshift(fft2(I));
[M,N]=size(s);
n1=floor(M/2);
n2=floor(N/2);
d0=5;
for i=1:M
for j=1:N
d=sqrt((i-n1)^2+(j-n2)^2);

h=1/(1+0.414*(d/d0)^(2*n1));

s(i,j)=h*s(i,j);
end
end
s=ifftshift(s);
s=uint8(real(ifft2(s)));
subplot(1,2,2);
imshow(s);
``````

``````clear all;
subplot(1,3,1),imshow(A,[]);
title('原图')
B=imnoise(A,'gauss',0.02);
subplot(1,3,2),imshow(B,[]);
title('添加高斯噪声后的图像')
f=double(B);
g=fft2(f);
g=fftshift(g);
[N1,N2]=size(g);
n=2;
d0=50;
n1=fix(N1/2);
n2=fix(N2/2);
for i=1:N1
for j=1:N2
d=sqrt((i-n1)^2+(j-n2)^2);
c=double(d<=d0);
result(i,j)=c*g(i,j);

end
end
resul=ifftshift(result);
X2=ifft2(result);
X3=uint8(real(X2));
subplot(1,3,3),imshow(X3);
title('D0=50,低通滤波器')
``````

Butterworth低通滤波器程序：

``````clc,clear,close all;
II=imnoise(I,'gauss',0.02);
I1=medfilt2(II,[3,3]);
subplot(1,3,1)
imshow(I,[]);%把图像显示出来
subplot(1,3,2)
imshow(I1,[]);

f=double(I1); %图像存储类型转换
g=fft2(f); %傅立叶变换
g=fftshift(g); %转换数据矩阵

[N1,N2]=size(g); %测量图像尺寸参数
n=2;
d0=50;
n1=fix(N1/2);
n2=fix(N2/2);

for i=1:N1
for j=1:N2
d=sqrt((i-n1)^2+(j-n2)^2);
h=1/(1+0.414*(d/d0)^(2*n));%计算Butterworth低通转换函数
result(i,j)=h*g(i,j);
end
end
result=ifftshift(result);
X2=ifft2(result);
X3=uint8(real(X2));

subplot(1,3,3)
imshow(X3)%显示频域增强后的图像
``````

（三） 频域高通滤波

``````I=zeros(64,64);
I(32-20:32+20,32-8:32+8)=255;
subplot(1,2,1);
imshow(I);
s=fftshift(fft2(I));
[M,N]=size(s);
n1=floor(M/2);
n2=floor(N/2);
d0=5;
for i=1:M
for j=1:N
d=sqrt((i-n1)^2+(j-n2)^2);
h=1-(1*exp(-1/2*(d^2/d0^2)));
s(i,j)=h*s(i,j);
end
end
s=ifftshift(s);
s=uint8(real(ifft2(s)));
subplot(1,2,2);
imshow(s);
``````

``````I=zeros(64,64);
I(32-20:32+20,32-8:32+8)=255;
subplot(1,2,1);
imshow(I);
s=fftshift(fft2(I));
[M,N]=size(s);
n1=floor(M/2);
n2=floor(N/2);
d0=5;
for i=1:M
for j=1:N
d=sqrt((i-n1)^2+(j-n2)^2);
h=1-(1/(1+0.414*(d/d0)^(2*n)));
s(i,j)=h*s(i,j);
end
end
s=ifftshift(s);
s=uint8(real(ifft2(s)));
subplot(1,2,2);
imshow(s)
``````

``````I1=imread('F:\Images\lena.bmp');
I=imnoise(I1, 'gauss', 0.02);
subplot(1,2,1);
imshow(I);
s=fftshift(fft2(I));
[M,N]=size(s);
n1=floor(M/2);
n2=floor(N/2);
d0=5;
for i=1:M
for j=1:N
d=sqrt((i-n1)^2+(j-n2)^2);
h=1-(1*exp(-1/2*(d^2/d0^2)));
s(i,j)=h*s(i,j);
end
end
s=ifftshift(s);
s=uint8(real(ifft2(s)));
subplot(1,2,2);
imshow(s);
``````

``````I1=imread('F:\Images\lena.bmp');
I=imnoise(I1, 'gauss', 0.02);
subplot(1,2,1);
imshow(I);
s=fftshift(fft2(I));
[M,N]=size(s);
n1=floor(M/2);
n2=floor(N/2);
d0=5;
for i=1:M
for j=1:N
d=sqrt((i-n1)^2+(j-n2)^2);
h=1-(1/(1+0.414*(d/d0)^(2*n)));
s(i,j)=h*s(i,j);
end
end
s=ifftshift(s);
s=uint8(real(ifft2(s)));
subplot(1,2,2);
imshow(s);
``````

**四、实验方法与步骤
1、顺序完成上述实验内容
2、按照实验内容要求，分析编程，将程序和实验结果整理成word文档，分析结果，编写实验报告。

1、本实验由学生单人独立完成。
2、每个实验均按统一格式编写实验报告。

**

暂无相关的资讯...

-->