轮廓跟踪
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
轮廓跟踪
(2009-05-25 15:10:14)
转载▼
标签:
杂谈
在识别图象中的目标时,往往需要对目标边缘作跟踪处理,也叫轮廓跟踪。顾名思义,轮廓跟踪就是通过顺序找出边缘点来跟踪边界的。若图象是二值图象或图象中不同区域具有不同的象素值,但每个区域内的象素值是相同的,则如下算法可完成基于4连通或8连通区域的轮廓跟踪。
步骤1:首先按从上到下,从左到右的顺序扫描图象,寻找没有标记跟踪结束记号的第一个边界起始点A0,A0是具有最小行和列值的边界点。定义一个扫描方向变量dir,该变量用于记录上一步中沿着前一个边界点到当前边界点的移动方向,其初始化取值为(1)对4连通区域取dir=3;
(2)对8连通区域取dir=7;
步骤2:按逆时针方向搜索当前象素的3*3邻域,其起始搜索方向设定如下:
(1)对4连通区域取(dir+3)mod 4;
(2)对8连通区域,若dir为奇数取(dir+7)mod 8;若dir为偶数去(dir+6)mod 8;
在3*3邻域中搜索到的第一个与当前像素值相同的像素便为新的边界点An,同时更新变量dir为新的方向值。
步骤3:如果An等于第二个边界点A1且前一个边界点An-1等于第一个边界点A0,则停止搜索,结束跟踪,否则重复步骤2继续搜索。
步骤4:由边界点A0、A1、A2、……、An-2构成的边界便为要跟踪的边界。
上述算法是图象轮廓跟踪最基本的算法,它只能跟踪目标图象的内边界(边界包含在目标点集内),另外,它也无法处理图象的孔和洞
轮廓跟踪问题
[m,n]=size(yy);
U=zeros(m,n);
for i=1:m
for j=1:n
if yy(i,j)==255
U(i,j)=yy(i,j);
break;
end;
end;
end;
figure,imshow(U);
[m,n]=size(yy);
V=zeros(m,n);
for i=1:m
for j=n:-1:1
if yy(i,j)==255
V(i,j)=yy(i,j);
break;
end;
end;
end;
figure,imshow(V);
[m,n]=size(yy);
X=zeros(m,n);
for j=1:n
for i=1:m
if yy(i,j)==255
X(i,j)=yy(i,j);
break;
end;
end;
end;
figure,imshow(X);
[m,n]=size(yy);
Y=zeros(m,n);
for j=1:n
for i=m:-1:1
if yy(i,j)==255
Y(i,j)=yy(i,j);
break;
end;
end;
end;
figure,imshow(Y);
[m,n]=size(R);
ZZ=zeros(m,n);
ZZ=U+V+X+Y;
figure,imshow(ZZ);
BWTRACEBOUNDARY Trace object in binary image.
B = BWTRACEBOUNDARY(BW,P,FSTEP) traces the outline of an object in a
binary image BW, in which nonzero pixels belong to an object and
0-pixels constitute the background. P is a two-element vector specifying
the row and column coordinates of the initial point on the object
boundary. FSTEP is a string specifying the initial search direction
for the next object pixel connected to P. FSTEP can be any of the
following strings: 'N','NE','E','SE','S','SW','W','NW', where N stands
for north, NE stands for northeast, etc. B is a Q-by-2 matrix, where
Q is the number of boundary pixels for the region. B holds the row and
column coordinates of the boundary pixels.
B = BWTRACEBOUNDARY(BW,P,FSTEP,CONN) specifies the connectivity to use when tracing the boundary. CONN may be either 8 or 4. The default value
for CONN is 8. For CONN equal to 4, FSTEP is limited to 'N','E','S' and
'W'.
B = BWTRACEBOUNDARY(...,N,DIR) provides the option to specify the maximum number of boundary pixels, N, to extract and direction, DIR, in which to
trace the boundary. DIR can be either 'clockwise' or 'counterclockwise'.
By default, or when N is set to Inf, the algorithm extracts all of the
pixels from the boundary and, if DIR is not specified, it searches in
the clockwise direction.
Class Support
-------------
BW can be logical or numeric and it must be real, 2-D, and nonsparse.
B, P, CONN and N are double. DIR and FSTEP are strings.
Example
-------
Read in and display binary image blobs.png. Starting from the top left,
project a 'beam' across the image searching for the first nonzero
pixel. Use the location of that pixel as the starting point for the
boundary tracing. Including the starting point, extract 50 pixels of
the boundary and overlay them on the image. Mark the starting points
with a green "x". Mark beams which missed their targets with a red "x".
BW = imread('blobs.png');
imshow(BW,[]);
s=size(BW);
for row = 2:55:s(1)
for col=1:s(2)
if BW(row,col),
break;
end