scipy ndimage measurement labeling is not working

2019-09-04 05:53发布

I am trying to label this image into two regions: enter image description here

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 enter image description here

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条回答
迷人小祖宗
2楼-- · 2019-09-04 06:06

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

查看更多
登录 后发表回答