scipy ndimage measurement labeling is not working

2019-09-04 05:23发布

问题:

I am trying to label this image into two regions:

And here is my code:

from scipy.ndimage import measurements,morphology 
from PIL import Image
from numpy import *
im = array(Image.open('two_leds.png').convert('L'))
im = 1*(im<200)
result = Image.fromarray((im * 255).astype(uint8))
result.save('results.png')
labels,  nbr_objects = measurements.label(im)
print 'number of objects equals: {}'.format(nbr_objects)  

Here is the filtered image "results.png" with low-pass filter

My problem is the output that I am getting is 1 while I am expecting 2

>>> number of objects equals: 1

Is there something wrong with the labeling code itself. It seems to me pretty straightforward

回答1:

I'm pretty sure you're counting the white pixels rather than the black pixels. Try inverting the image and then count.