Code covered by the BSD License  

Highlights from
Label connected components in 2-D array

4.5

4.5 | 4 ratings Rate this file 21 Downloads (last 30 days) File Size: 2.85 KB File ID: #26946
image thumbnail

Label connected components in 2-D array

by Damien Garcia

 

13 Mar 2010 (Updated 28 Jan 2011)

LABEL is a generalization of BWLABEL

| Watch this File

File Information
Description

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
-----

MATLAB release MATLAB 7.7 (R2008b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (5)
22 Mar 2010 Arturo Serrano  
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

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

23 May 2011 tacoyu  
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.

Please login to add a comment or rating.
Updates
17 Mar 2010

Only tags and help text were modified.

06 Jul 2010

The help text was incomplete.

28 Oct 2010

"Important remark" has been added in the help text

28 Jan 2011

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

Tag Activity for this File
Tag Applied By Date/Time
image processing Damien Garcia 15 Mar 2010 12:38:49
connectedcomponent labeling Damien Garcia 15 Mar 2010 12:38:49
connected component labeling Damien Garcia 17 Mar 2010 12:32:18

Contact us at files@mathworks.com