Skip to content

Instantly share code, notes, and snippets.

@sarthakpati
Created April 30, 2021 18:52
Show Gist options
  • Save sarthakpati/e3d38c5a2572b60b78cb7c21be8bceb0 to your computer and use it in GitHub Desktop.
Save sarthakpati/e3d38c5a2572b60b78cb7c21be8bceb0 to your computer and use it in GitHub Desktop.
Get bounding box of a label image
import SimpleITK as sitk
image_file = '/path/to/image.nii.gz'
mask_file = '/path/to/image.nii.gz'
image = sitk.ReadImage(image_file)
mask = sitk.ReadImage(mask_file)
bb_filter = sitk.LabelStatisticsImageFilter()
bb_filter.Execute(image, mask)
current_bb = bb_filter.GetBoundingBox(1)
x_size = current_bb[1] - current_bb[0]
y_size = current_bb[3] - current_bb[2]
z_size = current_bb[5] - current_bb[4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment