图像处理实验指导书.docx
《图像处理实验指导书.docx》由会员分享,可在线阅读,更多相关《图像处理实验指导书.docx(32页珍藏版)》请在冰点文库上搜索。
图像处理实验指导书
《图像处理》
实验指导书
10学时
指导教师:
张葵
实验报告要求
1、做好实验相关预习工作,了解实验的目的与内容。
每次实验报告中,首先写清实验目的。
然后,写出实验内容中的题目、程序清单(加上必要注释)、程序运行结果(可将图像处理效果图附在实验报告中;对综合应用性较高的题目,要求在给出代码前,绘制算法设计的流程图)。
最后,给出100-50字的实验体会或总结。
要求必须写在实验报告中,图片效果可以剪贴到报告册上。
每次实验报告首页页眉写上实验时间、指导老师姓名。
2、实验报告要求手写在实验报告册中,请勿打印。
3、实验报告中,有些内容是指导性的内容,不需要写在报告中。
因此有些编号需要调整,从1开始。
4、实验报告成绩按照报告书写的质量、注释的质量、程序的质量给分。
5、实验中需要用到的图片文件已经放到机房的E盘pics文件夹下。
实验一 Matlab图像显示与图像运算
(2学时)
【实验目的】
1、熟悉Matlab的编程环境及其基本用法;
2、掌握MATLAB语言中图像数据与信息的读取方法;了解m文件的编写与调试方法;
3、熟悉图像点运算和代数运算的实现方法;
4、掌握Matlab图像几何变换的基本函数;
5、了解图像几何运算的简单应用;
【实验内容】
1、Introduction
InthefollowingwegiveashortoverviewonaverylimitedsetofbasicimageprocessingfunctionsprovidedbyMATLAB.Thefunctiondescriptionsonlycoverthebasicusage,moredetailedinformationcanbefoundinthemanualpagesbytypingdocfunctionNameattheMATLABcommandline.MATLABprovideswithit’simageprocessingtoolboxmanypowerfulandveryefficientimageprocessingfunctions(seefunctionlistoftheimageprocessingtoolbox).Withthisitisverysimpletoimplementcompleximageprocessingapplications,especiallyforfastprototyping.Onthebackside,alotofunderstandinghowimageprocessingworksishiddenwithinblackboxesandtempstomakethingsmorecomplicatethantheyreallyare.
2、Imagerepresentation
InMATLABabinaryandgray-scaleimageisrepresentedbyone2-dimensionalarray,whereasacolorimagearerepresentedbya3-dimensionalarray(one2-dimensionalarrayforeachofthecolorplanesorcolorchannelsred,greenandblue):
Theoriginoftheimageisintheupperleftandthesizeoftheimageisdefinedbytheparameterwidth(numberofcolumnsofthearray)andheight(numberofrowsofthearray).Notethatthex-andy-coordinatesarechosensuchthatthez-axispointstothefront.
Asinglepointwithintheimageiscalledpixel.Agray-scaleorbinarypixelconsistsofonedatavalue,acolorpixelconsistsof3datavalues(eachforoneofthecolorchannels).Themostcommondatatypesoftheindividualpixelsare:
uint8unsignedinteger:
datarange0..255
doubledoubleprecisionfloat:
datarange0.0...1.0
Binaryimageshavepixelvaluesof0’sand1’sresp.0.0and1.0.Inthecaseofuint8images,thelogicalflagmustbeturnedon,toberecognizedasbinaryimage(fordetailsseebelow).
BecarefulwithdatatypesinMATLAB.Manyofthepredefinedfunctions,e.g.imadd(img1,img2)whichaddstwoimages,justtruncatesdatavaluesto255onuint8-arrays...makesureifthatiswhatyouwant.
Hints:
Toavoidproblemswithdatatypes,especiallywhenworkingwithpredefinedimageprocessingfunctions,itisadvisorytoworkwithtypedoubleandmakesurethatdataisscaledtotherange0...1.0.
3、BasicMATLABfunctions
3.1MATLABmanual
docfunctionnamedisplaysthemanualfortheMATLABfunctionfunctionname
docimagesthemanualfortheimageprocessingtoolbox
docimaqthemanualfortheimageacquisitiontoolbox
3.2Imageinformation
imfinfo(’foo.ext’)displaysinformationonimageformatetc.ofthefilefoo.ext
imformatsdisplaysanoverviewofallMATLABimageformats
whosimgdisplaysinformationaboutthearrayimg:
size,datatype,etc.
3.3Reading,writinganddisplayingimages
myImg=imread(’foo.ext’)readsfilefoo.extintoarraymyImg,imageformatisdeterminedbythefileextension(jpg,tiff,tif,gif,bmp,png,...)
imwrite(anImg,’foo.ext’)writestheimageanImgtothefilefoo.ext,wheretheimageformatischosenaccordingtotheextensionext.Validextensionsare:
tif,tiff,jpg,jpeg,gif,png
imshow(myImg)displaystheimagemyImgasgray-scale,binaryorcolorimage
dependingonthedatatypeofmyImg
imshow(myImg,[])theimagemyImgasgray-scale,binaryorcolorimagedependingonthedatatypeofmyImgandscalestheimageproperly
figure(n)opensanewwindowwithnumbern,thenextcalltoimshow()displaystheimagewithinthiswindow
3.4Basicimageprocessingfunctions
islogical(binImg)checkswhetherarraybinImghasthelogicalflagsetornot(returnsvalue1or0)
img=uint8(zeros(512,1024))createsablackimagewithwidth1024andheight512oftypeuint8
img=uint8(255*ones(512,1024))createsawhiteimagewithwidth1024andheight512oftypeuint8
img=double(zeros(512,1024))createsablackimagewithwidth1024andheight512oftypedouble
img=double(ones(512,1024))createsawhiteimagewithwidth1024andheight512oftypedouble
[heightwidthd]=size(myImg)retrievesheightandwidthandstoresthevaluesinvariablesheightandwidth,distsettothearraydimension.
red=myImg(:
:
1)storestheredcomponentofmyImg(rgb-image)inarrayred
green=myImg(:
:
2)storestheredcomponentofmyImginarraygreen
blue=myImg(:
:
3)storestheredcomponentofmyImginarrayblue
mx=max(myImg(:
)))computesthemaximumvalueofan2-darray
mi=min(myImg(:
)))computestheminimumvalueofan2-darray
img=double(myImg)/255convertsanuint8arraytoadoublearray(noscaling)
img=double(myImg)/double(mx)convertsuint8todoubleandscalesmaximumto1.0
img=uint8(anImg*255)convertsadoublearraytoanunit8arrayandrescalesthearraytotheproperdatarange
bw=logical(binImg)setsthelogicalflagontheunit8arraybinImg(datavalues0and1),arraybwistheninterpretedasablackandwhiteimageandlogicaloperationscanbeapplied
gray=(+bw)*255turnsthelogicalflagoffandrescalesthearraybwtobedisplayedasunit8array
3.5Examples
3.5.1Scalingimages
Thefollowingtwostatementsscaleadoubletypeimagetotherange0.0...1.0.Thisisimportant,whentheimagecontainsnegativepixelvalues,ase.g.afterapplyingedgedetectionalgorithms.
f=f-min(f(:
));
f=f/max(f(:
));
3.5.2Colorplanes
Thegreenandredcolorplaneofimagergbimage.jpgareswapped:
f=imread(’rgbimage.jpg’);
red=f(:
:
1);
g(:
:
1)=f(:
:
2);
g(:
:
2)=red;
g(:
:
3)=f(:
:
3);
imshow(g);
3.5.3Individualpixelprocessing
TheintensityoftheredcolorchannelofrgbImage.jpgisdividedby2.
f=imread(’rgbImage.jpg’);
[MNd]=size(f);
g=unint8(zeros(M,N,3));
forx=1:
M
fory=1:
N
g(x,y,1)=f(x,y,1)/2;
g(x,y,2)=f(x,y,2);
g(x,y,3)=f(x,y,3);
end;
end;
imshow(g);
UsingtheMATLABarraynotation,thismaybewrittenas:
f=imread(’rgbImage.jpg’);
g=f;
g(:
:
1)=g(:
:
1)/2;
imshow(g);
TheimagecolorimagergbImage.jpgisconvertedtoagrayscaleimage,bysimplycomputingthemeanofthethreecolorchannels(onepossiblemethod)andthenstoredinfilegrayImage.jpg.Notethatthequalityoftheresultingimageissetto100(nodataloss):
f=imread(’rgbImage.jpg’);
[MNd]=size(f);
g=unint8(zeros(M,N));
forx=1:
M
fory=1:
N
g(x,y)=(f(x,y,1)+f(x,y,2)+f(x,y,3))/3;
%Thelineabovedoesn’twork.
%Overflowoccurs,whileprocessinguint8,because
%thevaluerangeintheintermediateresultsarelimitedto255
g(x,y)=(f(x,y,1)/3+f(x,y,2)/3+f(x,y,3)/3);
end;
end;
imshow(g);
imwrite(g,’grayImage.jpg’,’Quality’,100);
UsingtheMATLABarraynotation,thismaybewrittenas:
f=imread(’rgbImage.jpg’);
g=uint8(mean(f,3));
imshow(g);
imwrite(g,’grayImage.jpg’,’Quality’,100);
TheimagegrayImage.jpgisslightlyblurredbycomputingthemeanofa3x3pixelenvironmentandbysettingtheresultingcenterpixeltothismeanvalue:
f=imread(’grayImage.jpg’);
[MNd]=size(f);
g=unint8(zeros(M,N));
forx=2:
M-1
fory=2:
N-1
sum=f(x-1,j-1)+f(x-1,j)+f(x-1,y+1);
sum=sum+f(x,y-1)+f(x,y)+f(x,y+1);
sum=sum+f(x+1,y-1)+f(x+1,y)f(x+1,y+1);
rImg(x,y)=unint8(sum/9);
end;
end;
imshow(g);
Thesamefunctionalitycouldbeachieved,byusingMATLAB’spowerfulimageprocessingfunctionsandinadditionavoidstheboundaryproblem:
f=imread(’grayImage.jpg’);
h=fspecial(’average’,3);
g=imfilter(f,h,’replicate’);
imshow(g);
蓝色字体部分不用写在实验报告上,此部分实验内容辅助大家了解相关matlab图像处理函数。
有些函数课件中没有提及,请使用matlab帮助文档获取函数的详细信息。
4、上机编程题思考题
4.1读入图像‘rice.tif’,通过图像点运算改变对比度。
rice=imread('rice.tif');
subplot(131),imshow(rice)
I=double(rice);%转换为双精度类型
J=I*0.43+60;
rice2=uint8(J);%转换为uint8
subplot(132),imshow(rice2)
J=I*1.5-60;
rice3=uint8(J);%转换为uint8
subplot(133),imshow(rice3)
4.2读入图像‘sf.tif’,改变图像大小,分别将原图像放大1.5倍和缩小0.5倍。
I=imread('sf.tif');
J=imresize(I,1.25);
K=imresize(I,0.8);
imshow(I)
figure,imshow(J)
figure,imshow(K)
Y=imresize(I,[100,150]);
figure,imshow(Y)
4.3将上述图像顺时针和逆时针旋转任意角度,观察显示效果。
I=imread('sf.tif');
J=imrotate(I,30,'bilinear');
J1=imrotate(I,30,'bilinear','crop');
imshow(I)
figure,imshow(J)
figure,imshow(J1)
J2=imrotate(I,-15,'bilinear');
figure,imshow(J2)
4.4通过交互式操作,从一幅图像中剪切一个矩形区域。
I=imread('sf.tif');
imshow(I);
I1=imcrop;
figure,imshow(I1)
I2=imcrop(I,[3060120160]);
figure,imshow(I2)
4.5读入图像‘tire.tif’,分别使用函数nlfilter和blkproc对图像进行滑动邻域操作和分离邻域操作。
I=imread('tire.tif');
f=inline('max(x(:
))');%构造复合函数
I2=nlfilter(I,[33],f);%滑动邻域操作
imshow(I)
figure,imshow(I2)
I=imread('tire.tif');
f=inline('uint8(round(mean2(x)*ones(size(x))))');%构造复合函数
I2=blkproc(I,[88],f);%滑动邻域操作
imshow(I)
figure,imshow(I2)
4.6对图像rice.tif和cameraman.tif进行图像叠加、旋转运算。
I=imread('rice.tif');%读取图像
J=imread('cameraman.tif');
I=I(1:
256,1:
256);
K=imadd(I,J,'uint16');%图像的叠加
subplot(1,2,1),imshow(K,[])
J=imrotate(K,-90,'bilinear');%图像顺时针旋转90度
subplot(1,2,2),imshow(J,[])
4.7分别显示彩色图像flowers.tif的R、G、B三基色图像。
I=imread('flowers.tif');
R=I(:
:
1); %图像的红色分量
G=I(:
:
2); %图像的绿色分量
B=I(:
:
3); %图像的蓝色分量
subplot(2,2,1),imshow(I);
subplot(2,2,2),imshow(R);
subplot(2,2,3),imshow(G);
subplot(2,2,4),imshow(B);
备注:
例如,在红色分量图像中,白色代表红色数值浓度最高的区域.图像中的黑色区域说明该区域不包含任何红色数值,即R=0.
实验二 图像直方图均衡化与图像滤波
(2学时)
【实验目的】
1、了解图像直方图的基本数学原理,会编写相应的m函数文件;
2、掌握matlab图像直方图均衡化、规定化函数.理解图像直方图处理的意义;
3、掌握噪