Matlab图像处理学习笔记(四):多边形检测

来源:互联网 发布:iu脸型数据 编辑:程序博客网 时间:2024/05/16 07:27

    本文用matlab实现了基本多边形的检测、提取。

    本文涉及到的知识点如下:

     1、Canny边缘检测。 bw = edge(gray,'canny',[0 , 50/256]);

     2、细化操作。 im=bwmorph(image,'thin',Inf);

     3、边界追踪。 edgelist=bwboundaries(im);

     4、边界的多边形近似。linefit_Prasad_RDP_opt(edgelist);

    本文算法思路借鉴了Nash的博客,地址:http://opencv-code.com/tutorials/detecting-simple-shapes-in-an-image/点击打开链接

      边界的多边形近似算法为:Douglas-Peucker algorithm,算法的matlab实现我引用了Dilip K. Prasad分享的文件。本文所有操作的理论基础均可在冈萨雷斯的《数字图像处理》中找到答案。

   Douglas-Peucker algorithm地址:https://docs.google.com/file/d/0B10RxHxW3I92dG9SU0pNMV84alk/edit?pli=1点击打开链接

算法实现过程可分为以下几步。

1、提取边缘。

2、进行形态学处理,分割图像。

3、进行细化操作,减小计算量。

4、用多边形近似边界。

5、判断该顶点是否有效。(根据相邻顶点之间的距离)

转载请注明出处:http://blog.csdn.net/u010278305

下面给出源代码:

%function:%       基于最小距离分类器的模板匹配%       寻找图片中与已知模板的匹配区域%       程序中调用了Dilip K. Prasad对Ramer–Douglas–Peucker algorithm的matlab实现%referrence:%      思路借鉴:http://opencv-code.com/tutorials/detecting-simple-shapes-in-an-image/%      Ramer–Douglas–Peucker algorithm:http://en.wikipedia.org/wiki/Ramer%E2%80%93Douglas%E2%80%93Peucker_algorithm%date:2015-1-10%author:chenyanan%转载请注明出处:http://blog.csdn.net/u010278305%清空变量,读取图像clear;close allsrc = imread('basic_shapes.png');figure('name','原始图像'),imshow(src),title('src'),%Convert to grayscalegray=rgb2gray(src); gray = im2double(gray);%Convert to binary image using Canny bw = edge(gray,'canny',[0 , 50/256]);%dilatedilateElement=strel('square', 5);bw=imdilate(bw, dilateElement);%提取每个连通区域stats = regionprops(bw, 'Image');statssize= numel(stats);plotsize=ceil(sqrt(statssize));figure('name','分离结果'),num=zeros(statssize,1);%算法核心for i=1:statssize    image=stats(i).Image;    %进行细化操作    im=bwmorph(image,'thin',Inf);    % getting the edge data.     edgelist=bwboundaries(im);edgelist=edgelist.';    % calling linefit_Prasad_RDP_opt    [edgelist,seglist,precision_list,reliability_list,precision_edge,reliability_edge, time_edge] = linefit_Prasad_RDP_opt(edgelist);    boundnum=length(seglist{1}(:,:));    bound=0;    sizepic=sum(size(im));    %判断每个顶点之间的间距是否符合要求    for j=1:boundnum-1        cornerdiff=seglist{1}(j,:)-seglist{1}(j+1,:);        cornerdiff=sqrt(sum(cornerdiff.^2));        if(cornerdiff>0.09*sizepic)            bound=bound+1;        end    end    num(i)=bound;    %进行绘图并标识    subplot(plotsize,plotsize,i);imshow(image),    if bound<7        title(bound);    else        title('圆');    endend 

运行效果如下:



源图像已上传:



0 0
原创粉丝点击