实验二图像分割与边缘检测一.实验目的及要求1.利用MATLAB研究图像分割与边缘检测的常用算法原理;2.掌握MATLAB图像域值分割与边缘检测函数的使用方法;3.了解边缘检测的算法和用途,比较Sobel、Prewitt、Canny等算子边缘检测的差异。
二、实验内容(一)研究以下程序,分析程序功能;输入执行各命令行,认真观察命令执行的结果。
熟悉程序中所使用函数的调用方法,改变有关参数,观察试验结果。
1.图像阈值分割clear all, close all;I = imread('rice.tif');figure (1),imshow(I)figure(2); imhist(I)T=120/255;Ibw1 = im2bw(I,T); %选择阈值T=120/255对图像二值化;figure(3);subplot(1,2,1), imshow(Ibw1);T=graythresh(I); %采用Otsu方法计算最优阈值T对图像二值化;L = uint8(T*255)Ibw2 = im2bw(I,T);subplot(1,2,2), imshow(Ibw2);help im2bw;help graythresh;(令T取不同值,重做上述试验,观察试验结果)以下是程序执行结果:Comand窗口:L =125IM2BW Convert image to binary image by thresholding.IM2BW produces binary images from indexed, intensity, or RGBimages. To do this, it converts the input image to grayscaleformat (if it is not already an intensity image), and thenconverts this grayscale image to binary by thresholding. Theoutput binary image BW has values of 0 (black) for all pixelsin the input image with luminance less than LEVEL and 1(white) for all other pixels. (Note that you specify LEVEL inthe range [0,1], regardless of the class of the input image.)BW = IM2BW(I,LEVEL) converts the intensity image I to blackand white.BW = IM2BW(X,MAP,LEVEL) converts the indexed image X withcolormap MAP to black and white.BW = IM2BW(RGB,LEVEL) converts the RGB image RGB to black andwhite.Note that the function GRAYTHRESH can be used to compute LEVELautomatically.Class Support-------------The input image can be of class uint8, uint16, or double.The output image BW is of class uint8.Example-------load treesBW = im2bw(X,map,0.4);imshow(X,map), figure, imshow(BW)See also GRAYTHRESH, IND2GRAY, RGB2GRAY.GRAYTHRESH Compute global image threshold using Otsu's method.LEVEL = GRAYTHRESH(I) computes a global threshold (LEVEL) that can be used to convert an intensity image to a binary image with IM2BW. LEVELis a normalized intensity value that lies in the range [0, 1].GRAYTHRESH uses Otsu's method, which chooses the threshold to minimize the intraclass variance of the thresholded black and white pixels.Class Support-------------The input image I can be of class uint8, uint16, or double. LEVELis a double scalar.Example-------I = imread('blood1.tif');level = graythresh(I);BW = im2bw(I,level);imshow(BW)See also IM2BW.下面是T取不同值时的所得的结果:T=60时:原图像原图像的灰度直方图T=60时分割的结果Otsu方法分割的结果T=120时:原图像原图像的灰度直方图T=120时分割的结果Otsu方法分割的结果T=200时:原图像原图像的灰度直方图T=120时分割的结果Otsu方法分割的结果对以上实验结果分析如下:由matalab命令窗口显示的内容可知,不同的阈值分割的结果并不一样。
由灰度直方图可以知道,可以利用双峰发对图像进行分割,即认为背景由前景和背景组成,图像的灰度分布曲线可近似认为是有两个正态分布函数叠加而成,图像的直方图将会出现两个分离的峰值,在波谷处即为最佳阈值。
所以在实验结果中,T=120时分割的结果要明显好于另外两个分割结果,因为其更接近“波谷值”。
graythresh函数是用大津法计算全局图像的阈值。
大津法是采用方差最大的方法区分前景和背景。
方差是灰度分布均匀的度量,其原理是;如果某个阈值使背景和前景像素相互参杂,那么图像的图像灰度像素肯定较为均匀,只有前景和背景分离开才会使均匀性最差,即方差最大。
2.边缘检测clear all, close all;I = imread('rice.tif');BW1 = edge(I,'sobel');BW2 = edge(I,'canny');BW3 = edge(I,'prewitt');BW4 = edge(I,'roberts');BW5 = edge(I,'log');figure(1), imshow(I), title('Original Image');figure(2), imshow(BW1), title('sobel');figure(3), imshow(BW2), title('canny');figure(4), imshow(BW3), title('prewitt');figure(5), imshow(BW4), title('roberts');figure(6), imshow(BW5), title('log');% 在完成上述试验后,查看函数edge()使用说明。
help edge% 仔细阅读函数edge()使用说明后,研究IPT提供的边缘检测演示程序。
edgedemo下面是程序执行的结果:command窗口:help edgeEDGE Find edges in intensity image.EDGE takes an intensity image I as its input, and returns a binary image BW of the same size as I, with 1's where the function finds edges in Iand 0's elsewhere.EDGE supports six different edge-finding methods:The Sobel method finds edges using the Sobel approximation to thederivative. It returns edges at those points where the gradient ofI is maximum.The Prewitt method finds edges using the Prewitt approximation tothe derivative. It returns edges at those points where the gradientof I is maximum.The Roberts method finds edges using the Roberts approximation tothe derivative. It returns edges at those points where the gradientof I is maximum.The Laplacian of Gaussian method finds edges by looking for zerocrossings after filtering I with a Laplacian of Gaussian filter.The zero-cross method finds edges by looking for zero crossingsafter filtering I with a filter you specify.The Canny method finds edges by looking for local maxima of thegradient of I. The gradient is calculated using the derivative of aGaussian filter. The method uses two thresholds, to detect strongand weak edges, and includes the weak edges in the output only ifthey are connected to strong edges. This method is therefore lesslikely than the others to be "fooled" by noise, and more likely todetect true weak edges.The parameters you can supply differ depending on the method youspecify. If you do not specify a method, EDGE uses the Sobel method.Sobel Method------------BW = EDGE(I,'sobel') specifies the Sobel method.BW = EDGE(I,'sobel',THRESH) specifies the sensitivity threshold forthe Sobel method. EDGE ignores all edges that are not stronger than THRESH. If you do not specify THRESH, or if THRESH is empty ([]), EDGE chooses the value automatically.BW = EDGE(I,'sobel',THRESH,DIRECTION) specifies directionality for the Sobel method. DIRECTION is a string specifying whether to look for'horizontal' or 'vertical' edges, or 'both' (the default).[BW,thresh] = EDGE(I,'sobel',...) returns the threshold value.Prewitt Method--------------BW = EDGE(I,'prewitt') specifies the Prewitt method.BW = EDGE(I,'prewitt',THRESH) specifies the sensitivity threshold forthe Prewitt method. EDGE ignores all edges that are not stronger than THRESH. If you do not specify THRESH, or if THRESH is empty ([]), EDGE chooses the value automatically.BW = EDGE(I,'prewitt',THRESH,DIRECTION) specifies directionality for the Prewitt method. DIRECTION is a string specifying whether to lookfor 'horizontal' or 'vertical' edges, or 'both' (the default).[BW,thresh] = EDGE(I,'prewitt',...) returns the threshold value.Roberts Method--------------BW = EDGE(I,'roberts') specifies the Roberts method.BW = EDGE(I,'roberts',THRESH) specifies the sensitivity threshold forthe Roberts method. EDGE ignores all edges that are not stronger than THRESH. If you do not specify THRESH, or if THRESH is empty ([]), EDGE chooses the value automatically.[BW,thresh] = EDGE(I,'roberts',...) returns the threshold value.Laplacian of Gaussian Method----------------------------BW = EDGE(I,'log') specifies the Laplacian of Gaussian method.BW = EDGE(I,'log',THRESH) specifies the sensitivity threshold for the Laplacian of Gaussian method. EDGE ignores all edges that are not stronger than THRESH. If you do not specify THRESH, or if THRESH is empty ([]), EDGE chooses the value automatically.BW = EDGE(I,'log',THRESH,SIGMA) specifies the Laplacian of Gaussian method, using SIGMA as the standard deviation of the LoG filter. The default SIGMA is 2; the size of the filter is N-by-N, whereN=CEIL(SIGMA*3)*2+1.[BW,thresh] = EDGE(I,'log',...) returns the threshold value.Zero-cross Method-----------------BW = EDGE(I,'zerocross',THRESH,H) specifies the zero-cross method, using the specified filter H. If THRESH is empty ([]), EDGE choosesthe sensitivity threshold automatically.[BW,THRESH] = EDGE(I,'zerocross',...) returns the threshold value.Canny Method----------------------------BW = EDGE(I,'canny') specifies the Canny method.BW = EDGE(I,'canny',THRESH) specifies sensitivity thresholds for the Canny method. THRESH is a two-element vector in which the first element is the low threshold, and the second element is the high threshold. Ifyou specify a scalar for THRESH, this value is used for the highthreshold and 0.4*THRESH is used for the low threshold. If you do not specify THRESH, or if THRESH is empty ([]), EDGE chooses low and high values automatically.BW = EDGE(I,'canny',THRESH,SIGMA) specifies the Canny method, using SIGMA as the standard deviation of the Gaussian filter. The default SIGMA is 1; the size of the filter is chosen automatically, basedon SIGMA.[BW,thresh] = EDGE(I,'canny',...) returns the threshold values as atwo-element vector.Class Support-------------I can be of class uint8, uint16, or double. BW is of class uint8.Remarks-------For the 'log' and 'zerocross' methods, if you specify athreshold of 0, the output image has closed contours, becauseit includes all of the zero crossings in the input image.Example-------Find the edges of the rice.tif image using the Prewitt and Canny methods:I = imread('rice.tif');BW1 = edge(I,'prewitt');BW2 = edge(I,'canny');imshow(BW1)figure, imshow(BW2)See also FSPECIAL.实验结果分析如下:以上是不同的算子对图像的分割结果。