• 大小: 5KB
    文件类型: .m
    金币: 1
    下载: 0 次
    发布日期: 2021-05-16
  • 语言: Matlab
  • 标签: function  %  

资源简介

function B=boundaries(BW,conn,dir) %输入二值图像,跟踪二值目标轮廓

资源截图

代码片段和文件信息

function B=boundaries(BWconndir)%目标的边缘跟踪
%BOUNDARIES Trace object boundaries.
%B=BOUNDARIES(BW) traces the exterior boundaries of objects in the binary
%image BW. B is a P-by-1 cell array where P is the number of objects in
%the image. Each cell contains a Q-by-2 matrix each row of which contains
%the row and column coordinates of a boundary pixel. Q is the number of
%boundary pixels for the corresponding object. object boundaries are traced
%in the clockwise direction.
%
%B=BOUNDARIES(BWCONN) specifies the connectivity to use when tracing
%boundaries. CONN may be either 8 or 4. The default value for CONN is 8.
%
%B=BOUNDARIES(BWCONNDIR) specifies the direction used for tracing
%boundaries. DIR should be either ‘cw‘(trace boundaries clockwise) or
%‘ccw‘(trace boundaries counterclockwise). If DIR is omitted BOUNDARIES
%traces in the clockwise direction.

if nargin<3
    dir=‘cw‘;
end
if nargin<2
    conn=8;
end
L=bwlabel(BWconn);
%The number of objects is the maximum value of L. Initializethe cell array
%B so that each cell initially contains a 0-by-2 matrix.
numobjects=max(L(:));
if numobjects>0
    B={zeros(02)};
    B=repmat(Bnumobjects1);
else
    B={};
end
%Pad lable matrix with zeros. This lets us write the boundary-following
%loop without worrying about going off the edge of the image.
Lp=padarray(L[1 1]0‘both‘);
%Compute the linear indexing offsets to take us from a pixel to its
%neighbors.
M=size(Lp1);
if conn==8
    %Order is N NE E SE S SW W NW.
    offsets=[-1M-1MM+11-M+1-M-M-1];
else 
    %Order is N E S W.
    offsets=[-1M1-M];
end
%next_search_direction_lut is a lookup table. Given the direction from pixel k to pixel k+1 what is the direction to start with when examining the neighborhood of pixel k+1?
if conn==8
    next_search_direction_lut = [8 8 2 2 4 4 6 6];
else 
    next_search_direction_lut= [4 1 2 3];
end
%next_direction_lut is a lookup table. Given that we just looked at
%neighbor in a biven direction which neighbor do we look at next?
if conn==8
    next_direction_lut = [2 3 4 5 6 7 8 1];
else 
    next_direction_lut= [2 3 4 1];
end
%Values used for marking the starting and boundary pixels.
START   = -1;
BOUNDARY= -2;
%initialize scratch space in which to record the boundary pixels as well as
%follow the boundary.
scratch =zeros(1001);
%Find candidate starting locations for boundaries.
[rrcc]=find((Lp(2:end-1:)>0)&(Lp(1:end-2:)==0));
rr=rr+1;
for k=1:length(rr)
    r=rr(k);
    c=cc(k);
    if (Lp(rc)>0)&(Lp(r-1c)==0)&isempty(B{Lp(rc)})
        %We‘ve found the start of the next boundary. Compute its linear
        %offset r

评论

共有 条评论