Search code examples
pythonmatlabscikit-image

What is equivalent to "bwlabeln (with 18 and 26-connected neighborhood)" in Python 3.x?


I have used bwlabeln of Matlab for three-dimensional connectives with 18-connected neighborhoodas below code:

[labeledImage, ~] = bwlabeln(maskImageVolume, 18); # maskImageVolume is 3D. e.g.:(200, 200, 126)

and equivalent of it in Python is:

from skimage import measure
labeledImage = measure.label(maskImageVolume, 8) 

However, bwlabeln in Matlab support the Three-dimensional connectives (with 18 and 26-connected neighborhood) but skimage.measure.label just support the 4- or 8-“connectivity”.

What is equivalent to bwlabeln for 18 and 26-connected neighborhood in Python?


Solution

  • The documentation to skimage.measure.label states for parameter neighbors:

    neighbors : {4, 8}, int, optional
    Whether to use 4- or 8-“connectivity”. In 3D, 4-“connectivity” means connected pixels have to share face, whereas with 8-“connectivity”, they have to share only edge or vertex.
    Deprecated, use connectivity instead.

    And for parameter connectivity:

    connectivity : int, optional
    Maximum number of orthogonal hops to consider a pixel/voxel as a neighbor. Accepted values are ranging from 1 to input.ndim. If None, a full connectivity of input.ndim is used.

    What this means is that, in 3D, the connectivity can be either 1, 2 or 3, indicating 6, 18 or 26 neighbors.

    Looking back through the various versions of the documentation, this syntax seems to have been introduced in scikit-image 0.11 (0.10 doesn't have it).

    For your case, with 18 connected neighbors:

    labeledImage = measure.label(maskImageVolume, connectivity=2)