|
a |
|
b/resizing.py |
|
|
1 |
''' |
|
|
2 |
The code for resizing has been taken from |
|
|
3 |
https://gist.github.com/zivy/79d7ee0490faee1156c1277a78e4a4c4 |
|
|
4 |
''' |
|
|
5 |
|
|
|
6 |
def resample(img, new_size, interpolator): |
|
|
7 |
dimension = img.GetDimension() |
|
|
8 |
|
|
|
9 |
# Physical image size corresponds to the largest physical size in the training set, or any other arbitrary size. |
|
|
10 |
reference_physical_size = np.zeros(dimension) |
|
|
11 |
|
|
|
12 |
reference_physical_size[:] = [(sz-1)*spc if sz*spc>mx else mx for sz,spc,mx in zip(img.GetSize(), img.GetSpacing(), reference_physical_size)] |
|
|
13 |
|
|
|
14 |
# Create the reference image with a zero origin, identity direction cosine matrix and dimension |
|
|
15 |
reference_origin = np.zeros(dimension) |
|
|
16 |
reference_direction = np.identity(dimension).flatten() |
|
|
17 |
reference_size = new_size |
|
|
18 |
reference_spacing = [ phys_sz/(sz-1) for sz,phys_sz in zip(reference_size, reference_physical_size) ] |
|
|
19 |
|
|
|
20 |
reference_image = sitk.Image(reference_size, img.GetPixelIDValue()) |
|
|
21 |
reference_image.SetOrigin(reference_origin) |
|
|
22 |
reference_image.SetSpacing(reference_spacing) |
|
|
23 |
reference_image.SetDirection(reference_direction) |
|
|
24 |
|
|
|
25 |
# Always use the TransformContinuousIndexToPhysicalPoint to compute an indexed point's physical coordinates as |
|
|
26 |
# this takes into account size, spacing and direction cosines. For the vast majority of images the direction |
|
|
27 |
# cosines are the identity matrix, but when this isn't the case simply multiplying the central index by the |
|
|
28 |
# spacing will not yield the correct coordinates resulting in a long debugging session. |
|
|
29 |
reference_center = np.array(reference_image.TransformContinuousIndexToPhysicalPoint(np.array(reference_image.GetSize())/2.0)) |
|
|
30 |
|
|
|
31 |
# Transform which maps from the reference_image to the current img with the translation mapping the image |
|
|
32 |
# origins to each other. |
|
|
33 |
transform = sitk.AffineTransform(dimension) |
|
|
34 |
transform.SetMatrix(img.GetDirection()) |
|
|
35 |
transform.SetTranslation(np.array(img.GetOrigin()) - reference_origin) |
|
|
36 |
# Modify the transformation to align the centers of the original and reference image instead of their origins. |
|
|
37 |
centering_transform = sitk.TranslationTransform(dimension) |
|
|
38 |
img_center = np.array(img.TransformContinuousIndexToPhysicalPoint(np.array(img.GetSize())/2.0)) |
|
|
39 |
centering_transform.SetOffset(np.array(transform.GetInverse().TransformPoint(img_center) - reference_center)) |
|
|
40 |
centered_transform = sitk.Transform(transform) |
|
|
41 |
centered_transform.AddTransform(centering_transform) |
|
|
42 |
# Using the linear interpolator as these are intensity images, if there is a need to resample a ground truth |
|
|
43 |
# segmentation then the segmentation image should be resampled using the NearestNeighbor interpolator so that |
|
|
44 |
# no new labels are introduced. |
|
|
45 |
|
|
|
46 |
return sitk.Resample(img, reference_image, centered_transform, interpolator, 0.0) |
|
|
47 |
|
|
|
48 |
new_size = [144,144,50] |
|
|
49 |
interp = sitk.sitkNearestNeighbour # for labels |
|
|
50 |
# interp = sitk.sitkLinear # for input features |
|
|
51 |
|
|
|
52 |
for file in sorted(glob.glob('train/Case*_segmentation.mhd')): |
|
|
53 |
# uncomment when resizing input images |
|
|
54 |
# file = file.replace('_segmentation', '') |
|
|
55 |
img = sitk.ReadImage(file) |
|
|
56 |
reshaped = resample(img, new_size, interp) |
|
|
57 |
sitk.WriteImage(reshaped, file) |
|
|
58 |
print(file, end='\r') |