Search code examples
image-processingscikit-image

Generate bounding boxes from elevation map/watershed transform


I am using scikit-image to perform image segmentation on some images I have, and I was referring to this tutorial (https://scikit-image.org/docs/dev/user_guide/tutorial_segmentation.html)

I am using the elevation map method described and although I am not able to get correct result after applying watershed algorithm, the elevation map itself seems good enough for my purpose.

I am getting this output when I plot the elevation map:

enter image description here

Now I want to get the coordinates for the bounding box for this object. How do I achieve this? I looked at http://scikit-image.org/docs/dev/auto_examples/edges/plot_contours.html but I am unable to figure out how do I choose the second argument for find_contours ?


Solution

  • Does this work for you?

    from skimage import filters
    from scipy import ndimage as ndi
    
    threshold = filters.threshold_otsu(elevation_map)
    binary = (elevation_map > threshold).astype(int)
    bounding_box = ndi.find_objects(binary)[0]