实验13.docx
- 文档编号:28380851
- 上传时间:2023-07-10
- 格式:DOCX
- 页数:23
- 大小:164.89KB
实验13.docx
《实验13.docx》由会员分享,可在线阅读,更多相关《实验13.docx(23页珍藏版)》请在冰豆网上搜索。
实验13
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得到图形结果如下: 对以上实验结果,分析如下: 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时,所得结果如下: 实验结果分析如下: 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); 执行程序,所得结果如下: 对实验结果分析如下: (二)用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'); 执行程序结果如下: 实验分析如下: 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'); 执行程序,所得结果如下: 实验分析如下: 三频域变换 1.傅立叶变换 (1)简单人工二值图像 clearall,closeall f=zeros(50,50); f(15: 35,23: 28)=1; figure (1),imshow(f,'notruesize') F=fft2(f,128,128); F1=fftshift(F); figure (2),imshow(log(abs(F1)),[-15]);colormap(gray);colorbar figure(3),mesh(1: 128,1: 128,abs(F1));colormap(gray);colorbar F2=fft2(imrotate(f,90),128,128); F3=fftshift(F2); figure(4),imshow(imrotate(f,90),'notruesize') figure(5),imshow(log(abs(F3)),[-15]);colormap(gray);colorbar figure(6),mesh(1: 128,1: 128,abs(F3));colormap(gray);colorbar 执行程序,所得实验结果如下: (2)实际图像傅立叶变换 clearall,closeall A=imread('rice.tif');%读一幅灰度值图象rice.tif imshow(A); FT=fft2(A);%傅立叶变换 AbsFT=abs(FT);%求FT的绝对值,AbsFT为A的振幅谱 %可用以下方式观看振幅谱: F2=log(AbsFT); figure,imshow(F2,[-110],'notruesize'); FinvT=ifft2(FT);%傅立叶反变换 AbsFinvT=abs(FinvT); F3=log(AbsFinvT); figure,imshow(mat2gray(FinvT)); 执行程序,所得实验结果如下: (3)频域滤波 closeall,clearall bw=imread('text.tif'); imshow(bw); C=real(ifft2(fft2(bw).*fft2(fspecial('average'),256,256))); figure,imshow(C,[]);%Display,scalingdatatoappropriaterange. 执行程序,所得实验结果如下: 对 (1) (2)(3)实验结果分析如下: 2.离散余弦变换(DCT) (1)对cameraman.tif图像的离散余弦变换及逆变换重建 clearall,closeall f=imread('cameraman.tif');%读一幅cameraman.tif imshow(f); F=dct2(f);%做余弦变换 AbsFT=abs(F); figure,imshow(log(AbsFT)); FinvT=idct2(F);%做余弦反变换 figure,imshow(mat2gray(FinvT)); %仅保留余弦变换频谱的左上角50*50个数据,然后做余弦反变换,观察输出图像 F1=F; [m,n]=size(F1); F1(50: m,
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验 13
![提示](https://static.bdocx.com/images/bang_tan.gif)