'Using Simple ITK to find bounding box

How can I capture the bounding box from the 3D mask by using Simple ITK in python? The ITK has the bounding box function, but I couldn't find similar function in SITK.



Solution 1:[1]

You need a LabelShapeStatisticsImageFilter, and after Execute you can get the BoundingBox around certain values. In case of several masks you can iterate on range(1,labelimfilter.GetNumberOfLabels()+1). (Works this way because you can't calculate BoundingBox on the value 0.)

import SimpleITK as sitk

bbox=[]

labelimfilter=sitk.LabelShapeStatisticsImageFilter()
labelimfilter.Execute(yourmaskimage)
for i in range(1,labelimfilter.GetNumberOfLabels()+1):
    box=labelimfilter.GetBoundingBox(i)
    bbox.append(box)

This will return the bounding box coordinates in [xstart, ystart, zstart, xsize, ysize, zsize] order

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1