View License

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

» Watch video

Highlights from
Label connected components in 2-D array

4.6 | 5 ratings Rate this file 20 Downloads (last 30 days) File Size: 2.85 KB File ID: #26946 Version: 1.4
image thumbnail

Label connected components in 2-D array


Damien Garcia (view profile)


13 Mar 2010 (Updated )

LABEL is a generalization of BWLABEL

| Watch this File

File Information

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.



This file inspired Berkeley Indices Trajectory Extractor (Bite).

MATLAB release MATLAB 7.7 (R2008b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (7)
09 May 2014 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);

19 Apr 2014 asan masraf

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

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

Comment only
20 Sep 2011 James Mure-Dubois

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.

23 May 2011 tacoyu

tacoyu (view profile)

28 Oct 2010 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.

Comment only
07 Oct 2010 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

22 Mar 2010 Arturo Serrano

Arturo Serrano (view profile)

17 Mar 2010 1.1

Only tags and help text were modified.

06 Jul 2010 1.2

The help text was incomplete.

28 Oct 2010 1.3

"Important remark" has been added in the help text

28 Jan 2011 1.4

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

Contact us