ENH: add bounding_box function to SciPy.ndimage

[email protected]
Newsgroups gmane.comp.python.scientific.devel
Message-ID <[email protected]>
Link to PR: https://github.com/scipy/scipy/pull/18170

I would like to propose a new function for the ndimage measurements module: `bounding_box()`

This function finds the bounding box of the nonzero elements in the input array. It accepts any dimensionality and dtype input.


> Why do I think this is needed?

It is true that same result can be obtained by:
- numpy: `nz=np.nonzero(arr)  ; nz[0].min(), nz[0].max(), nz[1].min(),....`   : SLOW
- scipy: `ndi.find_objects(arr!=0,max_label=1)`   : Faster, implemented in C

I actually implemented `bounding_box` simply by adapting the existing implementation of `find_objects`, so the logic is fundamentally the same.
However, it runs significantly faster - namely between 10% and 30% faster than `find_objects`, per my tests locally.
This is mainly due to the fact that `bounding_box` runs *in-place* on the input array, while running `find_objects(arr!=0) requires allocation of a new array, to store the boolean result of `arr!=0`, which is then passed as the argument to `find_objects`.
Of course, avoiding this allocation also means memory footprint is significantly reduced, and in case of large multidimensional arrays this can be crucial.

The only similar functionality I have found in Python libraries is `PIL.Image.getbbox()` : https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.getbbox
This is limited to 2D images.
_______________________________________________
SciPy-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/scipy-dev.python.org/
Member address: [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.