www.gusucode.com > images 案例代码 matlab源码程序 > images/OverlayRegionBoundariesOnImageAnnotatedWithRegionNumbersExample.m

    %% Overlay Region Boundaries on Image and Annotate with Region Numbers
% 
%%
% Read binary image into the workspace.
BW = imread('blobs.png');
%%
% Calculate boundaries of regions in the image.
[B,L,N,A] = bwboundaries(BW);
%%
% Display the image with the boundaries overlaid. Add the region number
% next to every boundary (based on the label matrix). Use the zoom tool to
% read individual labels.
imshow(BW); hold on;
colors=['b' 'g' 'r' 'c' 'm' 'y'];
for k=1:length(B),
  boundary = B{k};
  cidx = mod(k,length(colors))+1;
  plot(boundary(:,2), boundary(:,1),...
       colors(cidx),'LineWidth',2);

  %randomize text position for better visibility
  rndRow = ceil(length(boundary)/(mod(rand*k,7)+1));
  col = boundary(rndRow,2); row = boundary(rndRow,1);
  h = text(col+1, row-1, num2str(L(row,col)));
  set(h,'Color',colors(cidx),'FontSize',14,'FontWeight','bold');
end
%%
% Display the adjacency matrix using the |spy| function. 
figure
spy(A);