File Exchange

image thumbnail

Label connected components in 2-D array

version 1.4 (2.85 KB) by

LABEL is a generalization of BWLABEL

6 Ratings



View License

LABEL is a generalization of BWLABEL: BWLABEL works with 2-D binary images only, whereas LABEL works with 2-D arrays of any class.
L = LABEL(I,N) returns a matrix L, of the same size as I, containing labels for the connected components in I. Two adjacent components (pixels), of respective indexes IDX1 and IDX2, are connected if I(IDX1) and I(IDX2) are equal.

N can have a value of either 4 or 8, where 4 specifies 4-connected objects and 8 specifies 8-connected objects; if the argument is omitted, it defaults to 8.
Important remark:
NaN values are ignored and considered as background. Because LABEL works with arrays of any class, the 0s are NOT considered as the background.

The elements of L are integer values greater than or equal to 0. The pixels labeled 0 are the background (corresponding to the NaN components of the input array). The pixels labeled 1 make up one object, the pixels labeled 2 make up a second object, and so on.
[L,NUM] = LABEL(...) returns in NUM the number of connected objects found in I.

[L,NUM,SZ] = LABEL(...) returns a matrix SZ, of the same size as I, that contains the sizes of the connected objects. For a pixel whose index is IDX, we have: SZ(IDX) = NNZ(L==L(IDX)).

Enter "help label" in the Matlab Command Window for two examples.


Comments and Ratings (8)

Nick Zadeh

Thanks so much! Exactly what I was looking for.

Georg Stillfried

@asan masraf: You need to provide a matrix as input.
For example:
data = [
1 1 1 NaN NaN NaN NaN NaN
1 1 1 NaN 1 1 NaN NaN
1 1 1 NaN 1 1 NaN NaN
1 1 1 NaN NaN NaN 1 NaN
1 1 1 NaN NaN NaN 1 NaN
1 1 1 NaN NaN NaN 1 NaN
1 1 1 NaN NaN 1 1 NaN
1 1 1 NaN NaN NaN NaN NaN];
l4 = label(data,4);
l8 = label(data,8);

asan masraf

I tried to run but I couldn't
it gave me this error:

Error using label (line 80)
Not enough input arguments.

Nice work ! I used this function as an alternative to bwlabel. The only required modification in my code was to take into account the syntax difference : label puts 0 binary values into a connected object. As per the author's 'Important remark', zeros should be replaced by NaNs in this use case.


tacoyu (view profile)

Damien Garcia

Damien Garcia (view profile)

to Irwin Donis: Because LABEL works with arrays of any class, the 0s are NOT considered as the background. Only NaN values are considered as background. Transform your background into NaNs before using LABEL.

Irwin Donis

Hello! Nice work, the only problem is that for one reason or another, when I input a Binary Image, it recognizes the background pixels (0) as objects. So, if I have 5 objects and 2 empty spaces (background) it will indicate that I have 7 objects and labels them from 1 to 7, including the background. I even ran it with a standard image like I = imread('coins.png'), and it still had the same problem. Why do you think this is, and how would you correct it? Thanks

Arturo Serrano

Arturo Serrano (view profile)



"if nargin>1" (line 156) has been replaced by "if nargout>1"


"Important remark" has been added in the help text


The help text was incomplete.


Only tags and help text were modified.

MATLAB Release
MATLAB 7.7 (R2008b)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video