数字图像处理.docx
《数字图像处理.docx》由会员分享,可在线阅读,更多相关《数字图像处理.docx(31页珍藏版)》请在冰豆网上搜索。
数字图像处理
《数字图像处理》实验报告
张川1143032002
实验二灰度直方图及直方图均衡化
1、实验目的
1、直方图的显示
2、计算并绘制图像直方图
3、直方图的均衡化
2、实验内容
灰度直方图用于显示图像的灰度值分布情况,是数字图像处理中最简单和最实用的工具。
MATLAB中提供了专门绘制直方图的函数imhist()。
1、直方图的显示
imshow('C:
\Users\zc\Desktop\guge.tif');
title('原图像')%显示原图像
A=imread('C:
\Users\zc\Desktop\guge.tif','tif');
figure;
imhist(A);
title('对应直方图')
2、计算并绘制图像直方图
A:
用bar函数显示
A=imread('C:
\Users\zc\Desktop\guge.tif','tif');
h=imhist(A);
h1=h(1:
10:
256);
horz=1:
10:
256;
bar(horz,h1)%用bar函数显示
axis([0255015000])%设置水平轴和垂直轴的最大值和最小值
set(gca,'xtick',0:
50:
255)
set(gca,'xtick',0:
2000:
15000)
B:
用stem函数显示
A=imread('C:
\Users\zc\Desktop\guge.tif','tif');
h=imhist(A);
h1=h(1:
10:
256);
horz=1:
10:
256;
stem(horz,h1,'fill')%用stem函数显示
axis([0255015000])%设置水平轴和垂直轴的最大值和最小值
set(gca,'xtick',0:
50:
255)
set(gca,'xtick',0:
2000:
15000)
C:
用plot函数显示
A=imread('C:
\Users\zc\Desktop\guge.tif','tif');
h=imhist(A);
plot(h)
axis([0255015000])%设置水平轴和垂直轴的最大值和最小值
3、直方图均衡化
imshow('C:
\Users\zc\Desktop\guge.tif');
title('原图像')%显示原图像
I=imread('C:
\Users\zc\Desktop\guge.tif','tif');
figure;
imhist(I),title('对应直方图')
%从得到的直方图可以看出,图像对比度很低,灰度级集中在70-160范围内,如果只取这个
%范围内的灰度,并扩展到【0,255】,则会明显增强图像对比度
J=imadjust(I,[70/255160/255],[]);
figure;imshow(J),title('经灰度级调整后的图')
figure;imhist(J),title('灰度级调整后的直方图')
%MATLAB还提供了histeq函数(自动直方图均衡化)
K=histeq(I);
figure;imshow(K),title('直接用histeq函数均衡化后的图像')
实验三图像的傅里叶变换
1、实验目的
1、掌握二维DFT变换及其物理意义
2、掌握二维DFT变换的MATLAB程序
3、空域滤波与频域滤波
2、实验原理
1应用傅立叶变换进行图像处理
傅里叶变换是线性系统分析的一个有力工具,它能够定量地分析诸如数字化系统、采样点、电子放大器、卷积滤波器、噪音和显示点等的作用。
通过实验培养这项技能,将有助于解决大多数图像处理问题。
对任何想在工作中有效应用数字图像处理技术的人来说,把时间用在学习和掌握博里叶变换上是很有必要的。
2傅立叶(Fourier)变换的定义
对于二维信号,二维Fourier变换定义为:
逆变换:
二维离散傅立叶变换为:
逆变换:
图像的傅立叶变换与一维信号的傅立叶变换变换一样,有快速算法,具体参见参考书目,有关傅立叶变换的快速算法的程序不难找到。
实际上,现在有实现傅立叶变换的芯片,可以实时实现傅立叶变换。
3利用MATLAB实现数字图像的傅立叶变换
A:
I=imread('C:
\Users\zc\Desktop\guge.tif');%读入原图像文件
imshow(I);%显示原图像
fftI=fft2(I);%二维离散傅立叶变换
sfftI=fftshift(fftI);%直流分量移到频谱中心
RR=real(sfftI);%取傅立叶变换的实部
II=imag(sfftI);%取傅立叶变换的虚部
A=sqrt(RR.^2+II.^2);%计算频谱幅值
A=(A-min(min(A)))/(max(max(A))-min(min(A)))*225;%归一化
figure;%设定窗口
imshow(A);%显示原图像的频谱
B:
傅立叶变换在图像处理,特别是在图像增强、复原和压缩中,扮演着非常重要的作用。
实际中一般采用一种叫做快速傅立叶变换(FFT)的方法,MATLAB中的fft2指令用于得到二维FFT的结果,fft2指令用于得到二维FFT逆变换的结果。
近似冲击函数的二维快速傅立叶变换(FFT)
x=1:
99;y=1:
99;
[X,Y]=meshgrid(x,y);
A=zeros(99,99);
A(49:
51,49:
51)=1;
B=fft2(A);
subplot(1,2,1),imshow(A),xlabel('空域图像');
subplot(1,2,2),imshow(B),xlabel('时域图像');
figure
subplot(1,1,1),mesh(X,Y,abs(B)),xlabel('时域'),gridon;
4、空域滤波与频域滤波
%用于频域滤波的m函数
functiong=dftfilt(f,H)
F=fft2(f,size(H,1),size(H,2));
g=real(ifft2(H.*F));
g=g(1:
size(f,1),1:
size(f,2));
functionPQ=paddedsize(AB,CD,PARAM)
ifnargin==1
PQ=2*AB;
elseifnargin==2&~ischar(CD)%如果CD不为字符串
PQ=AB+CD-1;
PQ=2*ceil(PQ/2);
elseifnargin==2%如果CD处为字符串
m=max(AB);
P=2^nextpow2(2*m);%取2的整数次幂
PQ=[P,P];
elseifnargin==3
m=max([ABCD]);
P=2^nextpow2(2*m);
PQ=[P,P];
else
error('wrongnumberofinputs.')
end
functiong=gscale(f,varargin)
iflength(varargin)==0
method='full8';
elsemethod=varargin{1};
end
ifstrcmp(class(f),'double')&(max(f(:
))>1|min(f(:
))<0)
f=mat2gray(f);
end
switchmethod
case'full8'
g=im2uint8(mat2gray(double(f)));
case'full16'
g=im2uint16(mat2gray(double(f)));
case'minmax'
low=varargin{2};high=varargin{3};
iflow>1|low<0|high>1|high<0
error('Parameterslowandhighmustbeintherange[0,1]')
end
ifstrcmp(class(f),'double')
low_in=min(f(:
));
high_in=max(f(:
));
elseifstrcmp(class(f),'uint8')
low_in=double(min(f(:
)))./255;
high_in=double(max(f(:
)))./255;
elseifstrcmp(class(f),'uint16')
low_in=double(min(f(:
)))./65535;
high_in=double(max(f(:
)))./65535;
end
g=imadjust(f,[low_inhigh_in],[lowhigh]);
otherwise
error('Unknownmethod')
End
functiong=gscale(f,varargin)
%GSCALEScalestheintensityoftheinputimage.
%G=GSCALE(F,'full8')scalestheintensitiesofFtothefull
%8-bitintensityrange[0,255].Thisisthedefaultifthereis
%onlyoneinputargument.
%
%G=GSCALE(F,'full16')scalestheintensitiesofFtothefull
%16-bitintensityrange[0,65535].
%
%G=GSCALE(F,'minmax',LOW,HIGH)scalestheintensitiesofFto
%therange[LOW,HIGH].Thesevaluesmustbeprovided,andthey
%mustbeintherange[0,1],independentlyoftheclassofthe
%input.GSCALEperformsanynecessaryscaling.Iftheinputisof
%classdouble,anditsvaluesarenotintherange[0,1],then
%GSCALEscalesittothisrangebeforeprocessing.
%
%Theclassoftheoutputisthesameastheclassoftheinput.
%Copyright2002-2004R.C.Gonzalez,R.E.Woods,&S.L.Eddins
%DigitalImageProcessingUsingMATLAB,Prentice-Hall,2004
%$Revision:
1.5$$Date:
2003/11/2114:
36:
09$
iflength(varargin)==0%Ifonlyoneargumentitmustbef.
method='full8';
else
method=varargin{1};
end
ifstrcmp(class(f),'double')&(max(f(:
))>1|min(f(:
))<0)
f=mat2gray(f);
end
%Performthespecifiedscaling.
switchmethod
case'full8'
g=im2uint8(mat2gray(double(f)));
case'full16'
g=im2uint16(mat2gray(double(f)));
case'minmax'
low=varargin{2};high=varargin{3};
iflow>1|low<0|high>1|high<0
error('Parameterslowandhighmustbeintherange[0,1].')
end
ifstrcmp(class(f),'double')
low_in=min(f(:
));
high_in=max(f(:
));
elseifstrcmp(class(f),'uint8')
low_in=double(min(f(:
)))./255;
high_in=double(max(f(:
)))./255;
elseifstrcmp(class(f),'uint16')
low_in=double(min(f(:
)))./65535;
high_in=double(max(f(:
)))./65535;
end
%imadjustautomaticallymatchestheclassoftheinput.
g=imadjust(f,[low_inhigh_in],[lowhigh]);
otherwise
error('Unknownmethod.')
end
%图像f的傅里叶频谱
f=imread('C:
\Users\zc\Desktop\guge.tif');
F=fft2(f);
S=fftshift(log(1+abs(F)));
S=gscale(S);
figure,imshow(S)
%使用函数fspecial生成空间滤波器
h=fspecial('sobel');
freqz2(h)%查看相应频域滤波器图形
PQ=paddedsize(size(f));
H=freqz2(h,PQ
(1),PQ
(2));
H1=ifftshift(H);
figure,imshow(abs(H),[])
figure,imshow(abs(H1),[])
gs=imfilter(double(f),h);%生成滤波后的图像,并默认采用了0进行边界填充
gf=dftfilt(f,H1);
imshow(gs,[])
figure,imshow(gf,[])
figure,imshow(abs(gs),[])
figure,imshow(abs(gf),[])
%创建一副阈值2值图像
figure,imshow(abs(gs)>0.2*abs(max(gs(:
))))
figure,imshow(abs(gs)>0.2*abs(max(gf(:
))))
d=abs(gs-gf);
max(d(:
))
min(d(:
))
实验四图像的滤波处理与图像增强
1、实验目的
1、了解MATLAB工具箱中的滤波器
2、掌握空间滤波
3、学会对图像的空间变换
2、实验内容
A:
用滤波器祛除图像噪声
在数字图像处理中,常常会遇到图像中混杂有许多的噪声。
因此,在进行图像处理中。
有时要先进行祛除噪声的工作,最常用的祛除噪声的方法是用滤波器进行滤波处理。
MATLAB的图像处理工具箱里也设计了许多的滤波器。
如均值滤波器、中值滤波器、维纳滤波器等。
分别用均值滤波,中值滤波,以及维纳滤波祛除加入高斯噪声的图像
I=imread('C:
\Users\zc\Desktop\guge.tif');
J=imnoise(I,'gaussian',0,0.002);
%进行均值滤波
h=fspecial('average',3);
I2=uint8(round(filter2(h,I)));
%进行中值滤波
I3=medfilt2(J,[3,3]);
%进行维纳滤波
I4=wiener2(J,[3,3]);%进行一次维纳滤波
I5=wiener2(I4,[3,3]);%进行二次维纳滤波
subplot(2,3,1),imshow(I),title('原图像')
subplot(2,3,2),imshow(J),title('加噪声图像')
subplot(2,3,3),imshow(I2),title('均值滤波后图像')
subplot(2,3,4),imshow(I3),title('中值滤波后图像')
subplot(2,3,5),imshow(I4),title('一次维纳滤波后图像')
subplot(2,3,6),imshow(I5),title('二次维纳滤波后图像')
B、空间噪声滤波器
%用函数imnoise2生成具有表5.1中的CDF的随机数
functionR=imnoise2(type,M,N,a,b)
ifnargin==1
a=0;b=1;
M=1;N=1;
elseifnargin==3
a=0;b=1;
end
switchlower(type)
case'uniform'
R=a+(b-a)*rand(M,N);
case'gaussian'
R=a+b*randn(M,N);
case'salt&pepper'
ifnargin<=3
a=0.05;b=0.05;
end
if(a+b)>1;
error('ThesumPa+Pbmustnotexceed1.')
end
R(1:
M,1:
N)=0.5;
X=rand(M,N);
c=find(X<=a);
R(c)=0;
u=a+b;
c=find(X>a&X<=u);
R(c)=1;
case'rayleigh'
R=a+(-b*log(1-rand(M,N))).^0.5;
case'exponential'
ifnargin<=3;
a=1;
end
ifa<=0
error('Parameteramustbepositiveforexponentialtype.')
end
k=-1/a;
R=k*log(1-rand(M,N));
case'erlang'
ifnargin<=3
a=2;b=5;
end
if(b~=round(b)|b<=0)
error('ParameterbmustbeapositiveintegerforErlang')
end
k=-1/a;
R=zeros(M,N);
forj=1:
b
R=R+k*log(1-rand(M,N));
end
otherwise
error('unknowndistributiontype.')
end
functionimage=changeclass(class,varargin)
switchclass
case'uint8'
image=im2uint8(varargin{:
});
case'uint16'
image=im2uint16(varargin{:
});
case'double'
image=im2double(varargin{:
});
otherwise
error('UnsupportedIPTdataclass.');
end
functionf=spfilt(g,type,m,n,parameter)
ifnargin==2
m=3;n=3;Q=1.5;d=2;
elseifnargin==5
Q=parameter;d=parameter;
elseifnargin==4
Q=1.5;d=2;
else
error('wrongnumberofinputs');
end
switchtype
case'amean'
w=fspecial('average',[m,n]);
f=imfilter(g,w,'replicate');
case'gmean'
f=gmean(g,m,n);
case'hmean'
f=harmean(g,m,n);
case'chmean'
%f=charmean(g,m,n,Q);
f=charmean(g,m,n,Q);
case'median'
f=medfilt2(g,[mn],'symmetric');
case'max'
f=ordfilt2(g,m*n,ones(m,n),'symmetric');
case'min'
f=ordfilt2(g,1,ones(m,n),'symmetric');
case'midpoint'
f1=ordfilt2(g,1,ones(m,n),'symmetric');
f2=ordfilt2(g,m*n,ones(m,n),'symmetric');
f=imlincomb(0.5,f1,0.5,f2);
case'atrimmed'
if(d<0)|(d/2~=round(d/2))
error('dmustbeanonnegative,eveninteger.')
end
f=alphatrim(g,m,n,d);
otherwise
error('Unknownfiltertype.')
end
functionf=gmean(g,m,n)
inclass=class(g);
g=im2double(g);
warningoff;
f=exp(imfilter(log(g),ones(m,n),'replicate')).^(1/m/n);
warningon;
f=changeclass(inclass,f);
functionf=harmean(g,m,n)
inclass=class(g);
g=im2double(g);
f=m*n./imfilter(1./(g+eps),ones(m,n),'replicate');
f=changeclass(inclass,f);
functionf=charmean(g,m,n,q)
inclass=class(g);
g=im2double(g);
f=imfilter(g.^(q+1),ones(m,n),'replicate');
f=f./(imfilter(g.^q,ones(m,n),'replicate')+eps);
f=changeclass(inclass,f);
functionf=alphatrim