Search code examples
python-2.7image-segmentationbinary-image

Python: Binary image segmentation


Is there a easy way to implement the segmentation of a binary image in python?

My 2d-"images" are numpy arrays. The used values are 1.0 and 0.0. I would require a list of all objects with the value 1.0. Every black pixel is a pixel of an object. An object may contain many touching pixels with the value 1.0.

I can use numpy and also scipy.

I already tried to iterate over all pixels and create sets of pixels and fill the new pixel in old sets (or create a new set). Unfortunately the implementation was poor, extremely buggy and also very slow.

Hopyfully somthing like this already exists or there is an easy way to do this?

Thank you very much


Solution

  • To my mind, this is exactly what can be done using scipy.ndimage.measurements.label and scipy.ndimage.measurements.find_objects

    You have to specify what "touching" means. If it means edge-sharing, then the default structure of ndimage.measurements.label is the one you need so you just need to pass your array. If touching means also corner sharing, you will find the right structure in the docstring.

    find_objects can then yield a list of slices for the objects.