数字图像处理-第二章课件_第1页
数字图像处理-第二章课件_第2页
数字图像处理-第二章课件_第3页
数字图像处理-第二章课件_第4页
数字图像处理-第二章课件_第5页
已阅读5页,还剩85页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

chart02Fundamentalschart0212.1DigitalImageRepresentation数学图像的表示2.2ReadingImages读图像2.3DisplayingImages显示图像2.5DataClasses(omitted,self-study)数据的类型(略,自学)2.6ImageTypes图像的类型2.7ConvertingbetweenDataClassesandImageTypes图像类型与数据类型之间的转换2.8ArrayIndexing(omitted,self-study)数组的索引(下标)(略,自学)2.9SomeImportantStandardArrays(omitted)一些重要的标准数组(略)2.10M-FunctionProgramming(omitted,self-study)M-函数编程(略,自学)2.1DigitalImageRepresentat2Animagemaybedefinedasatwo-dimensionalfunction,f(x,y),wherexandyarespatial(plane)coordinates,andtheamplitudeoffatanypairofcoordinates(x,y)iscalledtheintensityorgrayleveloftheimageatthatpoint.一幅图像被定义为一个二元(维)函数f(x,y),x和y是空间(平面)坐坐标,在坐标对(x,y)处的幅值f称为图像在该点处的亮度值或灰度等级(或灰度级)Colorimagesareformedbyacombinationofindividual2-Dimages.Forexample,intheRGBcolorsystem,acolorimageconsistsofthree(red,green,andblue)individualcomponentimages.颜色图像可由单个2维图像组合而成。例如在RGB颜色系统中,颜色图像是由三个单色成份(即红、绿和蓝)的图像组成。Forthisreason,manyofthetechniquesdevelopedformonochromeimagescanbeextendedtocolorimagesbyprocessingthethreecomponentimagesindividually.据此,许多针对单色图像所开发的技术,要以扩展到彩色图像上。即通过单独对三个成份图像分别处理。2.1DigitalImageRepresentationAnimagemaybedefined3实例说明实例说明4数字图像处理-第二章课件5I=imread('2353.jpg');imshow(I);I1=I;I2=I;I3=I;I1(:,:,1)=I(:,:,1);I1(:,:,2)=0;I1(:,:,3)=0;I2(:,:,2)=I(:,:,2);I2(:,:,1)=0;I2(:,:,3)=0;I3(:,:,3)=I(:,:,3);I3(:,:,1)=0;I3(:,:,2)=0;figure;imshow(I);figure;imshow(I1);figure;imshow(I2);figure;imshow(I3);实现代码:I=imread('2353.jpg');实现代码:6Animagemaybecontinuouswithrespecttothex-andy-coordinates,andalsoinamplitude.一幅图像关于x和y坐标可以是连续的(即位置上连接),幅值也可以是连续的(即亮度上是连续的)。Convertingsuchanimagetodigitalformrequiresthatthecoordinates,aswellastheamplitude,bedigitized.将上述连续图像转换成数字形式,需要将坐标x、y和幅度f数字化。Digitizingthecoordinatevaluesiscalledsampling;digitizingtheamplitudevaluesiscalledquantization.将坐标值数字化称为采样;幅值的数字化称为量化。Thus,whenx,y,andtheamplitudevaluesoffareallfinite,discretequantities,wecalltheimageadigitalimage.因而,当x、y和幅度f都是有限的离散的量时,我们称图像为数字图像。2.1DigitalImageRepresentationAnimagemaybecontinuo7Theresultofsamplingandquantizationisamatrixofrealnumbers.Weusetwoprincipalwaysinthisbooktorepresentdigitalimages.采样和量化的结果将得到一个实数矩阵。在本书中我们使用两个主要方法来表示图像。Assumethatanimagef(x,y)issampledsothattheresultingimagehasMrowsandNcolumns.WesaythattheimageisofsizeM×N.Thevaluesofthecoordinates(x,y)arediscretequantities.设图像f(x,y)是一采样后的图像,因此它有M行和N列。我们称图像的大小(或尺寸)是M×N,坐标(x,y)的值是离散的量。Fornotationalclarityandconvenience,weuseintegervaluesforthesediscetecoordinates.为表示上的清晰和方便,我们使用整数值来表示这些离散的坐标2.1.1CoordinateConventionsTheresultofsamplinga8图采样网格(a)正方形网格;(b)正六角形网格图采样网格9图2-2采样示意图图2-2采样示意图10实例说明434×348实例说明434×34811图2-3量化示意图(a)量化;(b)量化为8bit图2-3量化示意图12图2-4不同采样点数对图像质量的影响(a)原始图像(256×256);(b)采样图像1(128×128);(c)采样图像2(64×64);(d)采样图像3(32×32);(e)采样图像4(16×16);(f)采样图像5(8×8)图2-4不同采样点数对图像质量的影响13图2-5不同量化级别对图像质量的影响(a)原始图像(256色);(b)量化图像1(64色);(c)量化图像2(32色);(d)量化图像3(16色);(e)量化图像4(4色);(f)量化图像5(2色)图2-5不同量化级别对图像质量的影响14一般,当限定数字图像的大小时,为了得到质量较好的图像可采用如下原则:(1)对缓变的图像,应该细量化,粗采样,以避免假轮廓。(2)对细节丰富的图像,应细采样,粗量化,以避免模糊(混叠)。对于彩色图像,是按照颜色成分——红(R)、绿(G)、蓝(B)分别采样和量化的。若各种颜色成分均按8bit量化,即每种颜色量级别是256,则可以处理256×256×256=16777216种颜色。一般,当限定数字图像的大小时,为了得到质量15Inmanyimageprocessingbooks,theimageoriginisdefinedtobeat(x,y)=(0,0).Thenextcoordinatevaluesalongthefirstrowoftheimageare(x,y)=(0,1).许多图像处理教材中,图像原点被定义为(x,y)=(0,0).即参考起始点的坐标值定义为(0,0),位置从第0行第0列位置开始计数,沿着图像第一行的下一个坐标值是(x,y)=(0,1),即第0行第1列的位置。Itisimportanttokeepinmindthatthenotation(0,1)isusedtosignifythesecondsamplealongthefirstrow.Itdoesnotmeanthatthesearetheactualvaluesofphysicalcoordinateswhentheimagewassampled.

需要记住的一个要点是:符号(0,1)被用于标记沿第一行的第二个采样点。当图像被采样时,(0,1)等坐标值并不代表物理坐标的真实值,它代表的仅仅是采样点的相对位置。如(i,j)坐标表示第i行第j个的采样点。2.1.1CoordinateConventionsInmanyimageprocessing16实例说明实例说明17Figure2.1(a)showsthiscoordinateconvention.Notethatxrangesfrom0toM-1,andyfrom0toN-1,inintegerincrements.

下图显示坐标约定。注意x的范围是0到M-1,y的范围是N-1,按整数值递增Figure2.1(a)Figure2.1(a)showsthis18Thecoordinateconventionusedinthetoolboxtodenotearraysisdifferentfromtheprecedingparagraphintwominorways.工具箱中的坐标约定和前面段落中的坐标约定,在两个小的方面上有所不同。◆First,insteadofusing(x,y),thetoolboxusesthenotation(r,c)toindicaterowsandcolumns.首先工具箱使用符号(r,c)来表示行数和列数,而不是使用(x,y)◆Theotherdifferenceisthattheoriginofthecoordinatesystemisat(r,c)=(1,1);另一个不同之处就是在工具箱的坐标系统中,坐标的原点是在(r,c)=(1,1),即第一行的第一个列位置上的采样点是从(1,1)开始计数,也就是说,第一行记为第1行,而不是第0行(别的教材将图像的第一行记为第0行),同样列亦如此。thus,rrangesfrom1toM,andcfrom1toN,inintegerincrements.ThiscoordinateconventionisshowninFig.2.1(b)因此,r的范围从1到M,c的范围是从1到N,按整数值递增。这一坐标约定如图2.1(b)所示Thecoordinateconventio19Figure2.1(b)Figure2.1(b)20Representationforadigitizedimagefunction:数字图像函数的表示2.1.2ImageasMatricesAdigitalimagecanberepresentednaturallyasaMATLABmatrix:在MATLAB的IPT中,数字图像表示成一个如下的MATLAB矩阵Representationforadig21ImagesarereadintotheMATLABenvironmentusingfunctionimread,whosesyntaxis:使用函数imread将图像读入到MATLAB工作环境中,其语法如下:A=imread(,fmt)[X,map]=imread(...)[...]=imread()[...]=imread(...,idx)CURorICO[A,map,alpha]=imread(...)CURorICO[...]=imread(...,idx)GIF[...]=imread(...,'frames',idx)GIF[...]=imread(...,'BackgroundColor',BG)PNG[A,map,alpha]=imread(...)PNG[...]=imread(...,idx)TIFF[...]=imread(...,'PixelRegion',{ROWS,COLS})TIFF2.2ReadingImagesImagesarereadintothe22A=imread(,fmt)readsagrayscaleorcolorimagefromthebythestring.Ifthenotinthecurrentdirectory,orinadirectoryontheMATLABpath,specifythefullpathname.

A=imread(,fmt)从字符串参数所指定的文件中读取一幅灰度图或彩色图像。如果文件不在当前路径,也不在MATLAB的搜索路径中,则应指定文件的完整路径。ThereturnvalueAisanarraycontainingtheimagedata.Iftheagrayscaleimage,AisanM-by-Narray.Iftheatruecolorimage,AisanM-by-N-by-3array.返回值A是是一个包含图像数据的矩阵(数组)。如果图像文件是一个灰度图,则A为一M×N矩阵(数组),如果图像文件是一个真彩色图像,则A是个M×N×3的矩阵(注MATLAB中数组和矩阵在内存分配上没有区别,有时称数组,有时称矩阵)2.2ReadingImagesA=imread(,fmt)reads23SupportedformatsMATLAB所支持的图像文件格式2.2ReadingImagesSupportedformats2.2Re24Forexample,thecommandline:2.2ReadingImagesf=imread('Fig0203(a)(chest-xray).tif');Readtheimagechest-xrayintoimagearrayf.Forexample,thecomman25When,asintheprecedingcommandline,nopathinformationisincludedin,imreadreadsthethecurrentdirectoryand,ifthatfails,ittriestofindthetheMATLABsearchpath.在前述的命令行中,当文件名中没有路径信息时,imread就从当前路径中读取,如果读取失败,就试着从MATLAB的搜索路径中读取。Thesimplestwaytoreadanimagefromaspecifieddirectoryistoincludeafullorrelativepathtothatdirectoryin.Forexample:从指定路径中读取图像的最简单方法就是在文件名中指定完整路径或相对路径。例如:

f=imread('E:\教学\数字图像处理\chart02\(chest-xray).tif');Functionsizegivestherowandcolumndimensionsofanimage:

函数给出图像的行数和列数,即图像的行列维数。2.2ReadingImages[MN]=size(f)M=494N=600s=size(f)s=494600When,asintheprecedin26ImagesaredisplayedontheMATLABdesktopusingfunctionimshow,whichhasthebasicsyntax:在MATLAB桌面环境下使用函数imshow显示图像,其语法如下:

imshow(f,G)wherefisanimagearray,andGisthenumberofintensitylevelsusedtodisplayit.IfGisomitted,itdefaultsto256levels.F是图像矩阵,G是显示图像所用的灰度等级数目。如果G省略,缺省为256Usingthesyntax:使用语法:

imshow(f,[lowhigh])displaysasblackallvalueslessthanorequaltolow,andaswhiteallvaluesgreaterthanorequaltohigh.Thevaluesinbetweenaredisplayedasintermediateintensityvaluesusingthedefaultnumberoflevels.

将小于或等于low的灰度级显示为黑,将大于或等于high的灰度级显示为白。low和high之间的值按缺省灰度级数目显示为黑、白之间的中间灰度级。2.3DisplayingImagesImagesaredisplayedont27Finally,thesyntax:imshow(f,[])setsvariablelowtotheminimumvalueofarrayfandhightoitsmaximumvalue.最后语法imshow(f,[])将low和high分别设置为矩阵f的最小值和最大值。Thisformofimshowisusefulfordisplayingimagesthathavealowdynamicrangeorthathavepositiveandnegativevalues.当图像的灰度动态范围较低或图像有正、负值时,这一语法形式非常有用Forexample:2.3DisplayingImagesfchest=imread('Fig0203(a)(chest-xray).tif');subplot(1,2,1),imshow(fchest),subplot(1,2,2),imshow(fchest,[])Finally,thesyntax:28Forexample:2.3DisplayingImagesfrose=imread('Fig0206(a)(rose-original).tif');imshow(frose)Forexample:2.3DisplayingIm29Imagesarewrittentodiskusingfunctionimwrite,whichhasthefollowingbasicsyntax:将图像写入硬盘的函数是imwrite,其基本语法:

imwrite(f,'')Forexample,thefollowingcommandwritesftoaTIFFfileanmedpatient10_run1:例如,如下命令将f写入名为patient10_run1的TIFF格式的文件中。>>imwrite(f,'patient10_run1','tif')

oralternatively,

imwrite(f,'patient10_run1.tif')

2.4WritingImagesImagesarewrittentodis30AmoregeneralimwritesyntaxapplicableonlytoJPEGimagesis:如果只针对JPEG图像,能适合的一般语法是:

imwrite(f,'','quality',q)wherqisanintegerbetween0and100(thelowerthenumberthehigherthedegradationduetoJPEGcompression)q是一个介于0到100的整数,其值越小,JPEG图像压缩的降质越厉害。Inordertoreducestorageandtransmissiontime,itisimportantthattheimagesbecompressedasmuchaspossiblewhilenotdegradingtheirvisualappearancebeyondareasonablelevel.为了减少图像文件的存贮容量和传输时间,重要的一点就是要将图像尽可能的压缩到合适的程度,以便不影响视觉效果。Inthiscase“reasonable”meansnoperceptiblefalsecontouring.Forexample,forq=25theapplicablesyntaxis:本例中的合适程度,意思是指没有可感知到的伪轮廓。如q=25时的所用的语法是:

imwrite(f,‘bubble25.jpg','quality',25)2.4WritingImagesAmoregeneralimwritesy31fbubble=imread('Fig0204(a)(bubbles-q-100jpg).tif');imwrite(fbubble,'bubbles0.jpg','quality',0);imwrite(fbubble,'bubbles5.jpg','quality',5);imwrite(fbubble,'bubbles15.jpg','quality',15);imwrite(fbubble,'bubbles25.jpg','quality',25);imwrite(fbubble,'bubbles50.jpg','quality',50);2.4WritingImagesimshow(fbubble)imshow(f50)imshow(f25)imshow(f15)imshow(f5)imshow(f0)f0=imread('bubbles0.jpg');f5=imread('bubbles5.jpg');f15=imread('bubbles15.jpg');f25=imread('bubbles25.jpg');f50=imread('bubbles50.jpg');fbubble=imread('Fig0204(a)(bu322.4WritingImagesOriginalimageq=50q=25q=0q=5q=152.4WritingImagesOriginalim332.4WritingImagesOriginalimageq=10q=8q=0q=4q=62.4WritingImagesOriginalim34Thetoolboxsupportsfourtypesofimages:工具箱支持四种图像类型◆Intensityimages灰度图像(或叫亮度图像)◆Binaryimages二值图像(或叫二进值图像,或叫黑白图像)◆Indexedimages索引图像◆RGBimagesRGB图像Mostmonochromeimageprocessingoperationsarecarriedoutusingbinaryorintensityimages,soourinitialfocusisonthesetwoimagetypes.大多数单色图像处理操作都是针对二值图像或灰度图像进行的。因此我们首先集中在这两类图像的研究上。2.6ImageTypesThetoolboxsupportsfourtype35实例说明灰度图像实例说明灰度图像36实例说明灰度图像实例说明灰度图像37实例说明索引图像实例说明索引图像38实例说明RGB图像实例说明RGB图像39Anintensityimageisadatamatrixwhosevalueshavebeenscaledtorepresentintensities.在MATLAB中,灰度图像是一个数值矩阵。它的值归一化到其所表示的图像。Whentheelementsofanintensityimageareofclassuint8,orclassuint16,theyhaveintegervaluesintherange[0,255]and[0,65535],respectively.当图像类型(即存贮各像素值的数据类型)是uint8或uint16时,其整数值的取值范围分别是[0,255]和[0,65535]。Valuesofscaled,classdoubleintensityimagesareintherange[0,1]byconvention.通过约定,归一化的双精度类型的灰度图像的数据取值范围是[0,1]2.6.1ImageTypesAnintensityimageisadatam40Abinaryimageisalogicalarrayof0sand1s.Thus,anarrayof0sand1swhosevaluesareofdataclass,say,uint8,isnotconsideredabinaryimageinMATLAB.二值图像是由0和1组成一个逻辑矩阵。其元素值是逻辑值。因此在MATLAB中,如果由数据类(而不是图像类)的0和1组成的矩阵,不能将其视为一个二值图像。注意,当0和1是数据类的逻辑值时,则可以将矩阵视为二值图像。Anumericarrayisconvertedtobinaryusingfunctionlogical.Thus,ifAisanumericarrayconsistingof0sand1s,wecreatealogicalarrayBusingthestatement:数值矩阵可通过函数logical转换成二值逻辑矩阵,因此,如果一个矩阵A由数值(非逻辑)0和1组成,我们可以使用如下声明来创建逻辑矩阵:B=logical(A)Totestifanarrayislogicalweusetheislogicalfunction:测试一个矩阵是否是一个逻辑型的,可以使用islogical函数:islogical(C)IfCisalogicalarray,thisfunctionreturnsa1.Otherwiseitreturnsa0.如果C是一个逻辑矩阵,则该函数返回1,否则返回0.2.6.2BinaryImagesAbinaryimageisalogicalar41ImageTypesandTypeConversionsdither:Convertimage,increasingapparentcolorresolutionbydithering通过抖动技术,多外观上增加灰度的等级。double:Convertdatatodoubleprecision将数据转换成双精度型。gray2ind:Convertgrayscaleorbinaryimagetoindexedimage将灰度图或二值图像转换成索引图。graythresh:GlobalimagethresholdusingOtsu'smethod使用Otsu方法将图像进行全局阈值化处理成二值图像。im2bw:Convertimagetobinaryimage,basedonthreshold使用阈值化方法,将图像转换成二值图像。im2double:Convertimagetodoubleprecision将图像转换成双精度型矩阵。im2int16:Convertimageto16-bitsignedintegers将图像转换成int16型矩阵im2java:ConvertimagetoJavaimage将MATLAB图像转换成Java类型图像im2java2d:ConvertimagetoJavabufferedimage将MATLAB转换成Javabuffered图像2.7ConvertingbetwwenDataClassesandImageTypesImageTypesandTypeConversio42im2single:Convertimagetosingleprecision将图像转换成单精度矩阵im2uint16:Convertimageto16-bitunsignedintegers将图像转换成uint16型矩阵im2uint8:Convertimageto8-bitunsignedintegers将图像转换成uint8型矩阵2.7ConvertingbetwwenDataClassesandImageTypesim2single:Convertimagetosin43ind2gray:Convertindexedimagetograyscaleimage将图像转换成灰度图像ind2rgb:ConvertindexedimagetoRGBimage将图像转换成rgb图像label2rgb:ConvertlabelmatrixintoRGBimage将标签图像转换成rgb图像。mat2gray:Convertmatrixtograyscaleimage将数据值(非图像类型)的矩阵转换成灰度图像。rgb2gray:ConvertRGBimageorcolormaptograyscale将rgb图像转换成灰度图像rgb2ind:ConvertRGBimagetoindexedimage将rgb图像转换成索引图像。uint16:Convertdatatounsigned16-bitintegers将数据转换成uint16类型uint8:Convertdatatounsigned8-bitintegers将数据转换成uint8类型PleaseturntohelpinMATLAB:ImageProcessingToolbox\Functions-ByCategory\ImageDisplayandExporation\ImageTypesandTypeConversions.2.7ConvertingbetwwenDataClassesandImageTypesind2gray:Convertindexedimage44>>f=[-0.50.5;0.751.5]f=-0.50000.50000.75001.5000>>g=im2uint8(f)g=0128191255>>mat2gray(f)ans=00.50000.62501.0000>>A=[00;11]A=0011>>im2double(A)ans=0011>>lA=logical(A)lA=0011>>im2double(lA)ans=0011>>u8A=uint8(A)u8A=0011>>im2double(u8A)ans=000.00390.0039>>f=[-0.50.5;0.751.5]>>45chart02Fundamentalschart02462.1DigitalImageRepresentation数学图像的表示2.2ReadingImages读图像2.3DisplayingImages显示图像2.5DataClasses(omitted,self-study)数据的类型(略,自学)2.6ImageTypes图像的类型2.7ConvertingbetweenDataClassesandImageTypes图像类型与数据类型之间的转换2.8ArrayIndexing(omitted,self-study)数组的索引(下标)(略,自学)2.9SomeImportantStandardArrays(omitted)一些重要的标准数组(略)2.10M-FunctionProgramming(omitted,self-study)M-函数编程(略,自学)2.1DigitalImageRepresentat47Animagemaybedefinedasatwo-dimensionalfunction,f(x,y),wherexandyarespatial(plane)coordinates,andtheamplitudeoffatanypairofcoordinates(x,y)iscalledtheintensityorgrayleveloftheimageatthatpoint.一幅图像被定义为一个二元(维)函数f(x,y),x和y是空间(平面)坐坐标,在坐标对(x,y)处的幅值f称为图像在该点处的亮度值或灰度等级(或灰度级)Colorimagesareformedbyacombinationofindividual2-Dimages.Forexample,intheRGBcolorsystem,acolorimageconsistsofthree(red,green,andblue)individualcomponentimages.颜色图像可由单个2维图像组合而成。例如在RGB颜色系统中,颜色图像是由三个单色成份(即红、绿和蓝)的图像组成。Forthisreason,manyofthetechniquesdevelopedformonochromeimagescanbeextendedtocolorimagesbyprocessingthethreecomponentimagesindividually.据此,许多针对单色图像所开发的技术,要以扩展到彩色图像上。即通过单独对三个成份图像分别处理。2.1DigitalImageRepresentationAnimagemaybedefined48实例说明实例说明49数字图像处理-第二章课件50I=imread('2353.jpg');imshow(I);I1=I;I2=I;I3=I;I1(:,:,1)=I(:,:,1);I1(:,:,2)=0;I1(:,:,3)=0;I2(:,:,2)=I(:,:,2);I2(:,:,1)=0;I2(:,:,3)=0;I3(:,:,3)=I(:,:,3);I3(:,:,1)=0;I3(:,:,2)=0;figure;imshow(I);figure;imshow(I1);figure;imshow(I2);figure;imshow(I3);实现代码:I=imread('2353.jpg');实现代码:51Animagemaybecontinuouswithrespecttothex-andy-coordinates,andalsoinamplitude.一幅图像关于x和y坐标可以是连续的(即位置上连接),幅值也可以是连续的(即亮度上是连续的)。Convertingsuchanimagetodigitalformrequiresthatthecoordinates,aswellastheamplitude,bedigitized.将上述连续图像转换成数字形式,需要将坐标x、y和幅度f数字化。Digitizingthecoordinatevaluesiscalledsampling;digitizingtheamplitudevaluesiscalledquantization.将坐标值数字化称为采样;幅值的数字化称为量化。Thus,whenx,y,andtheamplitudevaluesoffareallfinite,discretequantities,wecalltheimageadigitalimage.因而,当x、y和幅度f都是有限的离散的量时,我们称图像为数字图像。2.1DigitalImageRepresentationAnimagemaybecontinuo52Theresultofsamplingandquantizationisamatrixofrealnumbers.Weusetwoprincipalwaysinthisbooktorepresentdigitalimages.采样和量化的结果将得到一个实数矩阵。在本书中我们使用两个主要方法来表示图像。Assumethatanimagef(x,y)issampledsothattheresultingimagehasMrowsandNcolumns.WesaythattheimageisofsizeM×N.Thevaluesofthecoordinates(x,y)arediscretequantities.设图像f(x,y)是一采样后的图像,因此它有M行和N列。我们称图像的大小(或尺寸)是M×N,坐标(x,y)的值是离散的量。Fornotationalclarityandconvenience,weuseintegervaluesforthesediscetecoordinates.为表示上的清晰和方便,我们使用整数值来表示这些离散的坐标2.1.1CoordinateConventionsTheresultofsamplinga53图采样网格(a)正方形网格;(b)正六角形网格图采样网格54图2-2采样示意图图2-2采样示意图55实例说明434×348实例说明434×34856图2-3量化示意图(a)量化;(b)量化为8bit图2-3量化示意图57图2-4不同采样点数对图像质量的影响(a)原始图像(256×256);(b)采样图像1(128×128);(c)采样图像2(64×64);(d)采样图像3(32×32);(e)采样图像4(16×16);(f)采样图像5(8×8)图2-4不同采样点数对图像质量的影响58图2-5不同量化级别对图像质量的影响(a)原始图像(256色);(b)量化图像1(64色);(c)量化图像2(32色);(d)量化图像3(16色);(e)量化图像4(4色);(f)量化图像5(2色)图2-5不同量化级别对图像质量的影响59一般,当限定数字图像的大小时,为了得到质量较好的图像可采用如下原则:(1)对缓变的图像,应该细量化,粗采样,以避免假轮廓。(2)对细节丰富的图像,应细采样,粗量化,以避免模糊(混叠)。对于彩色图像,是按照颜色成分——红(R)、绿(G)、蓝(B)分别采样和量化的。若各种颜色成分均按8bit量化,即每种颜色量级别是256,则可以处理256×256×256=16777216种颜色。一般,当限定数字图像的大小时,为了得到质量60Inmanyimageprocessingbooks,theimageoriginisdefinedtobeat(x,y)=(0,0).Thenextcoordinatevaluesalongthefirstrowoftheimageare(x,y)=(0,1).许多图像处理教材中,图像原点被定义为(x,y)=(0,0).即参考起始点的坐标值定义为(0,0),位置从第0行第0列位置开始计数,沿着图像第一行的下一个坐标值是(x,y)=(0,1),即第0行第1列的位置。Itisimportanttokeepinmindthatthenotation(0,1)isusedtosignifythesecondsamplealongthefirstrow.Itdoesnotmeanthatthesearetheactualvaluesofphysicalcoordinateswhentheimagewassampled.

需要记住的一个要点是:符号(0,1)被用于标记沿第一行的第二个采样点。当图像被采样时,(0,1)等坐标值并不代表物理坐标的真实值,它代表的仅仅是采样点的相对位置。如(i,j)坐标表示第i行第j个的采样点。2.1.1CoordinateConventionsInmanyimageprocessing61实例说明实例说明62Figure2.1(a)showsthiscoordinateconvention.Notethatxrangesfrom0toM-1,andyfrom0toN-1,inintegerincrements.

下图显示坐标约定。注意x的范围是0到M-1,y的范围是N-1,按整数值递增Figure2.1(a)Figure2.1(a)showsthis63Thecoordinateconventionusedinthetoolboxtodenotearraysisdifferentfromtheprecedingparagraphintwominorways.工具箱中的坐标约定和前面段落中的坐标约定,在两个小的方面上有所不同。◆First,insteadofusing(x,y),thetoolboxusesthenotation(r,c)toindicaterowsandcolumns.首先工具箱使用符号(r,c)来表示行数和列数,而不是使用(x,y)◆Theotherdifferenceisthattheoriginofthecoordinatesystemisat(r,c)=(1,1);另一个不同之处就是在工具箱的坐标系统中,坐标的原点是在(r,c)=(1,1),即第一行的第一个列位置上的采样点是从(1,1)开始计数,也就是说,第一行记为第1行,而不是第0行(别的教材将图像的第一行记为第0行),同样列亦如此。thus,rrangesfrom1toM,andcfrom1toN,inintegerincrements.ThiscoordinateconventionisshowninFig.2.1(b)因此,r的范围从1到M,c的范围是从1到N,按整数值递增。这一坐标约定如图2.1(b)所示Thecoordinateconventio64Figure2.1(b)Figure2.1(b)65Representationforadigitizedimagefunction:数字图像函数的表示2.1.2ImageasMatricesAdigitalimagecanberepresentednaturallyasaMATLABmatrix:在MATLAB的IPT中,数字图像表示成一个如下的MATLAB矩阵Representationforadig66ImagesarereadintotheMATLABenvironmentusingfunctionimread,whosesyntaxis:使用函数imread将图像读入到MATLAB工作环境中,其语法如下:A=imread(,fmt)[X,map]=imread(...)[...]=imread()[...]=imread(...,idx)CURorICO[A,map,alpha]=imread(...)CURorICO[...]=imread(...,idx)GIF[...]=imread(...,'frames',idx)GIF[...]=imread(...,'BackgroundColor',BG)PNG[A,map,alpha]=imread(...)PNG[...]=imread(...,idx)TIFF[...]=imread(...,'PixelRegion',{ROWS,COLS})TIFF2.2ReadingImagesImagesarereadintothe67A=imread(,fmt)readsagrayscaleorcolorimagefromthebythestring.Ifthenotinthecurrentdirectory,orinadirectoryontheMATLABpath,specifythefullpathname.

A=imread(,fmt)从字符串参数所指定的文件中读取一幅灰度图或彩色图像。如果文件不在当前路径,也不在MATLAB的搜索路径中,则应指定文件的完整路径。ThereturnvalueAisanarraycontainingtheimagedata.Iftheagrayscaleimage,AisanM-by-Narray.Iftheatruecolorimage,AisanM-by-N-by-3array.返回值A是是一个包含图像数据的矩阵(数组)。如果图像文件是一个灰度图,则A为一M×N矩阵(数组),如果图像文件是一个真彩色图像,则A是个M×N×3的矩阵(注MATLAB中数组和矩阵在内存分配上没有区别,有时称数组,有时称矩阵)2.2ReadingImagesA=imread(,fmt)reads68SupportedformatsMATLAB所支持的图像文件格式2.2ReadingImagesSupportedformats2.2Re69Forexample,thecommandline:2.2ReadingImagesf=imread('Fig0203(a)(chest-xray).tif');Readtheimagechest-xrayintoimagearrayf.Forexample,thecomman70When,asintheprecedingcommandline,nopathinformationisincludedin,imreadreadsthethecurrentdirectoryand,ifthatfails,ittriestofindthetheMATLABsearchpath.在前述的命令行中,当文件名中没有路径信息时,imread就从当前路径中读取,如果读取失败,就试着从MATLAB的搜索路径中读取。Thesimplestwaytoreadanimagefromaspecifieddirectoryistoincludeafullorrelativepathtothatdirectoryin.Forexample:从指定路径中读取图像的最简单方法就是在文件名中指定完整路径或相对路径。例如:

f=imread('E:\教学\数字图像处理\chart02\(chest-xray).tif');Functionsizegivestherowandcolumndimensionsofanimage:

函数给出图像的行数和列数,即图像的行列维数。2.2ReadingImages[MN]=size(f)M=494N=600s=size(f)s=494600When,asintheprecedin71ImagesaredisplayedontheMATLABdesktopusingfunctionimshow,whichhasthebasicsyntax:在MATLAB桌面环境下使用函数imshow显示图像,其语法如下:

imshow(f,G)wherefisanimagearray,andGisthenumberofintensitylevelsusedtodisplayit.IfGisomitted,itdefaultsto256levels.F是图像矩阵,G是显示图像所用的灰度等级数目。如果G省略,缺省为256Usingthesyntax:使用语法:

imshow(f,[lowhigh])displaysasblackallvalueslessthanorequaltolow,andaswhiteallvaluesgreaterthanorequaltohigh.Thevaluesinbetweenaredisplayedasintermediateintensityvaluesusingthedefaultnumberoflevels.

将小于或等于low的灰度级显示为黑,将大于或等于high的灰度级显示为白。low和high之间的值按缺省灰度级数目显示为黑、白之间的中间灰度级。2.3DisplayingImagesImagesaredisplayedont72Finally,thesyntax:imshow(f,[])setsvariablelowtotheminimumvalueofarrayfandhightoitsmaximumvalue.最后语法imshow(f,[])将low和high分别设置为矩阵f的最小值和最大值。Thisformofimshowisusefulfordisplayingimagesthathavealowdynamicrangeorthathavepositiveandnegativevalues.当图像的灰度动态范围较低或图像有正、负值时,这一语法形式非常有用Forexample:2.3DisplayingImagesfchest=imread('Fig0203(a)(chest-xray).tif');subplot(1,2,1),imshow(fchest),subplot(1,2,2),imshow(fchest,[])Finally,thesyntax:73Forexample:2.3DisplayingImagesfrose=imread('Fig0206(a)(rose-original).tif');imshow(frose)Forexample:2.3DisplayingIm74Imagesarewrittentodiskusingfunctionimwrite,whichhasthefollowingbasicsyntax:将图像写入硬盘的函数是imwrite,其基本语法:

imwrite(f,'')Forexample,thefollowingcommandwritesftoaTIFFfileanmedpatient10_run1:例如,如下命令将f写入名为patient10_run1的TIFF格式的文件中。>>imwrite(f,'patient10_run1','tif')

oralternatively,

imwrite(f,'patient10_run1.tif')

2.4WritingImagesImagesarewrittentodis75AmoregeneralimwritesyntaxapplicableonlytoJPEGimagesis:如果只针对JPEG图像,能适合的一般语法是:

imwrite(f,'','quality',q)wherqisanintegerbetween0and100(thelowerthenumberthehigherthedegradationduetoJPEGcompression)q是一个介于0到100的整数,其值越小,JPEG图像压缩的降质越厉害。Inordertoreducestorageandtransmissiontime,itisimportantthattheimagesbecompressedasmuchaspossiblewhilenotdegradingtheirvisualappearancebeyondareasonablelevel.为了减少图像文件的存贮容量和传输时间,重要的一点就是要将图像尽可能的压缩到合适的程度,以便不影响视觉效果。Inthiscase“reasonable”meansnoperceptiblefalsecontouring.Forexample,forq=25theapplicablesyntaxis:本例中的合适程度,意思是指没有可感知到的伪轮廓。如q=25时的所用的语法是:

imwrite(f,‘bubble25.jpg','quality',25)2.4WritingImagesAmoregeneralimwritesy76fbubble=imread('Fig0204(a)(bubbles-q-100jpg).tif');imwrite(fbubble

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论