




已阅读5页,还剩17页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1实验三 图像分割与边缘检测上课老师:赵欢喜 实验指导:吴磊 实验地点:科技楼 4楼计算机实验中心 时间:2010.12.14一实验目的1. 理解图像分割的基本概念;2. 理解图像边缘提取的基本概念;3. 掌握进行边缘提取的基本方法;4. 掌握用阈值法进行图像分割的基本方法。二实验基本原理图象边缘检测图像理解是图像处理的一个重要分支,研究为完成某一任务需要从图像中提取哪些有用的信息,以及如何利用这些信息解释图像。边缘检测技术对于处理数字图像非常重要,因为边缘是所要提取目标和背景的分界线,提取出边缘才能将目标和背景区分开来。在图像中,边界表明一个特征区域的终结和另一个特征区域的开始,边界所分开区域的内部特征或属性是一致的,而不同的区域内部的特征或属性是不同的,边缘检测正是利用物体和背景在某种图像特性上的差异来实现的,这些差异包括灰度,颜色或者纹理特征。边缘检测实际上就是检测图像特征发生变化的位置。图象边缘检测必须满足两个条件:一能有效地抑制噪声;二必须尽量精确确定边缘的位置由于噪声和模糊的存在,检测到的边界可能会变宽或在某些点处发生间断,因此,边界检测包括两个基本内容:首先抽取出反映灰度变化的边缘点,然后剔除某些边界点或填补边界间断点,并将这些边缘连接成完整的线。边缘检测的方法大多数是基于方向导数掩模求卷积的方法。导数算子具有突出灰度变化的作用,对图像运用导数算子,灰度变化较大的点处算得的值比较高,因此可将这些导数值作为相应点的边界强度,通过设置门限的方法,提取边界点集。一阶导数与是最简单的导数算子,它们分别求出了灰度在 x 和 y 方向上的变化率,而方向 上的灰度变化率可以用相应公式进行计算;对2于数字图像,应该采用差分运算代替求导。一幅数字图像的一阶导数是基于各种二维梯度的近似值。图像 f(x,y)在位置(x,y)的梯度定义为下列向量:(3-4)yfxfG),(在边缘检测中,一般用这个向量的大小,用 表示f(3-5)2/12f函数 f 在某点的方向导数取得最大值的方向是,方向导数的最大值是称为梯度模。利用梯度模算子来检测边缘是一种很好的方法,它不仅具有位移不变性,还具有各向同性。为了运算简便,实际中采用梯度模的近似形式。或者 |fGxymax(|,|)fGy传统的边缘检测算法通过梯度算子来实现的,在求边缘的梯度时,需要对每个象素位置计算。在实际中常用小区域模板卷积来近似快速计算,简单有效,即梯度算子一般采用滤波算子的形式来完成,因此应用很广泛。模板是 N*N的权值方阵,经典的梯度算子模板有:Sobel 模板、Prewitt模板、Roberts 模板、Laplacian 模板等。具体模板请见书。拉普拉斯高斯(LoG)算法是一种二阶边缘检测方法。它通过寻找图像灰度值中二阶微分中的过零点(Zero Crossing)来检测边缘点。其原理为,灰度级变形成的边缘经过微风算子形成一个单峰函数,峰值位置对应边缘点;对单峰函数进行微分,则峰值处的微分值为 0,峰值两侧符号相反,而原先的极值点对应二阶微分中的过零点,通过检测过零点即可将图像的边缘提取出来。(a)原图 (b)边缘检测后的图 (c) 阈值处理后的图图 3-1 检测具有-45 度边缘的图例1点检测3原理:常数灰阶区域中的某孤立点对某种模板的响应绝对值肯定是最强烈的。最常用的模板有:Matlab 实现方法:g = abs(imfilter(double(f),w) = T; where w is a appropriate point detection mask which satisfies the above condition.实例:the detection of isolated bright point in the dark gray area of the northeast quadrant.(image size: 675*675)the original image the isolated point detected (only a part displayed)实例代码:f=imread(Fig1002(a)(test_pattern_with_single_pixel).tif);w = -1 -1 -1; -1 8 -1; -1 -1 -1;g = abs(imfilter(double(f), w);T = max(g(:);g = g= T;subplot(121);imshow(f);title(the original image);subplot(122);imshow(g(1:end-400, 400:end);title(the isolated point detected (only a part displayed);=2线 (通常假定一个象素厚度) 检 测 4原理与上同,典型模板有(主要方向性):实例:-45 0 方向线的检测:the original image results from the -45 detecting Zoomed view of the top, left region of (b)Zoomed view of the bottom, right region of (b) absolute value of (b) -45 line detected3边沿检测方法:使用一阶或者二阶导数。对一节导数,关键问题是怎样估计水平和垂直方向的梯度 Gx 和Gy,二阶导数通常使用 Laplacian 算子计算,但是 Laplacian 算子很少单独用来检测边缘,因为其对噪声非常敏感,而且其结果会产生双边沿,加大了边缘检测的困难。然而,如果 Laplacian 算子能与其他边缘检测算法相结合,如边缘定位算法,则其是一个强有力的补充。通常两个标准用来测度图像强度的迅速变化:(1) 找出强度的一阶导数值大于某个事先阈值标准的位置;(2) 找出图像二阶导数的跨零点。IPT 工具箱函数 edge 提供了几种基于上面两种标准的估计器:5其语法为:g, t = edge(f, method, parameters);这里 method 参数包括这几种类型的边缘检测子:Sobel, Prewitt, Roberts, Laplacian of a Gaussian (LoG), Zero crossings and Canny,前三种的模板见下图:另一个强有力的边缘检测器:Canny Edge Detector (Canny 1986),其算法的基本步骤如下:(1) First, the image is smoothed using a Gaussian filter with a specified standard deviation (2) The local gradient, g(x, y) = Gx2+Gy21/2, and edge direction, (x, y) = tan-1(Gy /Gx), are computed at each point. Any of the first three techniques can be used to computer the Gx and Gy. An edge point is defined to be a point whose strength is locally maximum in the direction of the gradient.(3) The edge points give rise to ridges in the gradient magnitude image. The algorithm then tracks along the top of these ridges and sets to zero all pixels that are not actually on the ridge top so as to give a thin line, a process known as nonmaximal suppression. The ridge pixels are the thresholded using thresholds, T1 and T2, with T1 T 结果为一个逻辑值矩阵,I 中大于 T 的值对应的位置为 1(真) ,其余位置为 0(假) 。I(I T) 表示 I 中所有大于 T 的值组成的向量。255* (IT) + 0*(I=T练习 1f=imread(moon.tif);w=-1 -1 -1; -1 8 -1; -1 -1 -1;g=abs(imfilter(double(f), w);T=max(g(:);T=T*0.5;g=g=T;imshow(f); figure, imshow(g);1.1.2 线检测水平模板、+45 度模板、垂直模板、-45 度模板。练习 2f=imread(circbw.tif);imshow(f);w=2 -1 -1; -1 2 -1; -1 -1 2;g=abs(imfilter(double(f), w);figure,imshow(g);1.1.3 使用 edge 函数的边缘检测语法:g,t=edge(f, method, parameter)说明:g 是一个逻辑数组,其值为:在 f 中检测到边缘的11位置为 1,其他位置为零;t 是 edge 是用的阈值;method 为边缘监测器方法,可选为: sobel, prewit, roberts, log(LoG), zerocoss, canny等;parameter 包含两部分:T 为指定的阈值,第二部分为 dir(检测边缘的首选方向: horizontal, vertical, both) ,或 sigma(标准方差) ,或 H(指定的滤波函数) 。练习 3f=imread(rice.tif);imshow(f);gsobel,t=edge(f, sobel);figure, imshow(gsobel);glog,t=edge(f, log);figure, imshow(glog);gcanny,t=edge(f, canny);figure, imshow(gcanny);22 使用 Hough 变换的线检测练习 4设计与实现一个基于 Hough 变换的直线检测器。23 阈值处理231 全局阈值处理语法:T=graythresh(f)说明:T 是阈值,归一化为 0 至 1 之间的值。232 局部阈值处理通过一个形态学顶帽算子并对得到的结果使用 graythresh来计算。练习 5f=imread(moon.tif);imshow(f);T=graythresh(f);g=f=T;figure, imshow(g);1224 基于区域的分割241 区域生长 242 区域分裂和合并练习 6设计与实现一个基于区域生长的分割程序。25 使用分水岭变换的分割练习 7f=imread(cell.tif);imshow(f);g=im2bw(f, graythresh(f);figure,imshow(g);gc=g;D=bwdist(gc);L=watershed(-D);w=L= =0;g2=gfigure,imshow(g2);26 分割后处理语法:BW2 = bwfill(BW1,c,r,n)说明:填充二进制图像的背景色。(形态学处理)练习 8BW1 =1 0 0 0 0 0 0 01 1 1 1 1 0 0 01 0 0 0 1 0 1 01 0 0 0 1 1 1 01 1 1 1 0 1 1 11 0 0 1 1 0 1 01 0 0 0 1 0 1 01 0 0 0 1 1 1 0BW2 = bwfill(BW1,3,3,8)I = imread(blood1.tif);BW3 = im2bw(I);BW4 = bwfill(BW3,holes);13imshow(BW3)figure, imshow(BW4)语法:bwareaopen说明:二进制图像区域打开,清除小物体。五. 部分参考程序和参考结果1房屋轮廓描绘代码:f = imread(Fig1006(a)(building).tif);gv, t = edge(f, sobel, vertical); % using threshold computed automatically, here t = 0.0516subplot(231);imshow(f, );title(the original image);subplot(232);imshow(gv, );title(vertical edge with threshold determined automatically);gv1 = edge(f, sobel, 0.15, vertical); % using a specified threshold.subplot(233);imshow(gv1, );title(vertical edge with a specified threshold);gboth = edge(f, sobel, 0.15); % edge detection of two directionssubplot(234);imshow(gboth, );title(horizontal and vertical edge);% edge detection of 450 direction using imfilter functionw45 = -2 -1 0; -1 0 1; 0 1 2;14g45 =imfilter(double(f), w45, replicate);T = 0.3*max(abs(g45(:);g45 = g45 = T;subplot(235);imshow(g45, );title(edge at 45 with imfilter);wm45 = 0 1 2; -1 0 1; -2 -1 0;g45 =imfilter(double(f), wm45, replicate);T = 0.3*max(abs(g45(:);g45 = g45 = T;subplot(236);imshow(g45, );title(edge at -45 with imfilter);the original image vertical edge with threshold determined automatically vertical edge with a specified thresholdhorizontal and vertical edge edge at 45 with imfilter edge at -45 with imfilter另一个实验:为比较三种检测方法的相对性能:Sobel, LoG 和 Canny edge detectors,和为了改善检测效果所需使用的技巧。% using the default thresholdf = imread(Fig1006(a)(building).tif);gs_default, ts = edge(f, sobel); % ts = 0.074gl_default, tl = edge(f, log); % tl = 0.002 and the default sigma = 0.2gc_default, tc = edge(f, canny); % tc = 0.0189 0.047 and the default 15sigma = 0.1% using the optimal threshold acquired by manual testgs_best = edge(f, sobel, 0.05);gl_best = edge(f, log, 0.003, 2.25);gc_best = edge(f, canny, 0.04 0.1, 1.5);Sobel operator with deafult threshold17LoG operator with deafult thresholdcany operator with deafult threshold18The left column in above figure shows the edge images obtained using the default syntax for the sobel, log and canny operator respectively, whereas the right column are the results using optimal threshold and sigma values obtained by try.2Hough 变换用于线检测从而增强边缘的连续性2.1 Hough transform for peak detectionPeak detection is the first step in using Hough transform for line detection and linking. However, finding a meaningful set of distinct peaks in a Hough transform can be challenging. Because of the quantization in space of the digital image and in parameter space of the Hough transform, as well as the fact that edges in typical images are not perfectly straight, Hough transform peaks tend to lie in more than one Hough transform cell. One strategy to overcome this problem is following:(1) find the HT cell containing the highest value and record its location;(2) suppress (set to zero) HT cells in the immediate neighborhood of the maximum;(3) repeat until the desired number of peaks has been found, or until a specified threshold has been reached.function r, c, hnew = houghpeaks(h, numpeaks, threshold, nhood)%HOUGHPEAKS Detect peaks in Hough transform.% R, C, HNEW = HOUGHPEAKS(H, NUMPEAKS, THRESHOLD, NHOOD) detects19% peaks in the Hough transform matrix H. NUMPEAKS specifies the% maximum number of peak locations to look for. Values of H below% THRESHOLD will not be considered to be peaks. NHOOD is a% two-element vector specifying the size of the suppression% neighborhood. This is the neighborhood around each peak that is% set to zero after the peak is identified. The elements of NHOOD% must be positive, odd integers. R and C are the row and column% coordinates of the identified peaks. HNEW is the Hough transform% with peak neighborhood suppressed. % If NHOOD is omitted, it defaults to the smallest odd values =% size(H)/50. If THRESHOLD is omitted, it defaults to% 0.5*max(H(:). If NUMPEAKS is omitted, it defaults to 1. =2.2 HT for line detection and linkingFor each peak, the first step is to find the location of all nonzero pixels in the image that contributed to that peak. This purpose can be implemented by the following function:function r, c = houghpixels(f, theta, rho, rbin, cbin)%HOUGHPIXELS Compute image pixels belonging to Hough transform bin.% R, C = HOUGHPIXELS(F, THETA, RHO, RBIN, CBIN) computes the% row-column indices (R, C) for nonzero pixels in image F that map% to a particular Hough transform bin, (RBIN, CBIN). RBIN and CBIN% are scalars indicating the row-column bin location in the Hough% transform matrix returned by function HOUGH. THETA and RHO are% the second and third output arguments from the HOUGH function.x, y, val = find(f);x = x - 1; y = y - 1;theta_c = theta(cbin) * pi / 180;rho_xy = x*cos(theta_c) + y*sin(theta_c);nrho = length(rho);slope = (nrho - 1)/(rho(end) - rho(1);rho_bin_index = round(slope*(rho_xy - rho(1) + 1);idx = find(rho_bin_index = rbin);20r = x(idx) + 1; c = y(idx) + 1;The pixels associated with the locations found using houghpixles must be grouped into line segments, which is programmed into the following function:function lines = houghlines(f,theta,rho,rr,cc,fillgap,minlength)%HOUGHLINES Extract line segments based on the Hough transform.% LINES = HOUGHLINES(F, THETA, RHO, RR, CC, FILLGAP, MINLENGTH)% extracts line segments in the image F associated with particular% bins in a Hough transform. THETA and RHO are vectors returned by% function HOUGH. Vectors RR and CC specify the rows and columns% of the Hough transform bins to use in searching for line% segments. If HOUGHLINES finds two line segments associated with% the same Hough transform bin that are separated by less than% FILLGAP pixels, HOUGHLINES merges them into a single line% segment. FILLGAP defaults to 20 if omitted. Merged line% segments less than MINLENGTH pixels long are discarded.% MINLENGTH defaults to 40 if omitted. % LINES is a structure array whose length equals the number of% merged line segments found. Each element of the structure array% has these fields: % point1 End-point of the line segment; two-element vector% point2 End-point of the line segment; two-element vector% length Distance between point1 and point2% theta Angle (in degrees) of the Hough transform bin% rho Rho-axis position
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2022年2月韶关市直遴选面试真题带答案详解
- 2022年11月三明市直遴选面试真题带详解
- 2014护士试题及答案
- 工作表现评语简笔女生
- 4月13日CMA考试试题及答案
- 2025民间借贷借款合同范本2
- iTrans供应链管理系统简介
- 2025湖北省商品供销合同
- 家庭农场农产品生产销售协议
- 2025大连市鲜活农产品购销合同范文
- 五下音乐测试题及答案
- 考评员考试题及答案
- 中医骨科试题及答案
- 2024年湖南省普通高中学业水平合格性考试地理试题(原卷版)
- 2025春季学期国开电大本科《理工英语3》一平台在线形考综合测试(形考任务)试题及答案
- 细致解读wps考试内容的试题及答案
- 资质买卖居间协议书
- 数据可视化与Python试题及答案
- 2025年儿童心理发展考试试题及答案
- 2025时政试题及答案(100题)
- 新22J01 工程做法图集
评论
0/150
提交评论