《数字图像处理实验的报告材料的三四五.doc》由会员分享,可在线阅读,更多相关《数字图像处理实验的报告材料的三四五.doc(26页珍藏版)》请在课桌文档上搜索。
1、实验三 图像的几何变换一实验目的与要求掌握图像几何变换的根本原理,熟练掌握数字图像的缩放、旋转、平移、镜像和转置的根本原理与其MATLAB编程实现方法。二、实验内容一研究以下程序,分析程序功能;输入执行各命令行,认真观察命令执行的结果。熟悉程序中所使用函数的调用方法,改变有关参数,观察试验结果。1. 图像缩放clear all, close allI = imread(cameraman.tif);Scale = 1.35; % 将图像放大1.35倍J1 = imresize(I, Scale, nearest); %using the nearest neighbor interpolati
2、onJ2 = imresize(I, Scale, bilinear); %using the bilinear interpolationimshow(I),title(Original Image);figure, imshow(J1), title(Resized Image- using the nearest neighbor interpolation );figure, imshow(J2), title(Resized Image- using the bilinear interpolation );% 查看imresize使用帮助help imresizemand窗口显示如
3、下:IMRESIZE Resize image. B = IMRESIZE(A, SCALE) returns an image that is SCALE times the size of A, which is a grayscale, RGB, or binary image. B = IMRESIZE(A, NUMROWS NUMCOLS) resizes the image so that it has the specified number of rows and columns. Either NUMROWS or NUMCOLS may be NaN, in which c
4、ase IMRESIZE putes the number of rows or columns automatically in order to preserve the image aspect ratio. Y, NEWMAP = IMRESIZE(X, MAP, SCALE) resizes an indexed image. Y, NEWMAP = IMRESIZE(X, MAP, NUMROWS NUMCOLS) resizes an indexed image. To control the interpolation method used by IMRESIZE, add
5、a METHOD argument to any of the syntaxes above, like this: IMRESIZE(A, SCALE, METHOD) IMRESIZE(A, NUMROWS NUMCOLS, METHOD), IMRESIZE(X, MAP, M, METHOD) IMRESIZE(X, MAP, NUMROWS NUMCOLS, METHOD) METHOD can be a string naming a general interpolation method: nearest - nearest-neighbor interpolation bil
6、inear - bilinear interpolation bicubic - cubic interpolation; the default method METHOD can also be a string naming an interpolation kernel: box - interpolation with a box-shaped kernel triangle - interpolation with a triangular kernel (equivalent to bilinear) cubic - interpolation with a cubic kern
7、el (equivalent to bicubic) lanczos2 - interpolation with a Lanczos-2 kernel lanczos3 - interpolation with a Lanczos-3 kernel Finally, METHOD can be a two-element cell array of the form f,w, where f is the function handle for a custom interpolation kernel, and w is the custom kernels width. f(x) must
8、 be zero outside the interval -w/2 = x w/2. Your function handle f may be called with a scalar or a vector input. You can achieve additional control over IMRESIZE by using parameter/value pairs following any of the syntaxes above. For example: B = IMRESIZE(A, SCALE, PARAM1, VALUE1, PARAM2, VALUE2, .
9、) Parameters include: Antialiasing - true or false; specifies whether to perform antialiasing when shrinking an image. The default value depends on the interpolation method you choose. For the nearest method, the default is false; for all other methods, the default is true. Colormap - (only relevant
10、 for indexed images) original or optimized; if original, then the output newmap is the same as the input map. If it is optimized, then a new optimized colormap is created. The default value is optimized. Dither - (only for indexed images) true or false; specifies whether to perform color dithering.
11、The default value is true. Method - As described above OutputSize - A two-element vector, MROWS NCOLS, specifying the output size. One element may be NaN, in which case the other value is puted automatically to preserve the aspect ratio of the image. Scale - A scalar or two-element vector specifying
12、 the resize scale factors. If it is a scalar, the same scale factor is applied to each dimension. If it is a vector, it contains the scale factors for the row and column dimensions, respectively. Examples - Shrink by factor of two using the defaults of bicubic interpolation and antialiasing. I = imr
13、ead(rice.png); J = imresize(I, 0.5); figure, imshow(I), figure, imshow(J) Shrink by factor of two using nearest-neighbor interpolation. (This is the fastest method, but it has the lowest quality.) J2 = imresize(I, 0.5, nearest); Resize an indexed image. X, map = imread(trees.tif); Y, newmap = imresi
14、ze(X, map, 0.5); imshow(Y, newmap) Resize an RGB image to have 64 rows. The number of columns is puted automatically. RGB = imread(peppers.png); RGB2 = imresize(RGB, 64 NaN); Note - The function IMRESIZE in previous versions of the Image Processing Toolbox used a somewhat different algorithm by defa
15、ult. If you need the same results produced by the previous implementation, call the function IMRESIZE_OLD. Class Support - The input image A can be numeric or logical and it must be nonsparse. The output image is of the same class as the input image. The input indexed image X can be uint8, uint16, o
16、r double. See also imresize_old, imrotate, imtransform, tformarray. Reference page in Help browser doc imresize执行程序所得结果如下:改变参数Scale =0.5得到图形结果如下:对以上实验结果,分析如下:通过查看命令窗口查看imresize函数的使用方法。本实验中利用了形式B = imresize(A,m,method)。实验中method采用了,nearest(默认值)最近邻插值 方法和bilinear双线性插值2. 图像旋转clear all, close allI = imre
17、ad(cameraman.tif);Theta = 45; %将图像逆时针旋转45。J1 = imrotate(I, Theta, nearest); %using the nearest neighbor interpolation%and enlarge the output imageTheta = -45; % 将图像顺时针旋转45。J2 = imrotate(I, Theta, bilinear,crop); %using the bilinear interpolation % and crops the output image imshow(I),title(Original
18、Image);figure, imshow(J1), title(Rotated Image- using the nearest neighbor interpolation );figure, imshow(J2), title( Rotated Image- using the bilinear interpolation );% 查看imrotate使用帮助help imrotatemand窗口显示如下:IMROTATE Rotate image. B = IMROTATE(A,ANGLE) rotates image A by ANGLE degrees in a countercl
19、ockwise direction around its center point. To rotate the image clockwise, specify a negative value for ANGLE. IMROTATE makes the output image B large enough to contain the entire rotated image. IMROTATE uses nearest neighbor interpolation, setting the values of pixels in B that are outside the rotat
20、ed image to 0 (zero). B = IMROTATE(A,ANGLE,METHOD) rotates image A, using the interpolation method specified by METHOD. METHOD is a string that can have one of the following values. The default value is enclosed in braces (). nearest Nearest neighbor interpolation bilinear Bilinear interpolation bic
21、ubic Bicubic interpolation. Note: This interpolation method can produce pixel values outside the original range. B = IMROTATE(A,ANGLE,METHOD,BBOX) rotates image A, where BBOX specifies the size of the output image B. BBOX is a text string that can have either of the following values. The default val
22、ue is enclosed in braces (). loose Make output image B large enough to contain the entire rotated image. B is generally larger than A. crop Make output image B the same size as the input image A, cropping the rotated image to fit. Class Support - The input image can be numeric or logical. The output
23、 image is of the same class as the input image. Example - % This example brings image I into horizontal alignment by % rotating the image by -1 degree. I = fitsread(solarspectra.fts); I = mat2gray(I); J = imrotate(I,-1,bilinear,crop); figure, imshow(I), figure, imshow(J) See also imcrop, imresize, i
24、mtransform, tformarray. Reference page in Help browser doc imrotate执行程序所得结果如下:改变参数,Theta = 135和-135时,所得结果如下:实验结果分析如下:通过查看命令窗口了解imrotate函数的使用。本实验中采用了函数的两种形式,B = imrotate(A,angle,method)和B = imrotate(A,angle,method,bbox)。实验中,method的设置与其原理同上个实验。实验中,bbox设置为了“crop,其作用是为了使输出图像和输入图像大小一样,可以看出当设置了该参数是,图像明显被裁
25、减了,这是因为图像旋转后面积变大了,而该参数的设置使图像须保持原来的大小i,因而图像被裁减了,未设置该参数时默认大小可以显示整个旋转后的图像。Angle为旋转角度,分别设置为了45和-45、135和-135,由上面两组图可以看出明显的效果和差异 3图像水平镜象clear all, close allI = 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,
26、1), imshow(I);subplot(2,1,2), imshow(I2);执行程序,所得结果如下:对实验结果分析如下:flipdim 函数的使用方法如下。B = flipdim(A,dim)沿指定的维翻转矩阵。当dim=1时,行翻转,等同于flipud ,当dim=2时,列翻转,等同于fliplr 。由上图可以看出翻转的效果。(二)用MATLAB编程实现以如下图像几何变换1图像平移程序代码如下:clc,clear all;I = imread(cameraman.tif);rows=size(I,1);cols=size(I,2);movx=50;movy=50;for i=1:row
27、s for j=1:cols Q(i+movx,j+movy)=I(i,j); endendfigure(1);subplot(121);imshow(I);title(origine picture);subplot(122);imshow(Q);title(modified picture);执行程序结果如下:实验分析如下:实验中,每个像素值以与其对应的坐标x和y都被平移了50,表现在整个图像上,即向右下角平移sqrt(50*50+50*50),显示结果如上图所示。2图像转置图像的转置是将给定图像像素的x坐标和y坐标互换的几何变换,设点P0(x0, y0) 转置后的对应点为P(x, y),
28、转置变换可表述为:或,对应的逆变换为:或转置后图像的宽、高发生改变,即输出图像的高度为原始图像的宽度,输出图像的宽度为原始图像的高度。 程序代码如下:clc,clear all;I = imread(cameraman.tif);rows=size(I,1);cols=size(I,2);for i=1:rows for j=1:cols Q(j,i)=I(i,j); endendsize(I),size(Q)figure(1);subplot(121);imshow(I);title(origine picture);subplot(122);imshow(Q);title(modified
29、 picture); 执行程序,所得结果如下:实验分析如下: 设图像中某个像素为p(j,i),如此其值为被p(i,j)被代替,其中p为整个图像的像素矩阵。对图像中的所有像素.逐列、逐行的进展此计算,即可实现转置。实验结果如上图所示,明显看出,转置后图像的宽、高发生改变,即输出图像的高度为原始图像的宽度,输出图像的宽度为原始图像的高度,整个图像被“转置了。三、实验设备1PIII以上微机;2MATLAB6.5;四、实验心得与体会实验四 图像形态学处理一实验目的与要求1利用MATLAB研究二值形态学图像处理常用算法;2掌握MATLAB形态学图像处理根本操作函数的使用方法;3了了解形态学的根本应用。二
30、、实验内容一研究以下程序,分析程序功能;输入执行各命令行,认真观察命令执行的结果。熟悉程序中所使用函数的调用方法,改变有关参数,观察试验结果。1膨胀与腐蚀(Dilation and Erosion)(1)对简单二值图像进展膨胀与腐蚀clear all, close allBW = zeros(9,10); BW(4:6,4:7) = 1;BW;SE = strel(square,3) BW1 = imdilate(BW,SE)BW2 = imerode (BW,SE)figure(1),subplot(1,2,1), imshow(BW,notruesize), title( Original
31、 Image );subplot(1,2,2), imshow(BW1,notruesize), title( Dilated Image );figure(2),subplot(1,2,1), imshow(BW,notruesize), title( Original Image );subplot(1,2,2), imshow(BW2,notruesize), title( Eroded Image );执行程序,所得结果如下:对实验结果分析如下:程序中采用结构元素对图像进展腐蚀或者膨胀。腐蚀操作,相当于结构元素的中心点沿被操作图像边缘内部走一圈,所有中心点的轨迹所包围的区域。膨胀操作,
32、相当于结构元素的中心点沿被操作图像边缘外部走一圈,所有中心点的轨迹所包围的区域。实验结果如上图所示,腐蚀以与膨胀的结果也非常明显。(2)对文本图像进展膨胀与腐蚀clear all, close allI = imread(text.tif);SE = 0,1,0;1,1,1;0,1,0BW1 = imdilate(I, SE);BW2 = imerode (I, SE);figure(1),subplot(1,2,1), imshow(I,notruesize), title( Original Image );subplot(1,2,2), imshow(BW1,notruesize), t
33、itle( Dilated Image );figure(2),subplot(1,2,1), imshow(I,notruesize), title( Original Image );subplot(1,2,2), imshow(BW2,notruesize) , title( Eroded Image );执行程序,结果如下:对实验结果分析如下:本实验中腐蚀与膨胀原理同上个实验,实验中用其对文本进展腐蚀,可以看出腐蚀与膨胀在文本处理中的作用。其中合理选择结构元素是其关键。由上图的处理效果可以看出,处理的效果并不是很理想,这是由于结构元素选择的并不是很适宜。2. 开、闭运算Open and
34、 Closeclear all, close allI = imread(nodules1.tif);bw = im2bw(I,graythresh(I);se = strel(disk,5);bw2 = imopen(bw,se);subplot(1,2,1), imshow(bw), title(Thresholded Image)subplot(1,2,2), imshow(bw2), title(After opening)bw3 = imclose(bw,se);figure;subplot(1,2,1), imshow(bw, truesize), title(Thresholde
35、d Image)subplot(1,2,2), imshow(bw3, truesize), title(After Closing)执行程序,结果如下:对以上实验结果分析如下:算和闭运算与腐蚀和膨胀操作有所不同,其具体差异表现在,算是先腐蚀后膨胀,闭运算是先膨胀后腐蚀。通俗的说,算与腐蚀操作不同的是,它包含了所有在被操作对象内部的结构元素,而腐蚀操作仅是包含中心点;闭运算也是类似与膨胀操作不同,它包含了所有与被操作对象有交集的结构元素,而膨胀操作仅是包含中心点。处理结果如上,可以看出物体与原图有所不同,算消除了一些小像素,而闭运算使图像边缘变得光滑了许多,消除了散枝。3. 击中/击不中变换h
36、it-and-miss operationclear all, close allbw = 0 0 0 0 0 0 0 0 1 1 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 0 1 1 0 0 0 0 1 0 0 0interval = 0 -1 -11 1 -10 1 0bw2 = bwhitmiss(bw,interval)subplot(1,3,1), imshow(bw,notruesize), title( Original Image );subplot(1,3,2), imshow(interval, truesize), title( Interval Ima
37、ge );subplot(1,3,3), imshow(bw2,notruesize) , title(after hit/miss transformation);执行程序,所得结果如下:对以上实验结果分析如下:击中击不中操作,是用结构元素与原图像进展比拟,可以把结构元素想象成一个模板,模板有一个中心点。用该中心点在原图像上扫描,能使模板完全重合的中心点保存下来,如上图实验结果所示,处理后的图像比原图像小了好多,这是因为只有符合结构元素的像素被保存了下来,即被击中的像素保存了下来。4细化与骨架抽取clear all, close allBW = imread(logo.tif);BW1 =
38、bwmorph(BW,thin,Inf);BW2 = bwmorph(BW,skel,Inf);subplot(1,3,1), imshow(BW), title( Original Image );subplot(1,3,2), imshow(BW1), title( Thinned Image );subplot(1,3,3), imshow(BW2), title( Image skeleton);%查看bwmorph函数使用说明help bwmorphmand窗口显示如下:BWMORPH Morphological operations on binary image. BW2 = B
39、WMORPH(BW1,OPERATION) applies a specific morphological operation to the binary image BW1. BW2 = BWMORPH(BW1,OPERATION,N) applies the operation N times. N can be Inf, in which case the operation is repeated until the image no longer changes. OPERATION is a string that can have one of these values: bo
40、that Subtract the input image from its closing bridge Bridge previously unconnected pixels clean Remove isolated pixels (1s surrounded by 0s) close Perform binary closure (dilation followed by erosion) diag Diagonal fill to eliminate 8-connectivity of background dilate Perform dilation using the structuring element ones(3) erode Perform erosion using the structuring element ones(3) fill Fill isolated interior pixels (0s surrounded by 1s) hbreak Remove H-connected pixels majority Set a pi