实验三 图像的几何变换.docx
- 文档编号:13344371
- 上传时间:2023-06-13
- 格式:DOCX
- 页数:18
- 大小:136.94KB
实验三 图像的几何变换.docx
《实验三 图像的几何变换.docx》由会员分享,可在线阅读,更多相关《实验三 图像的几何变换.docx(18页珍藏版)》请在冰点文库上搜索。
实验三图像的几何变换
实验三图像的几何变换
一.实验目的及要求
掌握图像几何变换的基本原理,熟练掌握数字图像的缩放、旋转、平移、镜像和转置的基本原理及其MATLAB编程实现方法。
二、实验内容
(一)研究以下程序,分析程序功能;输入执行各命令行,认真观察命令执行的结果。
熟悉程序中所使用函数的调用方法,改变有关参数,观察试验结果。
1.图像缩放
clearall,closeall
I=imread('cameraman.tif');
Scale=1.35;%将图像放大1.35倍
J1=imresize(I,Scale,'nearest');%usingthenearestneighborinterpolation
J2=imresize(I,Scale,'bilinear');%usingthebilinearinterpolation
imshow(I),title('OriginalImage');
figure,imshow(J1),title('ResizedImage--usingthenearestneighborinterpolation');
figure,imshow(J2),title('ResizedImage--usingthebilinearinterpolation');
%查看imresize使用帮助
helpimresize
Command窗口显示如下:
IMRESIZEResizeimage.
B=IMRESIZE(A,SCALE)returnsanimagethatisSCALEtimesthe
sizeofA,whichisagrayscale,RGB,orbinaryimage.
B=IMRESIZE(A,[NUMROWSNUMCOLS])resizestheimagesothatithas
thespecifiednumberofrowsandcolumns.EitherNUMROWSorNUMCOLS
maybeNaN,inwhichcaseIMRESIZEcomputesthenumberofrowsor
columnsautomaticallyinordertopreservetheimageaspectratio.
[Y,NEWMAP]=IMRESIZE(X,MAP,SCALE)resizesanindexedimage.
[Y,NEWMAP]=IMRESIZE(X,MAP,[NUMROWSNUMCOLS])resizesanindexed
image.
TocontroltheinterpolationmethodusedbyIMRESIZE,addaMETHOD
argumenttoanyofthesyntaxesabove,likethis:
IMRESIZE(A,SCALE,METHOD)
IMRESIZE(A,[NUMROWSNUMCOLS],METHOD),
IMRESIZE(X,MAP,M,METHOD)
IMRESIZE(X,MAP,[NUMROWSNUMCOLS],METHOD)
METHODcanbeastringnamingageneralinterpolationmethod:
'nearest'-nearest-neighborinterpolation
'bilinear'-bilinearinterpolation
'bicubic'-cubicinterpolation;thedefaultmethod
METHODcanalsobeastringnaminganinterpolationkernel:
'box'-interpolationwithabox-shapedkernel
'triangle'-interpolationwithatriangularkernel
(equivalentto'bilinear')
'cubic'-interpolationwithacubickernel
(equivalentto'bicubic')
'lanczos2'-interpolationwithaLanczos-2kernel
'lanczos3'-interpolationwithaLanczos-3kernel
Finally,METHODcanbeatwo-elementcellarrayoftheform{f,w},
wherefisthefunctionhandleforacustominterpolationkernel,and
wisthecustomkernel'swidth.f(x)mustbezerooutsidethe
interval-w/2<=x scalaroravectorinput. YoucanachieveadditionalcontroloverIMRESIZEbyusing parameter/valuepairsfollowinganyofthesyntaxesabove.For example: B=IMRESIZE(A,SCALE,PARAM1,VALUE1,PARAM2,VALUE2,...) Parametersinclude: 'Antialiasing'-trueorfalse;specifieswhethertoperform antialiasingwhenshrinkinganimage.The defaultvaluedependsontheinterpolation methodyouchoose.Forthe'nearest'method, thedefaultisfalse;forallothermethods, thedefaultistrue. 'Colormap'-(onlyrelevantforindexedimages)'original' or'optimized';if'original',thenthe outputnewmapisthesameastheinputmap. Ifitis'optimized',thenanewoptimized colormapiscreated.Thedefaultvalueis 'optimized'. 'Dither'-(onlyforindexedimages)trueorfalse; specifieswhethertoperformcolor dithering.Thedefaultvalueistrue. 'Method'-Asdescribedabove 'OutputSize'-Atwo-elementvector,[MROWSNCOLS], specifyingtheoutputsize.Oneelementmay beNaN,inwhichcasetheothervalueis computedautomaticallytopreservetheaspect ratiooftheimage. 'Scale'-Ascalarortwo-elementvectorspecifyingthe resizescalefactors.Ifitisascalar,the samescalefactorisappliedtoeach dimension.Ifitisavector,itcontains thescalefactorsfortherowandcolumn dimensions,respectively. Examples -------- Shrinkbyfactoroftwousingthedefaultsofbicubicinterpolation andantialiasing. I=imread('rice.png'); J=imresize(I,0.5); figure,imshow(I),figure,imshow(J) Shrinkbyfactoroftwousingnearest-neighborinterpolation. (Thisisthefastestmethod,butithasthelowestquality.) J2=imresize(I,0.5,'nearest'); Resizeanindexedimage. [X,map]=imread('trees.tif'); [Y,newmap]=imresize(X,map,0.5); imshow(Y,newmap) ResizeanRGBimagetohave64rows.Thenumberofcolumnsis computedautomatically. RGB=imread('peppers.png'); RGB2=imresize(RGB,[64NaN]); Note ---- ThefunctionIMRESIZEinpreviousversionsoftheImageProcessing Toolboxusedasomewhatdifferentalgorithmbydefault.Ifyouneed thesameresultsproducedbythepreviousimplementation,callthe functionIMRESIZE_OLD. ClassSupport ------------- TheinputimageAcanbenumericorlogicalanditmustbenonsparse. Theoutputimageisofthesameclassastheinputimage.Theinput indexedimageXcanbeuint8,uint16,ordouble. Seealsoimresize_old,imrotate,imtransform,tformarray. ReferencepageinHelpbrowser docimresize 执行程序所得结果如下: 改变参数Scale=0.5得到图形结果如下: 对以上实验结果,分析如下: 通过查看命令窗口查看imresize函数的使用方法。 本实验中利用了形式B=imresize(A,m,method)。 实验中method采用了,'nearest'(默认值)最近邻插值‘方法和'bilinear'双线性插值方法,由图片显示结果可以看出,双线性插值方法要好于最近邻插值方法。 这是由于最近邻插值方法仅是取离其最近的一个像素的像素值,而双线性插值方法采用了其周围的像素值参与计算,所以更能适应图像的局部特征。 m为放大倍数,由上面实验结果可以明显看出,放大1.35倍和0.5倍的效果差异。 2.图像旋转 clearall,closeall I=imread('cameraman.tif'); Theta=45;%将图像逆时针旋转45。 J1=imrotate(I,Theta,'nearest');%usingthenearestneighborinterpolation %andenlargetheoutputimage Theta=-45;%将图像顺时针旋转45。 J2=imrotate(I,Theta,'bilinear','crop');%usingthebilinearinterpolation %andcropstheoutputimage imshow(I),title('OriginalImage'); figure,imshow(J1),title('RotatedImage--usingthenearestneighborinterpolation'); figure,imshow(J2),title('RotatedImage--usingthebilinearinterpolation'); %查看imrotate使用帮助 helpimrotate Command窗口显示如下: IMROTATERotateimage. B=IMROTATE(A,ANGLE)rotatesimageAbyANGLEdegreesina counterclockwisedirectionarounditscenterpoint.Torotatetheimage clockwise,specifyanegativevalueforANGLE.IMROTATEmakestheoutput imageBlargeenoughtocontaintheentirerotatedimage.IMROTATEuses nearestneighborinterpolation,settingthevaluesofpixelsinBthat areoutsidetherotatedimageto0(zero). B=IMROTATE(A,ANGLE,METHOD)rotatesimageA,usingtheinterpolation methodspecifiedbyMETHOD.METHODisastringthatcanhaveoneofthe followingvalues.Thedefaultvalueisenclosedinbraces({}). {'nearest'}Nearestneighborinterpolation 'bilinear'Bilinearinterpolation 'bicubic'Bicubicinterpolation.Note: Thisinterpolation methodcanproducepixelvaluesoutsidetheoriginal range. B=IMROTATE(A,ANGLE,METHOD,BBOX)rotatesimageA,whereBBOXspecifies thesizeoftheoutputimageB.BBOXisatextstringthatcanhave eitherofthefollowingvalues.Thedefaultvalueisenclosedinbraces ({}). {'loose'}MakeoutputimageBlargeenoughtocontainthe entirerotatedimage.BisgenerallylargerthanA. 'crop'MakeoutputimageBthesamesizeastheinputimage A,croppingtherotatedimagetofit. ClassSupport ------------- Theinputimagecanbenumericorlogical.Theoutputimageisofthe sameclassastheinputimage. Example ------- %ThisexamplebringsimageIintohorizontalalignmentby %rotatingtheimageby-1degree. I=fitsread('solarspectra.fts'); I=mat2gray(I); J=imrotate(I,-1,'bilinear','crop'); figure,imshow(I),figure,imshow(J) Seealsoimcrop,imresize,imtransform,tformarray. ReferencepageinHelpbrowser docimrotate 执行程序所得结果如下: 改变参数,Theta=135和-135时,所得结果如下: 实验结果分析如下: 通过查看命令窗口了解imrotate函数的使用。 本实验中采用了函数的两种形式,B=imrotate(A,angle,method)和B=imrotate(A,angle,method,bbox)。 实验中,method的设置及其原理同上个实验。 实验中,bbox设置为了“crop”,其作用是为了使输出图像和输入图像大小相同,可以看出当设置了该参数是,图像明显被裁减了,这是因为图像旋转后面积变大了,而该参数的设置使图像须保持原来的大小i,因而图像被裁减了,未设置该参数时默认大小可以显示整个旋转后的图像。 Angle为旋转角度,分别设置为了45和-45、135和-135,由上面两组图可以看出明显的效果和差异 3.图像水平镜象 clearall,closeall I=imread('cameraman.tif'); I1=flipdim(I,2); I2=flipdim(I,1); figure (1),subplot(1,2,1),imshow(I); subplot(1,2,2),imshow(I1); figure (2),subplot(2,1,1),imshow(I); subplot(2,1,2),imshow(I2); 执行程序,所得结果如下: 对实验结果分析如下: flipdim函数的使用方法如下。 B=flipdim(A,dim) 沿指定的维翻转矩阵。 当dim=1时,行翻转,等同于flipud,当dim=2时,列翻转,等同于fliplr。 由上图可以看出翻转的效果。 (二)用MATLAB编程实现以下图像几何变换 1.图像平移 程序代码如下: clc,clearall; I=imread('cameraman.tif'); rows=size(I,1); cols=size(I,2); movx=50;movy=50; fori=1: rows forj=1: cols Q(i+movx,j+movy)=I(i,j); end end figure (1); subplot(121);imshow(I);title('originepicture'); subplot(122);imshow(Q);title('modifiedpicture'); 执行程序结果如下: 实验分析如下: 实验中,每个像素值以及其对应的坐标x和y都被平移了50,表现在整个图像上,即向右下角平移sqrt(50*50+50*50),显示结果如上图所示。 2.图像转置 图像的转置是将给定图像像素的x坐标和y坐标互换的几何变换,设点P0(x0,y0)转置后的对应点为P(x,y),转置变换可表述为: 或 ,对应的逆变换为: 或 转置后图像的宽、高发生改变,即输出图像的高度为原始图像的宽度,输出图像的宽度为原始图像的高度。 程序代码如下: clc,clearall; I=imread('cameraman.tif'); rows=size(I,1); cols=size(I,2); fori=1: rows forj=1: cols Q(j,i)=I(i,j); end end size(I),size(Q) figure (1); subplot(121);imshow(I);title('originepicture'); subplot(122);imshow(Q);title('modifiedpicture'); 执行程序,所得结果如下: 实验分析如下: 设图像中某个像素为p(j,i),则其值为被p(i,j)被代替,其中p为整个图像的像素矩阵。 对图像中的所有像素.逐列、逐行的进行此计算,即可实现转置。 实验结果如上图所示,明显看出,转置后图像的宽、高发生改变,即输出图像的高度为原始图像的宽度,输出图像的宽度为原始图像的高度,整个图像被“转置”了。 三、实验设备 1.PIII以上微机; 2.MATLAB6.5; 四、实验心得与体会
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验三 图像的几何变换 实验 图像 几何 变换