MATLAB图像处理工具箱中另外一个用来进行边界跟踪的函数是bwboundaries,它的常见调用方法如下:
B=bwboundaries (BW)
[B, L,N,A] =bwboundaries (BW, conn)
B=bwboundaries(BW, conn, options)
其中,BW是二值图像,值为1的像素构成了目标,值为0的像素构成了背景; conn刻画了跟踪边界的连续性,其值可以为4或者8;参数options的值可以为"holes'或者'noholes'; B是返回的边界行号和列号; L是返回的标签矩阵; N是返回的目标数; A是返回的邻接矩阵。
下面通过两个例子来看一下bwboundaries函数的用法。
利用bwboundaries函数跟踪外部边界,代码实现如下:
plot(x,y,'r','LineWidth',2)
axis off
saveas(gcf,'BW','jpg');
f=getframe(gcf);
imwrite(f.cdata,'BW.jpg'); %读取图像
BW=im2bw(f.cdata,graythresh(f.cdata)); %转化为二值图像
[B,L]=bwboundaries(BW,'noholes'); %返回边界和标签矩阵
imshow(label2rgb(L,@jet,[.5 .5 .5])); %显示彩色图像
hold on
for k=1:length(B)
boundary=B{k};
plot(boundary(:,2),boundary(:,1),'y'