数字图像处理 第二章课件.ppt
chart 02Fundamentals,2.1 Digital Image Representation 数学图像的表示2.2 Reading Images 读图像2.3 Displaying Images 显示图像2.5 Data Classes(omitted,self-study)数据的类型(略,自学)2.6 Image Types 图像的类型2.7 Converting between Data Classes and Image Types 图像类型与数据类型之间的转换2.8 Array Indexing(omitted,self-study)数组的索引(下标)(略,自学)2.9 Some Important Standard Arrays(omitted)一些重要的标准数组(略)2.10 M-Function Programming(omitted,self-study)M-函数编程(略,自学),An image may be defined as a two-dimensional function,f(x,y),where x and y are spatial(plane)coordinates,and the amplitude of f at any pair of coordinates(x,y)is called the intensity or gray level of the image at that point.一幅图像被定义为一个二元(维)函数f(x,y),x和y是空间(平面)坐坐标,在坐标对(x,y)处的幅值f称为图像在该点处的亮度值或灰度等级(或灰度级)Color images are formed by a combination of individual 2-D images.For example,in the RGB color system,a color image consists of three(red,green,and blue)individual component images.颜色图像可由单个2维图像组合而成。例如在RGB颜色系统中,颜色图像是由三个单色成份(即红、绿和蓝)的图像组成。For this reason,many of the techniques developed for monochrome images can be extended to color images by processing the three component images individually.据此,许多针对单色图像所开发的技术,要以扩展到彩色图像上。即通过单独对三个成份图像分别处理。,2.1 Digital Image Representation,实例说明,I=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);,实现代码:,An image may be continuous with respect to the x-and y-coordinates,and also in amplitude.一幅图像关于x和y坐标可以是连续的(即位置上连接),幅值也可以是连续的(即亮度上是连续的)。Converting such an image to digital form requires that the coordinates,as well as the amplitude,be digitized.将上述连续图像转换成数字形式,需要将坐标x、y和幅度f数字化。Digitizing the coordinate values is called sampling;digitizing the amplitude values is called quantization.将坐标值数字化称为采样;幅值的数字化称为量化。Thus,when x,y,and the amplitude values of f are all finite,discrete quantities,we call the image a digital image.因而,当x、y和幅度f都是有限的离散的量时,我们称图像为数字图像。,2.1 Digital Image Representation,The result of sampling and quantization is a matrix of real numbers.We use two principal ways in this book to represent digital images.采样和量化的结果将得到一个实数矩阵。在本书中我们使用两个主要方法来表示图像。Assume that an image f(x,y)is sampled so that the resulting image has M rows and N columns.We say that the image is of size MN.The values of the coordinates(x,y)are discrete quantities.设图像f(x,y)是一采样后的图像,因此它有M行和N列。我们称图像的大小(或尺寸)是MN,坐标(x,y)的值是离散的量。For notational clarity and convenience,we use integer values for these discete coordinates.为表示上的清晰和方便,我们使用整数值来表示这些离散的坐标,2.1.1 Coordinate Conventions,图 采样网格(a)正方形网格;(b)正六角形网格,图2-2 采样示意图,实例说明,434348,图2-3 量化示意图(a)量化;(b)量化为8 bit,图2-4 不同采样点数对图像质量的影响(a)原始图像(256256);(b)采样图像1(128128);(c)采样图像2(6464);(d)采样图像3(3232);(e)采样图像4(1616);(f)采样图像5(88),图2-5 不同量化级别对图像质量的影响(a)原始图像(256色);(b)量化图像1(64色);(c)量化图像2(32色);(d)量化图像3(16色);(e)量化图像4(4色);(f)量化图像5(2色),一般,当限定数字图像的大小时,为了得到质量较好的图像可采用如下原则:(1)对缓变的图像,应该细量化,粗采样,以避免假轮廓。(2)对细节丰富的图像,应细采样,粗量化,以避免模糊(混叠)。对于彩色图像,是按照颜色成分红(R)、绿(G)、蓝(B)分别采样和量化的。若各种颜色成分均按8 bit量化,即每种颜色量级别是256,则可以处理256256256=16 777 216种颜色。,In many image processing books,the image origin is defined to be at(x,y)=(0,0).The next coordinate values along the first row of the image are(x,y)=(0,1).许多图像处理教材中,图像原点被定义为(x,y)=(0,0).即参考起始点的坐标值定义为(0,0),位置从第0行第0列位置开始计数,沿着图像第一行的下一个坐标值是(x,y)=(0,1),即第0行第1列的位置。It is important to keep in mind that the notation(0,1)is used to signify the second sample along the first row.It does not mean that these are the actual values of physical coordinates when the image was sampled.需要记住的一个要点是:符号(0,1)被用于标记沿第一行的第二个采样点。当图像被采样时,(0,1)等坐标值并不代表物理坐标的真实值,它代表的仅仅是采样点的相对位置。如(i,j)坐标表示第i行第j个的采样点。,2.1.1 Coordinate Conventions,实例说明,Figure 2.1(a)shows this coordinate convention.Note that x ranges from 0 to M-1,and y from 0 to N-1,in integer increments.下图显示坐标约定。注意x的范围是0到M-1,y的范围是N-1,按整数值递增,Figure 2.1(a),The coordinate convention used in the toolbox to denote arrays is different from the preceding paragraph in two minor ways.工具箱中的坐标约定和前面段落中的坐标约定,在两个小的方面上有所不同。First,instead of using(x,y),the toolbox uses the notation(r,c)to indicate rows and columns.首先工具箱使用符号(r,c)来表示行数和列数,而不是使用(x,y)The other difference is that the origin of the coordinate system is at(r,c)=(1,1);另一个不同之处就是在工具箱的坐标系统中,坐标的原点是在(r,c)=(1,1),即第一行的第一个列位置上的采样点是从(1,1)开始计数,也就是说,第一行记为第1行,而不是第0行(别的教材将图像的第一行记为第0行),同样列亦如此。thus,r ranges from 1 to M,and c from 1 to N,in integer increments.This coordinate convention is shown in Fig.2.1(b)因此,r的范围从1到M,c的范围是从1到N,按整数值递增。这一坐标约定如图2.1(b)所示,Figure 2.1(b),Representation for a digitized image function:数字图像函数的表示,2.1.2 Image as Matrices,A digital image can be represented naturally as a MATLAB matrix:在MATLAB的IPT中,数字图像表示成一个如下的MATLAB矩阵,Images are read into the MATLAB environment using function imread,whose syntax is:使用函数imread 将图像读入到MATLAB工作环境中,其语法如下:A=imread(,fmt)X,map=imread(.).=imread().=imread(.,idx)CUR or ICO A,map,alpha=imread(.)CUR or ICO.=imread(.,idx)GIF.=imread(.,frames,idx)GIF.=imread(.,BackgroundColor,BG)PNG A,map,alpha=imread(.)PNG.=imread(.,idx)TIFF.=imread(.,PixelRegion,ROWS,COLS)TIFF,2.2 Reading Images,A=imread(,fmt)reads a grayscale or color image from the by the string.If the not in the current directory,or in a directory on the MATLAB path,specify the full pathname.A=imread(,fmt)从字符串参数所指定的文件中读取一幅灰度图或彩色图像。如果文件不在当前路径,也不在MATLAB的搜索路径中,则应指定文件的完整路径。The return value A is an array containing the image data.If the a grayscale image,A is an M-by-N array.If the a truecolor image,A is an M-by-N-by-3 array.返回值A是是一个包含图像数据的矩阵(数组)。如果图像文件是一个灰度图,则A为一MN矩阵(数组),如果图像文件是一个真彩色图像,则A是个MN3的矩阵(注MATLAB中数组和矩阵在内存分配上没有区别,有时称数组,有时称矩阵),2.2 Reading Images,Supported formats MATLAB所支持的图像文件格式,2.2 Reading Images,For example,the command line:,2.2 Reading Images,f=imread(Fig0203(a)(chest-xray).tif);,Read the image chest-xray into image array f.,When,as in the preceding command line,no path information is included in,imread reads the the current directory and,if that fails,it tries to find the the MATLAB search path.在前述的命令行中,当文件名中没有路径信息时,imread就从当前路径中读取,如果读取失败,就试着从MATLAB的搜索路径中读取。The simplest way to read an image from a specified directory is to include a full or relative path to that directory in.For example:从指定路径中读取图像的最简单方法就是在文件名中指定完整路径或相对路径。例如:f=imread(E:教学数字图像处理chart02(chest-xray).tif);Function size gives the row and column dimensions of an image:函数给出图像的行数和列数,即图像的行列维数。,2.2 Reading Images,M N=size(f)M=494N=600,s=size(f)s=494 600,Images are displayed on the MATLAB desktop using function imshow,which has the basic syntax:在MATLAB桌面环境下使用函数imshow显示图像,其语法如下:imshow(f,G)where f is an image array,and G is the number of intensity levels used to display it.If G is omitted,it defaults to 256 levels.F是图像矩阵,G是显示图像所用的灰度等级数目。如果G省略,缺省为256 Using the syntax:使用语法:imshow(f,low high)displays as black all values less than or equal to low,and as white all values greater than or equal to high.The values in between are displayed as intermediate intensity values using the default number of levels.将小于或等于low的灰度级显示为黑,将大于或等于high的灰度级显示为白。low和high之间的值按缺省灰度级数目显示为黑、白之间的中间灰度级。,2.3 Displaying Images,Finally,the syntax:imshow(f,)sets variable low to the minimum value of array f and high to its maximum value.最后语法 imshow(f,)将low和high分别设置为矩阵f的最小值和最大值。This form of imshow is useful for displaying images that have a low dynamic range or that have positive and negative values.当图像的灰度动态范围较低或图像有正、负值时,这一语法形式非常有用 For example:,2.3 Displaying Images,fchest=imread(Fig0203(a)(chest-xray).tif);subplot(1,2,1),imshow(fchest),subplot(1,2,2),imshow(fchest,),For example:,2.3 Displaying Images,frose=imread(Fig0206(a)(rose-original).tif);imshow(frose),Images are written to disk using function imwrite,which has the following basic syntax:将图像写入硬盘的函数是imwrite,其基本语法:imwrite(f,)For example,the following command writes f to a TIFF file anmed patient10_run1:例如,如下命令将f写入名为patient10_run1的TIFF格式的文件中。imwrite(f,patient10_run1,tif)or alternatively,imwrite(f,patient10_run1.tif),2.4 Writing Images,A more general imwrite syntax applicable only to JPEG images is:如果只针对JPEG图像,能适合的一般语法是:imwrite(f,quality,q)wher q is an integer between 0 and 100(the lower the number the higher the degradation due to JPEG compression)q是一个介于0到100的整数,其值越小,JPEG图像压缩的降质越厉害。In order to reduce storage and transmission time,it is important that the images be compressed as much as possible while not degrading their visual appearance beyond a reasonable level.为了减少图像文件的存贮容量和传输时间,重要的一点就是要将图像尽可能的压缩到合适的程度,以便不影响视觉效果。In this case“reasonable”means no perceptible false contouring.For example,for q=25 the applicable syntax is:本例中的合适程度,意思是指没有可感知到的伪轮廓。如q=25时的所用的语法是:imwrite(f,bubble25.jpg,quality,25),2.4 Writing Images,fbubble=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.4 Writing Images,imshow(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);,2.4 Writing Images,Original image,q=50,q=25,q=0,q=5,q=15,2.4 Writing Images,Original image,q=10,q=8,q=0,q=4,q=6,The toolbox supports four types of images:工具箱支持四种图像类型 Intensity images 灰度图像(或叫亮度图像)Binary images 二值图像(或叫二进值图像,或叫黑白图像)Indexed images 索引图像 RGB images RGB图像Most monochrome image processing operations are carried out using binary or intensity images,so our initial focus is on these two image types.大多数单色图像处理操作都是针对二值图像或灰度图像进行的。因此我们首先集中在这两类图像的研究上。,2.6 Image Types,实例说明,灰度图像,实例说明,灰度图像,实例说明,索引图像,实例说明,RGB图像,An intensity image is a data matrix whose values have been scaled to represent intensities.在MATLAB中,灰度图像是一个数值矩阵。它的值归一化到其所表示的图像。When the elements of an intensity image are of class uint8,or class uint16,they have integer values in the range 0,255 and 0,65535,respectively.当图像类型(即存贮各像素值的数据类型)是uint8或uint16时,其整数值的取值范围分别是0,255和0,65535。Values of scaled,class double intensity images are in the range 0,1 by convention.通过约定,归一化的双精度类型的灰度图像的数据取值范围是 0,1,2.6.1 Image Types,A binary image is a logical array of 0s and 1s.Thus,an array of 0s and 1s whose values are of data class,say,uint8,is not considered a binary image in MATLAB.二值图像是由0和1组成一个逻辑矩阵。其元素值是逻辑值。因此在MATLAB中,如果由数据类(而不是图像类)的0和1组成的矩阵,不能将其视为一个二值图像。注意,当0和1是数据类的逻辑值时,则可以将矩阵视为二值图像。A numeric array is converted to binary using function logical.Thus,if A is a numeric array consisting of 0s and 1s,we create a logical array B using the statement:数值矩阵可通过函数 logical转换成二值逻辑矩阵,因此,如果一个矩阵A由数值(非逻辑)0和1组成,我们可以使用如下声明来创建逻辑矩阵:B=logical(A)To test if an array is logical we use the islogical function:测试一个矩阵是否是一个逻辑型的,可以使用islogical函数:islogical(C)If C is a logical array,this function returns a 1.Otherwise it returns a 0.如果C是一个逻辑矩阵,则该函数返回1,否则返回0.,2.6.2 Binary Images,Image Types and Type Conversionsdither:Convert image,increasing apparent color resolution by dithering 通过抖动技术,多外观上增加灰度的等级。double:Convert data to double precision 将数据转换成双精度型。gray2ind:Convert grayscale or binary image to indexed image 将灰度图或二值图像转换成索引图。graythresh:Global image threshold using Otsus method 使用Otsu方法将图像进行全局阈值化处理成二值图像。im2bw:Convert image to binary image,based on threshold 使用阈值化方法,将图像转换成二值图像。im2double:Convert image to double precision 将图像转换成双精度型矩阵。im2int16:Convert image to 16-bit signed integers 将图像转换成int16型矩阵im2java:Convert image to Java image 将MATLAB图像转换成Java类型图像im2java2d:Convert image to Java buffered image 将MATLAB转换成Java buffered图像,2.7 Converting betwwen Data Classes and Image Types,im2single:Convert image to single precision 将图像转换成单精度矩阵im2uint16:Convert image to 16-bit unsigned integers 将图像转换成uint16型矩阵im2uint8:Convert image to 8-bit unsigned integers 将图像转换成uint8型矩阵,2.7 Converting betwwen Data Classes and Image Types,ind2gray:Convert indexed image to grayscale image 将图像转换成灰度图像ind2rgb:Convert indexed image to RGB image 将图像转换成rgb图像label2rgb:Convert label matrix into RGB image 将标签图像转换成rgb图像。mat2gray:Convert matrix to grayscale image 将数据值(非图像类型)的矩阵转换成灰度图像。rgb2gray:Convert RGB image or colormap to grayscale 将rgb图像转换成灰度图像rgb2ind:Convert RGB image to indexed image 将rgb图像转换成索引图像。uint16:Convert data to unsigned 16-bit integers 将数据转换成uint16类型uint8:Convert data to unsigned 8-bit integers 将数据转换成uint8类型Please turn to help in MATLAB:Image Processing ToolboxFunctions-By CategoryImage Display and ExporationImage Types and Type Conversions.,2.7 Converting betwwen Data Classes and Image Types,f=-0.5 0.5;0.75 1.5f=-0.5000 0.5000 0.7500 1.5000 g=im2uint8(f)g=0 128 191 255 mat2gray(f)ans=0 0.5000 0.6250 1.0000,A=0 0;1 1A=0 0 1 1 im2double(A)ans=0 0 1 1,lA=logical(A)lA=0 0 1 1 im2double(lA)ans=0 0 1 1 u8A=uint8(A)u8A=0 0 1 1 im2double(u8A)ans=0 0 0.0039 0.0039,