Labeling of a binary image is the operation of assigning a unique value to pixels belonging to the same connected region. Depending on the definition of a "connected region", different results can be obtained. Two widely used square grid topologies are shown below.
Rectangular grids for connectivity study of pixel P
Recommended use of these grids are:
In this experiment a small binary image is used. Shown below is our binary image zoomed by a factor of 4.
Binary image zoomed by a factor of 4
Labeling using the 8-neighborhood results in 10 connected regions plus the background. Using a 4-neighborhood results in 86 connected regions plus the background. Colors are used to better visualize the different connected regions.
Left:8-neighborhood, Right: 4-neighborhood