This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import SimpleITK as sitk | |
input_dicom = '/path/to/input/dicom' | |
output_dicom_dir = '/path/to/output' | |
# create the folder output_dicom_dir | |
series_IDs = sitk.ImageSeriesReader.GetGDCMSeriesIDs(input_dicom) | |
if not series_IDs: | |
print("ERROR: given directory \"" + input_dicom + "\" does not contain a valid DICOM series.") | |
sys.exit(1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Ref: https://github.com/razorx89/pydicom-seg#getting-started | |
import pydicom | |
import pydicom_seg | |
import SimpleITK as sitk | |
dcm_path = pydicom.dcmread('/path/to/segmentation.dcm') | |
reader = pydicom_seg.MultiClassReader() | |
result = reader.read(dcm_path) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SimpleITK as sitk | |
import numpy as np | |
def sanity_checker_with_files( | |
image_file_baseline: str, image_file_compare: str, threshold: float = 1e-5 | |
) -> bool: | |
""" | |
This function performs sanity check on 2 image files WITHOUT loading images into memory. | |
Supported image formats: https://simpleitk.readthedocs.io/en/master/IO.html |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SimpleITK as sitk | |
# read image to copy information from | |
image_reference = sitk.ReadImage('/path/to/reference.nii.gz') | |
image_to_copy_from = sitk.ReadImage('/path/to/image_to_change.nii.gz') | |
image_to_copy_from.CopyInformation(image_reference) | |
# write the image | |
sitk.WriteImage(image_to_copy_from, '/path/to/result.nii.gz') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Union, OrderedDict | |
import SimpleITK as sitk | |
import numpy as np | |
def one_hot_encode( | |
input_mask: Union[str, sitk.Image], | |
encoding_logic: dict = {"NET": [1], "TC": [1, 4], "WT": [1, 2, 4]}, | |
) -> dict: | |
""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import SimpleITK as sitk | |
input_directory = os.path.join(os.getcwd(), "data") | |
for filename in os.listdir(input_directory): | |
if filename.endswith(".nii.gz"): | |
image = sitk.ReadImage(os.path.join(input_directory, filename)) | |
# properties |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os, time, traceback, pathlib | |
import SimpleITK as sitk | |
def downsample_dicom( | |
dicom_path: str, output_path: str, downsample_factor: int = 0.5 | |
) -> None: | |
""" | |
Downsample DICOM images and save them to the output path. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SimpleITK as sitk | |
image_to_cast = sitk.ReadImage("/path/to/image.nii.gz") | |
options_for_casting = { | |
"int64": sitk.sitkInt64, | |
"int32": sitk.sitkInt32, | |
"int16": sitk.sitkInt16, | |
"int8": sitk.sitkInt8, | |
"uint64": sitk.sitkUInt64, | |
"uint32": sitk.sitkUInt32, |
OlderNewer