The LABEL_REGION function consecutively labels all of the regions, or blobs, of a bi-level image with a unique region index. This process is sometimes called "blob coloring". A region is a set of non-zero pixels within either a four-neighbor region (the default) or an eight-neighbor region (if the EIGHT keyword is specified) around the pixel under examination. The four-neighbor region around a pixel consists of two adjacent horizontal and two adjacent vertical neighbors. The eight-neighbor region around a pixel consists of all the immediately adjacent pixels.
The argument for LABEL_REGION is a two-dimensional bi-level integer type array--only zero and non-zero values are considered. The result of the function is a two-dimensional integer array of the same dimensions with each pixel containing its region index. A region index of zero indicates that the original pixel was zero and belongs to no region. Output values range from 0 to the number of regions.
Statistics on each of the regions may be easily calculated using the HISTOGRAM function as shown in the examples below.
If this keyword is set, LABEL_REGION searches the eight-neighbor region around a pixel rather than the four-neighbor region. The four-neighbor region around a pixel consists of two adjacent horizontal and two adjacent vertical neighbors. The eight-neighbor region around a pixel consists of all the immediately adjacent pixels.
Count the number of distinct regions within an image, and their population:
b = LABEL_REGION(image) ; Get blob indices.
h = HISTOGRAM(b) ; Get population of each blob.
FOR i=0, N_ELEMENTS(h)-1 DO PRINT 'Region ',i, $
Note that region 0 is the set of zero pixels that are not within a region.
As above, but also print the average value and standard deviation of each region:
b = LABEL_REGION(image) ; Get blob indices.
h = HISTOGRAM(b, REVERSE_INDICES=r); Get population and members of each blob.
FOR i=0, N_ELEMENTS(h)-1 DO BEGIN ; Each region
p = r(r[i]:r[i+1]-1) ; Find subscripts of members of region i.
q = image[p] ; Pixels of region i