L = bwlabel(BW, n)
Label connected components in 2-D binary image
L= bwlabel(BW, n) returns a matrix L, of the same size as BW, containing labels for the connected objects in BW.
The elements of L are integer values greater than or equal to 0. The pixels labeled 0 are the background. The pixels labeled 1 make up one object; the pixels labeled 2 make up a second object; and so on.
n: 4 or 8, where 4 specifies 4-connected objects and 8 specifies 8-connected objects. If the argument is omitted, it defaults to 8.
[L, num] = bwlabel(BW, n) returns in num the number of connected objects found in BW.
Find indices and values of nonzero elements
[row,col,v] = find(X, …) returns a column or row vector v of the nonzero entries in X, as well as row and column indices. If X is a logical expression, then v is a logical array. Output v contains the non-zero elements of the logical array obtained by evaluating the expression X.