image-processingnumpyscikit-imagemahotas

How can i count number of black spaces in scikit image or mahotas?


i have an image like this:

star

after I skeletonize it by scikit image's skeletonize function

from skimage import morphology
out = morphology.skeletonize(gray>0)

enter image description here

There is a way for counting the number of black spaces? (in this picture six) except the background in scikit-image or mahotas?


Solution

  • With this input:

    enter code here

    You can do:

    >>> from skimage import morphology
    >>> import numpy as np
    >>> from scipy.misc import imread
    >>> im = imread("Bju1h.png")
    >>> im = im > 0
    >>> np.unique(morphology.label(im))
    array([0, 1, 2, 3, 4, 5, 6, 7])
    >>> num_components = len(np.unique(morphology.label(im))) - 2
    >>> num_components
    6
    

    I subtract 2 to ignore the background component and the foreground/line component. From your original image you can skip out the skeletonize step and just run this with im = gray > 0, since the wide foreground/line will still be labelled as a single component.