Skip to content

Instantly share code, notes, and snippets.

View mrajchl's full-sized avatar

Martin Rajchl mrajchl

View GitHub Profile
@mrajchl
mrajchl / resample_itk_image.py
Created June 12, 2018 17:16
Resampling an itk image object with SimpleITK
def resample_img(itk_image, out_spacing=[2.0, 2.0, 2.0], is_label=False):
# Resample images to 2mm spacing with SimpleITK
original_spacing = itk_image.GetSpacing()
original_size = itk_image.GetSize()
out_size = [
int(np.round(original_size[0] * (original_spacing[0] / out_spacing[0]))),
int(np.round(original_size[1] * (original_spacing[1] / out_spacing[1]))),
int(np.round(original_size[2] * (original_spacing[2] / out_spacing[2])))]
@mrajchl
mrajchl / feed_from_generator.py
Created June 8, 2018 15:56
Feed .nii imaging data via a native python generator
# Generator function
def f():
fn = read_fn(file_references=all_filenames,
mode=tf.estimator.ModeKeys.TRAIN,
params=reader_params)
ex = next(fn)
# Yield the next image
yield ex
@mrajchl
mrajchl / read_fn.py
Created June 8, 2018 15:54
A read function for .nii images using SimpleITK
def read_fn(file_references, mode, params=None):
# We define a `read_fn` and iterate through the `file_references`, which
# can contain information about the data to be read (e.g. a file path):
for meta_data in file_references:
# Here, we parse the `subject_id` to construct a file path to read
# an image from.
subject_id = meta_data[0]
data_path = '../../data/IXI_HH/1mm'
@mrajchl
mrajchl / load_and_feed_from_TFRecord.py
Created June 8, 2018 15:42
Load data from a TFRecords database and feed into a graph
def decode(serialized_example):
# Decode examples stored in TFRecord
# NOTE: make sure to specify the correct dimensions for the images
features = tf.parse_single_example(
serialized_example,
features={'train/image': tf.FixedLenFeature([128, 224, 224, 1], tf.float32),
'train/label': tf.FixedLenFeature([], tf.int64)})
# NOTE: No need to cast these features, as they are already `tf.float32` values.
return features['train/image'], features['train/label']
@mrajchl
mrajchl / write_TFRecord.py
Created June 8, 2018 15:29
Write .nii data to a TFRecords database
def _int64_feature(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
def _float_feature(value):
return tf.train.Feature(float_list=tf.train.FloatList(value=value))
# path to save the TFRecords file
train_filename = 'train.tfrecords'
# open the file
@mrajchl
mrajchl / sitk_read_nii.py
Created June 8, 2018 15:01
Reading a .nii image with SimpleITK
import SimpleITK as sitk
import numpy as np
# A path to a T1-weighted brain .nii image:
t1_fn = './brain_t1_0001.nii'
# Read the .nii image containing the volume with SimpleITK:
sitk_t1 = sitk.ReadImage(t1_fn)
# and access the numpy array:
@mrajchl
mrajchl / load_feed_dict_from_memory.py
Last active October 6, 2019 11:57
Load a tf feed_dict with data from memory
# Load all data into memory
data = load_data(all_filenames, tf.estimator.ModeKeys.TRAIN, reader_params)
# Create placeholder variables and define their shapes (here,
# we input a volume image of size [128, 224, 244] and a single
# channel (i.e. greyscale):
x = tf.placeholder(reader_example_dtypes['features']['x'],
[None, 128, 224, 224, 1])
y = tf.placeholder(reader_example_dtypes['labels']['y'],
[None, 1])
@mrajchl
mrajchl / foo.py
Created June 8, 2018 11:46
An Introduction to Biomedical Image Analysis with Tensorflow and DLTK
def foo():
print('bar')