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

    %% Calculate connected components and display results
%  
%%
% Read binary image into the workspace.

% Copyright 2015 The MathWorks, Inc.

BW = imread('text.png');
%%
% Calculate the connected components, using |bwconncomp| .
CC = bwconncomp(BW);
%%
% Create a label matrix, using |labelmatrix| .
L = labelmatrix(CC);
%%
% For comparison, create a second label matrix, using |bwlabel| .
L2 = bwlabel(BW);
%%
% View both label matrices in the workspace. Note that |labelmatrix| is
% more memory efficient than |bwlabel| , using the smallest numeric class
% necessary for the number of objects.
whos L L2
%%
% Display the label matrix as an RGB image, using |label2rgb| .
figure
imshow(label2rgb(L));