Search code examples
pythonnumpyscipynumpy-ndarrayscikit-image

Extract yxz pixels coordinates from a labelled image


I am relatively new to image analysis and I need to extract the xy and z coordinates of the pixels that compose several objects within my image. The image is actually a stack where all the objects of interest have been segmented and are distinguished by a label (integer).

I am having some issues to find a smart and not too much expensive method for extracting all the coordinates of one object and save them in three different lists for x, y and z to compute further computations (ideally, I am planning to fit an ellipsoid for each object)... and iterate the whole thing for each object. Here is an example of one slice of my stack.

I was looking into numpy and skimage mainly and a bit scipy (the find_object() function), but I am honestly quite lost. Do you know how I could do that in an effective way using a python library? Thank you!

Lucre

enter image description here


Solution

  • You can use skimage.measure.regionprops. Even though the documentation says .coords is 2D, it actually works fine for 3D data. So you would do something like:

    props_list = regionprops(segmentation)
    
    for props in props_list:
        print(f'the label is {props.label}')
        print(f'the coordinates are {props.coords}')