File Exchange

image thumbnail

Label connected components in 2-D array

version 1.4.0.0 (2.85 KB) by Damien Garcia
LABEL is a generalization of BWLABEL

3 Downloads

Updated 28 Jan 2011

View Version History

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.

Note:
------
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)).

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

------
http://www.biomecardio.com/matlab
-----

Cite As

Damien Garcia (2021). Label connected components in 2-D array (https://www.mathworks.com/matlabcentral/fileexchange/26946-label-connected-components-in-2-d-array), MATLAB Central File Exchange. Retrieved .

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.

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.

tacoyu

Damien Garcia

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

MATLAB Release Compatibility
Created with R2008b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!