Skip to content

Instantly share code, notes, and snippets.

View sarthakpati's full-sized avatar
💡
Researching privacy-first approaches to ML

Sarthak Pati sarthakpati

💡
Researching privacy-first approaches to ML
View GitHub Profile
@sarthakpati
sarthakpati / get_bound_box.py
Created April 30, 2021 18:52
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)
import SimpleITK as sitk
# read image
inputImage = sitk.ReadImage('/path/to/input.nii.gz')
# get result in the form of a numpy array
npa_res = my_algorithm(sitk.GetArrayFromImage(inputImage)) # my_algorithm does something fancy
# Converting back to SimpleITK (assumes we didn't move the image in space as we copy the information from the original)
result_image = sitk.GetImageFromArray(npa_res)