-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
61 lines (45 loc) · 1.6 KB
/
utils.py
File metadata and controls
61 lines (45 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import numpy as np
import glob
import nibabel as nib
import os
import SimpleITK as sitk
import random
def resample1(image, transform):
reference_image = image
interpolator = sitk.sitkNearestNeighbor
default_value = 0.0
return sitk.Resample(image, reference_image, transform, interpolator, default_value)
def resample2(image, transform):
reference_image = image
interpolator = sitk.sitkCosineWindowedSinc
default_value = 0.0
return sitk.Resample(image, reference_image, transform, interpolator, default_value)
def get_rotation_transform(dim, angles):
if not isinstance(angles, list):
angles = [angles]
assert isinstance(angles, list), 'Angles parameter must be a list of floats, one for each dimension.'
assert len(angles) in [1, 3], 'Angles must be a list of length 1 for 2D, or 3 for 3D.'
t = sitk.AffineTransform(dim)
if len(angles) == 1:
# 2D
t.Rotate(0, 1, angle=angles[0])
elif len(angles) > 1:
# 3D
# rotate about x axis
t.Rotate(1, 2, angle=angles[0])
# rotate about y axis
t.Rotate(0, 2, angle=angles[1])
# rotate about z axis
t.Rotate(0, 1, angle=angles[2])
return t
def get_scale_transform(dim, scale):
if isinstance(scale, list) or isinstance(scale, tuple):
assert len(scale) == dim, 'Length of scale must be equal to dim.'
s = sitk.AffineTransform(dim)
s.Scale(scale)
return s
def get_translation_transform(offset):
dimension = 3
translation = sitk.TranslationTransform(dimension)
translation.SetOffset(offset)
return translation