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

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

    本文涉及到的知识点以下:google

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

     二、细化操做。 im=bwmorph(image,'thin',Inf);.net

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

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

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

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

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

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

一、提取边缘。

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

三、进行细化操做,减少计算量。

四、用多边形近似边界。

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

转载请注明出处: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 all
src = imread('basic_shapes.png');

figure('name','原始图像'),
imshow(src),title('src'),

%Convert to grayscale
gray=rgb2gray(src); gray = im2double(gray);

%Convert to binary image using Canny 
bw = edge(gray,'canny',[0 , 50/256]);

%dilate
dilateElement=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('圆');
    end
end


 

运行效果以下:



源图像已上传:

相关文章
相关标签/搜索