def boundingbox(self, labels = None, real = False):
"""
Return the bounding box of a label.
:Examples:
>>> import numpy as np
>>> a = np.array([[1, 2, 7, 7, 1, 1],
[1, 6, 5, 7, 3, 3],
[2, 2, 1, 7, 3, 3],
[1, 1, 1, 4, 1, 1]])
>>> from vplants.tissue_analysis.spatial_image_analysis import SpatialImageAnalysis
>>> analysis = SpatialImageAnalysis(a)
>>> analysis.boundingbox(7)
(slice(0, 3), slice(2, 4), slice(0, 1))
>>> analysis.boundingbox([7,2])
[(slice(0, 3), slice(2, 4), slice(0, 1)), (slice(0, 3), slice(0, 2), slice(0, 1))]
>>> analysis.boundingbox()
[(slice(0, 4), slice(0, 6), slice(0, 1)),
(slice(0, 3), slice(0, 2), slice(0, 1)),
(slice(1, 3), slice(4, 6), slice(0, 1)),
(slice(3, 4), slice(3, 4), slice(0, 1)),
(slice(1, 2), slice(2, 3), slice(0, 1)),
(slice(1, 2), slice(1, 2), slice(0, 1)),
(slice(0, 3), slice(2, 4), slice(0, 1))]
"""
if labels == 0:
return nd.find_objects(self.image==0)[0]
if self._bbox is None:
self._bbox = nd.find_objects(self.image)
if labels is None:
labels = copy.copy(self.labels())
if self.background() is not None:
labels.append(self.background())
# bbox of object labelled 1 to n are stored into self._bbox. To access i-th element, we have to use i-1 index
if isinstance (labels, list):
bboxes = [self._bbox[i-1] for i in labels]
if real : return self.convert_return([real_indices(bbox,self._voxelsize) for bbox in bboxes],labels)
else : return self.convert_return(bboxes,labels)
else :
try:
if real: return real_indices(self._bbox[labels-1], self._voxelsize)
else : return self._bbox[labels-1]
except:
return None
spatial_image_analysis.py 文件源码
python
阅读 23
收藏 0
点赞 0
评论 0
评论列表
文章目录