|
a |
|
b/sitk_functions.py |
|
|
1 |
#!/usr/bin/env python3 |
|
|
2 |
# -*- coding: utf-8 -*- |
|
|
3 |
""" |
|
|
4 |
Created on Tue Sep 11 12:23:12 2018 |
|
|
5 |
|
|
|
6 |
@author: Josefine |
|
|
7 |
""" |
|
|
8 |
import numpy as np |
|
|
9 |
import random |
|
|
10 |
import SimpleITK as sitk |
|
|
11 |
|
|
|
12 |
def affine_rotate(img_sitk,label_sitk,min_value): |
|
|
13 |
rotate = random.choice([-15,-10,10,15]) |
|
|
14 |
new_transform = sitk.AffineTransform(3) |
|
|
15 |
new_transform = sitk.AffineTransform(new_transform) |
|
|
16 |
matrix = np.array(new_transform.GetMatrix()).reshape((3,3)) |
|
|
17 |
radians_z = -np.pi * rotate / 180. |
|
|
18 |
rotation = np.array([[np.cos(radians_z), -np.sin(radians_z),0],[np.sin(radians_z), np.cos(radians_z),0],[0,0,1]]) |
|
|
19 |
new_matrix = np.dot(rotation, matrix) |
|
|
20 |
new_transform.SetMatrix(new_matrix.ravel()) |
|
|
21 |
interpolator = sitk.sitkNearestNeighbor |
|
|
22 |
img_rot = sitk.Resample(img_sitk, new_transform,interpolator,min_value) |
|
|
23 |
label_rot = sitk.Resample(label_sitk, new_transform,interpolator,0) |
|
|
24 |
return [img_rot,label_rot] |
|
|
25 |
|
|
|
26 |
def affine_shear(img_sitk,label_sitk,min_value): |
|
|
27 |
shear = (random.uniform(-0.1,0.1), random.uniform(-0.1,0.1),random.uniform(-0.1,0.1)) |
|
|
28 |
new_transform = sitk.AffineTransform(3) |
|
|
29 |
new_transform = sitk.AffineTransform(new_transform) |
|
|
30 |
matrix = np.array(new_transform.GetMatrix()).reshape((3,3)) |
|
|
31 |
matrix[0,1] = -shear[0] |
|
|
32 |
matrix[1,0] = -shear[1] |
|
|
33 |
matrix[0,2] = -shear[1] |
|
|
34 |
matrix[2,0] = -shear[0] |
|
|
35 |
matrix[1,2] = -shear[2] |
|
|
36 |
matrix[2,1] = -shear[2] |
|
|
37 |
new_transform.SetMatrix(matrix.ravel()) |
|
|
38 |
interpolator = sitk.sitkNearestNeighbor |
|
|
39 |
img_sh = sitk.Resample(img_sitk, new_transform,interpolator,min_value) |
|
|
40 |
label_sh = sitk.Resample(label_sitk, new_transform,interpolator,0) |
|
|
41 |
return [img_sh,label_sh] |
|
|
42 |
|
|
|
43 |
def mult_and_add_intensity_fields(original_image): |
|
|
44 |
''' |
|
|
45 |
Modify the intensities using multiplicative and additive Gaussian bias fields. |
|
|
46 |
''' |
|
|
47 |
# Gaussian image with same meta-information as original (size, spacing, direction cosine) |
|
|
48 |
# Sigma is half the image's physical size and mean is the center of the image. |
|
|
49 |
sigma = random.uniform(0.5,3) |
|
|
50 |
middel = random.uniform(1,3) |
|
|
51 |
g_mult = sitk.GaussianSource(original_image.GetPixelIDValue(), |
|
|
52 |
original_image.GetSize(), |
|
|
53 |
[(sz-1)*spc/sigma for sz, spc in zip(original_image.GetSize(), original_image.GetSpacing())], |
|
|
54 |
original_image.TransformContinuousIndexToPhysicalPoint(np.array(original_image.GetSize())/middel), |
|
|
55 |
255, |
|
|
56 |
original_image.GetOrigin(), |
|
|
57 |
original_image.GetSpacing(), |
|
|
58 |
original_image.GetDirection()) |
|
|
59 |
|
|
|
60 |
# Gaussian image with same meta-information as original (size, spacing, direction cosine) |
|
|
61 |
# Sigma is 1/8 the image's physical size and mean is at 1/16 of the size |
|
|
62 |
sigma = random.uniform(2,8) |
|
|
63 |
middel = random.uniform(4,10) |
|
|
64 |
g_add = sitk.GaussianSource(original_image.GetPixelIDValue(), |
|
|
65 |
original_image.GetSize(), |
|
|
66 |
[(sz-1)*spc/sigma for sz, spc in zip(original_image.GetSize(), original_image.GetSpacing())], |
|
|
67 |
original_image.TransformContinuousIndexToPhysicalPoint(np.array(original_image.GetSize())/middel), |
|
|
68 |
255, |
|
|
69 |
original_image.GetOrigin(), |
|
|
70 |
original_image.GetSpacing(), |
|
|
71 |
original_image.GetDirection()) |
|
|
72 |
l1 = g_mult*original_image+g_add |
|
|
73 |
return l1 |
|
|
74 |
|
|
|
75 |
def BSplineDeform(img, lab, dim, numcontrolpoints, stdDeform,min_value): |
|
|
76 |
transfromDomainMeshSize=[numcontrolpoints]*dim |
|
|
77 |
tx = sitk.BSplineTransformInitializer(img,transfromDomainMeshSize) |
|
|
78 |
params = tx.GetParameters() |
|
|
79 |
paramsNp=np.asarray(params,dtype=float) |
|
|
80 |
paramsNp = paramsNp + np.random.randn(paramsNp.shape[0])*stdDeform |
|
|
81 |
paramsNp[0:int(len(params)/3)]=0 #remove z deformations! The resolution in z is too bad |
|
|
82 |
params=tuple(paramsNp) |
|
|
83 |
tx.SetParameters(params) |
|
|
84 |
resampler = sitk.ResampleImageFilter() |
|
|
85 |
resampler.SetReferenceImage(img) |
|
|
86 |
resampler.SetInterpolator(sitk.sitkNearestNeighbor) # ORIGINAL: sitk.sitkLinear |
|
|
87 |
resampler.SetDefaultPixelValue(min_value) |
|
|
88 |
resampler.SetTransform(tx) |
|
|
89 |
resampler.SetDefaultPixelValue(min_value) # -1024 HU on CT image = air |
|
|
90 |
outimgsitk = resampler.Execute(img) |
|
|
91 |
resampler.SetDefaultPixelValue(0) |
|
|
92 |
outlabsitk = resampler.Execute(lab) |
|
|
93 |
return outimgsitk, outlabsitk |