Function Reference
— Function File: [l, num] = bwlabel(bw, n)

Labels foreground objects in the binary image bw. The output l is a matrix where 0 indicates a background pixel, 1 indicates that the pixel belong to object number 1, 2 that the pixel belong to object number 2, etc. The total number of objects is num.

To pixels belong to the same object if the are neighbors. By default the algorithm uses 6-connectivity to define a neighborhood, but this can be changed through the argument n that can be either 4, 6, or 8.

The algorithm is derived from BKP Horn, Robot Vision, MIT Press, 1986, p 65 - 89