数字图像处理期中大作业邓建平文档格式.docx
《数字图像处理期中大作业邓建平文档格式.docx》由会员分享,可在线阅读,更多相关《数字图像处理期中大作业邓建平文档格式.docx(33页珍藏版)》请在冰点文库上搜索。
figure,imshow(S,[])%显示原图及频谱图
Fc=fftshift(F)%进行图像的平移
figure,imshow(abs(Fc),[])%显示之前图像,显示平移后图像
S2=log(1+abs(Fc))%进行对数变换
figure,imshow(S2,[])%显示图像
图1灰度图像2进行DFT计算后图像
图3平移后图像图4进行对数变换后图像
(3)结果分析
通过调用函数fft2进行DFT计算,然后利用fftshift将变换的原点平移到频率矩阵中心,可以明显看到图像中心点的变化(图3),然后利用对数变化增强视觉效果(图4),结果变化明显,一目了然。
3、计算图像的直方图并对其进行均衡化和规定化;
(1.1)直方图均衡化
f2=rgb2gray(imread('
));
%读入图像fan,并进行灰度处理
imshow(f2);
%显示灰度图像f2
figure,imhist(f2);
%显示灰度图像f2,显示f2的直方图
ylim('
auto'
);
%设定刻度线和取值范围
g2=histeq(f2,256);
%对图像f2进行直方图均衡化处理,生成图像g2
figure,imshow(g2);
%显示之前的所有图像,显示g2
figure,imhist(g2)%显示之前所有图像,显示g2的直方图
)%设定刻度线和取值范围
imwrite(f2,'
f2.jpg'
)%将灰度图像f2写入计算机
imwrite(g2,'
g2.jpg'
)%将均衡化后的图像g2写入计算机
(1.2)直方图均衡化运行结果:
图1灰度处理后的图像f2图2f2直方图
图3均衡化处理后效果g2图4g2直方图
(2.1)直方图的规定化
f4=rgb2gray(imread('
))%读入图像fan,并进行灰度处理
imshow(f4)%显示灰度图像f4
imwrite(f4,'
f4.jpg'
)%将灰度图像f4写入计算机
figure,imhist(f4)%得到f4直方图
)%设定刻度值和取值范围
xlim('
g4=histeq(f4,[050100150200256])%进行直方图规定化,生成图像g4
figure,imshow(g4)%保持显示之前图像,显示g4
figure,imhist(g4)%保持显示之前图像,显示g4直方图
imwrite(g4,'
g4.jpg'
)%将g4写入计算机
(2.2)直方图规定化运行结果:
图1灰度图像f4图2f4直方图
图3进行规定化之后的效果g4图4g4直方图
将直方图进行均衡化之后,不难发现直方图抽样点被拉伸,对比图1,图3的平均亮度和对比度的增强十分明显。
均衡化后的图像的直方图中的灰度级平均值高于原始值。
而直方图规定化,是使图像获得最为匹配的效果。
使图像按我们所设定的方向进行,进行图像的规定化。
4、调用噪声函数对读入的图像加噪,然后调用空间噪声滤波函数进行滤波,并对滤波效果进行分析
w=rgb2gray(imread('
))%将图像灰度化
imshow(w)%显示图像
k=imnoise(w,'
salt&
pepper'
)%加椒盐噪声
figure,imshow(k)%显示原图,显示被噪声污染的图像
k1=medfilt2(k)%使用中值滤波器滤波
figure,imshow(k1)%保留显示之前所有图像,并显示滤波后图像
imwrite(k,'
k.jpg'
)%将噪声图像写入计算机
imwrite(w,'
w.jpg'
)%见灰度图像写入计算机
图1灰度图像w图2加椒盐噪声图像
图3滤波处理后图像
首先得到灰度图像,然后加入默认噪声密度为0.05的椒盐噪声。
得到被噪声轻度污染的图像(图2),然后用中值滤波器进行滤波,滤除椒盐噪声,且滤波效果良好,接近原图,较为清晰。
5、查找Matlab图像处理工具箱(IPT)中的亮度变换函数,并使用亮度变换函数完成一张灰度图片的亮度调整。
f1=rgb2gray(imread('
))%读入图像fan并进行灰度处理
g1=imadjust(f1,[0.50.85],[01])%对灰度图像f1进行亮度调整
imshow(f1),figure,imshow(g1)%显示灰度图像f1和亮度调整后图像g1
imwrite(f1,'
f1.jpg'
)%将灰度图像f1写入计算机
imwrite(g1,'
g1.jpg'
)%将亮度处理后的图像g1写入计算机
图1原灰度图像图2进行亮度处理后的图像
调用亮度处理函数imadjust处理图像后,将0.5至0.85之间的灰度级拓展到[01]。
突出其中的灰度级。
由图2可以看出,图像的亮暗发生了明显的变化。
6、调用库函数,完成对加噪图像的滤波,并和空间滤波函数效果进行比较。
f6=imread('
fanz.jpg'
)%读入噪声图像
h=fspecial('
motion'
)%创建一个滤波器
g6=imfilter(f6,h)%空间滤波
imshow(f6),figure,imshow(g6)%显示噪声图像和滤波后图像
imwrite(g6,'
g6.jpg'
)%将滤波后图像写入计算机
G6=medfilt2(f6)%调用库函数对噪声图像进行中值滤波
figure,imshow(G6)%显示滤波后图像
imwrite(G6,'
G6.jpg'
)%将中值滤波后图像写入计算机
图1噪声图像图2空间滤波处理后图像
图3调用库函数滤波后图像
(3)运行结果
通过分别调用空间滤波函数和库函数对图像进行滤波,不难发现,空间滤波函数在处理椒盐噪声图像始终不如中值滤波函数的处理效果好。
中值滤波对于去除椒盐噪声效果明显,是因为椒盐噪声只在画面上的部分点随机出现,而中值滤波根据数据排序,将未被污染的点代替噪声点的值的概率较大,所以抑制效果好。
第二部分:
自编函数完成下述算法
1、完成图像的几何变换算法设计,包括平移、旋转、缩放、错切等;
(1)程序:
.commond:
clear
closeall
clc
I=imread('
2.bmp'
%图像平移
figure
outimage=imtranslate1(I,50,50);
subplot(1,2,1),imshow(I),title('
原图'
)
subplot(1,2,2),imshow(outimage,[]),title('
平移后图像'
%图像镜像
outimage_h=immirr(I,'
horizontal'
outimage_v=immirr(I,'
vertical'
outimage=immirr(I,'
both'
subplot(2,2,1),imshow(I),title('
subplot(2,2,2),imshow(outimage_h,[]),title('
水平镜像'
subplot(2,2,3),imshow(outimage_v,[]),title('
垂直镜像'
subplot(2,2,4),imshow(outimage,[]),title('
水平垂直镜像'
%图像旋转
outimage=imrotate0(I,30);
旋转30°
图像'
%图像缩放
outimage_NNI=imzoom(I,0.2,'
NNI'
outimage_BL=imzoom(I,0.2,'
BL'
outimage_BC=imzoom(I,0.2,'
BC'
subplot(2,2,2),imshow(outimage_NNI,[]),title('
最近邻插值法放大0.2倍'
subplot(2,2,3),imshow(outimage_BL,[]),title('
双线性插值法放大0.2倍'
subplot(2,2,4),imshow(outimage_BC,[]),title('
双三次插值法放大0.2倍'
%图像错切
outimage=imageskew(I,45,0);
subplot(1,3,1),imshow(I),title('
subplot(1,3,2),imshow(outimage,[]),title('
沿水平方向错切45°
outimage=imageskew(I,-45,1);
subplot(1,3,3),imshow(outimage,[]),title('
沿垂直方向错切-45°
2).Imageskew:
functionr=imageskew(I,theta,mode)
[h0w0]=size(I);
th=theta*pi/180;
tga=tan(th);
ctga=1/tan(th);
%图像原四个顶点坐标
srcx1=0;
srcy1=0;
srcx2=w0;
srcy2=0;
srcx3=0;
srcy3=h0;
srcx4=w0;
srcy4=h0;
%图像旋转后四个顶点坐标
ifmode==0
dstx1=srcx1;
dsty1=srcy1;
dstx2=srcx2;
dsty2=tga*srcx2+srcy2;
dstx3=srcx3;
dsty3=srcy3;
dstx4=srcx4;
dsty4=tga*srcx4+srcy4;
else
dsty2=srcy2;
dstx3=srcx3+ctga*srcy3;
dstx4=srcx4+ctga*srcy4;
dsty4=srcy4;
end
%计算旋转后图像的宽与高度
h=max(abs(dsty4-dsty1),abs(dsty2-dsty3))+0.5;
w=max(abs(dstx4-dstx1),abs(dstx2-dstx3))+0.5;
h=floor(h);
w=floor(w);
r=zeros(h,w);
f1=w0*tga;
f2=h0*ctga;
forx=1:
w
fory=1:
h
ifmode==0
x0=x;
if(theta<
0)
y0=floor(-x*tga+y+f1);
else
y0=floor(-x*tga+y);
end
y0=y;
x0=floor(-y*ctga+x+f2);
x0=floor(-y*tga+x);
end
ifx0>
0&
&
x0<
=w0&
y0>
y0<
=h0
r(y,x)=I(y0,x0);
3).Immirr:
functionoutimage=immirr(Image,direction)
%Image-intensityimage
%directionis'
'
or'
.
[M,N]=size(Image);
outimage=zeros(M,N);
ifnargin==1
direction='
;
%computetheoutputimage
fory=1:
M
forx=1:
N
switchdirection
case'
x0=N-x+1;
y0=y;
y0=M-y+1;
%Directionalityfactor
%Directionalityfactor
outimage(y,x)=Image(y0,x0);
%showimage
subplot(121),imshow(Image);
subplot(122),imshow(outimage,[]);
4).imrotate0:
functionr=imrotate0(I,theta)
theta=(theta*pi/180);
cosa=cos(theta);
sina=sin(theta);
%以图像中心为坐标原点
srcx1=-w0*0.5;
srcy1=-h0*0.5;
srcx2=w0*0.5;
srcy2=-h0*0.5;
srcx3=w0*0.5;
srcy3=h0*0.5;
srcx4=-w0*0.5;
srcy4=h0*0.5;
dstx1=cosa*srcx1-sina*srcy1;
dsty1=sina*srcx1+cosa*srcy1;
dstx2=cosa*srcx2-sina*srcy2;
dsty2=sina*srcx2+cosa*srcy2;
dstx3=cosa*srcx3-sina*srcy3;
dsty3=sina*srcx3+cosa*srcy3;
dstx4=cosa*srcx4-sina*srcy4;
dsty4=sina*srcx4+cosa*srcy4;
%计算旋转后图像的宽与高
w=max(abs(dsty3-dsty1),abs(dsty2-dsty4))+0.5;
h=max(abs(dstx3-dstx1),abs(dstx2-dstx4))+0.5;
%计算两个常数
f1=-w*0.5*cosa-h*0.5*sina+0.5*w0;
f2=w*0.5*sina-h*0.5*cosa+0.5*h0;
x0=floor(x*cosa+y*sina+f1);
y0=floor(-x*sina+y*cosa+f2);
r(x,y)=I(x0,y0);
%显示原图和旋转后的图像
subplot(121),imshow(I);
subplot(122),imshow(r,[]);
5).imrotate4:
functionr=imrotate4(I,theta)
%按照教材算法计算旋转图像尺寸——找出坐标最值
%计算旋转后图像行列坐标
k=1;
x=zeros(1,h0*w0);
y=zeros(1,h0*w0);
forx0=1:
h0
fory0=1:
w0
x(k)=round(x0*cosa-y0*sina);
y(k)=round(x0*sina+y0*cosa);
k=k+1;
%计算旋转后图像尺寸并初始化
xmin=round(min(x));
xmax=round(max(x));
ymin=round(min(y));
ymax=round(max(y));
h=xmax-xmin+1;
w=ymax-ymin+1;
%计算旋转后图像所对应的原图像行列坐标
k=1;
x0=zeros(1,h*w);
y0=zeros(1,h*w);
forx=xmin:
xmax
fory=ymin:
ymax
x0(k)=round(x*cosa+y*sina);
y0(k)=round(-x*sina+y*cosa);
%计算旋转后的图像灰度
c=h*w;
a=zeros(1,h*w);
fork=1:
c
ifx0(k)>
x0(k)<
y0(k)>
y0(k)<
a(k)=I(x0(k),y0(k));
a(k)=0;
forx1=1:
fory1=1:
r(x1,y1)=a((x1-1)*w+y1);
end
subplot(121),imshow(I,[]);
title('
旋转图像'
6).Imtranslate:
functionoutimage=imtranslate(I,deltax,deltay,zoo)
%Imagetranslate
%Input:
Itheimagetobetranslate,deltaxanddeltayarethenumberof
%pixelstobetranslatedalongxandyaxies
%Output:
resulttheimagetranslated
[mn]=size(I);
zoom=0;
%zoom图形的变焦放大和缩小
ifnargin>
3
zoom=zoo;
ifzoom
outimage=zeros(m+deltay,n+deltax);
outimage=zeros(m,n);
[m0n0]=size(outimage);
m0
n0
x0=x-deltax;
y0=y-deltay;
=1&
=n&
=m
outimage(y,x)=I(y0,x0);
7).Imzoom:
functionresult=imzoom(I,r,mode)
Itheimagetobezoom
resulttheimagezoomed
%modeis'
if(ndims(I)~=2)
error('
theinputImustbeanimageoftwodimensional!
'
if(r<
=0)
theratiormustbe>
=0'
result=zeros(round(m*r),round(n*r));
fori=ceil(2*r):
m*r-2*r
forj=ceil(2*r):
n*r-2*r
switch(mode)
result(i,j)=I(ceil(i/r),ceil(j/r));
u=i/r-floor(i/r);
v=j/r-floor(j/r);
result(i,j)=(1-u)*(1-v)*I(floor(i/r),floor(j/r))+...
(1-u)*v*I(floor(i/r),floor(j/r)+1)+...
u*(1-v)*I(floor(i/r)+1,floor(j/r))+...
u*v*I(floor(i/r)+1,floor(j/r)+1);
A=[S(u+1)S(u+0)S(u-1)S(u-2)];
B=[I(floor(i/r)-1,floor(j/r)-1)I(floor(i/r)-1,floor(j/r)+0)I(floor(i/r)-1,floor(j/r)+1)I(floor(i/r)-1,floo