Question

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?

Was it helpful?

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.

OTHER TIPS

A solution with scipy.ndimage alone

from  scipy import ndimage as nd
from matplotlib import pyplot as plt

star = nd.imread('/home/jeanpat/Images/star.jpg')
star = star[:,:,0]
thr = star < 120
anti = star >119
mask = nd.morphology.binary_fill_holes(thr)

lab, n = nd.label(anti*mask)
plt.title(str(n)+' objects detected')
plt.imshow(lab)
plt.show()

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top